2023-2024-1 20232817《Linux内核原理与分析》第十二周作业

2023-2024-1 20232817《Linux内核原理与分析》第十二周作业

Python实现Zip文件的暴力破解

实验原理

zipfile

首先我们进入 Code 目录:

cd Code

创建一个用于我们实验的目录 deZip:

mkdir attack_zip

进入 attack_zip 目录:

cd attack_zip

image-20231203122458094

现在我们创建 1.txt 文件:

touch 1.txt

1.txt 文件压缩成加密的 1.zip 文件,密码为 1314

zip -r 1.zip 1.txt -P 1314

image-20231203122544999

编写demo.py

import zipfile
try:
    with zipfile.ZipFile('1.zip') as zFile:     #创建 ZipFile 对象
        #解压文件
        zFile.extractall(path='./', pwd=b'1314')
        print('Extract the Zip file successfully!')
except:
    print('Extract the Zip file failed!')

将代码保存为 demo.py 我们测试一下是否能正确解压文件:

image-20231203122641571

argparse

argparse 的用法:

demo.py

import argparse

parser = argparse.ArgumentParser(description='Regards to your name.')
parser.add_argument('-n', dest='m_name', type=str, help='your name')
options = parser.parse_args()
print('Hello',options.m_name)

运行demo.py

image-20231203122913985

暴力破解 Zip 文件

基本思路就是我们不断去读取密码字典尝试解压带密码的 Zip 文件,如果成功则表示这个密码正确,失败则继续读取密码字典中的密码并尝试解压缩,直到解压缩成功或者密码字典中的密码都尝试一遍。

我们先写一个函数专门用于解压缩 Zip 文件,这个压缩函数有三个参数 zipFilepassword,savePath。含义如下:

  • zipFile 表示一个 ZipFile 对象。
  • password 表示解压 ZipFile 的密码。
  • savePath 表示解压后文件存储的路径。

这个函数在尝试密码之后会返回一个布尔值,如果解压成功会返回 True,如果失败则会返回 False

import zipfile
import argparse
import os
from os.path import *

def tryZipPwd(zipFile, password, savePath):
    try:
        zipFile.extractall(path=savePath, pwd=password.encode('utf-8'))
        print('[+] Zip File decompression success,password: %s' % (password))
        return True
    except:
        print('[-] Zip File decompression failed,password: %s' % (password))
        return False


def main():
    # 这里用描述创建了 ArgumentParser 对象
    parser = argparse.ArgumentParser(description='Brute Crack Zip')
    # 添加 - H 命令 dest 可以理解为咱们解析时获取 - H 参数后面值的变量名, help 是这个命令的帮助信息
    parser.add_argument('-f', dest='zFile', type=str, help='The zip file path.')
    parser.add_argument('-w', dest='pwdFile', type =str, help='Password dictionary file.')
    zFilePath = None
    pwdFilePath = None
    try:
        options = parser.parse_args()
        zFilePath = options.zFile
        pwdFilePath = options.pwdFile
    except:
        print(parser.parse_args(['-h']))
        exit(0)

    if zFilePath == None or pwdFilePath == None:
        print(parser.parse_args(['-h']))
        exit(0)

    with zipfile.ZipFile(zFilePath) as zFile:
        with open(pwdFilePath) as f:
            for pwd in f.readlines():
                p,f = split(zFilePath)
                dirName = f.split('.')[0]
                dirPath = join(p, dirName)
                try:
                    os.mkdir(dirPath)
                except:
                    pass
                ok = tryZipPwd(zFile, pwd.strip('\n'), dirPath)
                if ok:
                    break
if __name__ == '__main__':
    main()

创建一个用于测试的密码字典 pwd.txt

vim pwd.txt

内容如下:

image-20231203123151502

测试一下程序

image-20231203123256682

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值