-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
executable file
·111 lines (89 loc) · 3.75 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'''
The arguments must given in order -> python main.py [pcap-file(without .pacp)] outputsFiles
outputFiles ->
1) csv
2) byte
3) unhex // First generates the hex file in the 'HexFiles' Directory, then unhexlify it to unhex file in 'UnHexFiles' Directory
4) ip
example:
1 ) python main.py iperf-mptcp-0-0 csv byte unhex
This will generate all of the given files
2 ) python main.py iperf-mptcp-0-0 byte
This will generate only byte File
** if you don't give output Files -> All of the outputs will be generated
example :
python main.py iperf-mptcp-0-0
'''
import binascii, os, sys, re
def generateCSV(pcapFile):
print("Going to make " + pcapFile + " csv File\n\n")
os.system("tshark -r " + "pcaps/" + pcapFile +".pcap -T fields -e _ws.col.Info > "+ "csvs/" + pcapFile +".csv")
print ("csv file created\n\n*******************\n\n")
def generateBytes(pcapFile):
print("Going to make " + pcapFile + " byte File\n\n")
os.system("tshark -r " + "pcaps/" + pcapFile + ".pcap -x >" + "Bytes/" + pcapFile)
print("byte file Created\n\n*******************\n\n")
def generateHEX(pcapFile):
print("Going to make " + pcapFile + " hex File\n\n")
os.system("tshark -r " + "pcaps/" + pcapFile + ".pcap -T fields -e data >" + "HexFiles/" + pcapFile + "-hex")
print("hex file Created\n\n*******************\n\n")
def generateUnHex(pcapFile):
print("Hex File should be generated first \n\n")
generateHEX(pcapFile)
print("Going to make " + pcapFile + " unhex File\n\n")
lines = filter(None, (line.rstrip() for line in open('HexFiles/' + pcapFile + "-hex", 'r')))
finalLines = []
for line in lines:
finalLines.append(binascii.unhexlify(line))
#re.sub("[^0-9]", "", finalLines[1])
myUnHexFile = open('UnHexFiles/' + pcapFile + "-unhex", 'w')
for finalLine in finalLines:
tmp = ""
flag = False
for inMyLine in finalLine:
if(inMyLine.isdigit() or inMyLine.isalpha()):
tmp += inMyLine
if(not flag):
flag = True
if((inMyLine == " " or inMyLine == ".") and flag):
tmp += inMyLine
finalLine = tmp
myUnHexFile.write(finalLine)
myUnHexFile.write("\n")
myUnHexFile.close()
print("unhex file Created\n\n*******************\n\n")
def generateIPs(pcapFile):
print("Going to make " + pcapFile + " IPs File\n\n")
os.system("tshark -r " + "pcaps/" + pcapFile + ".pcap -T fields -e ip.src -e ip.dst -e frame.time >" + "IPs/" + pcapFile + "-ips")
print("IPs file Created\n\n*******************\n\n")
def main():
myArgs = sys.argv
if(len(myArgs[1:]) == 0):
myFlag = False
print("You did not give any pcap file!")
else:
pcapFile = myArgs[1]
myFlag = True
print("pcap file -> " + pcapFile + " Captured\n\n*******************")
if(myFlag):
outputArgs = myArgs[2:]
if(len(outputArgs) == 0):
print("Going to Generate all the output files !\n\n*******************\n\n")
generateCSV(pcapFile)
generateBytes(pcapFile)
generateUnHex(pcapFile)
generateIPs(pcapFile)
else:
for outputArg in outputArgs:
if(outputArg == "csv"):
generateCSV(pcapFile)
elif(outputArg == "byte"):
generateBytes(pcapFile)
elif(outputArg == "unhex"):
generateUnHex(pcapFile)
elif(outputArg == "ip"):
generateIPs(pcapFile)
else:
print("Given Argument is not correct")
if __name__ == "__main__":
main()