|
1 | 1 | #!/usr/bin/python
|
2 | 2 |
|
3 | 3 | #Author: Suraj Patil
|
4 |
| -#Version: 1.0 |
| 4 | +#Version: 2.0 |
5 | 5 | #Date: 27th March 2014
|
6 | 6 |
|
7 | 7 | #Reads a pcap file into python using scapy and produces a csv file as output in the following format: Timestamp, source MAC address, source IP address, Destination MAC address, Destination IP address
|
| 8 | +#modular design, which makes it easy to include this source into another program, or to extend this one |
8 | 9 |
|
9 | 10 | from scapy.all import *
|
10 | 11 |
|
11 |
| -pcap_path = raw_input('Path of pcap file: ') # enter the path where you stored the pcap file |
12 |
| -file_path = raw_input('Enter target file name') #enter the path where you want to create a csv file |
| 12 | +def main(): |
| 13 | + pcap_path = raw_input('Path of pcap file: ') # enter the path where you stored the pcap file |
| 14 | + file_path = raw_input('Enter target file name') #enter the path where you want to create a csv file |
| 15 | + process(pcap_path, file_path) |
13 | 16 |
|
14 |
| -try: |
15 |
| - file = open(file_path+'.csv','w') #tries to opens the file in read mode |
16 |
| - pkts = rdpcap(pcap_path) #inbuilt function of scapy to read pcap files |
17 |
| - for i in range(len(pkts)): |
18 |
| - if ARP in pkts[i]: #checks if the packet is an ARP packet or not |
19 |
| - file.write('%d,%s,%s,%s,%s,%d,%s\n'%( pkts[i].time, pkts[i].hwsrc, pkts[i].psrc, pkts[i].hwdst, pkts[i].pdst, pkts[i].op)) |
20 |
| - print 'file write complete' |
21 |
| - #Timestamp, source MAC address, source IP address, Destination MAC address, Destination IP address |
22 |
| - file.close() |
23 |
| -except: |
24 |
| - print 'some error occured' |
| 17 | +def process(pcap_path, file_path): |
| 18 | + try: |
| 19 | + file = open(file_path+'.csv','w') #tries to opens the file in read mode |
| 20 | + pkts = rdpcap(pcap_path) #inbuilt function of scapy to read pcap files |
| 21 | + for i in range(len(pkts)): |
| 22 | + if ARP in pkts[i]: #checks if the packet is an ARP packet or not |
| 23 | + file.write('%d,%s,%s,%s,%s,%d,%s\n'%( pkts[i].time, pkts[i].hwsrc, pkts[i].psrc, pkts[i].hwdst, pkts[i].pdst, pkts[i].op)) |
| 24 | + print 'file write complete' |
| 25 | + #Timestamp, source MAC address, source IP address, Destination MAC address, Destination IP address |
| 26 | + file.close() |
| 27 | + except: |
| 28 | + print 'some error occured' |
| 29 | + |
| 30 | + |
| 31 | +main() |
0 commit comments