Advertisement
rfmonk

dpkt_parse_geo.py

Nov 30th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3.  
  4. # credit goes to book violentpython
  5. # copied here for educational reasons
  6.  
  7. import dpkt
  8. import socket
  9. import pygeoip
  10. import optparse
  11. gi = pygeoip.GeoIP('/opt/GeoIP/Geo.dat')
  12. def retGeoStr(ip):
  13.     try:
  14.         rec = gi.record_by_name(ip)
  15.         city = rec['city']
  16.         country = rec['country_code3']
  17.         if (city!=''):
  18.             geoLoc= city+", "+country
  19.         else:
  20.             geoLoc=country
  21.         return geoLoc
  22.     except Exception, e:
  23.         return 'Unregistered'
  24. def printPcap(pcap):
  25.     for (ts, buf) in pcap:
  26.         try:
  27.             eth = dpkt.ethernet.Ethernet(buf)
  28.             ip = eth.data
  29.             src = socket.inet_ntoa(ip.src)
  30.             dst = socket.inet_ntoa(ip.dst)
  31.             print '[+] Src: ' + src + ' --> Dst: ' + dst
  32.             print '[+] Src: ' + retGeoStr(src) + ' --> Dst: ' \
  33.              + retGeoStr(dst)
  34.         except:
  35.             pass
  36. def main():
  37.     parser = optparse.OptionParser('usage%prog -p <pcap file>')
  38.     parser.add_option('-p', dest='pcapFile', type='string',\
  39.         help='specify pcap filename')
  40.     (options, args) = parser.parse_args()
  41.     if options.pcapFile == None:
  42.         print parser.usage
  43.         exit(0)
  44.     pcapFile = options.pcapFile
  45.     f = open('geotest.pcap')
  46.     pcap = dpkt.pcap.Reader(f)
  47.     printPcap(pcap)
  48. if __name__ == '__main__':
  49.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement