-->

How to crack password of zip files using python

How to crack password of zip files using python



The time to crack a password is related to bit strength (see password strength), which is a measure of the password’s entropy, and the details of how the password is stored. Most methods of password cracking require the computer to produce many candidate passwords, each of which is checked. One example is brute-force cracking, in which a computer tries every possible key or password until it succeeds. More common methods of password cracking, such as dictionary attacks, pattern checking, word list substitution, etc. attempt to reduce the number of trials required and will usually be attempted before brute force. Higher password bit strength exponentially increases the number of candidate passwords that must be checked, on average, to recover the password and reduces the likelihood that the password will be found in any cracking dictionary.

why write such a script? Well, you might’ve forgotten the password for an important zip file or just don’t know the file’s password in the first place and you need extract its contents ASAP. This script comes to the rescue.

#import the required modules
import optparse
import zipfile
from time import time

  • optparse – to parse the arguments
  • zipfile – to handle the zipfiles
  • time – to find the time taken to crack the zip file

parser = optparse.OptionParser()
parser.add_option('-f', '--file', action="store", dest="file_path", help="Zip File Path", default=None)
parser.add_option('-w', '--word_list', action="store", dest="word_list", help="Word List Path", default=None)

Also Read

options, args = parser.parse_args()

We use a try…except statement to check if the password is the right one, if yes, the script prints the correct password and stops, if not, the script tries the next password in the list.


       

            #!/usr/bin/python

#import the required modules
import optparse 
import zipfile
from time import time

#parse arguments
parser = optparse.OptionParser()
parser.add_option('-f', '--file', action="store", dest="file_path", help="Zip File Path", default=None)
parser.add_option('-w', '--word_list', action="store", dest="word_list", help="Word List Path", default=None)
options, args = parser.parse_args()

def main(file_path, word_list):
 #check if the Zip file exists
 try:
  zip_ = zipfile.ZipFile(file_path)
 except zipfile.BadZipfile:
  print "Please check the file's path. It doesn't seem to be a zip file."
  quit()

 password = None #just in case if the word list is empty
 i = 0 #password count 
 c_t = time() #start time
 with open(word_list, "r") as f: #open the word list
  passes = f.readlines() #read the text file line by line
  for x in passes:
   i += 1 #increment the password try count
   password = x.split("\n")[0] #splitting the newline character 
   try:
    zip_.extractall(pwd=password) #try the password
    t_t = time() - c_t #total time
    print "\nPassword cracked: %s\n" % password #print the password
    print "Took %f seconds to crack the password. That is, %i attempts per second." % (t_t,i/t_t) #stats
    quit() #stop the script
   except Exception:
    pass
  print "Sorry, password not found."

if __name__ == '__main__':
 main(options.file_path, options.word_list)

       
 

Take your time to comment on this article.

Post a Comment