-
Notifications
You must be signed in to change notification settings - Fork 26
/
blhchecker.py
217 lines (185 loc) · 6.3 KB
/
blhchecker.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
from burp import IBurpExtender,IScannerCheck,IScanIssue,IHttpService
from array import array
import re,threading,requests
try:
import Queue as queue
except:
import queue as queue
from java.net import URL
import urlparse
from java.io import PrintWriter
from java.lang import RuntimeException
## FOR URL BASED, Use below regex
#REGEX = r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+'
## LINK BASED REGEX FOR HTML
regex_str = r'(href|src|icon|data|url)=[\'"]?([^\'" >;]+)'
WHITELIST_CODES = [200]
WHITELIST_PATTERN = ["/","../","http","www"]
WHITELIST_MEMES = ['HTML','Script','Other text','CSS','JSON']
class BurpExtender(IBurpExtender,IScannerCheck):
extension_name = "Link Hijacking"
q = queue.Queue()
temp_urls = []
down_urls = []
no_of_threads = 15
def registerExtenderCallbacks(self,callbacks):
try:
self._callbacks = callbacks
self._helpers = callbacks.getHelpers()
self._callbacks.setExtensionName(self.extension_name)
self._stdout = PrintWriter(self._callbacks.getStdout(),True)
self._stderr = PrintWriter(self._callbacks.getStderr(),True)
callbacks.registerScannerCheck(self)
self._stdout.println("Extension Successfully Installed")
return
except Exception as e:
self._stderr.println("Installation Problem ?")
self._stderr.println(str(e))
return
def split(self,strng, sep, pos):
strng = strng.split(sep)
return sep.join(strng[:pos]), sep.join(strng[pos:])
def Resolver(self,url):
try:
r = requests.get(url,verify=False)
return "UP"
except requests.exceptions.ConnectionError,e:
if 'getaddrinfo failed' in str(e):
return "DOWN"
else:
return "UP"
except Exception as e:
return "UP"
def _up_check(self,url):
parse_object = urlparse.urlparse(url)
hostname = parse_object.netloc
if parse_object.scheme == 'https':
port = 443
SSL = True
else:
port = 80
SSL = False
try:
check_status = self.Resolver(str(url))
if check_status == "UP":
req_bytes = self._helpers.buildHttpRequest(URL(str(url)))
res_bytes = self._callbacks.makeHttpRequest(hostname,port,SSL,req_bytes)
res = self._helpers.analyzeResponse(res_bytes)
if res.getStatusCode() not in WHITELIST_CODES:
return url
if check_status == "DOWN":
self.down_urls.append(str(url))
return None
except:
print('SKIPPING : ',url)
return None
def process_queue(self,res):
while not self.q.empty():
url = self.q.get()
furl = self._up_check(str(url))
if furl is not None:
#print(furl)
self.temp_urls.append(furl)
def _blf(self,baseRequestResponse,regex,host):
res = self._helpers.bytesToString(baseRequestResponse.getResponse())
re_r = regex.findall(res)
if len(re_r) == 0:
return
re_r = list(set([tuple(j for j in re_r if j)[-1] for re_r in re_r]))
re_r = list(set([i for i in re_r if i.startswith(tuple(WHITELIST_PATTERN))]))
#re_r = [i.encode('utf-8').strip('') for i in re_r if isinstance(i,bytes)]
#re_r = [i.replace('\\','/') for i in re_r]
final = []
for i in re_r:
if i.startswith('//') and ' ' not in i:
i = 'https:'+str(i)
final.append(i)
elif i.startswith('/') and ' ' not in i:
if '.' in i.split('/')[1]:
i = 'https:/'+str(i)
final.append(i)
else:
i = str(host)+str(i)
final.append(i)
elif (i.startswith('http') or i.startswith('www.')) and ' ' not in i:
final.append(i)
elif i.startswith('../') and ' ' not in i:
i = str(host)+'/'+str(i)
final.append(i)
for url in final:
self.q.put(str(url))
threads = []
for i in range(int(BurpExtender.no_of_threads)):
t = threading.Thread(target=self.process_queue,args=(baseRequestResponse,))
threads.append(t)
t.start()
for j in threads:
j.join()
return True
def doPassiveScan(self,baseRequestResponse):
reqInfo = self._helpers.analyzeRequest(baseRequestResponse.getHttpService(),baseRequestResponse.getRequest())
if self._callbacks.isInScope(reqInfo.getUrl()) and str(self._helpers.analyzeResponse(baseRequestResponse.getResponse()).getInferredMimeType()) in WHITELIST_MEMES:
_HTTP = baseRequestResponse.getHttpService()
print('ON : ',str(reqInfo.getUrl()))
_host = str(_HTTP.getProtocol())+"://"+str(_HTTP.getHost())
regex = re.compile(regex_str,re.VERBOSE)
res = self._blf(baseRequestResponse,regex,_host)
if res and len(self.down_urls) > 0:
final_down_urls = self.down_urls[:]
self.down_urls[:] = []
final_down_urls = list(set(final_down_urls))
return [CustomScanIssue(baseRequestResponse.getHttpService(),self._helpers.analyzeRequest(baseRequestResponse).getUrl(),
[self._callbacks.applyMarkers(baseRequestResponse,None,None)],"Broken Link Hijacking",final_down_urls,"Medium",_HTTP.getHost())]
if res and len(self.temp_urls) > 0:
final_urls = self.temp_urls[:]
self.temp_urls[:] = []
final_urls = list(set(final_urls))
return [CustomScanIssue(baseRequestResponse.getHttpService(),self._helpers.analyzeRequest(baseRequestResponse).getUrl(),
[self._callbacks.applyMarkers(baseRequestResponse,None,None)],"Broken Link Hijacking",final_urls,"Information",_HTTP.getHost())]
def consolidateDuplicateIssues(self,existingIssue,newIssue):
if existingIssue.getIssueName() == newIssue.getIssueName():
return -1
return 0
def extensionUnloaded(self):
self._stdout.println("Extension was unloaded")
return
class CustomScanIssue (IScanIssue):
def __init__(self, httpService, url, httpMessages, name, detail, severity, hostbased):
self._httpService = httpService
self._url = url
self._httpMessages = httpMessages
self._name = name
self._detail = detail
self._severity = severity
self._hostbased = hostbased
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 "Certain"
def getIssueBackground(self):
pass
def getRemediationBackground(self):
pass
def getIssueDetail(self):
host_b = []
for i in self._detail:
if self._hostbased not in i:
host_b.append(str(i))
if len(host_b) > 0:
final = '<br>'.join(host_b)
return str(final)
else:
final_without_host = '<br>'.join(self._detail)
return str(final_without_host)
def getRemediationDetail(self):
pass
def getHttpMessages(self):
return self._httpMessages
def getHttpService(self):
return self._httpService