forked from m4ll0k/SecretFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecretFinder.py
511 lines (477 loc) · 20.1 KB
/
SecretFinder.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/usr/bin/env python
# SecretFinder - Tool for discover apikeys/accesstokens and sensitive data in js file
# based to LinkFinder - github.com/GerbenJavado
# By m4ll0k (@m4ll0k2) github.com/m4ll0k
import os,sys
if not sys.version_info.major >= 3:
print("[ + ] Run this tool with python version 3.+")
sys.exit(0)
os.environ["BROWSER"] = "open"
import re
import glob
import argparse
import jsbeautifier
import webbrowser
import subprocess
import base64
import requests
import string
import random
from html import escape
import urllib3
import xml.etree.ElementTree
# disable warning
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# for read local file with file:// protocol
from requests_file import FileAdapter
from lxml import html
from urllib.parse import urlparse
# regex
_regex = {
'google_api' : r'AIza[0-9A-Za-z-_]{35}',
'firebase' : r'AAAA[A-Za-z0-9_-]{7}:[A-Za-z0-9_-]{140}',
'google_captcha' : r'6L[0-9A-Za-z-_]{38}|^6[0-9a-zA-Z_-]{39}$',
'google_oauth' : r'ya29\.[0-9A-Za-z\-_]+',
'amazon_aws_access_key_id' : r'A[SK]IA[0-9A-Z]{16}',
'amazon_mws_auth_toke' : r'amzn\\.mws\\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
'amazon_aws_url' : r's3\.amazonaws.com[/]+|[a-zA-Z0-9_-]*\.s3\.amazonaws.com',
'amazon_aws_url2' : r"(" \
r"[a-zA-Z0-9-\.\_]+\.s3\.amazonaws\.com" \
r"|s3://[a-zA-Z0-9-\.\_]+" \
r"|s3-[a-zA-Z0-9-\.\_\/]+" \
r"|s3.amazonaws.com/[a-zA-Z0-9-\.\_]+" \
r"|s3.console.aws.amazon.com/s3/buckets/[a-zA-Z0-9-\.\_]+)",
'facebook_access_token' : r'EAACEdEose0cBA[0-9A-Za-z]+',
'authorization_basic' : r'basic [a-zA-Z0-9=:_\+\/-]{5,100}',
'authorization_bearer' : r'bearer [a-zA-Z0-9_\-\.=:_\+\/]{5,100}',
'authorization_api' : r'api[key|_key|\s+]+[a-zA-Z0-9_\-]{5,100}',
'mailgun_api_key' : r'key-[0-9a-zA-Z]{32}',
'twilio_api_key' : r'SK[0-9a-fA-F]{32}',
'twilio_account_sid' : r'AC[a-zA-Z0-9_\-]{32}',
'twilio_app_sid' : r'AP[a-zA-Z0-9_\-]{32}',
'paypal_braintree_access_token' : r'access_token\$production\$[0-9a-z]{16}\$[0-9a-f]{32}',
'square_oauth_secret' : r'sq0csp-[ 0-9A-Za-z\-_]{43}|sq0[a-z]{3}-[0-9A-Za-z\-_]{22,43}',
'square_access_token' : r'sqOatp-[0-9A-Za-z\-_]{22}|EAAA[a-zA-Z0-9]{60}',
'stripe_standard_api' : r'sk_live_[0-9a-zA-Z]{24}',
'stripe_restricted_api' : r'rk_live_[0-9a-zA-Z]{24}',
'github_access_token' : r'[a-zA-Z0-9_-]*:[a-zA-Z0-9_\-]+@github\.com*',
'rsa_private_key' : r'-----BEGIN RSA PRIVATE KEY-----',
'ssh_dsa_private_key' : r'-----BEGIN DSA PRIVATE KEY-----',
'ssh_dc_private_key' : r'-----BEGIN EC PRIVATE KEY-----',
'pgp_private_block' : r'-----BEGIN PGP PRIVATE KEY BLOCK-----',
'json_web_token' : r'ey[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$',
'slack_token' : r"\"api_token\":\"(xox[a-zA-Z]-[a-zA-Z0-9-]+)\"",
'SSH_privKey' : r"([-]+BEGIN [^\s]+ PRIVATE KEY[-]+[\s]*[^-]*[-]+END [^\s]+ PRIVATE KEY[-]+)",
'Heroku API KEY' : r'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}',
'possible_Creds' : r"(?i)(" \
r"password\s*[`=:\"]+\s*[^\s]+|" \
r"password is\s*[`=:\"]*\s*[^\s]+|" \
r"pwd\s*[`=:\"]*\s*[^\s]+|" \
r"passwd\s*[`=:\"]+\s*[^\s]+)",
# Paul's edits from here
"AWS_secret_key" : r"(?<![A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=])",
"Heroku API Key" : "[hH][eE][rR][oO][kK][uU].{0,20}[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}",
"Json Web Token" : "eyJhbGciOiJ",
"LinkedIn Secret" : "[lL][iI][nN][kK][eE][dD][iI][nN].{0,14}['|\"][0-9a-f]{16}['|\"]",
"MailChimp API Key" : "[0-9a-f]{32}-us[0-9]{1,2}",
"Mailgun API Key" : "key-[0-9a-zA-Z]{32}",
"MongoDB Server" : "mongodb:\/\/",
"MongoDB Server" : "mongodb+srv:\/\/",
"Oracle Build" : "Oracle-Build",
"Oracle Build" : "Oracle-Build",
"Oracle Java" : "java - 1.[0-7].[0-9]_[1-9][0-9]",
"Password in URL" : "[a-zA-Z]{3,10}://[^/\\s:@]{3,20}:[^/\\s:@]{3,20}@.{1,100}[\"'\\s]",
"PayPal Braintree Access Token" : "access_token\\$production\\$[0-9a-z]{16}\\$[0-9a-f]{32}",
"Picatic API Key" : "sk_live_[0-9a-z]{32}",
"Slack Webhook" : "https://hooks\\.slack\\.com/services/T[a-zA-Z0-9_]{8}/B[a-zA-Z0-9_]{8}/[a-zA-Z0-9_]{24}",
"Stripe Secret API Key" : "sk_live_[0-9a-zA-Z]{24}",
"Stripe Secret API Key" : "sk_test_[0-9a-zA-Z]{24}",
"Stripe Public API Key" : "pk_live_[0-9a-zA-Z]{24}",
"Stripe Public API Key" : "pk_test_[0-9a-zA-Z]{24}",
"Stripe Restricted API Key" : "rk_live_[0-9a-zA-Z]{24}",
"Square Access Token" : "sq0atp-[0-9A-Za-z\\-_]{22}",
"Square OAuth Secret" : "sq0csp-[0-9A-Za-z\\-_]{43}",
"Telegram Bot API Key" : "[0-9]+:AA[0-9A-Za-z\\-_]{33}",
"Twilio API Key" : "SK[0 -9a-fA-F]{32}",
"Github Auth Creds" : "https:\/\/[a-zA-Z0-9]{40}@github\.com",
# "Twitter Access Token": "[tT][wW][iI][tT][tT][eE][rR].*[1-9][0-9]+-[0-9a-zA-Z]{40}",
# "Twitter OAuth": "[tT][wW][iI][tT][tT][eE][rR].*['|\"][0-9a-zA-Z]{35,44}['|\"]",
"Slack Token" : "(xox[pboa]-[0-9]{12}-[0-9]{12}-[0-9]{12}-[a-z0-9]{32})",
"Amazon MWS Auth Token" : "amzn\\.mws\\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"Amplitude Secret" : "amplitudeSecretKey:['|\"][0-9a-zA-Z]{32}['|\"]",
"AWS AppSync GraphQL Key" : "da2-[a-z0-9]{26}",
"Facebook OAuth" : "[fF][aA][cC][eE][bB][oO][oO][kK].{0,20}['|\"][0-9a-f]{32}['|\"]",
"Facebook OAuth" : "clientSecret:['|\"][0-9a-zA-Z]{32}['|\"]",
"GitHub Secret" : "clientSecret:['|\"][0-9a-zA-Z]{35,40}['|\"]",
"GitHub" : "[gG][iI][tT][hH][uU][bB].{0,20}['|\"][0-9a-zA-Z]{35,40}['|\"]",
# "Google API Key": "AIza[0-9A-Za-z\\-_]{35}",
# "Google Cloud Platform API Key": "AIza[0-9A-Za-z\\-_]{35}",
# "Google Cloud Platform OAuth": "[0-9]+-[0-9A-Za-z_]{32}\\.apps\\.googleusercontent\\.com",
# "Google Drive API Key": "AIza[0-9A-Za-z\\-_]{35}",
# "Google Drive OAuth": "[0-9]+-[0-9A-Za-z_]{32}\\.apps\\.googleusercontent\\.com",
"Google (GCP) Service-account" : "\"type\": \"service_account\"",
# "Google Gmail API Key": "AIza[0-9A-Za-z\\-_]{35}",
# "Google Gmail OAuth": "[0-9]+-[0-9A-Za-z_]{32}\\.apps\\.googleusercontent\\.com",
# "Google OAuth Access Token": "ya29\\.[0-9A-Za-z\\-_]+",
# "Google YouTube API Key": "AIza[0-9A-Za-z\\-_]{35}",
# "Google YouTube OAuth": "[0-9]+-[0-9A-Za-z_]{32}\\.apps\\.googleusercontent\\.com"
}
_template = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
h1 {
font-family: sans-serif;
}
a {
color: #000;
}
.text {
font-size: 16px;
font-family: Helvetica, sans-serif;
color: #323232;
background-color: white;
}
.container {
background-color: #e9e9e9;
padding: 10px;
margin: 10px 0;
font-family: helvetica;
font-size: 13px;
border-width: 1px;
border-style: solid;
border-color: #8a8a8a;
color: #323232;
margin-bottom: 15px;
}
.button {
padding: 17px 60px;
margin: 10px 10px 10px 0;
display: inline-block;
background-color: #f4f4f4;
border-radius: .25rem;
text-decoration: none;
-webkit-transition: .15s ease-in-out;
transition: .15s ease-in-out;
color: #333;
position: relative;
}
.button:hover {
background-color: #eee;
text-decoration: none;
}
.github-icon {
line-height: 0;
position: absolute;
top: 14px;
left: 24px;
opacity: 0.7;
}
</style>
<title>LinkFinder Output</title>
</head>
<body contenteditable="true">
$$content$$
<a class='button' contenteditable='false' href='https://github.com/m4ll0k/SecretFinder/issues/new' rel='nofollow noopener noreferrer' target='_blank'><span class='github-icon'><svg height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22" fill="none" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"></path></svg></span> Report an issue.</a>
</body>
</html>
'''
def parser_error(msg):
print('Usage: python %s [OPTIONS] use -h for help'%sys.argv[0])
print('Error: %s'%msg)
sys.exit(0)
def getContext(matches,content,name,rex='.+?'):
''' get context '''
items = []
matches2 = []
for i in [x[0] for x in matches]:
if i not in matches2:
matches2.append(i)
for m in matches2:
context = re.findall('%s%s%s'%(rex,m,rex),content,re.IGNORECASE)
item = {
'matched' : m,
'name' : name,
'context' : context,
'multi_context' : True if len(context) > 1 else False
}
items.append(item)
return items
def parser_file(content,mode=1,more_regex=None,no_dup=1):
''' parser file '''
if mode == 1:
if len(content) > 1000000:
content = content.replace(";",";\r\n").replace(",",",\r\n")
else:
content = jsbeautifier.beautify(content)
all_items = []
for regex in _regex.items():
r = re.compile(regex[1],re.VERBOSE|re.I)
if mode == 1:
all_matches = [(m.group(0),m.start(0),m.end(0)) for m in re.finditer(r,content)]
items = getContext(all_matches,content,regex[0])
if items != []:
all_items.append(items)
else:
items = [{
'matched' : m.group(0),
'context' : [],
'name' : regex[0],
'multi_context' : False
} for m in re.finditer(r,content)]
if items != []:
all_items.append(items)
if all_items != []:
k = []
for i in range(len(all_items)):
for ii in all_items[i]:
if ii not in k:
k.append(ii)
if k != []:
all_items = k
if no_dup:
all_matched = set()
no_dup_items = []
for item in all_items:
if item != [] and type(item) is dict:
if item['matched'] not in all_matched:
all_matched.add(item['matched'])
no_dup_items.append(item)
all_items = no_dup_items
filtered_items = []
if all_items != []:
for item in all_items:
if more_regex:
if re.search(more_regex,item['matched']):
filtered_items.append(item)
else:
filtered_items.append(item)
return filtered_items
def parser_input(input):
''' Parser Input '''
# method 1 - url
schemes = ('http://','https://','ftp://','file://','ftps://')
if input.startswith(schemes):
return [input]
# method 2 - url inpector firefox/chrome
if input.startswith('view-source:'):
return [input[12:]]
# method 3 - Burp file
if args.burp:
jsfiles = []
items = []
try:
items = xml.etree.ElementTree.fromstring(open(args.input,'r').read())
except Exception as err:
print(err)
sys.exit()
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 html_save(output):
''' html output '''
hide = os.dup(1)
os.close(1)
os.open(os.devnull,os.O_RDWR)
try:
text_file = open(args.output,"wb")
text_file.write(_template.replace('$$content$$',output).encode('utf-8'))
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 err:
print('Output can\'t be saved in %s due to exception: %s'%(args.output,err))
finally:
os.dup2(hide,1)
def cli_output(matched):
''' cli output '''
for match in matched:
print(match.get('name')+'\t->\t'+match.get('matched').encode('ascii','ignore').decode('utf-8'))
def urlParser(url):
''' urlParser '''
parse = urlparse(url)
urlParser.this_root = parse.scheme + '://' + parse.netloc
urlParser.this_path = parse.scheme + '://' + parse.netloc + '/' + parse.path
def extractjsurl(content,base_url):
''' JS url extract from html page '''
soup = html.fromstring(content)
all_src = []
urlParser(base_url)
for src in soup.xpath('//script'):
src = src.xpath('@src')[0] if src.xpath('@src') != [] else []
if src != []:
if src.startswith(('http://','https://','ftp://','ftps://')):
if src not in all_src:
all_src.append(src)
elif src.startswith('//'):
src = 'http://'+src[2:]
if src not in all_src:
all_src.append(src)
elif src.startswith('/'):
src = urlParser.this_root + src
if src not in all_src:
all_src.append(src)
else:
src = urlParser.this_path + src
if src not in all_src:
all_src.append(src)
if args.ignore and all_src != []:
temp = all_src
ignore = []
for i in args.ignore.split(';'):
for src in all_src:
if i in src:
ignore.append(src)
if ignore:
for i in ignore:
temp.pop(int(temp.index(i)))
return temp
if args.only:
temp = all_src
only = []
for i in args.only.split(';'):
for src in all_src:
if i in src:
only.append(src)
return only
return all_src
def send_request(url):
''' Send Request '''
# read local file
# https://github.com/dashea/requests-file
if 'file://' in url:
s = requests.Session()
s.mount('file://',FileAdapter())
return s.get(url).content.decode('utf-8','replace')
# set headers and cookies
headers = {}
default_headers = {
'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',
'Accept' : 'text/html, application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language' : 'en-US,en;q=0.8',
'Accept-Encoding' : 'gzip'
}
if args.headers:
for i in args.header.split('\\n'):
# replace space and split
name,value = i.replace(' ','').split(':')
headers[name] = value
# add cookies
if args.cookie:
headers['Cookie'] = args.cookie
headers.update(default_headers)
# proxy
proxies = {}
if args.proxy:
proxies.update({
'http' : args.proxy,
'https' : args.proxy,
# ftp
})
try:
resp = requests.get(
url = url,
verify = False,
headers = headers,
proxies = proxies
)
return resp.content.decode('utf-8','replace')
except Exception as err:
print(err)
sys.exit(0)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-e","--extract",help="Extract all javascript links located in a page and process it",action="store_true",default=False)
parser.add_argument("-i","--input",help="Input a: URL, file or folder",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="Support burp exported file",action="store_true")
parser.add_argument("-c","--cookie",help="Add cookies for authenticated JS files",action="store",default="")
parser.add_argument("-g","--ignore",help="Ignore js url, if it contain the provided string (string;string2..)",action="store",default="")
parser.add_argument("-n","--only",help="Process js url, if it contain the provided string (string;string2..)",action="store",default="")
parser.add_argument("-H","--headers",help="Set headers (\"Name:Value\\nName:Value\")",action="store",default="")
parser.add_argument("-p","--proxy",help="Set proxy (host:port)",action="store",default="")
args = parser.parse_args()
if args.input[-1:] == "/":
# /aa/ -> /aa
args.input = args.input[:-1]
mode = 1
if args.output == "cli":
mode = 0
# add args
if args.regex:
# validate regular exp
try:
r = re.search(args.regex,''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(random.randint(10,50))))
except Exception as e:
print('your python regex isn\'t valid')
sys.exit()
_regex.update({
'custom_regex' : args.regex
})
if args.extract:
content = send_request(args.input)
urls = extractjsurl(content,args.input)
else:
# convert input to URLs or JS files
urls = parser_input(args.input)
# conver URLs to js file
output = ''
for url in urls:
print('[ + ] URL: '+url)
if not args.burp:
file = send_request(url)
else:
file = url.get('js')
url = url.get('url')
matched = parser_file(file,mode)
if args.output == 'cli':
cli_output(matched)
else:
output += '<h1>File: <a href="%s" target="_blank" rel="nofollow noopener noreferrer">%s</a></h1>'%(escape(url),escape(url))
for match in matched:
_matched = match.get('matched')
_named = match.get('name')
header = '<div class="text">%s'%(_named.replace('_',' '))
body = ''
# find same thing in multiple context
if match.get('multi_context'):
# remove duplicate
no_dup = []
for context in match.get('context'):
if context not in no_dup:
body += '</a><div class="container">%s</div></div>'%(context)
body = body.replace(
context,'<span style="background-color:yellow">%s</span>'%context)
no_dup.append(context)
# --
else:
body += '</a><div class="container">%s</div></div>'%(match.get('context')[0] if len(match.get('context'))>1 else match.get('context'))
body = body.replace(
match.get('context')[0] if len(match.get('context')) > 0 else ''.join(match.get('context')),
'<span style="background-color:yellow">%s</span>'%(match.get('context') if len(match.get('context'))>1 else match.get('context'))
)
output += header + body
if args.output != 'cli':
html_save(output)