0

I am trying to unzip a file with a password that I know is between 900000000 and 900999999 but I am met with an bad crc error and a gov.csv that contains some non sense. How can I get past this without ending the program? I know what the actual password is, but creating this "brute force" attempt is part of a homework assignment. The error that I come across seems to be stemming from the unzip apt. My code is below. The yes was added because I was continually being asked to replace the file even though it was the wrong password.

for word in $(echo 900{000000..999999})
{
echo $word
yes | unzip -q -P $word $1
if [ -s gov.csv ]
then
echo "$word is the password"
exit
fi
}

I have tried adding an additional if statement that does nothing to improve the situation.

if yes | unzip -q -P $word $1 | grep -i "bad crc"
then
rm gov.csv
fi

I would also like to apologize if my formatting for the code section is bad, first time posting on this site.

  • Have you considered writing your code in Python or another language with a native zip-parsing library? When you use a command-line tool you're limited to the functionality its authors chose to implement. – Charles Duffy Jan 27 '21 at 19:30
  • As for the specific question, note that (1) `grep` only tests stdout, not stderr; (2) you should really be checking the _exit status_ of the `unzip` command, not its output; (3) unless `unzip` is even worse-designed than I expect it to be, you shouldn't need `yes |` -- any well-behaved UNIX command will let you specify a mode where it doesn't expect working stdin by using appropriate command-line arguments, or will just silently stop trying to read from stdin in the first place when it's attached to a non-TTY device (say, ` – Charles Duffy Jan 27 '21 at 19:32
  • 1
    Also, your code will have bugs if you ever extend it to support passwords with spaces or glob characters. It's critical to correctly quote your parameter expansions in shell scripts. `"$word"`, not just `$word`; `"$i"`, not just `$i`, etc. This is the same class of problem discussed in [I just assigned a variable, but `echo $varible` prints something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Jan 27 '21 at 19:34
  • (The Python suggestion will also improve performance _by a lot_; right now, for every password you check, you're starting a separate copy of `/usr/bin/ls`, a new copy of `/usr/bin/unzip`, etc; sure, these are all C programs, so on their own they're faster than native Python, but paying the startup costs over and over and over is going to eat all those gains very quickly; though to be sure, anyone actually writing a zip cracker would be adding a bunch more optimizations that a naive Python implementation wouldn't have either). – Charles Duffy Jan 27 '21 at 19:36
  • @CharlesDuffy unfortunately for this assignment, I will not be able to use python, I must use bash. I believe you are correct that unzip is a poor designed. It constantly asks for the prompts with an incorrect password, which is why I implemented the yes pipeline. When done on a smaller scale, closer to the known password, the script works fine, as the bad CRC error doesn't occur. This error occurs about 25,000 attempts in. Thank you for all your advice. I am still learning. How do I check the stderr as you mentioned? – sidiousrex Jan 27 '21 at 19:57
  • Making the stderr file descriptor point to the same place as the stdout one is an easy solution: `unzip ... 2>&1 | grep ...` (and of course, I meant a separate copy of `yes` instead of a separate copy of `ls`; brain/finger disconnect) – Charles Duffy Jan 27 '21 at 20:24
  • 1
    _However_, you probably don't need to do that if `unzip` sets its exit status correctly -- in which case there's no reason to use `grep` at all. See if just `if ! unzip -q -P "$word" "$1" – Charles Duffy Jan 27 '21 at 20:24
  • My "UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP." correctly returns failure for both CRC errors and bad passwords when I run `unzip -qo -P "$pass" foo.zip`, and success when the file was extracted – that other guy Jan 27 '21 at 21:25

1 Answers1

0
#!/bin/bash
for id in $(echo 900{000000..999999})
{
  echo $id
  unzip -q -o -P "$id" "$1"
  if [[ $? -eq 0 ]]
  then
    echo "Password is $id"
    exit
  fi
}

Thank y'all for the help, the -o is what replaced the yes pipeline, as well as $? -eq 0 verifying that the previous command did not return an error. I'm still learning and appreciate all the help offered.