Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
350 lines (321 loc) · 10.8 KB
/
Copy pathbackground.js
File metadata and controls
350 lines (321 loc) · 10.8 KB
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
let currentTab;
let version = "1.0";
chrome.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
if (tabs && tabs.length > 0) {
currentTab = tabs[0];
if (currentTab.id) {
chrome.scripting.executeScript({
target: { tabId: currentTab.id },
files: ["inject.js"]
});
}
}
});
chrome.storage.sync.get(["ranOnce"], (ranOnce) => {
if (!ranOnce.ranOnce) {
chrome.storage.sync.set({ ranOnce: true });
chrome.storage.sync.set({ originDenyList: ["https://www.google.com"] });
}
});
// =============================
// 1) REGEX PATTERNS & VARIABLES
// =============================
let specifics = {
// ... (keep all your existing regex patterns unchanged) ...
};
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"];
function checkData(data, src, regexes, fromEncoded = false, parentUrl = undefined, parentOrigin = undefined) {
let 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 = { src, match, key, encoded: fromEncoded, parentUrl };
findings.push(finding);
}
}
if (findings.length > 0 && parentOrigin) {
chrome.storage.sync.get(["leakedKeys"], function (result) {
chrome.storage.sync.get(["uniqueByHostname"], function (uniqueByHostname) {
let keys = {};
if (!Array.isArray(result.leakedKeys) && result.leakedKeys) {
keys = result.leakedKeys;
}
if (!keys[parentOrigin]) {
keys[parentOrigin] = [];
}
for (let finding of findings) {
let newFinding = true;
if (uniqueByHostname && uniqueByHostname["uniqueByHostname"]) {
for (let oldKey of keys[parentOrigin]) {
const oldHost = extractHostname(oldKey["src"]);
const newHost = extractHostname(finding["src"]);
if (
oldHost &&
newHost &&
oldHost === newHost &&
oldKey["match"] === finding["match"] &&
oldKey["key"] === finding["key"] &&
oldKey["encoded"] === finding["encoded"]
) {
newFinding = false;
break;
}
}
} else {
for (let oldKey of keys[parentOrigin]) {
if (
oldKey["src"] === finding["src"] &&
oldKey["match"] === finding["match"] &&
oldKey["key"] === finding["key"] &&
oldKey["encoded"] === finding["encoded"] &&
oldKey["parentUrl"] === finding["parentUrl"]
) {
newFinding = false;
break;
}
}
}
if (newFinding) {
keys[parentOrigin].push(finding);
chrome.storage.sync.set({ leakedKeys: keys }, function () {
updateTabAndAlert(finding);
});
}
}
});
});
}
let decodedStrings = getDecodedb64(data);
for (let encoded of decodedStrings) {
checkData(encoded[1], src, regexes, encoded[0], parentUrl, parentOrigin);
}
}
function updateTabAndAlert(finding) {
let key = finding["key"];
let src = finding["src"];
let match = finding["match"];
let fromEncoded = finding["encoded"];
chrome.storage.sync.get(["alerts"], function (result) {
chrome.storage.sync.get(["notifications"], function (notifications) {
let alertText;
let notifyText;
if (fromEncoded) {
alertText = key + ": " + match + " found in " + src + " decoded from " + fromEncoded.substring(0, 9) + "...";
notifyText = `${match.substring(0, 30)}... (orig was encoded) found in ${src}`;
} else {
alertText = key + ": " + match + " found in " + src;
notifyText = `${match.substring(0, 30)}... found in ${src}`;
}
if (result.alerts === undefined || result.alerts) {
chrome.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
if (tabs && tabs.length > 0) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
func: (msg) => alert(msg),
args: [alertText]
});
}
});
}
if (notifications && notifications["notifications"]) {
chrome.notifications.create(src + new Date(), {
type: "basic",
iconUrl: "icon.jpg",
title: `ShadowHound | ${key}`,
message: notifyText,
priority: 2
});
}
});
});
updateTab();
}
function updateTab() {
chrome.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
if (!tabs || !tabs.length) return;
let tabId = tabs[0].id;
let tabUrl = tabs[0].url;
let origin;
try {
origin = new URL(tabUrl).origin;
} catch (e) {
// Invalid URL, clear badge and exit
chrome.action.setBadgeText({ text: "", tabId: tabId });
return;
}
chrome.storage.sync.get(["leakedKeys"], function (result) {
if (result.leakedKeys && Array.isArray(result.leakedKeys[origin])) {
let originKeys = result.leakedKeys[origin].length.toString();
chrome.action.setBadgeText({ text: originKeys, tabId: tabId });
chrome.action.setBadgeBackgroundColor({ color: "#ff0000", tabId: tabId });
} else {
chrome.action.setBadgeText({ text: "", tabId: tabId });
}
});
});
}
chrome.tabs.onActivated.addListener(function (activeInfo) {
updateTab();
});
function getStringsOfSet(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;
}
function getDecodedb64(inputString) {
let b64CharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
let encodeds = getStringsOfSet(inputString, b64CharSet);
let decodeds = [];
for (let encoded of encodeds) {
try {
let decoded = [encoded, atob(encoded)];
decodeds.push(decoded);
} catch (e) {
// Ignore decoding errors
}
}
return decodeds;
}
function extractHostname(url) {
try {
return new URL(url).hostname;
} catch (e) {
return null;
}
}
function checkIfOriginDenied(check_url, cb) {
chrome.storage.sync.get(["originDenyList"], function (result) {
let skip = false;
if (!result.originDenyList) {
cb(skip);
return;
}
let originDenyList = result.originDenyList.filter((u) => u.length > 1);
for (let origin of originDenyList) {
try {
if (check_url.startsWith(origin)) {
skip = true;
break;
}
} catch (e) {
// Invalid URL comparison, skip
}
}
cb(skip);
});
}
function checkForGitDir(data, url) {
if (data.startsWith("[core]")) {
chrome.tabs.query({ currentWindow: true, active: true }).then((tabs) => {
if (tabs && tabs.length > 0) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
func: (msg) => alert(msg),
args: [`.git dir found in ${url}`]
});
}
});
}
}
chrome.runtime.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, false, parentUrl, parentOrigin))
.catch((err) => console.error("Fetch error:", err));
}
});
} else if (request.pageBody) {
checkIfOriginDenied(request.origin, function (skip) {
if (!skip) {
checkData(request.pageBody, request.origin, regexes, false, request.parentUrl, request.parentOrigin);
}
});
} else if (request.envFile) {
if (checkEnv["checkEnv"]) {
checkIfOriginDenied(request.envFile, function (skip) {
if (!skip) {
fetch(request.envFile, { credentials: "include" })
.then((response) => response.text())
.then((data) =>
checkData(data, ".env file at " + request.envFile, regexes, false, request.parentUrl, request.parentOrigin)
)
.catch((err) => console.error("Fetch error:", err));
}
});
}
} else if (request.openTabs) {
for (let tab of request.openTabs) {
chrome.tabs.create({ url: tab });
}
} else if (request.gitDir) {
if (checkGit["checkGit"]) {
checkIfOriginDenied(request.gitDir, function (skip) {
if (!skip) {
fetch(request.gitDir, { credentials: "include" })
.then((response) => response.text())
.then((data) => checkForGitDir(data, request.gitDir))
.catch((err) => console.error("Fetch error:", err));
}
});
}
}
});
});
});
});
});
});