Thursday, July 28, 2022

[SOLVED] How to check whether a bcrypt hash is present

Issue

so quite simply I'm looking to incorporate the bcrpyt regex into an egrep command to see whether a bcrypt hash is present on each line.

I currently do this with MD5 hashes, quite easily:

egrep -wa "[a-f0-9]{32}" DB.txt >> DB_md5.txt
egrep -v -a "[a-f0-9]{32}" DB.txt >> DB_nomd5.txt

I've researched and found: Regular expression to find bcrypt hash?

Along with the solution being: \$2[ayb]\$.{56}

I'm struggling to include it in my egrep command. For example:

egrep -wa "\$2[ayb]\$.{56}" DB.txt >> DB_bcrypt.txt

The above command is not working. Any help is greatly appreciated.

Note: The position of the bcrypt hash is not important, it can be in any position on the line, hence why I've removed the ^ and $ from the regex solution found in the link below.


Solution

You may use

grep -Ea '\$2[ayb]\$.{56}' DB.txt >> DB_bcrypt.txt

See the online demo.

NOTES

  • -E - POSIX ERE syntax on, now { and } in the {56} range quantifier do not need escaping (your regex was parsed with the POSIX BRE engine, and you need \{56\} there to match a pattern 56 times)
  • Single quotes around the regex make sure \$ is parsed as a \$ regex escape, else "\$" just dentotes a $ special char, that is, the end of string.


Answered By - Wiktor Stribiżew
Answer Checked By - Terry (WPSolving Volunteer)