Skip to content

Commit 6b1a4d2

Browse files
committed
add chapter
1 parent a31bb6f commit 6b1a4d2

2 files changed

Lines changed: 121 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
0x7 – [Web扫描和利用](https://github.com/smartFlash/pySecurity/blob/master/zh-cn/0x7.md)
3333

34-
0x8 – Whois查询
34+
0x8 – [Whois查询](https://github.com/smartFlash/pySecurity/blob/master/zh-cn/0x8.md)
3535

3636
0x9 – 系统命令调用
3737

zh-cn/0x8.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
##Whois自动查询
2+
3+
这一章将教大家一些技巧性的东西,教大家使用Cymru's团队提供的[whois模块](https://pypi.python.org/pypi/cymruwhois/1.0)来做一个whois信息查询工具,使用这个模块可以帮你节省大量的的时间,废话少说,现在就让我们开始吧!
4+
5+
首先你需要安装这个模块并且可以使用之前我们讲过的dir函数去看看这个模块提供了那些功能:
6+
7+
```
8+
>>> from cymruwhois import Client
9+
>>> c = Client()
10+
>>> dir(c)
11+
['KEY_FMT', '__doc__', '__init__', '__module__', '_begin', '_connect', '_connected', '_disconnect', '_lookupmany_raw', '_readline', '_sendline', 'c', 'cache', 'disconnect', 'get_cached', 'host', 'lookup', 'lookupmany', 'lookupmany_dict', 'port', 'read_and_discard']
12+
>>>
13+
```
14+
15+
现在我们使用lookup函数来查询一个单独的IP地址,在后面我们会使用"lookupmany"来查询一个IP数组列表:
16+
17+
```
18+
>>> google = c.lookup('8.8.8.8') #译者注:国内会被GFW
19+
>>> google
20+
<cymruwhois.record instance: 15169|8.8.8.8|8.8.8.0/24|US|GOOGLE - Google Inc.,US>
21+
>>> type(google)
22+
<type 'instance'>
23+
>>>
24+
```
25+
26+
现在我们有一个cymruwhois.record的实例,可以从中提取出下面这些信息:
27+
28+
```
29+
>>>
30+
>>> dir(google)
31+
['__doc__', '__init__', '__module__', '__repr__', '__str__', 'asn', 'cc', 'ip', 'owner', 'prefix']
32+
>>> google.ip
33+
'8.8.8.8'
34+
>>> google.owner
35+
'GOOGLE - Google Inc.,US'
36+
>>> google.cc
37+
'US'
38+
>>> google.asn
39+
'15169'
40+
>>> google.prefix
41+
'8.8.8.0/24'
42+
>>>
43+
```
44+
45+
我们以前思考处理多个ip列表的时候是使用for循环来处理的,但在这里我们并不需要使用for循环去遍历整个数组列表,我们可以使用Cymru团队提供的"lookupmany"函数去代替自己写的循环代码,下面将演示了一个比较复杂的脚本:从一个文件里面读取到IP列表,然后执行whois信息查询:
46+
47+
我们通常使用tcpdump,BPF还有bash-fu来处理IP列表,下面我们抓取了SYN包里面"tcp[13]=2"的ip并且通过awk的通道stdout与stdin把第六个元素使用" awk ‘{print $6}’"抓取出来,然后把使用awk抓取出来的ip写入到一个文件里面:
48+
49+
```
50+
~$ tcpdump -ttttnnr t.cap tcp[13]=2 | awk '{print $6}' | awk -F "." '{print $1"."$2"."$3"."$4}' > ips.txt
51+
reading from file t.cap, link-type LINUX_SLL (Linux cooked)
52+
~$ python ip2net.py -r ips.txt
53+
[+] Querying from: ips.txt
54+
173.194.0.0/16 # - 173.194.8.102 (US) - GOOGLE - Google Inc.,US
55+
~$
56+
```
57+
58+
现在让我看看ip2net.py这个脚本,里面有注释可以帮助你快速的理解这个脚本究竟是干些什么:
59+
60+
**ip2net.py**
61+
62+
```
63+
#!/usr/bin/env python
64+
import sys, os, optparse
65+
from cymruwhois import Client
66+
67+
def look(iplist):
68+
c=Client() # 创建一个Client的实例类
69+
try:
70+
if ips != None:
71+
r = c.lookupmany_dict(iplist) # 利用lookupmany_dict()函数传递IP列表
72+
for ip in iplist: #遍历lookupman_dict()传入的值
73+
net = r[ip].prefix; owner = r[ip].owner; cc = r[ip].cc #从字典里面获取连接后的信息
74+
line = '%-20s # - %15s (%s) - %s' % (net,ip,cc,owner) #格式化输出
75+
print line
76+
except:pass
77+
78+
def checkFile(ips): # 检查文件是否能够读取
79+
if not os.path.isfile(ips):
80+
print '[-] ' + ips + ' does not exist.'
81+
sys.exit(0)
82+
if not os.access(ips, os.R_OK):
83+
print '[-] ' + ips + ' access denied.'
84+
sys.exit(0)
85+
print '[+] Querying from: ' +ips
86+
87+
def main():
88+
parser = optparse.OptionParser('%prog '+ \
89+
'-r <file_with IPs> || -i <IP>')
90+
parser.add_option('-r', dest='ips', type='string', \
91+
help='specify target file with IPs')
92+
parser.add_option('-i', dest='ip', type='string', \
93+
help='specify a target IP address')
94+
(options, args) = parser.parse_args()
95+
ip = options.ip # Assigns a -i <IP> to variable 'ip'
96+
global ips; ips = options.ips # 赋值-r <fileName> 给变量 'ips'
97+
if (ips == None) and (ip == None): # 如果缺少参数就输出使用手册
98+
print parser.usage
99+
sys.exit(0)
100+
if ips != None: #检查ips
101+
checkFile(ips) #检查文件是否能够读取
102+
iplist = [] # 创建ipslist列表对象
103+
for line in open(ips, 'r'): # 解析文件内容
104+
iplist.append(line.strip('\n')) # 添加一行新内容并且删除换行字符
105+
look(iplist) # 调用look()函数
106+
107+
else: # 执行lookup()函数并且把内容存储到变量 'ip'
108+
try:
109+
c=Client()
110+
r = c.lookup(ip)
111+
net = r.prefix; owner = r.owner; cc = r.cc
112+
line = '%-20s # - %15s (%s) - %s' % (net,ip,cc,owner)
113+
print line
114+
except:pass
115+
116+
if __name__ == "__main__":
117+
main()
118+
```
119+
120+

0 commit comments

Comments
 (0)