1

grep -n [pattern] [file] should return the line number and output of the line of my file.

However, grep -n [my pw] /usr/share/wordlists/weakpass_3 is only returning

grep: /usr/share/wordlists/weakpass_3: binary file matches

The file exitsts and is readable by the user and the password is also in the wordlist. file /usr/share/wordlists/weakpass_3 returns /usr/share/wordlists/weakpass_3: data

How can I make grep output the line number with the line of the file?

1 Answers1

2

Your grep (likely GNU grep) detects that the file may not be text and instead of printing the matching lines, it only tells you there's a match to avoid printing what may just be garbage.

To bypass that and print it nonetheless, you can add the -a option (a GNU extension).

Also note that grep by default matches regular expressions (the re in grep), you need -F to find substrings.

Also, arguments to executed commands are public knowledge on a system, so you shouldn't pass a password or any sensitive data to grep as argument unless grep happens to be a built-in of your shell (very few shells have grep builtin though).

So instead, assuming a shell where printf is builtin (most these days):

printf '%s\n' "$password" | grep -anFf - /usr/share/wordlists/weakpass_3

Where:

  • -a: bypass the heuristic check for binary files
  • -F: Fixed string search instead of regular expression matching
  • -f -: takes the list of strings to search for from stdin (one per line)
  • -n: report line numbers