forked from GerbenJavado/LinkFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkfinder.py
executable file
·466 lines (389 loc) · 15.6 KB
/
linkfinder.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
#!/usr/bin/env python
# Python 3
# LinkFinder
# By Gerben_Javado
# Fix webbrowser bug for MacOS
import os
os.environ["BROWSER"] = "open"
# Import libraries
import re, sys, glob, html, argparse, jsbeautifier, webbrowser, subprocess, base64, ssl, xml.etree.ElementTree, urllib.error
from urllib.parse import urlparse, urljoin
from gzip import GzipFile
from string import Template
try:
from StringIO import StringIO
readBytesCustom = StringIO
except ImportError:
from io import BytesIO
readBytesCustom = BytesIO
try:
from urllib.request import Request, urlopen
except ImportError:
from urllib2 import Request, urlopen
# Regex used
regex_str = r"""
(?:"|') # Start newline delimiter
(
((?:[a-zA-Z]{1,10}://|//) # Match a scheme [a-Z]*1-10 or //
[^"'/]{1,}\. # Match a domainname (any character + dot)
[a-zA-Z]{2,}[^"']{0,}) # The domainextension and/or path
|
((?:/|\.\./|\./) # Start with /,../,./
[^"'><,;| *()(%%$^/\\\[\]] # Next character can't be...
[^"'><,;|()]{1,}) # Rest of the characters can't be
|
([a-zA-Z0-9_\-/]{1,}/ # Relative endpoint with /
[a-zA-Z0-9_\-/.]{1,} # Resource name
\.(?:[a-zA-Z]{1,4}|action) # Rest + extension (length 1-4 or action)
(?:[\?|#][^"|']{0,}|)) # ? or # mark with parameters
|
([a-zA-Z0-9_\-/]{1,}/ # REST API (no extension) with /
[a-zA-Z0-9_\-/]{3,} # Proper REST endpoints usually have 3+ chars
(?:[\?|#][^"|']{0,}|)) # ? or # mark with parameters
|
([a-zA-Z0-9_\-]{1,} # filename
\.(?:php|asp|aspx|jsp|json|
action|html|js|txt|xml) # . + extension
(?:[\?|#][^"|']{0,}|)) # ? or # mark with parameters
)
(?:"|') # End newline delimiter
"""
context_delimiter_str = "\n"
def parser_error(errmsg):
'''
Error Messages
'''
print("Usage: python %s [Options] use -h for help" % sys.argv[0])
print("Error: %s" % errmsg)
sys.exit()
def parser_input(input):
'''
Parse Input
'''
# Method 1 - URL
if input.startswith(('http://', 'https://',
'file://', 'ftp://', 'ftps://')):
return [input]
# Method 2 - URL Inspector Firefox
if input.startswith('view-source:'):
return [input[12:]]
# Method 3 - Burp file
if args.burp:
jsfiles = []
items = xml.etree.ElementTree.fromstring(open(args.input, "r").read())
for item in items:
jsfiles.append({"js":base64.b64decode(item.find('response').text).decode('utf-8',"replace"), "url":item.find('url').text})
return jsfiles
# Method 4 - Folder with a wildcard
if "*" in input:
paths = glob.glob(os.path.abspath(input))
for index, path in enumerate(paths):
paths[index] = "file://%s" % path
return (paths if len(paths) > 0 else parser_error('Input with wildcard does \
not match any files.'))
# Method 5 - Local file
path = "file://%s" % os.path.abspath(input)
return [path if os.path.exists(input) else parser_error("file could not \
be found (maybe you forgot to add http/https).")]
def retryable_request(url):
try:
return send_request(url)
except urllib.error.URLError as err:
if str(err).startswith('<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED]'):
return send_request(url, False)
raise err
def send_request(url, verify_ssl = True):
'''
Send requests with Requests
'''
q = Request(url)
q.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
q.add_header('Accept', 'text/html,\
application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
q.add_header('Accept-Language', 'en-US,en;q=0.8')
q.add_header('Accept-Encoding', 'gzip')
q.add_header('Cookie', args.cookies)
if verify_ssl:
sslcontext = ssl.create_default_context()
else:
sslcontext = ssl._create_unverified_context()
response = urlopen(q, timeout=args.timeout, context=sslcontext)
if response.info().get('Content-Encoding') == 'gzip':
data = GzipFile(fileobj=readBytesCustom(response.read())).read()
elif response.info().get('Content-Encoding') == 'deflate':
data = response.read().read()
else:
data = response.read()
return data.decode('utf-8', 'replace')
def getContext(list_matches, content, include_delimiter=0, context_delimiter_str="\n"):
'''
Parse Input
list_matches: list of tuple (link, start_index, end_index)
content: content to search for the context
include_delimiter Set 1 to include delimiter in context
'''
items = []
for m in list_matches:
match_str = m[0]
match_start = m[1]
match_end = m[2]
context_start_index = match_start
context_end_index = match_end
delimiter_len = len(context_delimiter_str)
content_max_index = len(content) - 1
while content[context_start_index] != context_delimiter_str and context_start_index > 0:
context_start_index = context_start_index - 1
while content[context_end_index] != context_delimiter_str and context_end_index < content_max_index:
context_end_index = context_end_index + 1
if include_delimiter:
context = content[context_start_index: context_end_index]
else:
context = content[context_start_index + delimiter_len: context_end_index]
item = {
"link": match_str,
"context": context
}
items.append(item)
return items
def parser_file(content, regex_str, mode=1, more_regex=None, no_dup=1):
'''
Parse Input
content: string of content to be searched
regex_str: string of regex (The link should be in the group(1))
mode: mode of parsing. Set 1 to include surrounding contexts in the result
more_regex: string of regex to filter the result
no_dup: remove duplicated link (context is NOT counted)
Return the list of ["link": link, "context": context]
The context is optional if mode=1 is provided.
'''
global context_delimiter_str
if mode == 1:
# Beautify
if len(content) > 1000000:
content = content.replace(";",";\r\n").replace(",",",\r\n")
else:
content = jsbeautifier.beautify(content)
regex = re.compile(regex_str, re.VERBOSE)
if mode == 1:
all_matches = [(m.group(1), m.start(0), m.end(0)) for m in re.finditer(regex, content)]
items = getContext(all_matches, content, context_delimiter_str=context_delimiter_str)
else:
items = [{"link": m.group(1)} for m in re.finditer(regex, content)]
if no_dup:
# Remove duplication
all_links = set()
no_dup_items = []
for item in items:
if item["link"] not in all_links:
all_links.add(item["link"])
no_dup_items.append(item)
items = no_dup_items
# Match Regex
filtered_items = []
for item in items:
# Remove other capture groups from regex results
if more_regex:
if re.search(more_regex, item["link"]):
filtered_items.append(item)
else:
filtered_items.append(item)
return filtered_items
def cli_output(endpoints):
'''
Output to CLI
'''
for endpoint in endpoints:
print(html.escape(endpoint["link"]).encode(
'ascii', 'ignore').decode('utf8'))
def html_save(html):
'''
Save as HTML file and open in the browser
'''
hide = os.dup(1)
os.close(1)
os.open(os.devnull, os.O_RDWR)
try:
s = Template(open('%s/template.html' % sys.path[0], 'r').read())
text_file = open(args.output, "wb")
text_file.write(s.substitute(content=html).encode('utf8'))
text_file.close()
print("URL to access output: file://%s" % os.path.abspath(args.output))
file = "file:///%s" % os.path.abspath(args.output)
if sys.platform == 'linux' or sys.platform == 'linux2':
subprocess.call(["xdg-open", file])
else:
webbrowser.open(file)
except Exception as e:
print("Output can't be saved in %s \
due to exception: %s" % (args.output, e))
finally:
os.dup2(hide, 1)
def check_url(url):
nopelist = ["node_modules", "jquery.js"]
if url[-3:] == ".js":
words = url.split("/")
for word in words:
if word in nopelist:
return False
if url[:2] == "//":
url = "https:" + url
if url[:4] != "http":
if url[:1] == "/":
url = args.input + url
else:
url = args.input + "/" + url
return url
else:
return False
def to_full_urls(base, endpoints, keep_query=False):
base = to_full_url(base)
for e in endpoints:
relative = e['link']
relative_p = urlparse(relative)
query = ''
if keep_query and len(relative_p.query) > 0:
query = '?' + relative_p.query
if keep_query and len(relative_p.fragment) > 0:
query += '#' + relative_p.fragment
if len(relative_p.netloc) == 0:
path = relative_p.path.replace('\\', '/')
e['link'] = urljoin(base, path) + query
else:
# this also work if relative hostname != base hostname
e['link'] = to_full_url(relative) + query
return endpoints
def to_full_url(url):
url = urlparse(url)
if url.scheme == '':
print(f"Invalid URL, no scheme: {url}")
return url
if url.netloc == '':
print(f"Invalid URL, no netloc: {url}")
return url
if url.port is None:
port = 80 if url.scheme == 'http' else 443
netloc = f"{url.netloc}:{port}"
else:
netloc = url.netloc
full = f"{url.scheme}://{netloc}"
path = url.path.replace('\\', '/')
full = urljoin(full, path)
return full
def usage():
print(f"""
{sys.argv[0]} [<base URL>] [<relative URL>]
You can also pass relative URLs on STDID but then you can't fill relative URL argument
""")
sys.exit(1)
if __name__ == "__main__":
# Parse command line
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--domain",
help="Input a domain to recursively parse all javascript located in a page",
action="store_true")
parser.add_argument("-i", "--input",
help="Input a: URL, file or folder. \
For folders a wildcard can be used (e.g. '/*.js').",
required="True", action="store")
parser.add_argument("-o", "--output",
help="Where to save the file, \
including file name. Default: output.html",
action="store", default="output.html")
parser.add_argument("-r", "--regex",
help="RegEx for filtering purposes \
against found endpoint (e.g. ^/api/)",
action="store")
parser.add_argument("-b", "--burp",
help="",
action="store_true")
parser.add_argument("-c", "--cookies",
help="Add cookies for authenticated JS files",
action="store", default="")
parser.add_argument('-q', '--query',
action='store_true',
help='Determine whether to keep query & fragment of collected URLs')
default_timeout = 10
parser.add_argument("-t", "--timeout",
help="How many seconds to wait for the server to send data before giving up (default: " + str(default_timeout) + " seconds)",
default=default_timeout, type=int, metavar="<seconds>")
args = parser.parse_args()
if args.input[-1:] == "/":
args.input = args.input[:-1]
mode = 1
if args.output == "cli":
mode = 0
# Convert input to URLs or JS files
urls = parser_input(args.input)
# Convert URLs to JS
output = ''
for url in urls:
if not args.burp:
try:
file = retryable_request(url)
except Exception as e:
parser_error("invalid input defined or SSL error: %s" % e)
else:
file = url['js']
url = url['url']
endpoints = parser_file(file, regex_str, mode, args.regex)
endpoints = to_full_urls(url, endpoints, args.query)
if args.domain:
for endpoint in endpoints:
endpoint = html.escape(endpoint["link"]).encode('ascii', 'ignore').decode('utf8')
endpoint = check_url(endpoint)
if endpoint is False:
continue
print("Running against: " + endpoint)
print("")
try:
file = retryable_request(endpoint)
new_endpoints = parser_file(file, regex_str, mode, args.regex)
new_endpoints = to_full_urls(endpoint, new_endpoints, args.query)
if args.output == 'cli':
cli_output(new_endpoints)
else:
output += '''
<h1>File: <a href="%s" target="_blank" rel="nofollow noopener noreferrer">%s</a></h1>
''' % (html.escape(endpoint), html.escape(endpoint))
for endpoint2 in new_endpoints:
url = html.escape(endpoint2["link"])
header = "<div><a href='%s' class='text'>%s" % (
html.escape(url),
html.escape(url)
)
body = "</a><div class='container'>%s</div></div>" % html.escape(
endpoint2["context"]
)
body = body.replace(
html.escape(endpoint2["link"]),
"<span style='background-color:yellow'>%s</span>" %
html.escape(endpoint2["link"])
)
output += header + body
except Exception as e:
print("Invalid input defined or SSL error for: " + endpoint)
continue
if args.output == 'cli':
cli_output(endpoints)
else:
output += '''
<h1>File: <a href="%s" target="_blank" rel="nofollow noopener noreferrer">%s</a></h1>
''' % (html.escape(url), html.escape(url))
for endpoint in endpoints:
url = html.escape(endpoint["link"])
header = "<div><a href='%s' class='text'>%s" % (
html.escape(url),
html.escape(url)
)
body = "</a><div class='container'>%s</div></div>" % html.escape(
endpoint["context"]
)
body = body.replace(
html.escape(endpoint["link"]),
"<span style='background-color:yellow'>%s</span>" %
html.escape(endpoint["link"])
)
output += header + body
if args.output != 'cli':
html_save(output)