zoukankan      html  css  js  c++  java
  • 20169203《Linux内核原理及分析》第十二周作业

    本周的实验是使用Python实现Zip文件的暴力破解
    从本次实验的主题可以看出我们的重点是对zip文件的操作,而zipfile就是本次实验的核心。zipfile模块是python中自带的模块,提供了对zip文件的创建读、写、追加、解压以及列出zip文件列表的工具。这里我们主要用到ZipFile对象的extractall 方法来解压zip文件,现在我们看一下ZipFile的文档,在shell中运行Python3交互环境,并使用help方法来查看模块的使用方法。

    可以看到extractall(path=None, members=None, pwd=None)方法主要有三个参数,我们来看一下每个参数的含义:
    path指定解压后文件的存储位置
    members(可选)指定要Zip文件中要解压的文件,这个文件名称必须是通过namelist()方法返回列表的子集
    pwd指定Zip文件的解压密码
    接下来我们尝试用zipfile模块解压一个带密码的Zip文件,创建1.txt文件,将1.txt文件压缩成加密的1.zip文件,密码为1234

    然后用python通过zipfile模块来对压缩文件进行解密,其python代码如下

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


    接下来我们尝试用密码暴力破解,给订指定的密码字典,逐条尝试,直到成破解。

    import zipfile
    
    zFile = zipfile.ZipFile("hello.zip");
    
    passFile = open('./pass.txt');
    
    for line in passFile.readlines():
    
    password = line.strip('
    ');
    
    try:
    
    zFile.extractall(path="./",pwd=password);
    
    print("当前测试密码为:"+password+" 密码正确!!!");
    
    break;
    
    exit(0);
    
    except:
    
    print("当前测试密码为:"+password+" 密码错误!!!");
    
    pass;
    

    最后给出最终的暴力破解代码

    import random
    
    import zipfile
    
    class Dictor():
    
    CSet=‘0123456789'
    
    'abcdefghijklmnopqrstuvwxyz'
    
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    '~!@#$%^&*()_+’
    
    def init(self,minlen,maxlen):
    
    if maxlen>minlen:
    
    self.__minlen=minlen
    
    self.__maxlen=maxlen
    
    else:
    
    self.__minlen=maxlen
    
    self.__maxlen=minlen
    
    def iter(self):
    
    return self
    
    def next(self):
    
    ret=''
    
    for i in range(0,random.randrange(self.__minlen,self.__maxlen+1)):
    
    ret+=random.choice(Dictor.CSet)
    
    return ret
    
    if name=='main':
    
    zFile = zipfile.ZipFile("hello.zip");
    
    for str in Dictor(5,6):
    
    try:
    
    zFile.extractall(path="./",pwd=str)
    
    print("当前测试密码为:"+str+" 密码正确!!!")
    
    break
    
    except:
    
    print("当前测试密码为:"+str+" 密码错误!!!");
    
    pass
    

  • 相关阅读:
    r语言 包说明
    python中的map()函数
    Java打印整数的二进制表示(代码与解析)
    python 两个字典合并
    判断一个字符串中是否含有中文字符:
    Python字符串的encode与decode研究心得——解决乱码问题
    javac编译成功,用java运行class文件出现“找不到或无法加载主类” 的问题
    python读取剪贴板报错 pywintypes.error: (1418, 'GetClipboardData', 'xcfxdfxb3xccxc3xbbxd3xd0xb4xf2xbfxaaxb5x
    python openpyxl 2.5.4 版本 excel常用操作封装
    pycharm 设置文件编码的位置:Editor-->File Encodings
  • 原文地址:https://www.cnblogs.com/lxy666666/p/6159871.html
Copyright © 2011-2022 走看看