Python zip暴力破解
使用python的zip和optparse模块结合之前生产的黑客字典,做一个可以暴力破解zip密码的小工具。
阅读文本前推荐您先浏览之前的文章:Python 创建黑客字典
前提
-
python提供了对zip文件的操作库zipfile。其中 ZipFile 类中的 extractall() 方法提供了 pwd 参数作为 zip 文件的密码。更多关于 zipfile
-
python 中的 optparse 模块可以用来处理命令行参数。其主要使用流程:首先,必须 import OptionParser 类,创建一个 OptionParser 对象,然后,使用 add_option 来定义命令行参数,每个命令行参数就是由参数名字符串和参数属性组成的。最后,一旦你已经定义好了所有的命令行参数,调用 parse_args() 来解析程序的命令行。更多关于 optparse
作为测试,我们将我们写一个名为 unzip.py 的脚本来破解密码,在它的同级目录下存在测试文件 test.zip 和字典文件 dict.txt(关于字典文件的创建),其中 zip 文件的密码为 123456 。
代码
import zipfile
import optparse
def extractFile(zFile, password):
try:
zFile.extractall(pwd=password)
print '[+] Found password ' + password + '\n'
return True
except:
return False
def main():
parser = optparse.OptionParser("usage%prog -f <zipfile> -d <dictionary>")
parser.add_option('-f', dest='zname', type='string', \
help='specify zipfile file')
parser.add_option('-d', dest='dname', type='string', \
help='specify dictionary file')
(options, args) = 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')
if extractFile(zFile, password):
break
if __name__ == '__main__':
main()