Using python to implement zip blasting

Experimental principle

The Python zipfile module is used to compress and decompress zip format encoding. To perform related operations, you first need to instantiate a ZipFile object. ZipFile accepts a compressed package name in string format as its required parameter. The second parameter is an optional parameter, indicating the open mode, similar to file operations. There are three modes: r/w/a, representing read, write, and Added, default is r, read mode.

There are two very important classes in zipfile, namely ZipFile and ZipInfo. In most cases, we only need to use these two classes. ZipFile is the main class used to create and read zip files, while ZipInfo stores information about each file in the zip file.

Experimental steps

Create a folder /root/python/zip

Create a compressed package

Open the terminal and switch to the /root/python/zip folder

Enter touch test.txt to create the test.txt file

echo12345678>test.txt, enter some content into it

(echo command: If the file does not exist, it will create a new file and write the content; if the file already exists, it will overwrite the original content.)

Use cat test.txt to check again

Then use the zip command to compress the test.txt file into a zip file, and set its name and password to abc123. The command is zip–passwordabc123test.ziptest.txt to compress it into test.zip.

After adding the compressed file, delete the original test.txt file (you can use the command rm test.txt) to prevent conflicts when the subsequent decompression is correct. Prepare the password dictionary file pass.txt. Create a password dictionary file: pass.txt in the python script directory. You can enter some common passwords into it by yourself. A pass.txt file has been prepared in this directory. The content of the file is as shown below.

Use python function to decompress zip format files

You need to customize a function. The function is to use try-except exception handling to handle exceptions in the password dictionary pass.txt file. If the password is wrong, print out the number of times the password is wrong. If the password is correct, print out the correct compressed password, and finally return password.

To write a Zip file, you need to start by learning how to use the zipfile library. First, enter python3 in the terminal to start the python command line.

Then enter help(”zipfile)

Check out the extractall() method

The extractall function requires three parameters, one is the path to the decompressed file, members is the name of the file to be decompressed, which is optional, and the last one is the password of the compressed file. If not passed in, the password will be empty.

Try this function first

First, use vim to write a simple decompression script to initially learn how to use the zipfile library, and pass in the correct password abc123.

(How to exit in vim state

1. Press the ESC key first to make sure you are in Command mode.

2. Enter “:wq” and press Enter to save changes and exit.

3. If you want to save the file with a different name, you can use the “:w filename” command to save the content as a new filename.

4. If you encounter an error or cannot write the file, you can use the “:wq!” command to force save and exit.

5. If you only want to save the changes but keep the vim editor open, you can use the “:w” command.

6. If you just want to exit the vim editor without saving any changes, you can enter the “:q!” command and press Enter. )

Write the following code

import zipfile
zfile=zipfile.ZipFile(r"test,zip")
try:
   zfile.extractall(pwd=bytes( "abc123,"utf8"))
except Exception as e:
    print(e)

After writing and saving, first check the path with pwd

Then ls to view the files in the current folder, you can see that there is no test.txt

After running the script, use the ls command again to see the test.txt file.

Try changing the password in the script to the wrong one.

Run the script again and you will see an error in the printed information, prompting an incorrect password.

Start writing function script

Combined with the above example, you can use the exception thrown due to incorrect password to test whether there is a password for the Zip file in the dictionary, traverse each word in the dictionary, and print out the correct password if the extractall() function executes without error. If a password error exception is thrown, ignore the exception and try the next password.

Now set up the following two functions:

The first function is named main() function. The first function is to use the zipfile module to initialize the all.zip file, open the pass.txt file, and then use a for loop to read pass.txt line by line. Each line is A password, and then use multiple threads to call the extractfile function, with the parameters file and password. The main() function code is as follows

def main():
    zfile=zipfile.ZipFile(r'test.zip')
    passfile=open(r'pass.txt')
    for line in passfile.readlines():
        Password=line.strip('\\
')
        t=threading.Thread(target=extractfile,args=(zfile,Password))
        t.start()
        t.join()

The second function is named extractfile function. Use try-except exception handling. If the password is wrong, print out the number of times the password is wrong. If the password is correct, print out the correct compression password and exit. Finally, return password. The extractfile function code is as follows

def extractfile(zfile,password):
    try:
        zfile.extractall(pwd=bytes(password,"utf8"))
        print("The password to decompress the file is:",password)
        exit(0)
    except:
        globali
        i=i+1
        print("Password incorrect for %s time"%i)

The final script is as follows

import zipfile
import threading
globali
i=0
def extractfile(zfile,password):
    try:
        zfile.extractall(pwd=bytes(password,"utf8"))
        print("The password to decompress the file is:",password)
        exit(0)
    except:
        globali
        i=i+1
        print("Password incorrect for %sth time"%i)
def main():
    zfile=zipfile.ZipFile(r'test.zip')
    passfile=open(r'pass.txt')
    for line in passfile.readlines():
        Password=line.strip('\\
')
        t=threading.Thread(target=extractfile,args=(zfile,Password))
        t.start()
        t.join()#In multi-threading, the join() method is used to wait for the current thread to complete execution before continuing to execute other threads.
                 It is usually used with the start() method to ensure that the main thread does not end until all child threads have finished executing.
if __name__ == '__main__':
    main()

Analysis:

  1. Import zipfile and threading modules.
  2. Define global variable i with an initial value of 0.
  3. Define the extractfile function, which receives two parameters: zfile (compressed file object) and password (password string).
  4. In the try statement, try to decompress the compressed file using the given password. If it succeeds, print the decompression password and exit the program; if it fails, catch the exception and add i to 1, and print the number of errors.
  5. Define the main function to perform the following operations: a. Open the compressed file named “test.zip”. b. Open the file named “pass.txt” and read the password line by line. c. For each password, create a new thread and pass the compressed file object and password as parameters to the extractfile function. d. Start the thread and wait for it to complete.
  6. If the current script is running as the main program, the main function is called.

)

Run this script