Python hack zip file with password protection

Source: Internet
Author: User
Tags password protection python script

Today, a funny thing happened, a friend issued a password-protected zip file to me, but do not give me the password, I wonder how this can ' guess ' to the password?

After a series of attempts, we finally use Python to ' guess ' the password. to write a script to crack the password need to know about Python ZipFile Library, where the resources of Baidu is very much, I will directly to everyone on the code one by one explanation.

Import zipfilefrom Threading Import Thread

First import ZipFile and the thread library under the threading, the role is: You can open the zip file by code, there is password protection we provide the password on the line (as to how to provide me later will say ^_^); Turn on multithreading to ' guess ' passwords, making them more efficient!

Here we can practice how ZipFile open the file?

ZipFile = ZipFile. ZipFile ("Test1.zip") ZipFile = Extractall (pwd= bytes ("123", "UTF8"))

  We are in this exercise with the Python script under the same path to generate a compressed password: 123 of the compressed package, named Test1.zip of course you can generate the practice files anywhere, open the time remember to write the absolute path is OK ~ ~

You will find this code run after your compressed files are extracted, we think about the password error, of course, it is not open. We use this feature, using the Python prevention error mechanism try, except This mechanism means: if I put in try: Inside to execute the code has a problem, the program does not error and will run except: The following code.

Core: Then we use this, let the program to try to give us almost unlimited possible combinations of passwords, if the password is wrong to skip, until the password is found ~

Digression: I think that my friend set the password should be a number, all I wrote a. txt file in python: Write all 0 to 10000000 digits. Seems to have more than 80 MB .... It's really big ...

Then we start to guess the password:

def extractfile (ZipFile, password):   # # #提取文件的类    try:        zipfile.extractall (pwd= bytes (password, "UTF8"))     # # #打开压缩文件, provide password        ... Print ("This file\ 's password is" + password)       # # #破解到密码    except:        pass    # # #假如失败, skip to continue

 Wrote a method: This method requires us to provide information about the files that need to be cracked, and then we need to provide the password for this file (we certainly do not know the password, but we will know soon ~ ~ ~ ~)

Def mainstep ():    ZipFile = ZipFile. ZipFile (' C:\\users\\flash\\desktop\\secret.zip ')   # Here the second argument with R means to read the zip file, W is to create a zip file, the default is R    pwdlists = Open (' C:\\users\\flash\\desktop\\dictionary.txt ') #读入所有密码 for line in    pwdlists.readlines ():   # Write password        Pwd = Line.strip (' \ n ')        t = Thread (Target=extractfile, args= (ZipFile, PWD))        T.start ()

  Then we write a method: This method is for the Extractfile method to provide the information needed to crack the file, and then open the ' Password dictionary ' to the password dictionary all the things are read into the pwdlists inside, through the for loop to the pwdlists inside each password is taken to try, Until you have the correct password t = thread (Target=extractfile, args= (ZipFile, PWD)) This means that the thread first runs to make the attempt faster, specifically Baidu knows the threading knowledge.

This is my password dictionary generated and I cracked a friend's password ~~~/proud O (∩_∩) o haha ~

I post all the code:

Import zipfilefrom Threading Import Threaddef extractfile (ZipFile, password):   # # #提取文件的类    try:        Zipfile.extractall (pwd= bytes (password, "UTF8")     # # #打开压缩文件, provide password        ... Print ("This file\ 's password is" + password)       # # #破解到密码    except:        pass    # # #假如失败, skip Continue def mainstep ():    ZipFile = ZipFile. ZipFile (' C:\\users\\flash\\desktop\\secret.zip ')   # Here the second argument with R means to read the zip file, W is to create a zip file, the default is R    pwdlists = Open (' C:\\users\\flash\\desktop\\dictionary.txt ') #读入所有密码 for line in    pwdlists.readlines ():   # Write password        Pwd = Line.strip (' \ n ')        t = Thread (Target=extractfile, args= (ZipFile, PWD))        T.start () if __ name__ = = ' __main__ ':    mainstep ()

  Note: I use the absolute path, you can modify according to their own path OH

Code is not difficult, but it is very interesting, do not have absolute interest in what is not absolutely boring, we have to make ourselves feel interesting: this is very interesting, this is very interesting ... (Heart: Not a bit!!) Haha, a joke. )

Hope we have a happy mentality to learn and progress. Let's all work together ~ ~

Python hack zip file with password protection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.