首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python 3 Zip密码破解器

Python 3 Zip密码破解器
EN

Stack Overflow用户
提问于 2017-09-01 05:11:21
回答 2查看 3.2K关注 0票数 0

我正在为一个学校项目开发一个简单的zip文件密码破解程序,我需要它来显示密码,一旦它从字典单词列表中破解它。每当我运行它时,它只提取文件,而不打印任何东西。我如何解决这个问题,使其也显示密码?这是我的代码。

代码语言:javascript
复制
import optparse
import zipfile
from threading import Thread

def extract_zip(zFile, password):
        try:
                password = bytes(password.encode('utf-8'))
                zFile.extractall(pwd=password)
                print ("[+] Password Found: " + password + '\n')
        except:
                pass

def Main():
        parser = optparse.OptionParser("useage &prog "+\
                        "-f <zipfile> -d <dictionary>")

        parser.add_option('-f', dest='zname', type='string',\
                        help='specify zip file')
        parser.add_option('-d', dest='dname', type='string',\
                        help='specify dictionary file')
        (options, arg) = parser.parse_args()
        if (options.zname == None) | (options.dname == None):
                print (parser.usage)
                exit(0)
        else:
                zname = options.zname
                dname = options.dname

        zFile = zipfile.ZipFile(zname)
        passFile = open(dname)

        for line in passFile.readlines():
            password = line.strip('\n')
            t = Thread(target=extract_zip, args=(zFile, password))
            t.start()

if __name__ == '__main__':
        Main()
EN

回答 2

Stack Overflow用户

发布于 2017-09-01 06:14:43

问题是,您试图打印编码后的密码,而不是原始密码。不能将字节连接到字符串。因此,打印原始密码,而不是bytes()的结果。

使用testzip()测试您是否可以解密它们,而不是从归档中提取所有文件。但要做到这一点,每个线程都需要自己的ZipFile对象。否则,它们将设置另一个线程使用的密码。

代码语言:javascript
复制
def extract_zip(filename, password):
    with ZipFile(filename) as zFile:
        try:
            password_encoded = bytes(password.encode('utf-8'))
            zFile.setpassword(password_encoded)
            zFile.testzip()
            print ("[+] Password Found: " + password + '\n')
        except:
            pass

然后将调用者更改为将文件名传递给线程,而不是zFile

票数 1
EN

Stack Overflow用户

发布于 2020-05-20 04:41:54

代码语言:javascript
复制
import zipfile
from tqdm import tqdm

def chunck(fd,size=65536):
    while 1:
        x=fd.read(size)
        if not x: 
            break
        yield x

def file_len(path):
    with open(path,'r',encoding='utf-8',errors='ignore') as fd:
        return sum(x.count('\n') for x in chunck(fd))

def linear_zip_crack(zip_path,pwd_path):
    ln=file_len(pwd_path)
    zp=zipfile.ZipFile(zip_path)
    with open(pwd_path,'rb') as fd:
        for x in tqdm(fd,total=ln,unit='x'):
            try:
                zp.extractall(pwd=x.strip())
            except:
                continue
            else:
                print(f'pwd={x.decode().strip()}')
                exit(0)
    print('Not found')

linear_zip_crack('spn.zip','pwds.txt')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45990156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档