forked from xnih/satori
-
Notifications
You must be signed in to change notification settings - Fork 0
/
satoriSMB.py
330 lines (284 loc) · 12.7 KB
/
satoriSMB.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
import untangle
import struct
from pathlib import Path
from datetime import datetime
from pypacker.layer12 import ethernet
from pypacker.layer3 import ip
#from pypacker.layer567 import smb
import smbHeader
# 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
#
#
# while I'd like to build this one out, it requires using the sequence of previous ones to compare to current to see if it is a factor of "blah" (256 for example).
# as I'm not tracking stuf in this version of Satori, for now this module will not be completed, but it is at least started.
#
def networkByteOrder(data): #use struct to handle this instead!
list = []
val = hex(data).strip("0x")
print(data)
print(val)
for i in range(0, len(val), 2):
list.insert(0,val[i:i+2])
return(list)
def parseBuffer(buf, unicode):
val = ''
if unicode == False:
for i in range(0, len(buf)):
if buf[i] == 0:
val = val + ','
else:
val = val + chr(buf[i])
else:
for i in range(0, len(buf), 2):
if buf[i] == 0:
val = val + ','
else:
val = val + chr(buf[i])
return(val)
def smbUDPProcess(eth, ts, browserExactList, browserPartialList):
ip4 = eth.upper_layer
udp1 = ip4.upper_layer
if udp1.sport != 138:
sys.exit(1)
nbds1 = smbHeader.NBDS_Header(udp1.body_bytes)
smb = smbHeader.UDPSMB_Header(nbds1.body_bytes)
if smb.command == 0x25:
trans = smbHeader.transRequest_Header(smb.body_bytes)
mail = smbHeader.SMBMailSlot_Header(trans.body_bytes)
mailname = ''
i = 0
while True: #since this is variable length have to look through it until 0x00
mailname = mailname + chr(mail.body_bytes[i])
i = i + 1
if mail.body_bytes[i] == 0x00:
break
if mail.body_bytes[i+1] == 0x01:
announce = smbHeader.MWBP_HostAnnounce(mail.body_bytes[i+1:])
osVersion = str(announce.osMajorVer) + '.' + str(announce.osMinVer)
browVersion = str(announce.browMajorVer) + '.' + str(announce.browMinVer)
timeStamp = datetime.utcfromtimestamp(ts).isoformat()
if (osVersion != '') and (browVersion != ''):
fingerprint = osVersion + ',' + browVersion
osGuess = SMBUDPFingerprintLookup(browserExactList, browserPartialList, fingerprint)
print("%s;%s;%s;SMBBROWSER;%s;%s" % (timeStamp, eth[ip.IP].src_s, eth[ethernet.Ethernet].src_s, fingerprint, osGuess))
def smbTCPProcess(eth, ts, nativeExactList, lanmanExactList, nativePartialList, lanmanPartialList):
ip4 = eth.upper_layer
tcp1 = ip4.upper_layer
#if (_tcp.src_portno <> 139) and (_tcp.dst_portno <> 139) and (_tcp.src_portno <> 445)and (_tcp.dst_portno <> 445) then exit;
#need to take tcp1.body_bytes and shove the info into stuff now......
x = len(smbHeader.netbiosSessionService())
if len(tcp1.body_bytes) >= x:
nbss1 = smbHeader.netbiosSessionService(tcp1.body_bytes)
smb1 = smbHeader.tcpSMB(nbss1.body_bytes)
if smb1.proto == 0xFF534D42:
if smb1.cmd == 0x73: #may look at others later, but for now, this is the only one of use for fingerprinting
flags2 = struct.unpack('@H',smb1.flags2)[0]
if "{0:>16b}".format(flags2)[0] == '1': #probably a better way to do this with bit shifting, but this works for now *in pascal had: (_tcp_smb.flags2 and 32768)
unicode=True
else: #0 or space
unicode=False
nativeOS = ''
nativeLanMan = ''
if smb1.body_bytes[0] == 0X0:
pass
elif smb1.body_bytes[0] == 0X3:
SS1 = smbHeader.SSAndRequestHeader_w3(smb1.body_bytes)
if len(SS1.body_bytes) % 2 == 0:
buffer = SS1.body_bytes[1:]
else:
buffer = SS1.body_bytes
#Native OS; Native LAN Manager; Primary Domain
info = parseBuffer(buffer, unicode)
info = info.split(',')
nativeOS = info[0]
nativeLanMan = info[1]
primaryDomain = info[2]
elif smb1.body_bytes[0] == 0X4:
SS1 = smbHeader.SSAndRequestHeader_w4(smb1.body_bytes)
x = struct.unpack('@h',SS1.SecurityBlobLen)[0]
securityBlob = SS1.body_bytes[0:x]
# if x > 0:
# if securityBlob[0:7] == b'NTLMSSP':
# print(securityBlob[8])
# if securityBlob[8] == 1:
# version = smbHeader.NTLMSecurityBlobType1(securityBlob)
# major = struct.unpack('@s',version.major)[0]
# minor = struct.unpack('@s',version.minor)[0]
# build = struct.unpack('@h',version.build)[0]
# revision = struct.unpack('@s',version.revision)[0]
# print(str(major) + '.' + str(minor) + ' ' + str(build) + ' ' + str(revision))
if x % 2 == 0:
buffer = SS1.body_bytes[x+1:]
else:
buffer = SS1.body_bytes[x:]
#Native OS; Native LAN Manager
info = parseBuffer(buffer, unicode)
info = info.split(',')
nativeOS = info[0]
nativeLanMan = info[1]
elif smb1.body_bytes[0] == 0XC:
SS1 = smbHeader.SSAndRequestHeader_w12(smb1.body_bytes)
x = struct.unpack('@h',SS1.SecurityBlobLen)[0]
securityBlob = SS1.body_bytes[0:x]
# if x > 0:
# if securityBlob[0:7] == b'NTLMSSP':
# print(securityBlob[8])
# if securityBlob[8] == 1:
# version = smbHeader.NTLMSecurityBlobType1(securityBlob)
# major = struct.unpack('@b',version.major)[0]
# minor = struct.unpack('@b',version.minor)[0]
# build = struct.unpack('@h',version.build)[0]
# revision = struct.unpack('@b',version.revision)[0]
# print(str(major) + '.' + str(minor) + ' ' + str(build) + ' ' + str(revision))
if x % 2 == 0:
buffer = SS1.body_bytes[x+1:]
else:
buffer = SS1.body_bytes[x:]
#Native OS; Native LAN Manager
info = parseBuffer(buffer, unicode)
info = info.split(',')
nativeOS = info[0]
nativeLanMan = info[1]
elif smb1.body_bytes[0] == 0XD:
SS1 = smbHeader.SSAndRequestHeader_w13(smb1.body_bytes)
buffer = SS1.body_bytes
#Account; Primary Domain; Native OS; Native LAN Manager
ansi = struct.unpack('@h',SS1.ANSIPasswordLen)[0]
uni = struct.unpack('@h',SS1.UniCodePassLen)[0]
info = parseBuffer(buffer[ansi + uni:], unicode)
info = info.split(',')
nativeOS = info[2]
nativeLanMan = info[3]
timeStamp = datetime.utcfromtimestamp(ts).isoformat()
if nativeOS != '':
osGuess = SMBTCPFingerprintLookup(nativeExactList, nativePartialList, nativeOS)
print("%s;%s;%s;SMBNATIVE;NativeOS;%s;%s" % (timeStamp, eth[ip.IP].src_s, eth[ethernet.Ethernet].src_s, nativeOS, osGuess))
if nativeLanMan != '':
osGuess = SMBTCPFingerprintLookup(lanmanExactList, lanmanPartialList, nativeLanMan)
print("%s;%s;%s;SMBNATIVE;NativeLanMan;%s;%s" % (timeStamp, eth[ip.IP].src_s, eth[ethernet.Ethernet].src_s, nativeLanMan, osGuess))
def BuildSMBUDPFingerprintFiles():
# converting from the xml format to a more flat format that will hopefully be faster than walking the entire xml every FP lookup
browserExactList = {}
browserPartialList = {}
satoriPath = str(Path(__file__).resolve().parent)
obj = untangle.parse(satoriPath + '/fingerprints/browser.xml')
fingerprintsCount = len(obj.SMBBROWSER.fingerprints)
for x in range(0,fingerprintsCount):
os = obj.SMBBROWSER.fingerprints.fingerprint[x]['name']
testsCount = len(obj.SMBBROWSER.fingerprints.fingerprint[x].smbbrowser_tests)
test = {}
for y in range(0,testsCount):
test = obj.SMBBROWSER.fingerprints.fingerprint[x].smbbrowser_tests.test[y]
if test is None: #if testsCount = 1, then untangle doesn't allow us to iterate through it
test = obj.SMBBROWSER.fingerprints.fingerprint[x].smbbrowser_tests.test
weight = test['weight']
matchtype = test['matchtype']
osversion = test['osversion']
browserversion = test['browserversion']
if matchtype == 'exact':
fingerprint = osversion + ',' + browserversion
if fingerprint in browserExactList:
oldValue = browserExactList.get(fingerprint)
browserExactList[fingerprint] = oldValue + '|' + os + ':' + weight
else:
browserExactList[fingerprint] = os + ':' + weight
else:
fingerprint = osversion + ',' + browserversion
if fingerprint in browserPartialList:
oldValue = browserPartialList.get(fingerprint)
browserPartialList[fingerprint] = oldValue + '|' + os + ':' + weight
else:
browserPartialList[fingerprint] = os + ':' + weight
return [browserExactList, browserPartialList]
def BuildSMBTCPFingerprintFiles():
# converting from the xml format to a more flat format that will hopefully be faster than walking the entire xml every FP lookup
nativeExactList = {}
nativePartialList = {}
lanmanExactList = {}
lanmanPartialList = {}
satoriPath = str(Path(__file__).resolve().parent)
obj = untangle.parse(satoriPath + '/fingerprints/smb.xml')
fingerprintsCount = len(obj.SMB.fingerprints)
for x in range(0,fingerprintsCount):
os = obj.SMB.fingerprints.fingerprint[x]['name']
testsCount = len(obj.SMB.fingerprints.fingerprint[x].smb_tests)
test = {}
for y in range(0,testsCount):
test = obj.SMB.fingerprints.fingerprint[x].smb_tests.test[y]
if test is None: #if testsCount = 1, then untangle doesn't allow us to iterate through it
test = obj.SMB.fingerprints.fingerprint[x].smb_tests.test
weight = test['weight']
matchtype = test['matchtype']
smbnativename = test['smbnativename']
smbnativelanman = test['smbnativelanman']
if matchtype == 'exact':
if smbnativename != None:
if smbnativename in nativeExactList:
oldValue = nativeExactList.get(smbnativename)
nativeExactList[smbnativename] = oldValue + '|' + os + ':' + weight
else:
nativeExactList[smbnativename] = os + ':' + weight
elif smbnativelanman != None:
if smbnativelanman in lanmanExactList:
oldValue = lanmanExactList.get(smbnativelanman)
lanmanExactList[smbnativelanman] = oldValue + '|' + os + ':' + weight
else:
lanmanExactList[smbnativelanman] = os + ':' + weight
else:
if smbnativename != None:
if smbnativename in nativePartialList:
oldValue = nativePartialList.get(smbnativename)
nativePartialList[smbnativename] = oldValue + '|' + os + ':' + weight
else:
nativePartialList[smbnativename] = os + ':' + weight
elif smbnativelanman != None:
if smbnativelanman in lanmanPartialList:
oldValue = lanmanPartialList.get(smbnativelanman)
lanmanPartialList[smbnativelanman] = oldValue + '|' + os + ':' + weight
else:
lanmanPartialList[smbnativelanman] = os + ':' + weight
return [nativeExactList, lanmanExactList, nativePartialList, lanmanPartialList]
def SMBUDPFingerprintLookup(exactList, partialList, value):
#same as DHCP one, may be able to look at combining in the future?
exactValue = ''
partialValue = ''
if value in exactList:
exactValue = exactList.get(value)
for key, val in partialList.items():
if value.find(key) > -1:
partialValue = partialValue + '|' + val
if partialValue.startswith('|'):
partialValue = partialValue[1:]
if partialValue.endswith('|'):
partialValue = partialValue[:-1]
fingerprint = exactValue + '|' + partialValue
if fingerprint.startswith('|'):
fingerprint = fingerprint[1:]
if fingerprint.endswith('|'):
fingerprint = fingerprint[:-1]
return fingerprint
def SMBTCPFingerprintLookup(exactList, partialList, value):
#same as DHCP one, may be able to look at combining in the future?
exactValue = ''
partialValue = ''
if value in exactList:
exactValue = exactList.get(value)
for key, val in partialList.items():
if value.find(key) > -1:
partialValue = partialValue + '|' + val
if partialValue.startswith('|'):
partialValue = partialValue[1:]
if partialValue.endswith('|'):
partialValue = partialValue[:-1]
fingerprint = exactValue + '|' + partialValue
if fingerprint.startswith('|'):
fingerprint = fingerprint[1:]
if fingerprint.endswith('|'):
fingerprint = fingerprint[:-1]
return fingerprint