This article mainly introduces how to use Python to crack Zip or rar compressed file password. The sample code is introduced in great detail, which has a certain reference learning value for everyone’s study or work. Friends who need it can refer to it
We often download some compressed packages with password from the network. If we want to get the content inside, we often have to pay some fees to the provider. Want to white whoring one of the content, common practice is Baidu search some compressed package password cracking software, but the consequences believe that people who have experienced all know. This article will use Python to crack the password of the compressed package, taking zip / rar as an example.
In fact, the principle is very simple. In a word, python has two compressed file libraries: zipfile and rarfile. The extraction method provided by these two libraries is extractall () You can specify a password. In this way, you first generate a password dictionary (manually or programmatically), and then try the password in turn. If you can decompress it normally, it means that the password is correct.
- Zipfile: Python standard library, which can be imported directly when using
- Rarfile: Python third-party library, need to install when using, API documentation（ https://rarfile.readthedocs.io/en/latest/api.html )
Using pipenv to install rarfile
pipenv install rarfile
Finally, put a compressed package with password into the experimental environment.
After knowing the principle, the coding will be very simple
Prepare the codebook
“Password book” is actually a file containing all possible passwords. Users can enter them manually or by program. There will be an introduction at the end of the article.
Read compressed file
Try to unzip
First try to decompress without password. If it is successful, it means that the compressed file has no password
If you want to use the above code, we just need to execute Python on the command line main.py < file name > is OK. For example, python main.py test.zip
Results of operation:
How to obtain the extended password book?
Seeing this, careful friends will surely find that the core is not the code, but the codebook. In theory, as long as there are enough passwords in the codebook, you can get the password of the compressed package, which is commonly known as “library collision”.
How to speed up the cracking process?
After solving the problem of the codebook, there will be new questions for those who think deeply. Since the codebook is so huge, how can we speed up the cracking process? Here are two ideas
Multi thread (process) cracking
If there are a lot of password books and the number of passwords is huge, we can read the password by multi thread (process). One process reads one password book, and one thread reads the password in sections. Of course, if it is in Python, it is recommended not to use multithreading, because threads in Python are chicken ribs. You can read relevant materials if you are interested.
Speed up with GPU
The above codes are all running on the CPU. Even if multithreading (process) is turned on, we only use the CPU resources. But if we want to speed up the cracking process, we can actually use the idle GPU resources.
Before introducing why we can use GPU acceleration, we need to make a clear point that both are designed to complete the computing task.
Why do you think of GPU acceleration? This is about to talk about the difference between the two: Although the CPU has multiple cores, the total number does not exceed two digits, and the computing power of each core is extremely powerful. The number of cores of GPU is far more than that of CPU, but the computing power of each core is far less than that of CPU.
We can give a simple example, to solve a problem, CPU is a doctoral student, GPU is a primary school student, CPU is responsible for understanding the problem and sorting out the steps and solutions, and GPU is responsible for a very simple but large number of simple operations.
Therefore, in theory, we can use GPU to accelerate the process of password cracking.
The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.