使用python发出暴力破解密码

时间:2019-05-31 02:57:06

标签: python

我正在为某个分配工作编写一些代码,该代码应该蛮力压缩zip文件。为我提供了密码的第一个单词,我知道它还有3个字母字符,包括大写和小写字母。

我能够打印出这些组合中的每一个,但是似乎找不到密码。有人可以看一下我的代码,看看是否找到错误或其他东西吗?

import zipfile
import itertools
import time

# Function for extracting zip files to test if the password works!
def extractFile(zip_file, password):
    try:
        zip_file.extractall(pwd=password)
        return True
    except KeyboardInterrupt:
        exit(0)
    except Exception:
        pass

# Main code starts here...
# The file name of the zip file.
zipfilename = 'planz.zip'
# The first part of the password. We know this for sure!
first_half_password = 'Super'
# We don't know what characters they add afterwards...
# This is case sensitive!
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
zip_file = zipfile.ZipFile(zipfilename)

# We know they always have 3 characters after Super...
# For every possible combination of 3 letters from alphabet...
for c in itertools.product(alphabet, repeat=3):
    # Slowing it down on purpose to make it work better with the web terminal
    # Remove at your peril
    time.sleep(0.009)
    # Add the three letters to the first half of the password.
    password = first_half_password+''.join(c)
    # Try to extract the file.
    print ("Trying: %s" % password)
    # If the file was extracted, you found the right password.
    if extractFile(zip_file, password):
        print ('*' * 20)
        print ('Password found: %s' % password)
        print ('Files extracted...')
        exit(0)

# If no password was found by the end, let us know!
print ('Password not found.')

我希望程序找到的密码应该是Super +另外3个字母字符。

1 个答案:

答案 0 :(得分:3)

zip_file.extractall(pwd=password)期望使用字节而不是字符串形式的密码。因此,由于将字符串作为密码传递,因此extractFile中的try / except块始终被触发,因此永远找不到密码。我已经更新了您的代码,以包含密码字符串的字节转换:

import zipfile
import itertools
import time

# Function for extracting zip files to test if the password works!
def extractFile(zip_file, password):
    try:
        zip_file.extractall(pwd=password)
        return True
    except KeyboardInterrupt:
        exit(0)
    except Exception:
        pass

# Main code starts here...
# The file name of the zip file.
zipfilename = 'test_archive.zip'
# The first part of the password. We know this for sure!
first_half_password = 'Super'
# We don't know what characters they add afterwards...
# This is case sensitive!
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
zip_file = zipfile.ZipFile(zipfilename)

# We know they always have 3 characters after Super...
# For every possible combination of 3 letters from alphabet...
for c in itertools.product(alphabet, repeat=3):
    # Slowing it down on purpose to make it work better with the web terminal
    # Remove at your peril
    time.sleep(0.009)
    # Add the three letters to the first half of the password.
    password = first_half_password+''.join(c)
    # Try to extract the file.
    print ("Trying: %s" % password)
    # If the file was extracted, you found the right password.
    if extractFile(zip_file, str.encode(password)):
        print ('*' * 20)
        print ('Password found: %s' % password)
        print ('Files extracted...')
        exit(0)

# If no password was found by the end, let us know!
print ('Password not found.')

这是通过内置的str.encode方法实现的。