-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestPacketCaptureScript.py
More file actions
executable file
·186 lines (160 loc) · 5.73 KB
/
TestPacketCaptureScript.py
File metadata and controls
executable file
·186 lines (160 loc) · 5.73 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
The program when run, detects the use of anonymizing services in a test dataset
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import socket
import csv
from struct import *
import tkinter as tk
from threading import Thread
from pathvalidate import ValidationError, validate_filename
import tkinter.simpledialog as simpledialog
import platform
from toolTipText import *
#test_file is the name of the .csv file
def packet_capture(test_file):
c = True
#Create an INET, StreamingSocket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
except socket.error as e:
tk.messagebox.showerror("Socket Error",e)
c = False
except Exception as e:
tk.messagebox.showerror("Error!!!",e)
c = False
finally:
if not c:
startButton['state'] = 'normal'
stopButton['state'] = 'disabled'
label['text'] = ""
return
#create a csv file to store the captured packets
outputFile = open('Datasets/Test/'+test_file,'w',newline='')
writer = csv.writer(outputFile)
#Write top row
writer.writerow(['Version', 'Protocol', 'TTL', 'SrcAddress', 'DestAddress', 'SrcPort', 'DestPort', 'SeqNum', 'AckNum', 'Flag', 'DataSize', 'Service'])
#Receive packet
while True:
if stop == 1:
label['text'] = ""
startButton['state'] = 'normal'
break
packet = s.recvfrom(65565)
#Transfer tuple content to string type
packet = packet[0]
#Take 1st 20 bytes for IP header
ip_header = packet[0:20]
#Unpack from bytes format
iph = unpack('!BBHHHBBH4s4s', ip_header)
version_ihl = iph[0]
version = version_ihl >> 4
ihl = version_ihl & 0xF
iph_length = ihl * 4
ttl = iph[5]
protocol = iph[6]
s_addr = socket.inet_ntoa(iph[8])
d_addr = socket.inet_ntoa(iph[9])
#TCP header starts right after IP header and is ususally 20 bytes long
tcp_header = packet[20:40]
#Unpack from byte format
tcph = unpack('!HHLLBBHHH', tcp_header)
source_port = tcph[0]
dest_port = tcph[1]
sequence = tcph[2]
ack = tcph[3]
doff_reserved = tcph[4]
tcph_length = doff_reserved >> 4
h_size = iph_length + tcph_length * 4
data_size = len(packet) - h_size
#Select byte containing TCP flags
tcpFlag = packet[33:34].hex()
if tcpFlag == "01":
Flag = "FIN"
elif tcpFlag == "02":
Flag = "SYN"
elif tcpFlag == "03":
Flag = "FIN-SYN"
elif tcpFlag == "08":
Flag = "PSH"
elif tcpFlag == "09":
Flag = "FIN-PSH"
elif tcpFlag == "0A":
Flag = "SYN-PSH"
elif tcpFlag == "10":
Flag = "ACK"
elif tcpFlag == "11":
Flag = "FIN-ACK"
elif tcpFlag == "12":
Flag = "SYN-ACK"
elif tcpFlag == "18":
Flag = "PSH-ACK"
else:
Flag = "OTH"
#Select only HTTP/HTTPS packets for logging
if source_port == 80 or source_port == 443:
if source_port == 80:
writer.writerow([str(version), str(protocol), str(ttl), str(s_addr), str(d_addr), str(source_port), str(dest_port), str(sequence), str(ack), Flag, str(data_size), "HTTP"])
print("Packets captured")
else:
writer.writerow([str(version), str(protocol), str(ttl), str(s_addr), str(d_addr), str(source_port), str(dest_port), str(sequence), str(ack), Flag, str(data_size), "HTTPS"])
print("Packets captured")
#after stopping capture
outputFile.close()
tk.messagebox.showinfo("Capture Complete", "Test Capture is complete. You may now close the window")
#create thread to capture packets
def start_capture():
startButton['state'] = 'disabled'
stopButton['state'] = 'normal'
#ask for file name
test_file_name = simpledialog.askstring(title="File Name", prompt="Enter the test file name (without .csv extension): ", initialvalue='test')
if test_file_name is None:
startButton['state'] = 'normal'
stopButton['state'] = 'disabled'
return
try:
validate_filename(test_file_name, platform=platform.system()) #validate the filename for current OS
except ValidationError as e:
tk.messagebox.showerror("Invalid Filename", "The entered filename is invalid:\n" + str(e))
startButton['state'] = 'normal'
stopButton['state'] = 'disabled'
return
test_file_name += '.csv'
global stop
stop = 0
t1 = Thread(target=packet_capture, args=(test_file_name,))
t1.start()
label['text'] = "Capturing packets..."
#complete execution of thread
def stop_capture():
stopButton['state'] = 'disabled'
global stop
stop = 1
label['text'] = 'Please wait...'
#main window
root = tk.Tk()
root.title("Packet Capture")
root.geometry("450x100")
root.resizable(False,False)
root.option_add('*Dialog.msg.font', 'Helvetica 12') #change font of popup messages
app = tk.Frame(root)
app.grid()
startButton = tk.Button(app, text="Start capture", width=15, font=('Courier',12), bg='gray', command=lambda: start_capture())
stopButton = tk.Button(app, text="Stop capture", width=15, font=('Courier',12), bg='gray', state='disabled', command=stop_capture)
CreateToolTip(startButton, text="Start packet capture")
CreateToolTip(stopButton, text="Stop packet capture")
startButton.grid(row=0,column=0)
stopButton.grid(row=0,column=1)
label = tk.Label(app, text="For better accuracy, capture packets for 1-2 minutes!", font=('Courier',9), anchor='w', justify='left')
label.grid(row=1, column=0, columnspan=2)
app.mainloop()