Y
Y
Yaroslav2021-01-07 23:39:19
Python
Yaroslav, 2021-01-07 23:39:19

7z archive password guessing script in python?

Task: to find the forgotten password to the archive.
Conditions: the length of the password is no more than 4 words without spaces. There is a list of 5 words that can be in the password in an unknown order.
Done:
With the help of itertools.product, I generated all possible variants of word order in the password and wrote it to a file.

for word in itertools.product(['first','second','third','fourth','fiftht'], repeat=4):
    print(''.join(word))

To unpack a 7z archive, I use the py7zr library. To open an archive with a password in the library, use the function
with py7zr.SevenZipFile('encrypted.7z', mode='r', password='secret') as Archive: 
     Archive.extractall()
.
Below is my code.
passfile = open('\pass.txt', 'r') # открываю файл с паролями для чтения 
Lines = passfile.readlines() # в файле каждый вариант пароля на новой строке. Читаем файл построчно.
for word in Lines: 
    arch = py7zr.SevenZipFile('\Archive.7z', mode='r', password=word)
    arch.extractall(path="\")

Question: how to substitute word (suggested password from the file) in the function parameters in password for a complete enumeration of all options.
Help me please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MinTnt, 2021-01-08
@puankare

You need to add a kind of "crash" check:

passfile = open('\pass.txt', 'r') # открываю файл с паролями для чтения 
Lines = passfile.readlines() # в файле каждый вариант пароля на новой строке. Читаем файл построчно.
for word in Lines: 
    try:
        arch = py7zr.SevenZipFile('\Archive.7z', mode='r', password=word)
        arch.extractall(path="\\")
        print('Пароль "' + word + '" подошёл!')
    except Exception:
        print('Пароль "' + word + '" не подошёл')

Y
Yaroslav, 2021-01-08
@puankare

The speed is approximately 1 password per second. I'm not a programmer myself, I didn't teach PEP 8. I apologize to those who will get unpleasant reflexes from this code.

PossiblePassList = ['first','second','third']
for word in PossiblePassList:
    try:
        arch = py7zr.SevenZipFile('\arch.7z', mode='r', password=word)
        arch.extractall(path="\")
        arch.reset() #из документации Once extract() called, the SevenZipFIle object become exhausted and EOF state. If you want to call extractall() again, you should call reset() before it.
        print('Password detected - ' + word)
        break
    except py7zr.Bad7zFile:
        print('Wrong password! ' + word)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question