0x8 Python Tutorials - Whois Automation

2015. 9. 10. 01:52·primalsecurity.net/Python Tutorials

 

이번 포스팅에서는 "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
'primalsecurity.net/Python Tutorials' 카테고리의 다른 글
  • 0xA Python Tutorials - Python for Metasploit Automation
  • 0x9 Python Tutorials - Command Automation
  • 0x7 Python Tutorials - Web Scanning and Exploitation
  • 0x6 Python Tutorials - Spidering
초보 & 뉴비
초보 & 뉴비
보안과 개발(프론트는 좀 약함, 미적 감각 부재 이슈)을 좋아하며 업으로 삼고 있습니다.
  • 초보 & 뉴비
    보안과 그 개발, 그 어딘가
    초보 & 뉴비
  • 전체
    오늘
    어제
    • 분류 전체보기 (331)
      • 옵시디언 (1)
      • 도커&쿠버네티스 (1)
      • NAS(시놀로지&헤놀로지) (1)
      • Webhacking.kr (62)
      • Lord_of_SQL-Injections_I (27)
      • DVWA (0)
      • Root-Me.org (0)
      • Pwnable.kr (6)
      • HackerSchool_FTZ (20)
      • CodeEngn_Basic (20)
      • CodeEngn_Advance (0)
      • Lord_of_BoF_Redhat (1)
      • Lord_of_BoF_FC3 (5)
      • io_smashthestack (6)
      • n00bs CTF Labs (1)
      • 블록체인 (3)
      • Machine Learning (25)
        • Tensorflow (3)
        • PyTorch (18)
        • Visualize (4)
      • Kali 2.0 & Metasploit (16)
        • Windows Hacking (5)
        • Linux Hacking (0)
        • Malware (3)
        • ETC (8)
      • Fuzzing (2)
      • Windows (1)
      • Linux (4)
      • Android (2)
      • Android_Vuln (26)
      • 익스플로잇 (12)
      • 모의해킹 (4)
        • 워드프레스 (4)
      • SQL Injection (1)
      • System Hacking(OS) (5)
        • Shellcode (5)
      • Buffer OverFlow (9)
      • Reversing (44)
        • Lena's Reversing Tutorial f.. (41)
        • 이것 저것 (3)
      • ===== 번역 ===== (0)
      • primalsecurity.net (14)
        • Python Tutorials (14)
      • securityxploded.com (1)
        • IDA Pro (1)
      • 개인 정리 (11)
        • Burpsuite (11)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
  • 링크

  • 공지사항

    • 정보보안 관련 포스팅 주의사항
  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
초보 & 뉴비
0x8 Python Tutorials - Whois Automation
상단으로

티스토리툴바