Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python -tt
- from scapy.all import *
- import sys
- from datetime import datetime
- '''Parse PCAP files into easy to read NETFLOW like output\n
- Usage:\n
- python cap2netflow.py <[ pcap filename or -l ]>\n
- -l is live capture switch\n
- ICMP packets print as source ip, type --> dest ip, code''
- def parse_netflow(pkt):
- # grabs 'netflow-esqe' fields from packets in a PCAP file
- if pkt.haslayer(IP):
- type = pkt.getlayer(IP).proto
- else:
- type = 0
- snifftime = datetime.fromtimestamp(pkt.time).strftime('%Y-%m-%d %H:%M:%S').split(' ')[1]
- if type == 6:
- type = 'TCP'
- if type == 17:
- type = 'UDP'
- if type == 1:
- type = 'ICMP'
- if type == 'TCP' or type == 'UDP':
- print( ' '.join([snifftime, type.rjust(4, ' '), str(pkt.getlayer(IP).src).rjust(15, ' ') , str(pkt.getlayer(type).sport).rjust(5, ' ') , '-->' , str(pkt.getlayer(IP).dst).rjust(15, ' ') , str(pkt.getlayer(type).dport).rjust(5, ' ')]))
- elif type == 'ICMP':
- print(' '.join([snifftime, 'ICMP'.rjust(4, ' '), str(pkt.getlayer(IP).src).rjust(15, ' ') , ('t: '+ str(pkt.getlayer(ICMP).type)).rjust(5, ' '), '-->' , str(pkt.getlayer(IP).dst).rjust(15, ' '), ('c: ' + str(pkt.getlayer(ICMP).code)).rjust(5, ' ')]))
- else:
- pass
- if '-l' in sys.argv:
- sniff(prn=parse_netflow)
- else:
- pkts = rdpcap(sys.argv[1])
- print(' '.join(['Date: ',datetime.fromtimestamp(pkts[0].time).strftime('%Y-%m-%d %H:%M:%S').split(' ')[0]]))
- for pkt in pkts:
- parse_netflow(pkt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement