-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathJSpector.3.0.0.py
312 lines (240 loc) · 15 KB
/
JSpector.3.0.0.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
from burp import (IBurpExtender, IHttpListener, IScannerListener,
IExtensionStateListener, IContextMenuFactory, IScanIssue)
import re
from java.util import ArrayList
from javax.swing import JMenuItem, JOptionPane
from java.awt import Toolkit
from java.awt.datatransfer import StringSelection
class BurpExtender(IBurpExtender, IHttpListener, IScannerListener, IExtensionStateListener, IContextMenuFactory):
def __init__(self):
self._exclusion_regex = re.compile(r'http://www\.w3\.org')
self._url_pattern = re.compile(
r'(?:http|https|ftp|ftps|sftp|file|tftp|telnet|gopher|ldap|ssh)://[^\s"<>]+')
self._endpoint_pattern1 = re.compile(
r'(?:(?<=["\'])/(?:[^/"\']+/?)+(?=["\']))')
self._endpoint_pattern2 = re.compile(
r'http\.(?:post|get|put|delete|patch)\(["\']((?:[^/"\']+/?)+)["\']')
self._endpoint_pattern3 = re.compile(
r'httpClient\.(?:post|get|put|delete|patch)\(this\.configuration\.basePath\+["\']/(?:[^/"\']+/?)+["\']')
self._dangerousmethods = re.compile(r'(?:[a-zA-Z0-9\'\".\-_ \t]*\.(?:eval|write|writeln|innerHTML|outerHTML|insertAdjacentHTML|setAttribute|createElement|setTimeout|setInterval|Function|execScript|send|open|exec|compile|globalEval|decodeURI|decodeURIComponent|atob|addEventListener|createAttribute|link|unescape|new\ Function|navigate|setState|forceUpdate|unmountComponentAtNode|createPortal|createElementNS|defineProperties|\.unescape|\$\.(?:html|append|replaceWith|ajax|get|post|getJSON|getScript|parseJSON))|reactDOM\.render|dangerouslySetInnerHTML|v-html|\.trustAsHtml|ng-bind-html-unsafe|flatmap-stream[^\w]|_\.__proto__[^\w]|_\.prototype[^\w]|(?:marked|Buffer\.alloc|Buffer\.from|serialize-javascript|kind-of|static-eval|http-proxy|acorn|is-promise|concat-stream|negotiator|st|fresh|send|clean-css|serialize|node-serialize|cookie-signature|bcrypt|node.extend|mime|uglify-js|ws|merge|sanitize-html|deep-extend|pac-resolver|glob-parent|css-what|browserslist)\(\)|angular\.(?:bind|copy|extend|forEach|fromJson|identity|injector|isArray|isDate|isDefined|isElement|isFunction|isNumber|isObject|isString|isUndefined|lowercase|mock\.\w+|module|noop|toJson|uppercase|version)|react\.(?:createClass|createElement|cloneElement|createFactory|isValidElement|DOM\.\w+|PropTypes\.\w+|Children\.\w+|render|unmountComponentAtNode|findDOMNode|batchedUpdates|renderToString|renderToStaticMarkup|version))\((?:\s*"[^"]*"\s*,)*\s*[a-zA-Z_]\w*\s*(?:,\s*(?:"[^"]*"|[a-zA-Z_]\w*)\s*)*\)')
self._invocation = None
self._scanned_js_files = set()
def registerExtenderCallbacks(self, callbacks):
self._callbacks = callbacks
self._helpers = callbacks.getHelpers()
callbacks.setExtensionName("JSpector")
callbacks.registerHttpListener(self)
callbacks.registerScannerListener(self)
callbacks.registerExtensionStateListener(self)
callbacks.registerContextMenuFactory(self)
print("JSpector extension loaded successfully.\nWarning: the size of the output console content is limited, we recommend that you save your results in a file.\n")
def processHttpMessage(self, toolFlag, messageIsRequest, messageInfo):
def is_js_file(url):
return url.lower().endswith('.js')
if not messageIsRequest and self._callbacks.isInScope(messageInfo.getUrl()):
js_url = messageInfo.getUrl().toString()
if js_url not in self._scanned_js_files:
self._scanned_js_files.add(js_url)
response = messageInfo.getResponse()
if response:
response_info = self._helpers.analyzeResponse(response)
headers = response_info.getHeaders()
content_type = next((header.split(':', 1)[1].strip(
) for header in headers if header.lower().startswith('content-type:')), None)
content_type_is_js = content_type and 'javascript' in content_type.lower()
if content_type_is_js or is_js_file(js_url):
body = response[response_info.getBodyOffset():]
urls = self.extract_urls_from_js(body)
if urls:
self.create_issue(messageInfo, urls)
if toolFlag == self._callbacks.TOOL_PROXY:
self._scanned_js_files.add(js_url)
def extract_urls_from_js(self, js_code):
urls = set(re.findall(self._url_pattern, js_code))
endpoints1 = set(re.findall(self._endpoint_pattern1, js_code))
endpoints2 = set(re.findall(self._endpoint_pattern2, js_code))
endpoints3 = set(re.findall(self._endpoint_pattern3, js_code))
dangerousmethods = set(re.findall(self._dangerousmethods, js_code))
urls = set(url for url in urls if not self._exclusion_regex.search(url))
return urls.union(endpoints1, endpoints2, endpoints3, dangerousmethods)
def create_issue(self, messageInfo, urls):
issue = JSURLsIssue(self._helpers, messageInfo, urls)
self._callbacks.addScanIssue(issue)
js_full_url = messageInfo.getUrl().toString()
self.output_results(urls, js_full_url)
def extensionUnloaded(self):
print("JSpector extension unloaded.")
def newScanIssue(self, issue):
pass
def createMenuItems(self, invocation):
self._invocation = invocation
menu_items = ArrayList()
menu_item1 = JMenuItem("Export URLs to clipboard",
actionPerformed=self.export_urls_to_clipboard)
menu_items.add(menu_item1)
menu_item2 = JMenuItem("Export endpoints to clipboard",
actionPerformed=self.export_endpoints_to_clipboard)
menu_items.add(menu_item2)
menu_item3 = JMenuItem("Export Dangerous JS Methods to clipboard",
actionPerformed=self.export_dangerousmethods_to_clipboard)
menu_items.add(menu_item3)
menu_item4 = JMenuItem("Export all results to clipboard",
actionPerformed=self.export_results_to_clipboard)
menu_items.add(menu_item4)
return menu_items
def is_js_file(self, url):
return url.lower().endswith('.js')
def export_data_to_clipboard(self, export_type):
messages = self._invocation.getSelectedMessages()
if messages is None:
JOptionPane.showMessageDialog(None, "No JS file selected")
return
all_results = ""
for message in messages:
if self._callbacks.isInScope(message.getUrl()):
js_url = message.getUrl().toString()
if js_url in self._scanned_js_files:
response = message.getResponse()
if response:
response_info = self._helpers.analyzeResponse(response)
headers = response_info.getHeaders()
content_type = next((header.split(':', 1)[1].strip(
) for header in headers if header.lower().startswith('content-type:')), None)
content_type_is_js = content_type and 'javascript' in content_type.lower()
if content_type_is_js or self.is_js_file(js_url):
body = response[response_info.getBodyOffset():]
urls = self.extract_urls_from_js(body)
if urls:
if export_type == "urls":
urls_list, _, _ = self.sort_urls_endpoints(
urls)
for url in urls_list:
all_results += url + "\n"
elif export_type == "endpoints":
_, endpoints_list, _ = self.sort_urls_endpoints(
urls)
for endpoint in endpoints_list:
all_results += endpoint + "\n"
elif export_type == "dangerousmethods":
_, _, dangerousmethods_list = self.sort_urls_endpoints(
urls)
for dangerousmethod in dangerousmethods_list:
all_results += dangerousmethod + "\n"
elif export_type == "results":
results = self.format_results(js_url, urls)
all_results += results
return all_results
def export_urls_to_clipboard(self, event):
all_results = self.export_data_to_clipboard("urls")
self.copy_results_to_clipboard(all_results, "URLs")
def export_endpoints_to_clipboard(self, event):
all_results = self.export_data_to_clipboard("endpoints")
self.copy_results_to_clipboard(all_results, "endpoints")
def export_dangerousmethods_to_clipboard(self, event):
all_results = self.export_data_to_clipboard("dangerousmethods")
self.copy_results_to_clipboard(all_results, "dangerousmethods")
def export_results_to_clipboard(self, event):
all_results = self.export_data_to_clipboard("results")
self.copy_results_to_clipboard(all_results, "results")
def copy_results_to_clipboard(self, all_results, export_type):
if all_results:
num_results = len(all_results.split('\n')) - 1
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
clipboard.setContents(StringSelection(all_results), None)
message = "{} {} exported to clipboard".format(
num_results, export_type)
JOptionPane.showMessageDialog(None, message)
else:
JOptionPane.showMessageDialog(None, "No results found to export")
def format_results(self, js_full_url, urls):
urls_list, endpoints_list, dangerousmethods_list = self.sort_urls_endpoints(
urls)
formatted_results = ""
for url in urls_list:
formatted_results += url + "\n"
for endpoint in endpoints_list:
formatted_results += endpoint + "\n"
for dangerousmethod in dangerousmethods_list:
formatted_results += dangerousmethod + "\n"
return formatted_results
def output_results(self, urls, js_full_url):
urls_list, endpoints_list, dangerousmethods_list = self.sort_urls_endpoints(
urls)
print("JSpector results for {}:".format(js_full_url))
print("-----------------")
print("\nPotentially dangerous JS Methods found ({}):".format(
len(dangerousmethods_list)))
if dangerousmethods_list:
print("-----------------\n{}".format('\n'.join(dangerousmethods_list)))
else:
print("No dangerous JS Methods found.")
print("-----------------")
print("URLs found ({}):\n-----------------\n{}".format(len(urls_list),
'\n'.join(urls_list)))
print("\nEndpoints found ({}):".format(len(endpoints_list)))
if endpoints_list:
print("-----------------\n{}".format('\n'.join(endpoints_list)))
else:
print("No endpoints found.")
print("-----------------")
@staticmethod
def sort_urls_endpoints(urls):
urls_list = []
endpoints_list = []
dangerousmethods_list = []
for url in urls:
if re.match('^(?:http|https|ftp|ftps|sftp|file|tftp|telnet|gopher|ldap|ssh)://', url):
urls_list.append(url)
elif re.match('(?:[a-zA-Z0-9\'\".\-_ \t]*\.(?:eval|write|writeln|innerHTML|outerHTML|insertAdjacentHTML|setAttribute|createElement|setTimeout|setInterval|Function|execScript|send|open|exec|compile|globalEval|decodeURI|decodeURIComponent|atob|addEventListener|createAttribute|link|unescape|new\ Function|navigate|setState|forceUpdate|unmountComponentAtNode|createPortal|createElementNS|defineProperties|\.unescape|\$\.(?:html|append|replaceWith|ajax|get|post|getJSON|getScript|parseJSON))|reactDOM\.render|dangerouslySetInnerHTML|v-html|\.trustAsHtml|ng-bind-html-unsafe|flatmap-stream[^\w]|_\.__proto__[^\w]|_\.prototype[^\w]|(?:marked|Buffer\.alloc|Buffer\.from|serialize-javascript|kind-of|static-eval|http-proxy|acorn|is-promise|concat-stream|negotiator|st|fresh|send|clean-css|serialize|node-serialize|cookie-signature|bcrypt|node.extend|mime|uglify-js|ws|merge|sanitize-html|deep-extend|pac-resolver|glob-parent|css-what|browserslist)\(\)|angular\.(?:bind|copy|extend|forEach|fromJson|identity|injector|isArray|isDate|isDefined|isElement|isFunction|isNumber|isObject|isString|isUndefined|lowercase|mock\.\w+|module|noop|toJson|uppercase|version)|react\.(?:createClass|createElement|cloneElement|createFactory|isValidElement|DOM\.\w+|PropTypes\.\w+|Children\.\w+|render|unmountComponentAtNode|findDOMNode|batchedUpdates|renderToString|renderToStaticMarkup|version))\((?:\s*"[^"]*"\s*,)*\s*[a-zA-Z_]\w*\s*(?:,\s*(?:"[^"]*"|[a-zA-Z_]\w*)\s*)*\)', url):
dangerousmethods_list.append(url)
else:
endpoints_list.append(url)
urls_list.sort()
endpoints_list.sort()
dangerousmethods_list.sort()
return urls_list, endpoints_list, dangerousmethods_list
class JSURLsIssue(IScanIssue):
def __init__(self, helpers, messageInfo, urls):
self._helpers = helpers
self._httpService = messageInfo.getHttpService()
self._url = messageInfo.getUrl()
self._urls = urls
def getUrl(self):
return self._url
def getHttpMessages(self):
return []
def getHttpService(self):
return self._httpService
def getIssueName(self):
return "JSPector results"
def getIssueType(self):
return 0x08000000
def getSeverity(self):
return "Information"
def getConfidence(self):
return "Certain"
def getIssueBackground(self):
return "The following URLs were found in a JavaScript file. This information may be useful for further testing."
def getRemediationBackground(self):
return None
def getIssueDetail(self):
urls_list, endpoints_list, dangerousmethods_list = BurpExtender.sort_urls_endpoints(
self._urls)
details = self.build_list("Dangerous JS Methods found",
dangerousmethods_list)
details += self.build_list("URLs found", urls_list)
details += self.build_list("Endpoints found", endpoints_list)
return details
def getRemediationDetail(self):
return None
@staticmethod
def build_list(title, items):
if not items:
return ""
details = "<b>{title} ({num_items}):</b>".format(title=title,
num_items=len(items))
details += "<ul>"
for item in items:
details += "<li>{item}</li>".format(item=item)
details += "</ul>"
return details