This article describes how to compress and decompress the zip file in Python and try to crack the password of the zip file. It provides a simple example of using the zipfile module, for more information about how to compress and decompress zip files, see python zipfile.
For example, in the directory where The py script is located, there are the following files:
readability/readability.jsreadability/readability.txtreadability/readability-print.cssreadability/sprite-readability.pngreadability/readability.css
Compress the files in the readability directory to the readability.zip file in the script directory, maintain the same file structure, and then print the file list of the generated compressed package, decompress the file to the output directory and output/bak directory of the script directory in two ways.
The script is as follows:
#! /Usr/vin/env python # coding: UTF-8 "compression and decompression zip file" "import osimport zipfiledef compress (zip_file, input_dir): f_zip = zipfile. zipFile (zip_file, 'w') for root, dirs, files in OS. walk (input_dir): for f in files: # obtain the relative path of the file, and create the same directory structure in the compressed package. abs_path = OS. path. join (OS. path. join (root, f) rel_path = OS. path. relpath (abs_path, OS. path. dirname (input_dir) f_zip.write (abs_path, rel_path, zipfile. ZIP_STORED) def extract (zip_file, output_dir): f_zip = zipfile. zipFile (zip_file, 'R') # decompress all files to the specified directory f_zip.extractall (output_dir) # decompress the files one by one to the specified directory for f in f_zip.namelist (): f_zip.extract (f, OS. path. join (output_dir, 'bak') def printdir (zip_file): f_zip = zipfile. zipFile (zip_file, 'R') print '= printdir () ========================== 'f_zip.printdir () print '= namelist () ======================== 'for f in f_zip.namelist (): print fif _ name _ = '_ main _': zip_file = 'readability.zip 'compress (zip_file, OS. path. join (OS. getcwd (), 'readability ') printdirzip_file) extract (zip_file, 'output ')
Python brute-force cracking of zip files containing passwords
Ideas:
1. Generate a txt password dictionary first.
2. Read the code and try it one by one. When the password is incorrect, the program reports an error and is interrupted. Therefore, if the password is incorrect, pass is required.
3. After the decompression is successful, interrupt the program and output the correct password.
Import zipfileimport osfrom threading import Threadimport time # path of the compressed file = r'c: \ Users \ Administrator \ Desktop \ moeMaid-master.zip '# password = '000000' def pojie_zip (path, password): if path [-4: Invalid parameter '.zip ': # path = dir +' \ '+ file # print path zip = zipfile. zipFile (path, "r", zipfile. zlib. DEFLATED) # print zip. namelist () try: # If the decompression is successful, True is returned, and the password is zip. extractall (path = 'C: \ Users \ Administrator \ Desktop \ ', members = z Ip. namelist (), pwd = password) print '---- success !, The password is % s' % password zip. close () return True when T: pass # If an exception occurs, print 'error' def get_pass (): # password dictionary path passPath = 'C: \ Users \ Administrator \ Desktop \ zip.txt 'passfile = open (passPath, 'R') for line in passFile. readlines (): password = line. strip ('\ n') print 'Try the password % s' % password if pojie_zip (path, password): break passFile. close () if _ name __= = '_ main _': start = time. clock () get_pass () print "done (%. 2f seconds) "% (time. clock ()-start)
This is the speed at which I extract a M file. In fact, all the time is spent learning about compression.