-
Notifications
You must be signed in to change notification settings - Fork 30
/
RegexScanner.py
194 lines (153 loc) · 6.54 KB
/
RegexScanner.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
__author__ = 'Jake Miller (@LaconicWolf)'
__date__ = '20190215'
__version__ = '0.01'
__description__ = """\
Burp Extension that implements a passive scanner that looks
for a regular expression.
Adapted from: https://github.com/OpenSecurityResearch/CustomPassiveScanner/blob/master/CustomPassiveScanner.py
"""
'''
use of:
atob
eval
admin
role
document.location
document.referrer
comments
localstorage
'''
# Burp imports
from burp import IBurpExtender, IScannerCheck, IScanIssue
# Python imports
import re
from array import array
# For easier debugging, if you want.
# https://github.com/securityMB/burp-exceptions
import sys
try:
from exceptions_fix import FixBurpExceptions
except ImportError:
pass
class BurpExtender(IBurpExtender, IScannerCheck):
def registerExtenderCallbacks(self, callbacks):
# Required for easier debugging:
sys.stdout = callbacks.getStdout()
# Keep a reference to our callbacks object
self._callbacks = callbacks
# Obtain an extension helpers object
self._helpers = callbacks.getHelpers()
# Set our extension name
self._callbacks.setExtensionName("Regex Passive Scanner")
# Register our extension as a custom scanner check, so Burp will use this extension
# to perform active or passive scanning and report on scan issues returned
self._callbacks.registerScannerCheck(self)
return
# This method is called when multiple issues are reported for the same URL
# In this case we are checking if the issue detail is different, as the
# issues from our scans include affected parameters/values in the detail,
# which we will want to report as unique issue instances
def consolidateDuplicateIssues(self, existingIssue, newIssue):
if (existingIssue.getIssueDetail() == newIssue.getIssueDetail()):
return -1
else:
return 0
# Implement the doPassiveScan method of IScannerCheck interface
# Burp Scanner invokes this method for each base request/response that is passively scanned.
def doPassiveScan(self, baseRequestResponse):
# Local variables used to store a list of ScanIssue objects
scan_issues = []
tmp_issues = []
# Create an instance of our CustomScans object, passing the
# base request and response, and out callbacks object
self._CustomScans = CustomScans(baseRequestResponse, self._callbacks)
# Call the findRegEx method of our CustomScans object to check
# the response for anything matching a specified regular expression
regex = r'"(/.*?)"'
issuename = 'Regex Detected'
issuelevel = 'Information'
issuedetail = """The application response contains the following value
<br><br><b>$value$</b><br><br> which matches the format of the
specified regex."""
tmp_issues = self._CustomScans.findRegEx(regex, issuename, issuelevel, issuedetail)
# Add the issues from findRegEx to the list of issues to be returned
scan_issues = scan_issues + tmp_issues
# Finally, per the interface contract, doPassiveScan needs to return a
# list of scan issues, if any, and None otherwise
if len(scan_issues) > 0:
return scan_issues
else:
return None
class CustomScans:
def __init__(self, requestResponse, callbacks):
# Set class variables with the arguments passed to the constructor
self._requestResponse = requestResponse
self._callbacks = callbacks
# Get an instance of IHelprs, which has lots of usefule methods, as a class
# variable, so we have class-level scope to all the helper methods
self._helpers = self._callbacks.getHelpers()
# Put the parameters from the HTTP message in a class variable so we have class-level scope.
self._params = self._helpers.analyzeRequest(requestResponse.getRequest()).getParameters()
return
# This is a custom scan method to Look for all occurrences in the response
# that match the passed regular expression
def findRegEx(self, regex, issuename, issuelevel, issuedetail):
scan_issues = []
offset = array('i', [0, 0])
response = self._requestResponse.getResponse()
responseLength = len(response)
# Compile the regular expression, telling Python to ignore EOL/LF
myre = re.compile(regex, re.DOTALL)
# Using the regular expression, find all occurences in the base response
match_vals = myre.findall(self._helpers.bytesToString(response))
# For each matched value found, find its start position, so that we can create
# the offset needed to apply appropriate markers in the resulting Scanner issue
for ref in match_vals:
print ref
offsets = []
start = self._helpers.indexOf(response, ref, True, 0, responseLength)
offset[0] = start
offset[1] = start + len(ref)
offsets.append(offset)
# Create a ScanIssue object and append it to our list of issues, marking
# the matched value in the response.
scan_issues.append(ScanIssue(self._requestResponse.getHttpService(),
self._helpers.analyzeRequest(self._requestResponse).getUrl(),
[self._callbacks.applyMarkers(self._requestResponse, None, offsets)],
issuename, issuelevel, issuedetail.replace("$value$", ref)))
return (scan_issues)
# Implementation of the IScanIssue interface with simple constructor and getter methods
class ScanIssue(IScanIssue):
def __init__(self, httpservice, url, requestresponsearray, name, severity, detailmsg):
self._url = url
self._httpservice = httpservice
self._requestresponsearray = requestresponsearray
self._name = name
self._severity = severity
self._detailmsg = detailmsg
def getUrl(self):
return self._url
def getHttpMessages(self):
return self._requestresponsearray
def getHttpService(self):
return self._httpservice
def getRemediationDetail(self):
return None
def getIssueDetail(self):
return self._detailmsg
def getIssueBackground(self):
return None
def getRemediationBackground(self):
return None
def getIssueType(self):
return 0
def getIssueName(self):
return self._name
def getSeverity(self):
return self._severity
def getConfidence(self):
return "Certain"
try:
FixBurpExceptions()
except:
pass