-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathunsafe_jar_rs_scan.py
1069 lines (777 loc) · 45.5 KB
/
unsafe_jar_rs_scan.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
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
try:
from burp import IBurpExtender
from burp import IScannerCheck
from burp import IExtensionStateListener
from burp import IHttpRequestResponse
from burp import IScanIssue
from burp import IParameter
from burp import IScannerInsertionPointProvider
from burp import IScannerInsertionPoint
from array import array
from random import *
from string import *
from re import *
import StringIO
import gzip
from urlparse import urlparse
except ImportError:
print "Failed to load dependencies. This issue maybe caused by using an unstable Jython version."
VERSION = '0.1'
DEBUG = False # Turn on/off debug info in console
callbacks = None
helpers = None
def debug2console(title, *args):
if DEBUG:
print "[ debug ]", "Begin", title
for arg in args:
print arg
print "[ debug ]", "End", title
def gzip_encode(str):
out = StringIO.StringIO()
with gzip.GzipFile(fileobj=out, mode="w") as f:
f.write(str)
return out.getvalue()
def safe_bytes_to_string(bytes):
if bytes is None:
bytes = ''
return helpers.bytesToString(bytes)
def should_trigger_per_request_attacks(request_info, insertionPoint): ### Hack to make per-request scan from @albinowax
params = request_info.getParameters()
if params:
first_parameter_offset = 999999
first_parameter = None
for param_type in (IParameter.PARAM_BODY, IParameter.PARAM_URL, IParameter.PARAM_JSON, IParameter.PARAM_XML,
IParameter.PARAM_XML_ATTR, IParameter.PARAM_MULTIPART_ATTR, IParameter.PARAM_COOKIE):
for param in params:
if param.getType() != param_type:
continue
if param.getNameStart() < first_parameter_offset:
first_parameter_offset = param.getNameStart()
first_parameter = param
if first_parameter:
break
if first_parameter and first_parameter.getName() == insertionPoint.getInsertionPointName():
return True
elif insertionPoint.getInsertionPointType() == IScannerInsertionPoint.INS_HEADER and \
insertionPoint.getInsertionPointName() == 'User-Agent':
return True
debug2console('Skiping insertion point', insertionPoint.getInsertionPointName())
return False
def get_random_string(len):
return "".join([choice(ascii_letters) for _ in range(len)])
def get_request_headers_as_dict(request):
rawHeaders = helpers.analyzeRequest(request).getHeaders()
return dict((header.split(':')[0], header.split(':', 1)[1].strip()) for header in rawHeaders[1:])
def get_response_headers_as_dict(response):
rawHeaders = helpers.analyzeResponse(response).getHeaders()
return dict((header.split(':')[0], header.split(':', 1)[1].strip()) for header in rawHeaders[1:])
def get_response_status_code(response):
return helpers.analyzeResponse(response).getStatusCode();
def get_response_body(response):
offset = helpers.analyzeResponse(response).getBodyOffset()
return response[offset:]
def get_request_body(request):
offset = helpers.analyzeRequest(request).getBodyOffset()
return request[offset:]
# Add header or modify existing
def add_header_to_request(request, header_name, header_value):
info = helpers.analyzeRequest(request)
requestBodyOffset = info.getBodyOffset()
requestHeaders = request[:requestBodyOffset].split('\r\n')
requestBody = request[requestBodyOffset:]
headerExists = len(filter( lambda x: header_name in x, requestHeaders )) > 0
modifiedHeaders = ""
if headerExists:
modifiedHeaders = "\r\n".join([header if header_name not in header else header_name + header_value for header in requestHeaders])
else:
modifiedHeaders = "\r\n".join([header if "Host: " not in header else header + "\r\n" + header_name + header_value for header in requestHeaders])
return modifiedHeaders + requestBody
def remove_header_from_request(request, header_name):
info = helpers.analyzeRequest(request)
requestBodyOffset = info.getBodyOffset()
requestHeaders = request[:requestBodyOffset].split('\r\n')
requestBody = request[requestBodyOffset:]
headerExists = len(filter( lambda x: header_name in x, requestHeaders )) > 0
if headerExists:
modifiedHeaders = "\r\n".join([header for header in requestHeaders if header_name not in header])
return modifiedHeaders + requestBody
return request
def add_body_to_request(request, body):
info = helpers.analyzeRequest(request)
requestBodyOffset = info.getBodyOffset()
requestHeaders = request[:requestBodyOffset]
requestBody = request[requestBodyOffset:]
h = helpers.stringToBytes(requestHeaders)
b = helpers.stringToBytes(body)
h.extend(b)
return add_header_to_request(safe_bytes_to_string(h),"Content-Length: ",str(len(body)))
def prepare(basePair):
request = safe_bytes_to_string(basePair.getRequest())
response = basePair.getResponse()
request_headers = get_request_headers_as_dict(request)
response_headers = get_response_headers_as_dict(response)
info_request = helpers.analyzeRequest(request)
info_response = helpers.analyzeResponse(response)
return (request, response, request_headers, response_headers, info_request, info_response)
def hyperlink(text, href):
return "<a href={}>{}</a>".format(href, text)
def isOk(status):
return status in (200, 201, 202, 204)
class BurpExtender(IBurpExtender):
def registerExtenderCallbacks(self, this_callbacks):
global callbacks, helpers
callbacks = this_callbacks
helpers = callbacks.getHelpers()
callbacks.setExtensionName("Unsafe JAX-RS")
callbacks.registerScannerCheck(JaxRsScanner())
callbacks.registerScannerCheck(JerseyScanner())
callbacks.registerScannerCheck(CXFJaxRsScanner())
callbacks.registerScannerCheck(ResteasyScanner())
print "Successfully loaded Unsafe JAX-RS v" + VERSION
return
class ScanIssue(IScanIssue):
def __init__(self, httpService, url, httpMessages, name, detail, confidence, severity):
self.HttpService = httpService
self.Url = url
self.HttpMessages = httpMessages
self.Name = name
self.Detail = detail
self.Severity = severity
self.Confidence = confidence
print "Reported: " + name + " on " + str(url)
return
def getUrl(self):
return self.Url
def getIssueName(self):
return self.Name
def getIssueType(self):
return 0
def getSeverity(self):
return self.Severity
def getConfidence(self):
return self.Confidence
def getIssueBackground(self):
return None
def getRemediationBackground(self):
return None
def getIssueDetail(self):
return self.Detail
def getRemediationDetail(self):
return None
def getHttpMessages(self):
return self.HttpMessages
def getHttpService(self):
return self.HttpService
class JaxRsScanner(IScannerCheck):
DESCR_WADL_SCAN = "JAX-RS application exposes {0}. You should check manually or using {1} that all resource methods " \
"have proper authentication/authorization.".format(
hyperlink("WADL","https://en.wikipedia.org/wiki/Web_Application_Description_Language"),
hyperlink("SOAPUI","https://www.soapui.org/downloads/soapui.html"))
DESCR_CONF_SCAN = "It seems that resource method of JAX-RS application lacks {0} annotation or have permissive media type specification " \
"e.g. \"application/*\". This can lead to situation when attacker can select \"bad\" {1} instead of intended provider. " \
"See {2}, {3}, {4} for more information about entity provider selection confusion vulnerabilities.".format(
hyperlink("@Consumes","https://docs.oracle.com/cd/E19776-01/820-4867/ggqqr/"),
hyperlink("Entity Provider","https://jersey.java.net/documentation/latest/message-body-workers.html"),
hyperlink("CVE-2016-7050","https://bugzilla.redhat.com/show_bug.cgi?id=CVE-2016-7050"),
hyperlink("CVE-2016-9571","https://bugzilla.redhat.com/show_bug.cgi?id=CVE-2016-9571"),
hyperlink("CVE-2016-8739","https://bugzilla.redhat.com/show_bug.cgi?id=1406811"))
DESCR_CSRF_SCAN = "It seems that resource method lacks {0} or allows text/plain media type. If class of entity parameter " \
"has valueOf(String) method or String constructor, this JAX-RS method might be vulnerable to CSRF attack.".format(
hyperlink("@Consumes","https://docs.oracle.com/cd/E19776-01/820-4867/ggqqr/"))
DESCR_DOS_GZIP_SCAN = "JAX-RS resource method is vulnerable to DoS attack via decompression bomb. See {0}. " \
"Futher reading about decompression bombs - {1}.".format(
hyperlink("CVE-2016-6346","https://access.redhat.com/security/cve/cve-2016-6346"),
hyperlink("I Came to Drop Bombs ","https://www.blackhat.com/docs/us-16/materials/us-16-Marie-I-Came-to-Drop-Bombs-Auditing-The-Compression-Algorithm-Weapons-Cache.pdf"))
DESCR_JSONP_SCAN = "JAX-RS resource method supports {0}. It might be vulnerable to {1} attack.".format(
hyperlink("JSONP","https://en.wikipedia.org/wiki/JSONP"),
hyperlink("XSSI","https://www.scip.ch/en/?labs.20160414"))
DESCR_EXC_MAP_SCAN = "JAX-RS application uses exception mappers which exposes stacktrace and allows to identify JAX-RS library."
DESCR_EXC_MAP_SCAN_XSS = "JAX-RS application uses exception mapper for JSON unmarshalling which is vulnerable to XSS attack. " \
"XSS vulnerability in RESTEasy - CVE-2016-6347".format(
hyperlink("CVE-2016-6347","https://access.redhat.com/security/cve/cve-2016-6347"))
DESCR_URI_CT_NEG = "JAX-RS resource method supports negotiation of response media type via URI extension, e.g. /something.json." \
"It might lead to XSSI or XSS attacks."
DESCR_XXE_SCAN = "It seems that resource method lacks {0} or specifies it too permisively. Additionally JAX-RS application has entity provider that is vulnerable to XXE. " \
"For example, JacksonJaxbXMLProvider which is the part of Jackson is known to be vulnerable to {1}.".format(
hyperlink("@Consumes", "https://docs.oracle.com/cd/E19776-01/820-4867/ggqqr/"),
hyperlink("CVE-2016-3720", "https://bugzilla.redhat.com/show_bug.cgi?id=1328427"))
def doPassiveScan(self, basePair):
return []
def doActiveScan(self, basePair, insertionPoint):
issues = []
self.request, self.response, self.request_headers, self.response_headers, self.info_request, self.info_response = prepare(basePair)
self.httpService = basePair.getHttpService()
self.URL = helpers.analyzeRequest(basePair).getUrl()
if not should_trigger_per_request_attacks(self.info_request, insertionPoint):
return []
issues.extend(self.wadl_scan())
issues.extend(self.confusion_scan())
issues.extend(self.gzip_dos_scan())
issues.extend(self.jsonp_scan())
issues.extend(self.exception_mapper_scan())
issues.extend(self.csrf_scan())
issues.extend(self.uri_based_negotiation_scan())
issues.extend(self.xxe_scan())
return issues
def xxe_scan(self):
content_types = [
"application/xml",
"text/xml"
]
if not self.request_headers.has_key('Content-Length') or \
int(self.request_headers.get('Content-Length', 0)) == 0:
return []
for ct in content_types:
request = safe_bytes_to_string(add_header_to_request(self.request, "Content-Type: ", ct))
body = '<?xml version="1.0" encoding="utf-8"?>' + \
'<!DOCTYPE foo SYSTEM "%s://dummy" []>' + \
'<foo/>'
r = get_random_string(10)
body = body % r
request = safe_bytes_to_string(add_body_to_request(request, body))
newPair = callbacks.makeHttpRequest(self.httpService, request)
response_modif = safe_bytes_to_string(newPair.getResponse())
debug2console("XXE Scan", request, response_modif)
if get_response_status_code(response_modif) in (400, 500) and r in response_modif:
return [
ScanIssue(self.httpService, self.URL, [newPair],
"JAX-RS application has entity provider which is vulnerable to XXE",
CXFJaxRsScanner.DESCR_XXE_SCAN,
'Certain', 'High'),
]
return []
def csrf_scan(self):
if not isOk(self.info_response.getStatusCode()):
return []
if self.info_request.getMethod() not in ('GET','POST'):
return []
if not self.request_headers.has_key('Content-Length') or \
int(self.request_headers.get('Content-Length',0)) == 0:
return []
request = safe_bytes_to_string( add_header_to_request(self.request, "Content-Type: ", "text/plain") )
newPair = callbacks.makeHttpRequest(self.httpService, request)
response_modif = safe_bytes_to_string( newPair.getResponse() )
debug2console("CSRF scan valueOf", request, response_modif)
if get_response_status_code(response_modif) != 415:
return [
ScanIssue(self.httpService, self.URL, [ newPair ],
'JAX-RS resource method is vulnerable to CSRF',
JaxRsScanner.DESCR_CSRF_SCAN,
'Firm', 'Medium'),
]
return []
def wadl_scan(self):
""" Possible wadl servlet path values """
names = [
'application.xml',
'application.wadl'
]
path = self.info_request.getHeaders()[0].split(' ')[1]
path_list = path.split('/')
request = self.request
if self.info_request.getMethod() != 'GET':
request = safe_bytes_to_string( helpers.toggleRequestMethod(request) )
for i in range(1,len(path_list)) :
for name in names:
_path = "/".join(path_list[:i]) + "/" + name
_request = request.replace(path, _path, 1)
modifPair = callbacks.makeHttpRequest(self.httpService, _request)
response_modif = safe_bytes_to_string(modifPair.getResponse())
debug2console("WADL Scan generic",_request,response_modif)
_ct = get_response_headers_as_dict(response_modif).get('Content-Type')
if isOk(get_response_status_code(response_modif)) and \
_ct in ("application/xml", "application/vnd.sun.wadl+xml"):
return [
ScanIssue(self.httpService, self.URL, [ modifPair ],
"JAX-RS application exposes WADL",
JaxRsScanner.DESCR_WADL_SCAN,
'Certain', 'Medium'),
]
return []
def confusion_scan(self):
""" Interesting Content-Types to check """
confusion_cts = (
"", # empty
"application/xml",
"text/xml",
"application/atom+xml",
"application/x-yaml",
"text/yaml",
"application/x-kryo",
"application/x-stream",
"application/x-java-serialized-object",
"text/plain;charset=" + get_random_string(10)
)
if not isOk(self.info_response.getStatusCode()):
return []
if not self.request_headers.has_key('Content-Length') or \
int(self.request_headers.get('Content-Length',0)) == 0:
return []
issues = []
for ct in confusion_cts:
_request = safe_bytes_to_string( add_header_to_request(self.request, "Content-Type: ", ct) )
body = get_random_string(10)
_request = safe_bytes_to_string( add_body_to_request(_request, body) )
newPair = callbacks.makeHttpRequest(self.httpService, _request)
response_modif = safe_bytes_to_string(newPair.getResponse())
debug2console("Confusion Scan", ct, _request, response_modif)
if get_response_status_code(response_modif) in (500, 400):
issues.append(ScanIssue(self.httpService, self.URL, [ newPair ],
"JAX-RS application is vulnerable to entity provider selection confusion",
JaxRsScanner.DESCR_CONF_SCAN,
"Firm", "High"))
return issues
def gzip_dos_scan(self):
if not isOk(self.info_response.getStatusCode()):
return []
if not self.request_headers.has_key('Content-Length') or \
int(self.request_headers.get('Content-Length',0)) == 0:
return []
body = gzip_encode(get_request_body( self.request ))
_request = safe_bytes_to_string(add_header_to_request(self.request, 'Content-Encoding: ', 'gzip'))
_request = safe_bytes_to_string( add_body_to_request(_request, body) )
modifPair = callbacks.makeHttpRequest(self.httpService, _request )
response_modif = safe_bytes_to_string(modifPair.getResponse())
debug2console("GZIP DoS Scan", response_modif)
if isOk(get_response_status_code(response_modif)) and \
self.response_headers.get('Content-Type') == get_response_headers_as_dict(response_modif).get('Content-Type'):
return [
ScanIssue(self.httpService, self.URL, [ modifPair, ],
"JAX-RS resource is vulnerable to GZIP bombing DoS",
JaxRsScanner.DESCR_DOS_GZIP_SCAN,
"Certain", "Medium"),
]
return []
def jsonp_scan(self):
""" Possible parameter names for JSONP """
jsnop_param_names = [
"callback",
"_callback",
"__callback",
"jsonp",
"_jsonp",
"__jsonp",
"func",
"function"
]
if not isOk(self.info_response.getStatusCode()):
return []
value = get_random_string(10)
for param_name in jsnop_param_names:
param = helpers.buildParameter(param_name , value, IParameter.PARAM_URL)
_request = safe_bytes_to_string( helpers.addParameter(self.request,param) )
newPair = callbacks.makeHttpRequest(self.httpService, _request)
response_modif = safe_bytes_to_string( newPair.getResponse() )
debug2console("JSONP Scan", _request, response_modif)
if value + "(" in response_modif:
return [
ScanIssue(self.httpService, self.URL, [ newPair ],
"JAX-RS resource method supports JSONP",
JaxRsScanner.DESCR_JSONP_SCAN,
'Firm', 'Medium'),
]
return []
def exception_mapper_scan(self):
issues = []
if not isOk(self.info_response.getStatusCode()):
return []
path = self.info_request.getHeaders()[0].split(' ')[1]
path_list = path.split('/')
for i in range(1,len(path_list)): ### Check for PathParam processing exceptions
_path_list = path_list[:]
_path_list[i] = choice("{}[]\\")
_path = "/".join(_path_list)
_request = self.request.replace(path, _path, 1)
modifPair = callbacks.makeHttpRequest(self.httpService, _request )
response_modif = safe_bytes_to_string(modifPair.getResponse())
debug2console("PathParam processing exception", _request, response_modif)
if get_response_status_code(response_modif) == 500:
issues.append(
ScanIssue(self.httpService, self.URL, [ modifPair ],
"Exception occured during Path Param processing",
JaxRsScanner.DESCR_EXC_MAP_SCAN,
'Certain', 'Low')
)
break
ACCEPT = get_random_string(10) + "/" + get_random_string(10) ### Check for exceptions during marshalling
_request = add_header_to_request(self.request, "Accept: ", ACCEPT)
modifPair = callbacks.makeHttpRequest(self.httpService, _request )
response_modif = safe_bytes_to_string(modifPair.getResponse())
debug2console("Marshalling exceptions", _request, response_modif)
if get_response_status_code(response_modif) == 500:
issues.append(
ScanIssue(self.httpService, self.URL, [ modifPair ],
"Exception occured during marshalling",
JaxRsScanner.DESCR_EXC_MAP_SCAN,
'Certain', 'Low')
)
if not self.request_headers.has_key('Content-Length') or \
int(self.request_headers.get('Content-Length',0)) == 0:
return issues
if 'application/json' in self.request_headers['Content-Type']: ### Check for exceptions during JSON unmarshalling
value = get_random_string(10)
body = '{"<%s>":1}' % value
_request = safe_bytes_to_string(add_body_to_request(self.request, body))
modifPair = callbacks.makeHttpRequest(self.httpService, _request )
response_modif = safe_bytes_to_string(modifPair.getResponse())
debug2console("Unmarshalling exceptions", _request, response_modif)
if "<%s>" % value in response_modif:
if "text/html" in get_response_headers_as_dict(response_modif).get('Content-Type',''):
issues.append(
ScanIssue(self.httpService, self.URL, [ modifPair ],
"JAX-RS exception mapper is vulnerable to XSS",
JaxRsScanner.DESCR_EXC_MAP_SCAN_XSS,
'Certain', 'Medium')
)
else:
issues.append(
ScanIssue(self.httpService, self.URL, [ modifPair ],
"Exception occured during JSON unmarshalling",
JaxRsScanner.DESCR_EXC_MAP_SCAN,
'Certain', 'Low')
)
return issues
def uri_based_negotiation_scan(self):
""" Content-Types to extension mapping """
mappings = {
"application/json" : ".json",
"application/xml" : ".xml",
"text/xml": ".xml",
"application/atom+xml": ".atom",
"text/plain": ".txt",
"text/html": ".html",
"application/x-javascript": ".js"
}
if not isOk(self.info_response.getStatusCode()):
return []
ct = self.response_headers.get('Content-Type','')
ext = mappings.get(ct, ".json")
ACCEPT = get_random_string(10) + "/" + get_random_string(10)
request = add_header_to_request(self.request, "Accept: ", ACCEPT)
path = urlparse(str(self.URL)).path
request = request.replace(path, path + ext, 1)
modifPair = callbacks.makeHttpRequest(self.httpService, request)
response_modif = safe_bytes_to_string( modifPair.getResponse() )
debug2console("URI-based negotiation scan", request, response_modif)
if isOk(get_response_status_code(response_modif)) and \
self.response_headers.get('Content-Type') == get_response_headers_as_dict(response_modif).get('Content-Type'):
return [
ScanIssue(self.httpService, self.URL, [ modifPair ],
"JAX-RS resource method supports URI-based content negotiation",
JaxRsScanner.DESCR_URI_CT_NEG,
"Firm", "Medium"),
]
return []
class JerseyScanner(IScannerCheck):
DESCR_WADL_SCAN = "Jersey application exposes {0}. You should check manually or using {1} that all resource methods " \
"have proper authentication/authorization.".format(
hyperlink("WADL","https://en.wikipedia.org/wiki/Web_Application_Description_Language"),
hyperlink("SOAPUI","https://www.soapui.org/downloads/soapui.html"))
def doPassiveScan(self, basePair):
return []
def doActiveScan(self, basePair, insertionPoint):
issues = []
self.request, self.response, self.request_headers, self.response_headers, self.info_request, self.info_response = prepare(basePair)
self.httpService = basePair.getHttpService()
self.URL = helpers.analyzeRequest(basePair).getUrl()
if not should_trigger_per_request_attacks(self.info_request, insertionPoint):
return []
issues.extend(self.wadl_scan())
return issues
def wadl_scan(self):
request = self.request
if self.info_request.getMethod() != 'GET':
request = safe_bytes_to_string(helpers.toggleRequestMethod(request))
request = safe_bytes_to_string(add_header_to_request(request, "Accept: ", "application/vnd.sun.wadl+xml"))
request_options = request.replace("GET ", "OPTIONS ", 1)
newPair = callbacks.makeHttpRequest(self.httpService, request_options)
resp = safe_bytes_to_string(newPair.getResponse())
debug2console("WADL Scan Jersey", request, resp)
if not (isOk(get_response_status_code(resp)) and \
get_response_headers_as_dict(resp).get('Content-Type') == 'application/vnd.sun.wadl+xml'):
return []
m = search('(?im)<resources\s+base\s*=\s*"([^"]*)"',resp)
if not m:
return []
new_path = urlparse(m.group(1)).path
request = add_header_to_request(request, "GET ", "%sapplication.wadl?detail=true HTTP/1.1" % new_path)
newPair = callbacks.makeHttpRequest(self.httpService, request)
resp = safe_bytes_to_string(newPair.getResponse())
debug2console("WADL Scan Jersey", request, resp)
return [
ScanIssue(self.httpService, self.URL, [ newPair ],
"Jersey application exposes WADL",
JerseyScanner.DESCR_WADL_SCAN,
'Certain', 'Medium'),
]
class CXFJaxRsScanner(IScannerCheck):
DESCR_CXF_SCAN = "Apache CXF RS application is detected. CXF supposts _method URL parameter, see - {0}.".format(
hyperlink("JAX-RS Debugging","http://cxf.apache.org/docs/jax-rs.html#JAX-RS-Debugging"))
DESCR_GZIP_DOS_SCAN = "Apache CXF RS application is vulnerbale to DoS via GZIP decompression bombing. See - {0}.".format(
hyperlink("CVE-2016-6346","https://access.redhat.com/security/cve/cve-2016-6346"))
DESCR_WADL_SCAN = "Apache CXF RS application exposes {0}. You should check manually or using {1} that all resource methods " \
"have proper authentication/authorization.".format(
hyperlink("WADL","https://en.wikipedia.org/wiki/Web_Application_Description_Language"),
hyperlink("SOAPUI","https://www.soapui.org/downloads/soapui.html"))
DESCR_CVE_2016_8739_SCAN = "Resource method of Apache CXF RS application lacks {0} annotation or have permissive media type specification. " \
"Apache CXF is vulnerable to XXE attack - {1}. Attacker can trigger unmarshalling using vulnerable Atom provider " \
"by specifying application/atom+xml Content-Type header.".format(
hyperlink("@Consumes","https://docs.oracle.com/cd/E19776-01/820-4867/ggqqr/"),
hyperlink("CVE-2016-8739","https://bugzilla.redhat.com/show_bug.cgi?id=1406811"))
DESCR_CSRF_SCAN = "Apache CXF RS resource method migth be vulnerable to CSRF attack. URL parameters {0} and {1} are supported." \
"Parameter _method allows to override HTTP request method. Parameter _ctype allows to override Content-Type header".format(
hyperlink("_method","http://cxf.apache.org/docs/jax-rs.html#JAX-RS-Debugging"),
hyperlink("_ctype","http://cxf.apache.org/docs/jax-rs.html#JAX-RS-Debugging")
)
def doActiveScan(self, basePair, insertionPoint):
issues = []
self.request, self.response, self.request_headers, self.response_headers, self.info_request, self.info_response = prepare(basePair)
self.httpService = basePair.getHttpService()
self.URL = helpers.analyzeRequest(basePair).getUrl()
if not should_trigger_per_request_attacks(self.info_request, insertionPoint):
return []
issues.extend(self.cxf_scan())
issues.extend(self.wadl_scan())
issues.extend(self.csrf_scan())
issues.extend(self.detect_gzip_dos())
issues.extend(self.cve_2016_8739_scan())
return issues
def doPassiveScan(self, basePair):
return []
def cxf_scan(self):
_methods = ["GET", "POST", "PUT", "DELETE", "PATCH"]
if not isOk(self.info_response.getStatusCode()):
return []
param = helpers.buildParameter("_method", self.info_request.getMethod(), IParameter.PARAM_URL)
_methods.remove(self.info_request.getMethod())
request = safe_bytes_to_string( helpers.addParameter(self.request, param) )
request = request.replace(self.info_request.getMethod(), choice(_methods), 1)
modifPair = callbacks.makeHttpRequest(self.httpService, request)
response_modif = safe_bytes_to_string(modifPair.getResponse())
debug2console("Detect CXF Scan", request, response_modif)
if isOk(get_response_status_code(response_modif)) and \
self.response_headers.get('Content-Type') == get_response_headers_as_dict(response_modif).get('Content-Type'):
return [
ScanIssue(self.httpService, self.URL,[ modifPair ],
"Apache CXF RS application",
CXFJaxRsScanner.DESCR_CXF_SCAN,
'Certain', 'Information'),
]
return []
def detect_gzip_dos(self):
_message = "java.util.zip.ZipException"
if not isOk(self.info_response.getStatusCode()):
return []
request = self.request
if self.info_request.getMethod() == 'GET':
request = helpers.toggleRequestMethod(request)
param = helpers.buildParameter("_method", "GET", IParameter.PARAM_URL)
request = safe_bytes_to_string(helpers.addParameter(request,param))
request = safe_bytes_to_string( add_body_to_request(request, get_random_string(10)) )
request = add_header_to_request(request, 'SOAPJMS_contentEncoding: ', 'x-gzip')
newPair = callbacks.makeHttpRequest(self.httpService, request)
resp = safe_bytes_to_string(newPair.getResponse())
debug2console("CXF GZIP DoS Scan", request, resp)
if _message in get_response_body(resp):
return [
ScanIssue(self.httpService, self.URL, [ newPair ],
"Apache CXF RS application GZIP DoS attack",
CXFJaxRsScanner.DESCR_GZIP_DOS_SCAN,
'Certain', 'Medium')
]
return []
def wadl_scan(self):
request = self.request
if self.info_request.getMethod() != 'GET':
request = helpers.toggleRequestMethod(request)
param = helpers.buildParameter("_wadl", "true", IParameter.PARAM_URL)
request = safe_bytes_to_string(helpers.addParameter(request, param))
newPair = callbacks.makeHttpRequest(self.httpService, request)
resp = safe_bytes_to_string(newPair.getResponse())
debug2console("CXF WADL Scan", request, resp)
if not (isOk(get_response_status_code(resp)) and \
get_response_headers_as_dict(resp).get('Content-Type') == 'application/xml'):
return []
m = search('(?im)<resources\s+base\s*=\s*"([^"]*)"',resp)
if not m:
return []
new_path = urlparse(m.group(1)).path
request = add_header_to_request(request, "GET ", "%s?_wadl=true HTTP/1.1" % new_path)
newPair = callbacks.makeHttpRequest(self.httpService, request )
resp = safe_bytes_to_string(newPair.getResponse())
debug2console("CXF WADL Scan", request, resp)
return [
ScanIssue(self.httpService, self.URL, [ newPair ],
"Apache CXF RS application exposes WADL",
CXFJaxRsScanner.DESCR_WADL_SCAN,
'Certain', 'Medium'),
]
def csrf_scan(self):
""" Content-Type to _ctype values mapping """
ctype_dict = {
"application/json" : "json",
"application/xml" : "xml",
"application/atom+xml" : "atom"
}
if not isOk(self.info_response.getStatusCode()):
return []
if self.info_request.getMethod() == 'GET' and \
(not self.request_headers.has_key('Content-Length') or \
int(self.request_headers.get('Content-Length',0)) == 0):
return []
body = get_request_body(safe_bytes_to_string(self.request))
request = helpers.toggleRequestMethod(self.request)
if self.info_request.getMethod() != 'GET':
param = helpers.buildParameter("_method", self.info_request.getMethod(), IParameter.PARAM_URL)
request = safe_bytes_to_string(helpers.addParameter(request, param))
ctype = ctype_dict.get(self.request_headers['Content-Type'], "json")
param = helpers.buildParameter("_ctype", ctype, IParameter.PARAM_URL)
request = safe_bytes_to_string(helpers.addParameter(request, param))
request = safe_bytes_to_string( add_body_to_request(request, body) )
newPair = callbacks.makeHttpRequest(self.httpService, request)
response_modif = safe_bytes_to_string(newPair.getResponse())
debug2console("CXF CSRF Scan", request, response_modif)
if isOk(get_response_status_code(response_modif)) and \
self.response_headers.get('Content-Type') == get_response_headers_as_dict(response_modif).get('Content-Type'):
return [
ScanIssue(self.httpService, self.URL, [ newPair ],
"Apache CXF RS resource method is vulnerable to CSRF",
CXFJaxRsScanner.DESCR_CSRF_SCAN,
'Firm', 'Medium'),
]
return []
def cve_2016_8739_scan(self):
if not self.request_headers.has_key('Content-Length') or \
int(self.request_headers.get('Content-Length',0)) == 0:
return []
request = safe_bytes_to_string( add_header_to_request(self.request, "Content-Type: ", "application/atom+xml") )
body = '<?xml version="1.0" encoding="utf-8"?>' + \
'<!DOCTYPE feed SYSTEM "nosuch://%s" []>' + \
'<feed xmlns="http://www.w3.org/2005/Atom"></feed>'
r = get_random_string(10)
body = body % r
request = safe_bytes_to_string( add_body_to_request(request, body) )
newPair = callbacks.makeHttpRequest(self.httpService, request)
response_modif = safe_bytes_to_string(newPair.getResponse())
debug2console("CVE-2016-8739 Scan", request, response_modif)
if get_response_status_code(response_modif) in (400, 500) and r in response_modif:
return [
ScanIssue(self.httpService, self.URL, [ newPair ],
"Apache CXF RS resource method is vulnerable to CVE-2016-8739",
CXFJaxRsScanner.DESCR_CVE_2016_8739_SCAN,
'Certain', 'High'),
]
return []
class ResteasyScanner(IScannerCheck):
DESCR_CVE_2016_7050_SCAN = "Resource method of RESTEasy application lacks {0} annotation or have permissive media type specification. " \
"RESTEasy is vulnerable to Java deserialization attack - {1}. Attacker can trigger unmarshalling using vulnerable Serializable provider " \
"by specifying application/x-java-serialized-object Content-Type header.".format(
hyperlink("@Consumes","https://docs.oracle.com/cd/E19776-01/820-4867/ggqqr/"),
hyperlink("CVE-2016-7050","https://bugzilla.redhat.com/show_bug.cgi?id=CVE-2016-7050"))
DESCR_ASYNCH_SCAN = "RESTEasy application supports {0}. It might be vulnerable to {1}.".format(
hyperlink("Async jobs","http://docs.jboss.org/resteasy/docs/3.1.0.Final/userguide/html_single/index.html#async_job_service"),
hyperlink("CVE-2016-6345","https://access.redhat.com/security/cve/cve-2016-6345"))
DESCR_CVE_2016_9571_SCAN = "Resource method of RESTEasy application lacks {0} annotation or have permissive media type specification. " \
"RESTEasy is vulnerable to Yaml unmarshalling attack - {1}. Attacker can trigger unmarshalling using vulnerable Yaml provider " \
"by specifying text/yaml Content-Type header.".format(
hyperlink("@Consumes","https://docs.oracle.com/cd/E19776-01/820-4867/ggqqr/"),
hyperlink("CVE-2016-9571","https://bugzilla.redhat.com/show_bug.cgi?id=CVE-2016-9571"))
DECSR_URL_PARAM_CT_MAPPING_SCAN = "RESTEasy application supports response content type negotiation via {0}. " \
"It might be vulnerable to XSSI or XSS attacks.".format(
hyperlink("query string parameter","http://docs.jboss.org/resteasy/docs/3.1.0.Final/userguide/html_single/index.html#param_media_mappings"))
def doActiveScan(self, basePair, insertionPoint):
issues = []
self.request, self.response, self.request_headers, self.response_headers, self.info_request, self.info_response = prepare(basePair)
self.httpService = basePair.getHttpService()
self.URL = helpers.analyzeRequest(basePair).getUrl()
if not should_trigger_per_request_attacks(self.info_request, insertionPoint):
return []
issues.extend(self.cve_2016_7050_scan())
issues.extend(self.cve_2016_9571_scan())
issues.extend(self.asynch_scan())
issues.extend(self.url_mapping_scan())
return issues
def doPassiveScan(self, basePair):
return []
def cve_2016_7050_scan(self):
if not self.request_headers.has_key('Content-Length') or \
int(self.request_headers.get('Content-Length',0)) == 0:
return []
request = safe_bytes_to_string( add_header_to_request(self.request, "Content-Type: ", "application/x-java-serialized-object") )
body = get_random_string(4)
request = safe_bytes_to_string( add_body_to_request(request, body) )
newPair = callbacks.makeHttpRequest(self.httpService, request)
response_modif = safe_bytes_to_string( newPair.getResponse() )
debug2console("CVE-2016-7050 Scan", request, response_modif)
if get_response_status_code(response_modif) in (400, 500) and "java.io.StreamCorruptedException" in response_modif:
return [
ScanIssue(self.httpService, self.URL, [ newPair ],
"RESTEasy resource method is vulnerable to CVE-2016-7050",
ResteasyScanner.DESCR_CVE_2016_7050_SCAN,
'Certain', 'High'),
]
return []
def cve_2016_9571_scan(self):
if not self.request_headers.has_key('Content-Length') or \
int(self.request_headers.get('Content-Length',0)) == 0:
return []
request = safe_bytes_to_string( add_header_to_request(self.request, "Content-Type: ", "text/yaml") )
body = get_random_string(10)
request = safe_bytes_to_string( add_body_to_request(request, body) )
newPair = callbacks.makeHttpRequest(self.httpService, request)
response_modif = safe_bytes_to_string( newPair.getResponse() )
debug2console("CVE-2016-9571 Scan", request, response_modif)