forked from MLiONS/ECH-ENSI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archived_code.py
217 lines (195 loc) · 7.94 KB
/
archived_code.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# def snie_get_host():
# import socket
# s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# s.connect(("8.8.8.8", 80))
# host = s.getsockname()[0]
# print(host)
# return host
# TLS_VERSIONS = {
# # SSL
# "0x0002": "SSL_2_0",
# "0x0300": "SSL_3_0",
# # TLS:
# "0x0301": "TLS_1_0",
# "0x0302": "TLS_1_1",
# "0x0303": "TLS_1_2",
# "0x0304": "TLS_1_3",
# # DTLS
# "0x0100": "PROTOCOL_DTLS_1_0_OPENSSL_PRE_0_9_8f",
# "0x7f10": "TLS_1_3_DRAFT_16",
# "0x7f12": "TLS_1_3_DRAFT_18",
# "0xfeff": "DTLS_1_0",
# "0xfefd": "DTLS_1_1",
# }
# def snie_handle_tcp_packet(fp, packet):
# if packet['tcp'].dport == 443 or packet['tcp'].sport == 443: # Encrypted TCP packet
# if packet.haslayer('TLS'):
# tlsx = packet['TLS']
# if isinstance(tlsx, bytes):
# return packet
# tlsxtype = tlsx.type
# if tlsxtype == 22: # TLS Handshake
# for tls_msg in tlsx.msg:
# if isinstance(tls_msg, bytes):
# continue
# try:
# if tls_msg.msgtype is not None and tls_msg.msgtype == 1: # Client Hello
# snie_update_ch_info(fp, tls_msg, packet)
# snie_update_datasize(packet)
# elif tls_msg.msgtype == 11: # Certificate
# snie_update_cert_info(fp, tls_msg, packet)
# except AttributeError:
# pass
# # else:
# # print("Unsupported TLS message : " + str(tlsxtype))
# return packet
# def snie_update_datasize(packet):
# if not packet.haslayer('TCP'):
# return
# fe = open("./Output_data/e.txt", "a")
# f2 = open('./Output_data/snie_temp.csv', 'a', newline='')
# writer = csv.writer(f2)
# dwriter = csv.DictWriter(f2, fieldnames=csv_header)
# flow_id = str(packet['ip'].src) + "_" + str(packet['ip'].dst) + "_" + str(packet['tcp'].sport) + "_" \
# + str(packet['tcp'].dport)
# # print("Flow id : " + str(flow_id) + str(reader))
# f1 = open('./Output_data/snie.csv', 'r')
# reader = csv.reader(f1)
# dreader = csv.DictReader(f1, fieldnames=csv_header)
# for row in dreader:
# output_data = " P : " + str(packet['ip'].src) + ":" + str(packet['ip'].dst) + ":" + str(packet['tcp'].sport) + ":" + \
# str(packet['tcp'].dport) + "\n"
# output_data += " F : " + row["Source IP address"] + ":" + row["Destination IP address"] + ":" + \
# row["Source port"] + ":" + row["Destination Port"] + "\n"
# fe.write(output_data)
# if "Protocol" == row["Protocol"]:
# continue
# if "TCP" != str(row["Protocol"]):
# dwriter.writerow(row)
# continue
# if ((str(packet['ip'].src) == row["Source IP address"] and
# str(packet['ip'].dst) == row["Destination IP address"]) ) and \
# ((str(packet['tcp'].sport) == row["Source port"] and
# str(packet['tcp'].dport) == row["Destination Port"])):
# osize = int(row["Downloaded Data size (bytes)"])
# ti = float(row['Time'])
# # print("I time : " + str(ti))
# # print("osize : " + str(osize))
# psize = snie_get_tcppayloadlen(packet)
# # print("psize = " + str(psize))
# dsize = osize + psize
# # print("new size " + str(dsize))
# row['Downloaded Data size (bytes)'] = dsize
# te = packet.sniff_timestamp
# # print("E time : " + str(te))
# tdiff = te - ti
# # print("Diff = " + str(tdiff))
# tdiff = tdiff.total_seconds()
# # print("DiffS = " + str(tdiff))
# row["TLS session duration (s)"] = tdiff
# dwriter.writerow(row)
# else:
# # print("Not Updated row : " + str(row))
# dwriter.writerow(row)
# f1.close()
# f2.close()
# os.chdir('Output_data')
# os.system('del snie.csv')
# os.system('ren snie_temp.csv snie.csv')
# os.chdir('..')
# fe.close()
# def snie_fill_cert_info(tls_msg):
# cert = "NA"
# print("Certificate message detected")
# clen = tls_msg.certslen
# print("Certificate length = " + str(clen))
# for cert in tls_msg.certs:
# print(cert)
# def snie_fill_ch_info(fp, tls_msg, sni_info):
# #print("Printing TLS SNI info" + "\n")
# #print(tls_msg)
# #exit(0)
# ver = tls_msg.version
# sni_info[1] = str(ver)
# snil = ["NA"]
# for sniinfo in tls_msg['TLS_Ext_ServerName'].servernames:
# sni = ""
# #print("SNI Info per packet " + str(ver) + "\n")
# if True: #ver != "TLS_1_3":
# sni = sniinfo.servername.decode('utf-8')
# output_data = str(sni) + "\n"
# fp.write(output_data)
# #print(output_data + "\n")
# fe = open("./Output_data/e.txt", "a")
# fe.write("SNI added " + str(sni))
# fe.close()
# f1 = open('./Output_data/snie_temp.csv', 'a', newline='')
# if snil[0] == "NA":
# snil[0] = str(sni)
# else:
# snil.append(str(sni))
# f1.close()
# sni_info[2] = snil
# return sni_info
# def snie_get_proto_info(sni_info, packet):
# sni_info.append(str(packet['ip'].src))
# sni_info.append(str(packet['ip'].dst))
# sni_info.append(str(packet['tcp'].sport))
# sni_info.append(str(packet['tcp'].dport))
# sni_info.append(snie_get_tr_proto(packet['ip'].proto))
# sni_info.append(snie_get_tcppayloadlen(packet))
# sni_info.append(str(0))
# sni_info.append(str(0))
# sni_info.append(str(0))
# return sni_info
# def snie_update_ch_info(fp, tls_msg, packet):
# # print("ClientHello message detected")
# sni_info = []
# sni_info.append(str(packet.time))
# ver = tls_msg.version
# sni_info.append(ver)
# for sniinfo in tls_msg['TLS_Ext_ServerName'].servernames:
# sni = ""
# # print("SNI Info per packet ")
# if True: #ver != "TLS_1_3":
# sni = sniinfo.servername.decode('utf-8')
# output_data = str(sni) + "\n"
# fp.write(output_data)
# fe = open("./Output_data/e.txt", "a")
# fe.write("SNI added " + str(sni))
# fe.close()
# f1 = open('./Output_data/snie_temp.csv', 'a', newline='')
# writer = csv.writer(f1)
# sni_info.append(str(sni))
# sni_info = snie_get_proto_info(sni_info, packet)
# writer.writerow(sni_info)
# f1.close()
# def snie_update_cert_info(fp, tls_msg, packet):
# cert = "NA"
# print("Certificate message detected")
# clen = tls_msg.certslen
# print("Certificate length = " + str(clen))
# for cert in tls_msg.certs:
# print(cert)
# def snie_handle_tcp_packet(fp, packet):
# if packet['tcp'].dport == 443 or packet['tcp'].sport == 443: # Encrypted TCP packet
# if packet.haslayer('TLS'):
# tlsx = packet['TLS']
# if isinstance(tlsx, bytes):
# return packet
# tlsxtype = tlsx.type
# if tlsxtype == 22: # TLS Handshake
# for tls_msg in tlsx.msg:
# if isinstance(tls_msg, bytes):
# continue
# try:
# if tls_msg.msgtype is not None and tls_msg.msgtype == 1: # Client Hello
# snie_update_ch_info(fp, tls_msg, packet)
# snie_update_datasize(packet)
# elif tls_msg.msgtype == 11: # Certificate
# snie_update_cert_info(fp, tls_msg, packet)
# except AttributeError:
# pass
# # else:
# # print("Unsupported TLS message : " + str(tlsxtype))
# return packet