Need Regex Help


#1

So I have a large amount of seperate password hashes in a file. I just need to blank out the hash portion and leave the password field blank.

Example:
pass = INSERT LONG CRAZY HASH HERE

needs to just be
pass =

I’m horriible with regex and have tried a few sites but keep getting confused on what i want to sort on and what not to. If it helps there is no space after the “pass =” and the hash ends in a =.

TIA
Phil


#2

I will suppose there is only one couple pass/hash per line.

if it’s just manually, you can perform a “cat my_file | cut -d = -f 1 > my_fille_cut” to extract the desire field.

If you want to do it with javascript, you can cut the end of each line with a .slice (if all hash have the same size), it will be faster than a regex.

And for the regex, you could try this : /^(.* =) [$$$$$$$]=

replace the $$$$$$ with the caracters composing your hash, and perform a Regex.exec(line)[1] to get the pass plus the equal sign.

In bonus, here is a quik detail of what the regex is doing :

  • ^ catch the begining of the line
  • .* catch everything it can, exept newline
  • " =" catch the space followed by the equal sign
  • the ( ) surrounding .* = create a subdivision in the result of the exec function ([0] to get the full match, [1] to get the first parenthesis, [2] for the next paranthesis…)
  • [] to catch the hash part.

#3

Awesome thanks for you the info.

The only issue I run into is that with each line it’s a different hash. I’m trying to purify an ssh connections file to get rid of the password hash for over 150+ connections. Each connection having a different hash, so I was trying to do something like

pass = * replace with pass =

Obviously not the correct way.


#4

Hashes are typically some specific number of hexadecimal digits. So count out the digits and then the regex to match the hash will be: [0-9A-Fa-f]{X} where X is the number of digits.

Once you have that then the regex replacement is easy:

  • Search for: pass = [0-9A-Fa-f]{X}
  • Replace with: pass = (with no space after the equal sign)

#5

awesome ty. Trying it right now, can’t seem to get it to match.

pass =[0-9A-Fa-f]{44}

44 total char, does it include + and = in there? Or should i skip those and just count char?

I can get some random lower numbres to work like 5 but they only match the first couple char not the entire string.

Example (hash changed for security purposes)

pass =Z764fSos0nXy0xPPGbGTqf9BhCzO2P4ymn4YREpvoGw= Will only match up to the first 5 char of the hash. It only works on this one out over the hundreds in there too.

Phil


#6

Since every single character in regular expressions are significant, it would be helpful if you could show us a sample of what you’re actually trying to match.

If you’re not comfortable with that (and even if you are) you can use one of the many regular expression tester sites on the web like http://reg-exp.com/ to try things out until it matches.

Also, I strongly recommend the O’Reilly book Mastering Regular Expressions. Learning and becoming proficient with regular expressions is something that every developer should do.


#7

Just posted one above :smile:


#8

Think i got it, thanks everyone.

pass =[0-9a-zA-z/+]{NUMBER}