-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
326 lines (294 loc) · 12.6 KB
/
background.js
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
// this is the background code...
// listen for our browerAction to be clicked
// for the current tab, inject the "inject.js" file & execute it
var currentTab;
var version = "1.0";
chrome.tabs.query( //get current Tab
{
currentWindow: true,
active: true
},
function(tabArray) {
currentTab = tabArray[0];
chrome.tabs.executeScript(currentTab.ib, {
file: 'inject.js'
});
}
)
chrome.storage.sync.get(['ranOnce'], function(ranOnce) {
if (! ranOnce.ranOnce){
chrome.storage.sync.set({"ranOnce": true});
chrome.storage.sync.set({"originDenyList": ["https://www.google.com"]});
}
})
let specifics = {
"Slack Token": "(xox[pboa]-[0-9]{12}-[0-9]{12}-[0-9]{12}-[a-z0-9]{32})",
"RSA private key": "-----BEGIN RSA PRIVATE KEY-----",
"SSH (DSA) private key": "-----BEGIN DSA PRIVATE KEY-----",
"SSH (EC) private key": "-----BEGIN EC PRIVATE KEY-----",
"PGP private key block": "-----BEGIN PGP PRIVATE KEY BLOCK-----",
"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 Access Token": "EAACEdEose0cBA[0-9A-Za-z]+",
"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",
"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 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}['|\"]"
}
let generics = {
"Generic API Key": "[aA][pP][iI]_?[kK][eE][yY].{0,20}['|\"][0-9a-zA-Z]{32,45}['|\"]",
"Generic Secret": "[sS][eE][cC][rR][eE][tT].{0,20}['|\"][0-9a-zA-Z]{32,45}['|\"]",
}
let aws = {
"AWS API Key": "((?:A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16})",
}
let denyList = ["AIDAAAAAAAAAAAAAAAAA"]
a = ""
b = ""
var checkData = function(data, src, regexes, fromEncoded=false, parentUrl=undefined, parentOrigin=undefined){
var findings = [];
for (let key in regexes){
let re = new RegExp(regexes[key])
let match = re.exec(data);
if (Array.isArray(match)){match = match.toString()}
if (denyList.includes(match)){
continue;
}
if (match){
let finding = {};
finding = {src: src, match:match, key:key, encoded:fromEncoded, parentUrl:parentUrl};
a = data;
b = re;
findings.push(finding);
}
}
if (findings){
chrome.storage.sync.get(["leakedKeys"], function(result) {
if (Array.isArray(result.leakedKeys) || ! result.leakedKeys){
var keys = {};
}else{
var keys = result.leakedKeys;
};
for (let finding of findings){
if(Array.isArray(keys[parentOrigin])){
var newFinding = true;
for (key of keys[parentOrigin]){
if (key["src"] == finding["src"] && key["match"] == finding["match"] && key["key"] == finding["key"] && key["encoded"] == finding["encoded"] && key["parentUrl"] == finding["parentUrl"]){
newFinding = false;
break;
}
}
if(newFinding){
keys[parentOrigin].push(finding)
chrome.storage.sync.set({"leakedKeys": keys}, function(){
updateTabAndAlert(finding);
});
}
}else{
keys[parentOrigin] = [finding];
chrome.storage.sync.set({"leakedKeys": keys}, function(){
updateTabAndAlert(finding);
})
}
}
})
}
let decodedStrings = getDecodedb64(data);
for (encoded of decodedStrings){
checkData(encoded[1], src, regexes, encoded[0], parentUrl, parentOrigin);
}
}
var updateTabAndAlert = function(finding){
var key = finding["key"];
var src = finding["src"];
var match = finding["match"];
var fromEncoded = finding["encoded"];
chrome.storage.sync.get(["alerts"], function(result) {
console.log(result.alerts)
if (result.alerts == undefined || result.alerts){
if (fromEncoded){
alert(key + ": " + match + " found in " + src + " decoded from " + fromEncoded.substring(0,9) + "...");
}else{
alert(key + ": " + match + " found in " + src);
}
}
})
updateTab();
}
var updateTab = function(){
chrome.tabs.getSelected(null, function(tab) {
var tabId = tab.id;
var tabUrl = tab.url;
var origin = (new URL(tabUrl)).origin
chrome.storage.sync.get(["leakedKeys"], function(result) {
if (Array.isArray(result.leakedKeys[origin])){
var originKeys = result.leakedKeys[origin].length.toString();
}else{
var originKeys = "";
}
chrome.browserAction.setBadgeText({text: originKeys});
chrome.browserAction.setBadgeBackgroundColor({color: '#ff0000'});
})
});
}
chrome.tabs.onActivated.addListener(function(activeInfo) {
updateTab();
});
var getStringsOfSet = function(word, char_set, threshold=20){
let count = 0;
let letters = "";
let strings = [];
if (! word){
return []
}
for(let char of word){
if (char_set.indexOf(char) > -1){
letters += char;
count += 1;
} else{
if ( count > threshold ){
strings.push(letters);
}
letters = "";
count = 0;
}
}
if(count > threshold){
strings.push(letters);
}
return strings
}
var getDecodedb64 = function(inputString){
let b64CharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
let encodeds = getStringsOfSet(inputString, b64CharSet);
let decodeds = [];
for (encoded of encodeds){
try {
let decoded = [encoded, atob(encoded)];
decodeds.push(decoded);
} catch(e) {
}
}
return decodeds;
}
var checkIfOriginDenied = function(check_url, cb){
let skip = false;
chrome.storage.sync.get(["originDenyList"], function(result) {
let originDenyList = result.originDenyList;
for (origin of originDenyList){
if(check_url.startsWith(origin)){
skip = true;
}
}
cb(skip);
})
}
var checkForGitDir = function(data, url){
if(data.startsWith("[core]")){
alert(".git dir found in " + url + " feature to check this for secrets not supported");
}
}
var js_url;
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
chrome.storage.sync.get(['generics'], function(useGenerics) {
chrome.storage.sync.get(['specifics'], function(useSpecifics) {
chrome.storage.sync.get(['aws'], function(useAws) {
chrome.storage.sync.get(['checkEnv'], function(checkEnv) {
chrome.storage.sync.get(['checkGit'], function(checkGit) {
let regexes = {};
if(useGenerics["generics"] || useGenerics["generics"] == undefined){
regexes = {
...regexes,
...generics
}
}
if(useSpecifics["specifics"] || useSpecifics["specifics"] == undefined){
regexes = {
...regexes,
...specifics
}
}
if(useAws["aws"] || useAws["aws"] == undefined){
regexes = {
...regexes,
...aws
}
}
if (request.scriptUrl) {
let js_url = request.scriptUrl;
let parentUrl = request.parentUrl;
let parentOrigin = request.parentOrigin;
checkIfOriginDenied(js_url, function(skip){
if (!skip){
fetch(js_url, {"credentials": 'include'})
.then(response => response.text())
.then(data => checkData(data, js_url, regexes, undefined, parentUrl, parentOrigin));
}
})
}else if(request.pageBody){
checkIfOriginDenied(request.origin, function(skip){
if (!skip){
checkData(request.pageBody, request.origin, regexes, undefined, request.parentUrl, request.parentOrigin);
}
})
}else if(request.envFile){
if(checkEnv['checkEnv']){
fetch(request.envFile, {"credentials": 'include'})
.then(response => response.text())
.then(data => checkData(data, ".env file at " + request.envFile, regexes, undefined, request.parentUrl, request.parentOrigin));
}
}else if(request.openTabs){
for (tab of request.openTabs){
window.open(tab);
console.log(tab)
}
}else if(request.gitDir){
if(checkGit['checkGit']){
fetch(request.gitDir, {"credentials": 'include'})
.then(response => response.text())
.then(data => checkForGitDir(data, request.gitDir));
}
}
});
});
});
});
});
});