뉴비에욤
0x8 Python Tutorials - Whois Automation 본문
이번 포스팅에서는 "Cymru's Team" 에서 제작한 파이썬 모듈을 보여줄 것이다. 정보보호라는 분야에서 공격적인 부분과 방어적인 부분 모두에서 상당히 많은 "whois" 작업을 하게 될 것인데 소개하고자 하는 파이썬 모듈이 큰 도움이 될 것이다. 우선 모듈의 기능부터 알아 볼 것이다.
모듈 주소 : https://pypi.python.org/pypi/cymruwhois/1.0
모듈을 설치한 뒤 임포트 하여 어떤 기능을 제공하는지 볼 수 있다.
해당 모듈에서 제공하는 "lookup" 함수를 이용하여 단일 IP에 대한 "lookup"을 수행할 수 있다. 나중에는 "lookupmany" 함수를 파이썬 스크립트 파일에서 사용하여 수행하고자 하는 IP 목록에 대하여 "lookup"을 수행할 것이다.
"cymruwhois" 모듈의 예제 (google 변수)를 이용하여 아래와 같은 함수를 이용하여 특정 정보를 뽑아 낼 수 있다.
위 사진에서 보여지는 명령어들을 루프를 이용하여 다양한 IP에 대하여 "lookup"을 수행 할 수 있지만 제작자는 이런 것들을 예측했는지 아예 "lookupmany" 라는 함수를 제공한다. 아래 스크립트는 "whois lookup"을 수행하고자 하는 IP 주소가 존재하는 파일로부터 IP 주소를 파싱하여 정보를 뽑아내는 예제 코드이다.
~# tcpdump -ttttnnr t.cap tcp[13]=2 | awk '{print $6}' | awk -F "." '{print $1"."$2"."$3"."$4}' > ips.txt reading from file t.cap, link-type LINUX_SLL (Linux cooked) ~# python ip2net.py -r ips.txt [+] Querying from: ips.txt 173.194.0.0/16 # - 173.194.8.102 (US) - GOOGLE - Google Inc.,US ~#
아래의 "ip2.net" 파이썬 파일은 이것 저것 모두 사용하는 파일이다. 한줄 한줄 보면서 이해하길 바란다.
#!/usr/bin/python import sys, os, optparse from cymruwhois import Client def look(iplist): c=Client() # creates an instance of the Client class try: if ips != None: r = c.lookupmany_dict(iplist) # leverages the lookupmany_dict() function to pass in a list of IPs for ip in iplist: # Iterates over the ips in the list to use a key value in the dictionary from lookupman_dict() net = r[ip].prefix; owner = r[ip].owner; cc = r[ip].cc # gets the networking information from the dictionary line = '%-20s # - %15s (%s) - %s' % (net,ip,cc,owner) # formats the line to print cleanly print line except: pass def checkFile(ips): # Checks to ensure the file can be read if not os.path.isfile(ips): print '[-] ' + ips + ' does not exist.' sys.exit(0) if not os.access(ips, os.R_OK): print '[-] ' + ips + ' access denied.' sys.exit(0) print '[+] Querying from: ' +ips def main(): parser = optparse.OptionParser('%prog '+ '-r <file_with IPs> || -i <IP>') parser.add_option('-r', dest='ips', type='string', help='specify target file with IPs') parser.add_option('-i', dest='ip', type='string', help='specify a target IP address') (options, args) = parser.parse_args() ip = options.ip # Assigns a -i <IP> to variable 'ip' global ips ips = options.ips # Assigns a -r <fileName> to variable 'ips' if (ips == None) and (ip == None): # If proper arguments aren't given print the script usage print parser.usage sys.exit(0) if ips != None: # Execute if ips has a value checkFile(ips) # Execute the function to check if the file can be read iplist = [] # create the ipslist list object for line in open(ips, 'r'): # Parse File to create a list iplist.append(line.strip('\n')) # Appends that line in the file to list and removes the new line char look(iplist) # pass the iplist list object to the look() function else: # Executes lookup() function for a single IP stored in variable 'ip' try: c=Client() r = c.lookup(ip) net = r.prefix; owner = r.owner; cc = r.cc line = '%-20s # - %15s (%s) - %s' % (net,ip,cc,owner) print line except: pass if __name__ == "__main__": main()
'primalsecurity.net > Python Tutorials' 카테고리의 다른 글
0xA Python Tutorials - Python for Metasploit Automation (0) | 2015.09.20 |
---|---|
0x9 Python Tutorials - Command Automation (0) | 2015.09.10 |
0x7 Python Tutorials - Web Scanning and Exploitation (0) | 2015.09.08 |
0x6 Python Tutorials - Spidering (0) | 2015.09.08 |
0x5 Python Tutorials - Web Requests (0) | 2015.09.08 |