標籤:

0x8:Whois自動查詢

##Whois自動查詢

這一章將教大家一些技巧性的東西,教大家使用Cymrus團隊提供的whois模塊來做一個whois信息查詢工具,使用這個模塊可以幫你節省大量的的時間,廢話少說,現在就讓我們開始吧!

首先你需要安裝這個模塊並且可以使用之前我們講過的dir函數去看看這個模塊提供了那些功能:

>>> from cymruwhois import Clientn>>> c = Client()n>>> dir(c)n[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]n>>>n

現在我們使用lookup函數來查詢一個單獨的IP地址,在後面我們會使用"lookupmany"來查詢一個IP數組列表:

>>> google = c.lookup(8.8.8.8) #譯者注:國內會被GFWn>>> googlen<cymruwhois.record instance: 15169|8.8.8.8|8.8.8.0/24|US|GOOGLE - Google Inc.,US>n>>> type(google)n<type instance>n>>>n

現在我們有一個cymruwhois.record的實例,可以從中提取出下面這些信息:

>>>n>>> dir(google)n[__doc__, __init__, __module__, __repr__, __str__, asn, cc, ip, owner, prefix]n>>> google.ipn8.8.8.8n>>> google.ownernGOOGLE - Google Inc.,USn>>> google.ccnUSn>>> google.asnn15169n>>> google.prefixn8.8.8.0/24n>>>n

我們以前思考處理多個ip列表的時候是使用for循環來處理的,但在這裡我們並不需要使用for循環去遍歷整個數組列表,我們可以使用Cymru團隊提供的"lookupmany"函數去代替自己寫的循環代碼,下面將演示了一個比較複雜的腳本:從一個文件裡面讀取到IP列表,然後執行whois信息查詢:

我們通常使用tcpdump,BPF還有bash-fu來處理IP列表,下面我們抓取了SYN包裡面"tcp[13]=2"的ip並且通過awk的通道stdout與stdin把第六個元素使用" awk 『{print $6}』"抓取出來,然後把使用awk抓取出來的ip寫入到一個文件裡面:

~$ tcpdump -ttttnnr t.cap tcp[13]=2 | awk {print $6} | awk -F "." {print $1"."$2"."$3"."$4} > ips.txtnreading from file t.cap, link-type LINUX_SLL (Linux cooked)n~$ python ip2net.py -r ips.txtn[+] Querying from: ips.txtn173.194.0.0/16 # - 173.194.8.102 (US) - GOOGLE - Google Inc.,USn~$n

現在讓我看看ip2net.py這個腳本,裡面有注釋可以幫助你快速的理解這個腳本究竟是幹些什麼:

ip2net.py

#!/usr/bin/env pythonnimport sys, os, optparsenfrom cymruwhois import Clientn ndef look(iplist):n c=Client() # 創建一個Client的實例類n try:n if ips != None:n r = c.lookupmany_dict(iplist) # 利用lookupmany_dict()函數傳遞IP列表n for ip in iplist: #遍歷lookupman_dict()傳入的值n net = r[ip].prefix; owner = r[ip].owner; cc = r[ip].cc #從字典裡面獲取連接後的信息n line = %-20s # - %15s (%s) - %s % (net,ip,cc,owner) #格式化輸出n print linen except:passn ndef checkFile(ips): # 檢查文件是否能夠讀取n if not os.path.isfile(ips):n print [-] + ips + does not exist.n sys.exit(0)n if not os.access(ips, os.R_OK):n print [-] + ips + access denied.n sys.exit(0)n print [+] Querying from: +ipsn ndef main():n parser = optparse.OptionParser(%prog + n -r <file_with IPs> || -i <IP>)n parser.add_option(-r, dest=ips, type=string, n help=specify target file with IPs)n parser.add_option(-i, dest=ip, type=string, n help=specify a target IP address)n (options, args) = parser.parse_args()n ip = options.ip # Assigns a -i <IP> to variable ipn global ips; ips = options.ips # 賦值-r <fileName> 給變數 ipsn if (ips == None) and (ip == None): # 如果缺少參數就輸出使用手冊n print parser.usagen sys.exit(0)n if ips != None: #檢查ipsn checkFile(ips) #檢查文件是否能夠讀取n iplist = [] # 創建ipslist列表對象n for line in open(ips, r): # 解析文件內容n iplist.append(line.strip(n)) # 添加一行新內容並且刪除換行字元n look(iplist) # 調用look()函數n n else: # 執行lookup()函數並且把內容存儲到變數 ipn try:n c=Client()n r = c.lookup(ip)n net = r.prefix; owner = r.owner; cc = r.ccn line = %-20s # - %15s (%s) - %s % (net,ip,cc,owner)n print linen except:passn nif __name__ == "__main__":n main()n

推薦閱讀:

碎片化學習Python的又一神作:termux
Python入門到精通視頻課程(8)
用印象筆記的Python SDK介面開發個自用小工具

TAG:Python教程 |