Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to crack the password of ZIP or RAR File

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use Python to crack the password of ZIP or RAR files". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Python to crack the password of ZIP or RAR files.

I. the principle of cracking

In fact, the principle is very simple, a sentence summary is "make great efforts to achieve a miracle", Python has two compressed file libraries: zipfile and rarfile, these two libraries provide decompression method extractall () can specify a password, then first generate a password dictionary (manual or with the program), and then try the password in turn, if you can decompress normally that the password is correct.

Second, the experimental environment

The virtual environment adopted in this paper is Pipenv.

Library

Zipfile:Python standard library, which can be directly imported when in use.

Rarfile:Python third-party library

Install rarfile with Pipenv

Pipenv install rarfile

Finally, a compressed package with a password is put into the experimental environment.

III. Coding

Once you know the principle, the coding will be very simple.

Prepare the codebook

The "password book" is actually a file containing all possible passwords, which can be entered manually or programmatically. There will be an introduction at the end of the article.

Read a compressed file

# use different libraries if filename.endswith ('.zip'): fp = zipfile.ZipFile (filename) elif filename.endswith ('.rar'): fp = rarfile.RarFile (filename) depending on the file extension

Try to decompress

First try decompressing without a password. If successful, it means that the compressed file does not have a password.

Fp.extractall (desPath) fp.close () print ('No password') return

Brute force cracking

Try: # read codebook file fpPwd = open ('pwd.txt') except: print (' No dict file pwd.txt in current directory.') Returnfor pwd in fpPwd: pwd= pwd.rstrip () try: fp.extractall (path=desPath, pwd=pwd.encode ()) print ('Success! = >' + pwd) fp.close () break except: passfpPwd.close ()

Program entry

If _ _ name__ = ='_ main__': filename = sys.argv [1] if os.path.isfile (filename) and filename.endswith (('.zip', '.rar'): decryptRarZipFile (filename) else: print ('Must be Rar or Zip file') IV, use

If we want to use the above code, we just need to execute python main.py on the command line. For example, python main.py test.zip

Running result:

$python main.py test.zip

Success! = > 323126

V. expansion

How to get the password book?

See here, careful friends will find that the core is not the code, but * * "password book". In theory, as long as there are enough passwords in the password book, you will be able to get the password of the compressed package, which is commonly known as "hit the library".

How to speed up the cracking process?

Solved the problem of the password book, in-depth thinking of the small partners must have new questions, since the password book is so large, how to speed up the cracking process? Here are two ideas.

Multithread (process) cracking

If there are a lot of password books and a large number of passwords, we can read the password in a multi-threaded (process) way, one process reads a password book, and one thread reads the password in segments. Of course, if you are in python, it is recommended not to use multithreading, because the threads in python are chicken ribs, and those who are interested can read the relevant materials.

Accelerate with GPU

All of our above code runs on CPU, and even if we start multithreading (processes), we can only use CPU resources, but if we want to speed up the cracking process, we can actually take advantage of idle GPU resources.

Before we explain why we can take advantage of GPU acceleration, we need to make it clear that both are designed to accomplish computing tasks.

So why would you want to use GPU acceleration? This is about to talk about the difference between the two: although CPU has multiple cores, the total is no more than two digits, and the computing power of each core is extremely powerful. The number of cores of GPU is much higher than that of CPU, but the computing power of each core is very different from 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, while GPU is responsible for a very simple but large number of simple operations.

So in theory, we can use GPU to speed up the process of cracking passwords.

In fact, such tools have already emerged, Hashcat is the most famous one, it claims to be the world's fastest password recovery tool, can be based on CPU/GPU and other work.

At this point, I believe you have a deeper understanding of "how to use Python to crack the password of ZIP or RAR files". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report