《Python绝技 运用Python成为顶级黑客》
《Python绝技 运用Python成为顶级黑客》
书评:
- 可能是为了便于Linux使用,例子使用了Python2的语法(如
print
语句没有了括号),这导致了使用python3运行时可能会有点问题
目录
入门
安装python-nmap包
Linux安装
wget http://xael.org/norman/python/python-nmap/pythonmap-0.2.4.tar.gz-On map.tar.gz
tar -xzf nmap.tar.gz
cd python-nmap-0.2.4
python setup.py install
用easy_install模块进行安装
easy_install python-nmap
安装常用的库
easy install pyPdf python-nmap pygeoip mechanize BeautifulSoup4
实战程序
UNIX口令破解器
知识点:
socket模块
、crypt模块
、hashlib模块
加密口令获得:(存入
passwords.txt
)
$ cat /etc/passwd
victim: HX9LLTdc/jiDE: 503:100:Iama Victim:/home/victim:/bin/sh
root: DFNFxgW7C05fo: 504:100: Markus Hess:/root:/bin/bash
- demo:
import crypt # 一种加密方式的模块(UNIX口令)
def testPass(cryptPass): # 密码尝试函数
salt = crypyPass[0:2]
dictFile = open('dictionary.txt','r') # 字典
for word in dictFile.readlines():
word = word.strip('\n')
cryptWord = crypt.crypt(word,salt)
if (cryptWord == cryptPass):
print "[+] Found Passeord: "+word+"\n"
return
def main(): # 入口函数,
passFile = open('passwords.txt') # 密码
for line in passFile.readlines():
if ":" in line:
user = line.split(':')[0]
cryptPass = line.split(':')[1].strip(' ')
print "[*] Crack Password For: "+user
testPass(cryptPass) # 调用函数
if __name__ == "__main__": # 入口函数模拟
main()
- 调用与输出:
$ python crack.py
[*] Cracking Password For: victim
[+] Found Password: egg
[*] Cracking Password For: root
[-] Password Not Found.
- 补充:
在基于
*Nix
的现代操作系统中,/etc/shadow文件存储口令的hash,并能使用更多安全的hash算法比如SHA-512 hash算法。
SHA-512
函数需要在hashlib库
找到
Zip文件口令破解机
- 知识点:
zipfile模块
、threading模块Thread类
、optparse模块
- 使用:
help('zipfile')
查看用法 - demo,模块基本用法(
unzip.py
)
import zipfile
zFile = zipfile.zipFile('evil.zip')
passFile = open('dictionary.txt')
for line in passFile.readlines():
- demo,Zip文件口令破解机(
zip-file-cracker.py
)
import zipfile # zip文件模块
from threading import Thread # 多线程模块
def extractFile(zFile, password): # 尝试使用指定密码破解
try:
zFile.extractall(pwd=password)
return password
except:
return
def main():
zFile = zipfile.ZipFile('evil.zip') # 用字典作参构造对象实例
passFile = open('dictionary.txt') # 打开字典
for line in passFile.readlines(): # 并遍历字典
password = line.strip('\n')
''' 普通版本
guess = extractFile(zFile, password) # 尝试调用函数破解密码
if guess:
print '[+] Password = '+password+'\n'
exit(0) # 退出程序
'''
''' 多线程版本 '''
t = Thread(target=extractFile, args=(zFile, password)) # 创建多线程实例
t.start() # 运行多线程
if __name__ == '__main__':
main()
- demo,使用
optparse
进行口令保护的优化
# 使用:`$ python unzip.py -f evil.zip -d dictionary.txt`
import zipfile
import optparse
from threading import Thread
def extractFile(zFile, password): # 尝试使用指定密码破解
try:
zFile.extractall(pwd=password)
return password
except:
return
def main():
# 会生成一个参数解析器(option parser)类的实例
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')
(opetions, args) = parser.parse_args() # python的解压缩赋值写法
if(options.zname == None) | (options.dname == None): # 检验参数是否正确
print parser.usage
exit(0)
else:
zname = option.zname
dname = option.dname
zFile = zipfile.ZipFile(zname) # 用字典作参构造对象实例
passFile = open(dname) # 打开字典
for line in passFile.readlines(): # 并遍历字典
password = line.strip('\n')
''' 普通版本
guess = extractFile(zFile, password) # 尝试调用函数破解密码
if guess:
print '[+] Password = '+password+'\n'
exit(0) # 退出程序
'''
''' 多线程版本 '''
t = Thread(target=extractFile, args=(zFile, password)) # 创建多线程实例
t.start() # 运行多线程
if __name__ == '__main__':
main()
其他
本书中用到的模块
内置模块 / 标准库模块(不用下载,不用显式导入)
【内置】string模块
【内置】文件输入/输出模块
- demo
f = open("vuln_banners.txt",'r')
for line in f.readlines():
if line.strip('\n') in list:
pass
自带模块(不用下载,要显式导入)
【标准】socket模块(网络模块)
官网参考文档:https://docs.python.org/zh-cn/3/library/socket.html
demo
import socket
socket.setdefaulttimeout(2) # 设置超时
s = socket.socket() # 创建实例
s.connect(("192.168.95.148",21)) # 连接指定ip和端口
ans = s.recv(1024) # 读取接下来1KB数据
print ans # 打印
# 200 FreeFloat Ftp Server (Version 1.00)
【标准】optparse(参数解析)
- 载入:
import optparse
- 作用:比sys模块的.argv参数更进一步。用于解析脚本标志和可选参数
- demo:详见
Zip口令破解器demo
【标准】os模块
- 作用:提供了丰富的适用于各种操作系统的函数,可以交互的对象:
- 操作系统环境、文件系统、用户数据、权限
- demo
import os
# ...
if not os.path.isfile(filename): # 没有该文件
pass
if not os.access(filename, os.R_OK): # 没有权限访问该文件
pass
【标准】OrderedDict类(有序字典)
- 载入:
from collections import OrderedDict
【标准】threading的Thread类
- 载入:
from threading import Thread
【标准】time
- 载入:
import time
- 作用:主要提供
sleep
Hack模块
【标准】crypy(UNIX加密)
- 使用:详见第一个程序——UNIX加密
【标准】hashlib(SHA-512函数等hash算法)
- 使用:详见第一个程序——UNIX加密
【标准】zipfile
- 使用:详见第二个程序——Zip文件口令破解机
【第三方】namp
第三方模块地址:http://xael.org/norman/python/python-nmap/(手动下载地址)
载入:
easy_install python-nmap
,import nmap
使用:详见端口扫描器demo(namp版本)
作用:提供多种类型的扫描,如Namp工具包提供的
ACK
、RST
、FIN
、SYN-ACK
扫描等扫描类型 说明 TCP SYN SCAN
半开放扫描。发送一个SYN包,启动一个TCP会话,并等待响应的数据包
若接收到reset包,则表示端口关闭。若接受到SYN/ACK包,则表示端口打开TCP NULL SCAN
把TCP头中的所有标志都设为NULL
若接收到的是一个RST包,则表示端口关闭TCP FIN SCAN
发送一个表示拆除一个活动的TCP连接的FIN包,让对方关闭连接
若接收到一个RST包,则表示相应端口关闭TCP XMAS SCAN
发送PSH、DIN、URG和TCP标志位设为1的数据包
若接收到一个RST包,则表示相应端口关闭
【第三方】Pexpect
- 第三方模块地址:http://pexpect.sourceforge.net(手动下载地址)
- 作用:实现与程序交互、等待预期的屏幕输出,并根据此做出不同的响应
【第三方】Pxssh(包含Pexpect)
链接到当前文件 0
没有文件链接到当前文件