• 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
    

  • 相关阅读:
    WCF相关
    MiniUI级联
    大家一起来学 NHibernate+NUnit (VS2012+SQL Server2008)
    C# 复杂算法
    sql自定义日期函数,返回范围内日期和星期数表。
    RDLC开发笔记
    解决IE7和IE6不支持javaScript中的indexOf函数的问题
    Sql获取周、月、年的首尾时间。
    Sql Server中实现Mysql中的group_concat函数效果
    RDLC隔行变色的实现
  • 原文地址:https://www.cnblogs.com/lxy666666/p/6159871.html
Copyright © 2020-2023  润新知