Detailed explanation of the idea of cracking the password of compressed files by Python

  • 2021-10-13 08:09:47
  • OfStack

Often encountered Baidu network disk compressed file encryption, today we will crack it!

Realization thought

The last article introduced the idea of blasting password, and interested friends can understand it.

In fact, they are all the same: it is nothing more than dictionary explosion, depending on whether you have a ready-made password dictionary or generate your own password dictionary, and then enter the password cyclically until you enter the correct position. At present, many of them have anti-explosion restrictions, which can't be cracked by brute force at all, but it seems that zip is a relatively simple password and there is no restriction.

Therefore, the realization idea is

Generate a dictionary- > Enter password- > Successful decompression

Implementation process

1. Generate a dictionary

Generating a password dictionary is actually a process of combining characters. Little friends don't use the list to combine oh, it's easy to overflow memory, and it's best to use the generator. Here I choose to use the itertools module of python. itertools
Is a function module added in version 2.3 for creating iterators for loops.

And

The itertools. product (* iterables [, repeat]) function corresponds to an ordered repeated sampling process
.

Write out the method of generating password dictionary: (Output 1 and 2 to form all passwords with length 4)


import itertools
def allkeyword(dic,num):
 allkey1 = itertools.product(dic,repeat=num)
 allkey2 = (''.join(i) for i in allkey1)
 return allkey2

dictionaries = ['1', '2']
print(list(allkeyword(dictionaries,4)))
# ['1111', '1112', '1121', '1122', '1211', '1212', '1221', '1222', '2111', '2112', '2121', '2122', '2211', '2212', '2221', '2222']

2. Unzip the file

Boy, zipfile of python
Module can compress and decompress files? Refer to the official document for usage: https://docs.python.org/zh-cn/2/library/zipfile.html


import zipfile
try:
 ZIPFILE = zipfile.ZipFile(r'D:\123\1.zip') #  Pay attention to the path 
 ZIPFILE.extractall(path=r'D:\123',pwd=b'1234') #  Which path to unzip to 
 print(" Successful decompression ")

except:
 print(" Decompression failed ")

There are no unexpected test files should be able to unzip successfully.

3. Imitate the encrypted compressed files required by the project

Create a new abc. txt file and enter abc

Right-click the txt file, add it to the compressed file, set the password, and confirm

Here we delete the original txt file, easy to test, crack the successful decompression to the current path

4. Use the generated dictionary to explode the password

Combined with steps 1 and 2, the complete code: (pay attention to annotation learning)


import zipfile
import itertools
#  Crack 1 A 4 Digit password numbers and letters are 23ab Roughly 5-10 Minutes, for reference only. 
dictionaries = ['1', '2', '3', '4','5','6','7','8','9','0',
    'a','b','c','d','e','f','g','h','i','j','k',
    'l','m','n','o','p','q','r','s','t','u','v',
    'w','x','y','z']   # Key characters that make up the cracking dictionary (you can add them according to your own needs) 
end_for = True  #  Variable used to stop the loop after successful cracking 
#  Set the length of the password 1 To 16 Bit cipher 
for x in range (1,17):
 if end_for:
  def allkeyword():
   allkey1 = itertools.product(dictionaries,repeat=x)
   allkey2 = (''.join(i) for i in allkey1)
   return allkey2

  def trypassword (password):
   try:
    ZIPFILE = zipfile.ZipFile(r'D:\zip\abc.zip') #  Need to unzip the local with password abc.zip
    ZIPFILE.extractall(path=r'D:\zip',pwd=password.encode('utf-8'))  #  Which path to unzip to 
    print(f" Successful decompression , The correct password is: {password}")  #  Unzip successfully and print the correct password 
    global end_for  #  Declared as a global variable, not declared, reassignment is invalid 
    end_for = False  #  Decompress successfully, stop loop 
    return True
   except:
    print(f" Decompression failed , The attempted password is: {password}") 
    return False

  # Use trypassword Function returned by the True Or Flase To determine whether the program is terminated. 
  for pwd in allkeyword() : 
   if trypassword(pwd):
    break

After executing the code, the 4-digit password (alphanumeric combination) can be successfully decompressed in about 5-10 minutes, and the printed password can be cracked.

After cracking the password, we can see that the abc. txt file comes out because we set it to decompress to the current path.


Related articles: