【问题标题】:Brute Force zip file with password returning syntax error带有密码返回语法错误的蛮力 zip 文件
【发布时间】:2020-08-13 19:47:29
【问题描述】:

我遇到了一个要解决的问题,要求在“Super”中再添加 3 个字母,然后用它来解锁一个 zip 文件。我的代码如下:

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, e:
        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.001)
    # 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.'

但我的代码返回

./code.py:第 6 行:意外令牌附近的语法错误 ('./code.py: line 6: def extractFile(zip_file, password):'

什么是语法错误,因为我找不到它?

【问题讨论】:

    标签: python zipfile


    【解决方案1】:

    你必须添加一个shebang: 在第一行添加

    #! /usr/bin/env python2
    

    【讨论】:

    • 谢谢,但我不知道为什么需要这样做?
    • 否则操作系统不知道用哪个解释器来运行这个文件
    • 啊,好的,谢谢!
    • 如果我能解决你的问题,请接受我的回答
    猜你喜欢
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    相关资源
    最近更新 更多