-
Notifications
You must be signed in to change notification settings - Fork 26
/
satoriTCP.py
420 lines (349 loc) · 12.7 KB
/
satoriTCP.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import untangle
import struct
import satoriCommon
from pathlib import Path
from pypacker.layer12 import ethernet
from pypacker.layer3 import ip
from datetime import datetime
# grab the latest fingerprint files:
# wget chatteronthewire.org/download/updates/satori/fingerprints/tcp.xml -O tcp.xml
#
# looking for new fingerprints
# python3 satori.py > output.txt
# cat output.txt | awk -F';' '{print $3, $4, $5, $6, $7}' | sort -u > output2.txt
# cat output.txt | awk -F';' '{print $5";"$6";"$7}' | sort -u > output2.txt
#
def version():
dateReleased='satoriTCP.py - 2024-05-27'
print(dateReleased)
def tcpProcess(pkt, layer, ts, pypackerVersion, sExactList, saExactList, sPartialList, saPartialList): #instead of pushing the fingerprint files in each time would it make sense to make them globals? Does it matter?
if layer == 'eth':
src_mac = pkt[ethernet.Ethernet].src_s
else:
#fake filler mac for all the others that don't have it, may have to add some elif above
src_mac = '00:00:00:00:00:00'
# print(pkt)
ip4 = pkt.upper_layer
tcp1 = pkt.upper_layer.upper_layer
timeStamp = datetime.utcfromtimestamp(ts).isoformat()
fingerprint = None
# lets verify we have tcp options and it is a SYN or SYN/ACK packet
if (len(tcp1.opts) > 0) and ((tcp1.flags == 0x02) or (tcp1.flags == 0x12)):
p0fSignature = ''
tcpSignature = ''
ethercapSignature = ''
[ipVersion, ipHdrLen] = computeIP(ip4.v_hl)
[ethTTL, ttl] = computeNearTTL(ip4.ttl)
#this seems to work for now, but may not be a perfect fix for the changes from 4.9 to 5.0
if int(pypackerVersion[0]) >= 5:
[df, mf, offset] = computeIPOffset(ip4.frag_off)
else: #4.9 or below use .off
[df, mf, offset] = computeIPOffset(ip4.off)
winSize = tcp1.win
tcpFlags = computeTCPFlags(tcp1.flags)
tcpHdrLen = computeTCPHdrLen(tcp1.off_x2)
[tcpOpts, tcpTimeStampEchoReply, mss] = decodeTCPOptions(tcp1.opts)
odd = detectOddities(ip4, ipHdrLen, ipVersion, tcpHdrLen, tcpFlags, tcp1, tcpOpts, tcpTimeStampEchoReply)
#build p0fv2 signature
found = False
if (winSize != 0) and (mss != 0):
if ((winSize % mss) == 0):
p0fSignature = p0fSignature + 'S' + str(winSize // mss) + ':'
found = True
mtu = mss + 40 #probably should verify if this should be 40 or _ip_hlen + _tcp_hlen
if ((winSize % mtu) == 0):
p0fSignature = p0fSignature + 'T' + str(winSize // mtu) + ':'
found = True
if (found == False):
p0fSignature = p0fSignature + str(winSize) + ':'
else:
p0fSignature = p0fSignature + str(winSize) + ':'
p0fSignature = p0fSignature + str(ttl) + ':' + str(df) + ':' + str(ipHdrLen + tcpHdrLen) + ':' + tcpOpts + ':' + odd
#build EtterCap Signature (needs finished out, not complete)
if winSize == '':
etterWinSize = '_MSS'
else:
etterWinSize = hex(winSize).lstrip("0x").upper()
etterMSS = hex(mss).lstrip("0x").rjust(4,"0").upper()
try:
x = tcpOpts.find('W')
if (x > 0):
ws = tcpOpts[x+1::]
x = ws.find(',')
if (x > 0):
ws = ws[0:x]
ws = hex(int(ws)).lstrip("0x").rjust(2,"0")
else:
ws = 'WS'
except:
ws = 'WS' #may need to do something else, but good enough for now
ettercapSignature = etterWinSize + ':' + etterMSS + ':' + hex(ttl).lstrip("0x") + ':' + ws + ':' # + sack, NOP anywhere, DF, Timestamp Present, Flag of packet (s or a), len
#build Satori tcp Signature
#ignore anything that is not S or SA, but should probably clean that up prior to this point!
tcpSignature = str(winSize) + ':' + str(ttl) + ':' + str(df) + ':' + str(ipHdrLen + tcpHdrLen) + ':' + tcpOpts + ':' + odd
if tcpFlags == 'S':
tcpFingerprint = TCPFingerprintLookup(sExactList, sPartialList, tcpSignature)
elif tcpFlags == 'SA':
tcpFingerprint = TCPFingerprintLookup(saExactList, saPartialList, tcpSignature)
fingerprint = ip4.src_s + ';'+ src_mac + ';TCP;' + tcpFlags + ';' + tcpSignature + ';' + tcpFingerprint
#print("%s;%s;00:00;00:00:00:00;p0fv2;%s;%s;%s" % (timeStamp, eth[ip.IP].src_s, eth[ethernet.Ethernet].src_s, tcpFlags, p0fSignature, p0fv2Fingerprint))
#print("%s;%s;00:00;00:00:00:00;Ettercap;%s;%s;%s" % (timeStamp, eth[ip.IP].src_s, eth[ethernet.Ethernet].src_s, tcpFlags, ettercapSignature, ettercapFingerprint))
return [timeStamp, fingerprint]
def BuildTCPFingerprintFiles():
# converting from the xml format to a more flat format that will hopefully be faster than walking the entire xml every FP lookup
sExactList = {}
saExactList = {}
sPartialList = {}
saPartialList = {}
satoriPath = str(Path(__file__).resolve().parent)
obj = untangle.parse(satoriPath + '/fingerprints/tcp.xml')
fingerprintsCount = len(obj.TCP.fingerprints)
for x in range(0,fingerprintsCount):
os = obj.TCP.fingerprints.fingerprint[x]['name']
testsCount = len(obj.TCP.fingerprints.fingerprint[x].tcp_tests)
test = {}
for y in range(0,testsCount):
test = obj.TCP.fingerprints.fingerprint[x].tcp_tests.test[y]
if test is None: #if testsCount = 1, then untangle doesn't allow us to iterate through it
test = obj.TCP.fingerprints.fingerprint[x].tcp_tests.test
matchtype = test['matchtype']
tcpflag = test['tcpflag']
tcpsig = test['tcpsig']
weight = test['weight']
if matchtype == 'exact':
if tcpflag == 'S':
if tcpsig in sExactList:
oldValue = sExactList.get(tcpsig)
sExactList[tcpsig] = oldValue + '|' + os + ':' + weight
else:
sExactList[tcpsig] = os + ':' + weight
else: #SA packets
if tcpsig in saExactList:
oldValue = saExactList.get(tcpsig)
saExactList[tcpsig] = oldValue + '|' + os + ':' + weight
else:
saExactList[tcpsig] = os + ':' + weight
else:
if tcpflag == 'S':
if tcpsig in sPartialList:
oldValue = sPartialList.get(tcpsig)
sPartialList[tcpsig] = oldValue + '|' + os + ':' + weight
else:
sPartialList[tcpsig] = os + ':' + weight
else: #SA packets
if tcpsig in saPartialList:
oldValue = saPartialList.get(tcpsig)
saPartialList[tcpsig] = oldValue + '|' + os + ':' + weight
else:
saPartialList[tcpsig] = os + ':' + weight
return [sExactList, saExactList, sPartialList, saPartialList]
def TCPFingerprintLookup(exactList, partialList, value):
exactValue = ''
partialValue = ''
if value in exactList:
exactValue = exactList.get(value)
if '*' in value:
#create values with * in the correct potential locations as well
newValue4 = ''
splitValue = value.split(':')
splitValue4 = splitValue[4].split(',')
for x in range(0, len(splitValue4)):
if 'W' in splitValue4[x]:
newValue4 = newValue4 + 'W*,'
else:
newValue4 = newValue4 + splitValue4[x] + ','
newValue4 = newValue4[:-1]
newValue1 = splitValue[0] + ':' + splitValue[1] + ':*:' + splitValue[3] + ':' + newValue4 + ':' + splitValue[5]
newValue2 = splitValue[0] + ':' + splitValue[1] + ':' + splitValue[2] + ':' + splitValue[3] + ':' + newValue4 + ':' + splitValue[5]
newValue3 = splitValue[0] + ':' + splitValue[1] + ':*:' + splitValue[3] + ':' + splitValue[4] + ':' + splitValue[5]
if newValue1 in partialList:
partialValue = partialList.get(newValue1)
if newValue2 in partialList:
partialValue = partialList.get(newValue2)
if newValue3 in partialList:
partialValue = partialList.get(newValue3)
fingerprint = exactValue + '|' + partialValue
if fingerprint.startswith('|'):
fingerprint = fingerprint[1:]
if fingerprint.endswith('|'):
fingerprint = fingerprint[:-1]
fingerprint = satoriCommon.sortFingerprint(fingerprint)
return fingerprint
def detectOddities(_ip, _ip_hlen, _ip_type, _tcp_hlen, _tcp_flags, _tcp, _tcp_options, _options_er):
odd = ''
if _tcp_options[-1] == 'E':
odd = odd + 'P'
if _ip.id == 0:
odd = odd + 'Z'
if _ip_hlen > 20:
odd = odd + 'I'
if _ip_type == 4:
len = _ip.len - _tcp_hlen - _ip_hlen
# not sure if my code even in Delphi handled ipv6, so commented out for now.
# if _ip_type == 6:
# len = _ipv6.dlen - _tcp_hlen - _ip_hlen
if len > 0:
odd = odd + 'D'
if ('U' in _tcp_flags):
odd = odd + 'U'
if ((_tcp_flags == 'S' or _tcp_flags == 'SA') and _tcp.ack != 0):
odd = odd + 'A'
if (_tcp_flags == 'S' and _options_er != 0):
odd = odd + 'T'
if _tcp_flags == 'SA':
if ('T' in _tcp_options):
odd = odd + 'T'
temp = _tcp_flags
temp = temp.replace('S', '')
temp = temp.replace('A', '')
if (temp != ''):
odd = odd + 'F'
if odd == '':
odd = '.'
return odd
def decodeTCPOptions(opts):
res = ''
mss = 0
tcpTimeStampEchoReply = ''
for i in opts:
if i.type == 0:
res = res + 'E,'
elif i.type == 1:
res = res + 'N,'
elif i.type == 2:
mss = struct.unpack('!h',i.body_bytes)[0]
res = res + 'M' + str(mss) + ','
elif i.type == 3:
x = struct.unpack('!b',i.body_bytes)[0]
res = res + 'W' + str(x) + ','
elif i.type == 4:
res = res + 'S,'
elif i.type == 5:
res = res + 'K,'
elif i.type == 6:
res = res + 'J,'
elif i.type == 7:
res = res + 'F,'
#print("Options Echo (need to compute?): %s" % (i.body_bytes))
elif i.type == 8:
res = res + 'T,'
tcpTimeStamp = struct.unpack('!I',i.body_bytes[0:4])[0]
tcpTimeStampEchoReply = struct.unpack('!I',i.body_bytes[4:8])[0]
elif i.type == 9:
res = res + 'P,'
elif i.type == 10:
res = res + 'R,'
# elif i.type == 11:
# res = res + ','
# elif i.type == 12:
# res = res + ','
# elif i.type == 13:
# res = res + ','
# elif i.type == 14:
# res = res + ','
# elif i.type == 15:
# res = res + ','
# elif i.type == 16:
# res = res + ','
# elif i.type == 17:
# res = res + ','
# elif i.type == 18:
# res = res + ','
# elif i.type == 19:
# res = res + ','
# elif i.type == 20:
# res = res + ','
# elif i.type == 21:
# res = res + ','
# elif i.type == 22:
# res = res + ','
# elif i.type == 23:
# res = res + ','
# elif i.type == 24:
# res = res + ','
# elif i.type == 25:
# res = res + ','
# elif i.type == 26:
# res = res + ','
# elif i.type == 27:
# res = res + ','
else:
res = res + 'U,'
#print('unknown TCP Options')
# x=len(i.body_bytes)
# if x == 0:
# print("Type: %s" % (i.type))
# else:
# we should check endianness, but based on quick tests, even though I'm little it was big on MSS, so just using ! for now.
#print(sys.byteorder)
# if x == 1:
# val = "!b"
# elif x == 2:
# val = "!h"
# elif x == 4:
# val = "!l"
# elif x == 8: #while timestamp (type 8) is length 8, it is really 2x 4's, so have to look at this closer later
# val = "!d"
# print("Type: %s, Value: %s" % (i.type, struct.unpack(val,i.body_bytes)[0]))
#print("Type: %s, Value: %s" % (i.type, i.body_bytes))
return(res[:-1], tcpTimeStampEchoReply, mss)
def computeTCPFlags(flags):
tcpFlags = ''
if flags == 0x02:
tcpFlags = 'S'
# elif flags == 0x10:
# tcpFlags = 'A'
elif flags == 0x12:
tcpFlags = 'SA'
# else:
# tcpFlags = flags #do I need/want this? If we have packets with tcp options that are not S or SA we are ignoring, so NO, we do not want this
return(tcpFlags)
def computeIP(info):
ipVersion = int('0x0' + hex(info)[2],16)
ipHdrLen = int('0x0' + hex(info)[3],16) * 4
return [ipVersion, ipHdrLen]
def computeNearTTL(info):
if (info>0) and (info<=16):
ttl = 16
ethTTL = 16
elif (info>16) and (info<=32):
ttl = 32
ethTTL = 43
# elif (info>32) and (info<=60):
# ttl = 60 #unlikely to find many of these anymore
# ethTTL = 64
# elif (info>60) and (info<=64):
elif (info>32) and (info<=64):
ttl = 64
ethTTL = 64
elif (info>64) and (info<=128):
ttl = 128
ethTTL = 128
elif (info>128):
ttl = 255
ethTTL = 255
else:
ttl = info
ethTTL = info
return [ethTTL, ttl]
def computeIPOffset(info):
# need to see if I can find a way to import these from ip.py as they are already defined there.
# Fragmentation flags (ip_off)
IP_RF = 0x4 # reserved
IP_DF = 0x2 # don't fragment
IP_MF = 0x1 # more fragments (not last frag)
res = 0
df = 0
mf = 0
flags = (info & 0xE000) >> 13
offset = (info & ~0xE000)
if (flags & IP_RF) > 0:
res = 1
if (flags & IP_DF) > 0:
df = 1
if (flags & IP_MF) > 0:
mf = 1
return [df, mf, offset]
def computeTCPHdrLen(info):
tcpHdrLen = (info >> 4) * 4
return tcpHdrLen