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?

  • 4
    Related: [What makes grep consider a file to be binary?](https://unix.stackexchange.com/questions/19907/what-makes-grep-consider-a-file-to-be-binary) – steeldriver Dec 10 '22 at 21:17
  • updated question to include the output of `file /usr/share/wordlists/weakpass_3` – UndercoverDog Dec 10 '22 at 21:18
  • 3
    @UndercoverDog use `grep -a 'text' /yourpath`. Related: [grep returns "Binary file matches"](https://unix.stackexchange.com/q/335716/414186) – Edgar Magallon Dec 10 '22 at 21:38
  • Add output of this command to your question (no comment here): `file /usr/share/wordlists/weakpass_3` – Cyrus Dec 10 '22 at 21:57
  • `grep` seems to interfere with at least one character in your file and then classifies it as data. A possible workaround might be: `strings /usr/share/wordlists/weakpass_3 | grep -n 'text'` – Cyrus Dec 10 '22 at 23:13

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
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501