-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSSRFScanner.py
276 lines (216 loc) · 10 KB
/
SSRFScanner.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
from burp import IBurpExtender
from burp import IScannerCheck
from burp import IScanIssue
from array import array
from java.io import PrintWriter
from burp import IBurpCollaboratorClientContext
from burp import IBurpCollaboratorInteraction
import re
import threading
import os,time,base64,struct
urlpattern = re.compile(r'(http|https|ftp)(:|%3A)(\/|\%2F){2}(\w+[^\s&]+)(\.[^\s&]+){1,}',re.I|re.M|re.U)
ctpat = re.compile(r'Content-Length:\s*\d+',re.I)
import random
import string
def get_random_string(length):
# Random string with the combination of lower and upper case
letters = string.ascii_letters
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str.lower()
def b2i(b):
return int(struct.unpack("B", b)[0])
class BurpExtender(IBurpExtender, IScannerCheck,IBurpCollaboratorClientContext):
#
# implement IBurpExtender
#
def registerExtenderCallbacks(self, callbacks):
# keep a reference to our callbacks object
self._callbacks = callbacks
# obtain an extension helpers object
self._helpers = callbacks.getHelpers()
# set our extension name
callbacks.setExtensionName("ssrfscanner")
self.stdout = PrintWriter(callbacks.getStdout(), True)
self.stderr = PrintWriter(callbacks.getStderr(), True)
self.collaboratorContext = callbacks.createBurpCollaboratorClientContext()
# global GcollaboratorContext,Gstdout
# GcollaboratorContext = self.collaboratorContext
# Gstdout = self.stdout
self.ssrfpayload = self.collaboratorContext.generatePayload(True)
# register ourselves as a custom scanner check
callbacks.registerScannerCheck(self)
# helper method to search a response for occurrences of a literal match string
# and return a list of start/end offsets
def _get_matches(self, response, match):
matches = []
start = 0
reslen = len(response)
matchlen = len(match)
while start < reslen:
start = self._helpers.indexOf(response, match, True, start, reslen)
if start == -1:
break
matches.append(array('i', [start, start + matchlen]))
start += matchlen
return matches
def dealmatch(self,matchobj):
#self.stdout.println("[ssrfpayload:]"+self.randomstr + '.'+self.ssrfpayload+'/')
return matchobj.group(0).replace(matchobj.group(4),self.randomstr + '.'+self.ssrfpayload+'/')
#
# implement IScannerCheck
#
def doPassiveScan(self, baseRequestResponse):
# look for matches of our passive check grep string
#matches = self._get_matches(baseRequestResponse.getResponse(), GREP_STRING_BYTES)
#if (len(matches) == 0):
# return None
self.randomstr = get_random_string(5)
url = self._helpers.analyzeRequest(baseRequestResponse).getUrl()
urlpath = url.getPath()
if('.js' in urlpath or '.css' in urlpath or '.font' in urlpath or '.jpg' in urlpath or '.js' in urlpath or '.png' in urlpath or '.webp' in urlpath or '.gif' in urlpath or '.svg' in urlpath):
return None
OldReq = self._helpers.bytesToString(baseRequestResponse.getRequest())
OrigLen = len(OldReq)
#self.stdout.println("Scanning: "+(url.getHost()+url.getPath()).replace("/","").replace("\\","").replace("&",""))
#self.stdout.println("[url]: "+url.toString())
#self.stdout.println(self.ssrfpayload)
randomssrfpayload = self.randomstr + '.'+self.ssrfpayload
#self.stdout.println("[random payload]: "+randomssrfpayload)
#self.stdout.println("OLd:"+OldReq)
#self.ssrfpayload = "zzz."+self.collaboratorContext.generatePayload(True)
firstlineindex = OldReq.index("\r\n")
firstlinelist = OldReq[:firstlineindex].split("?",2)
NewReq = ""
if len(firstlinelist) == 2:
NewReq = "".join((firstlinelist[0],'?',re.sub(urlpattern,self.dealmatch,firstlinelist[1]),OldReq[firstlineindex:]))
else:
NewReq = OldReq
#NewReq = "".join((re.sub(urlpattern,self.dealmatch,OldReq[:firstlineindex]),OldReq[firstlineindex:]))
#headers,body = NewReq.split("\r\n\r\n")
postindex = NewReq.index("\r\n\r\n")
headers = NewReq[0:postindex]
body = NewReq[postindex:]
#print(body)
if len(body) > 4:
NewReq = "".join((headers,re.sub(urlpattern,self.dealmatch,body)))
newbodyindex = NewReq.index("\r\n\r\n")
newbody = NewReq[newbodyindex+4:]
#newheaders,newbody = NewReq.split("\r\n\r\n")
NewReq = re.sub(ctpat,"Content-Length: "+str(len(newbody)),NewReq)
if(len(NewReq) == OrigLen):
return None
#self.stdout.println("New:"+NewReq)
#NewReq = OldReq.replace(Rurl, PreviousPath+"/"+p)
self.stdout.println("[url]: "+url.toString())
self.stdout.println("[random payload]: "+randomssrfpayload)
checkRequestResponse = self._callbacks.makeHttpRequest(baseRequestResponse.getHttpService(), self._helpers.stringToBytes(NewReq))
#collresult = self.collaboratorContext.fetchAllCollaboratorInteractions()
collresult = self.collaboratorContext.fetchCollaboratorInteractionsFor(self.ssrfpayload)
self.stdout.println(len(collresult))
if(len(collresult) == 0):
return None
vulnflag = False
othervulnflag = False
otherdomain = ""
issueresult = []
domainresult = []
for coll in collresult:
self.stdout.println(coll.getProperties())
type = coll.getProperty('type')
if type == 'DNS':
rq = base64.b64decode(coll.getProperty('raw_query'))
#self.stdout.println(rq)
index = 12
domains = ""
count = 10
while count > 0:
curindex = index + 1
#self.stdout.println(curindex)
curlen = b2i(rq[index])
endindex = curindex + curlen
partdomain = ''.join(chr (b2i(x)) for x in rq[curindex:endindex])
domains += partdomain
if(partdomain == 'net'):
break
domains += '.'
index = endindex
count = count -1
self.stdout.println("[SSRF----------------------------------]:"+domains)
#self.stdout.println("[SSRF---------------RAND-STR-------------------]:"+randomssrfpayload)
if not vulnflag and domains == randomssrfpayload:
#self.stdout.println("[SSRF---------------EXACTLY----------------]:"+domains)
vulnflag = True
issueresult.append(CustomScanIssue(
baseRequestResponse.getHttpService(),
self._helpers.analyzeRequest(baseRequestResponse).getUrl(),
[self._callbacks.applyMarkers(baseRequestResponse, None, None)],
"SSRF",
"The response contains the string: " + randomssrfpayload,
"High","Certain"))
elif domains != "" and domains != randomssrfpayload:
self.stdout.println("[SSRF---------------YOU-SHOULD-CHECK-IT-YOURSELF----------------]:"+domains)
#self.stdout.println(domains != self.ssrfpayload)
othervulnflag = True
otherdomain = domains
if domains not in domainresult:
domainresult.append(domains)
issueresult.append(CustomScanIssue(
baseRequestResponse.getHttpService(),
self._helpers.analyzeRequest(baseRequestResponse).getUrl(),
[self._callbacks.applyMarkers(baseRequestResponse, None, None)],
"SSRFOther",
"Found %s please check history for vuln" % otherdomain,
"High","Tentative"))
#self._helpers.base64Encode
# report the issue
if vulnflag or othervulnflag:
return issueresult
def doActiveScan(self, baseRequestResponse, insertionPoint):
# make a request containing our injection test in the insertion point
return None
def consolidateDuplicateIssues(self, existingIssue, newIssue):
# This method is called when multiple issues are reported for the same URL
# path by the same extension-provided check. The value we return from this
# method determines how/whether Burp consolidates the multiple issues
# to prevent duplication
#
# Since the issue name is sufficient to identify our issues as different,
# if both issues have the same name, only report the existing issue
# otherwise report both issues
if existingIssue.getIssueName() == newIssue.getIssueName():
return -1
return 0
#
# class implementing IScanIssue to hold our custom scan issue details
#
class CustomScanIssue (IScanIssue):
def __init__(self, httpService, url, httpMessages, name, detail, severity, confidence):
self._httpService = httpService
self._url = url
self._httpMessages = httpMessages
self._name = name
self._detail = detail
self._severity = severity
self._confidence = confidence
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):
pass
def getRemediationBackground(self):
pass
def getIssueDetail(self):
return self._detail
def getRemediationDetail(self):
pass
def getHttpMessages(self):
return self._httpMessages
def getHttpService(self):
return self._httpService