Violent-Python
  • Introduction
  • 第 1 章 入门
  • 第 2 章 用 Python 进行渗透测试
  • 第 3 章 用 Python 进行取证调查
  • 第 4 章 用 Python 分析网络流量
  • 第 5 章 用 Python 进行无线网络攻击
  • 第 6 章 用 Python 刺探网络
  • 第 7 章 用 Python 实现免杀
Powered by GitBook
On this page
  • 第一个程序: UNIX 口令破解机
  • 第二个程序:一个 Zip 文件口令破解机

Was this helpful?

第 1 章 入门

第一个程序: UNIX 口令破解机

需要使用 UNIX 计算口令 hash 的 crypt() 算法,可以看到 Python 标准库中已自带 crypt

import crypt
crypt.crypt("egg", "HX")    # => word, salt

在基于 *Nix 的现代操作系统中,/etc/shadow 文件中存储了口令的 hash,并能使用更多安全的 hash 算法

第二个程序:一个 Zip 文件口令破解机

需要学习 zipfile 库,其中有 extractall() 方法,用可选参数指定密码

import zipfile
zFile = zipfile.ZipFile("evil.zip")
zFile.extractall(pwd="secret")

如果用错误的口令执行脚本,可以看到抛出异常了

可以利用线程同时测试多个口令,而不是只能逐个测试词库中的单词

import zipfile
from threading import Thread
def extractFile(zFile, password):
    try:
        zFile.extractall(pwd=password)
        print("[+] Found password {}".format(password))
    except:
        pass

def main():
    zFile = zipfile.ZipFile("evil.zip")
    passFile = open("dictionary.txt")
    for line in passFile.readlines():
        password = line.strip("\n")
        t = Thread(target=extractFile, args=(zFile, password))
        t.start()

还可以用 optparse 库提供参数选择

import zipfile
import optparse
from threading import Thread
def extractFile(zFile, password):
    try:
        zFile.extractall(pwd=password)
        print("[+] Found password {}".format(password))
    except:
        pass

def main():
    parser = optparse.OptionParser("usage%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, 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")
        t = Thread(target=extractFile, args=(zFile, password))
        t.start()
PreviousIntroductionNext第 2 章 用 Python 进行渗透测试

Last updated 5 years ago

Was this helpful?