-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcontent.js
644 lines (550 loc) · 23.3 KB
/
content.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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//
// taintString - the unique string to taint data with
// filterString - additional characters to detect filtering/encoding
//
const taintString = 't4inT3d';
const filterString = '"\\\'%3ch1>lol';
// Fetch our options, so they are ready when page is loaded
if (top == window) {
chrome.storage.local.get(['autoTaint'], function(result) {
var autoTaintEnabled = result['autoTaint'];
var script = document.createElement('script');
script.textContent = `window.autoTaintEnabled = ${autoTaintEnabled}`;
(document.head||document.documentElement).appendChild(script);
script.remove();
});
}
//
// Code to inject into the page before load, must be inline.
// Any delay in fetching it and we will inject too late!
//
var injectedCode = `
function addTaintHooks( thisWindow ) {
// Don't process already instrumented windows
if (!thisWindow || thisWindow.orgEval) {
return;
}
const taintString = '${taintString}';
const filterString = '${filterString}';
const taintRegex = new RegExp(taintString);
//
// Some helper functions
//
// report a warning to the content script
thisWindow.addWarning = function (warningLabel, warningText) {
// try to weed out false positives
var match, regEx = /${taintString}/ig;
var matches = 0, falseMatches = 0;
while (match = regEx.exec(warningText)) {
matches++;
// look at string just before and after the match
var pre = match.input.slice(match.index-Math.min(match.index,'href = "https://'.length), match.index);
var post = match.input.slice(match.index+match[0].length);
post = post.slice(0, post.search(/[^a-z.0-9-]/i)+filterString.length);
//console.log(pre + " : " + match[0] + " : " + post);
// if filterString appears this is a good match
if (post.includes(filterString)) continue;
// if we find "special" characters that are not encoded, this is a good match
if (post.match(/["<>]/)) continue;
// make sure that single quotes are actually in use before accepting them
if (post.includes("'") && warningText.match(/[:=] *\'/)) continue;
// report all tainted src and href attributes
if (pre.match(/src\s?=/) || pre.match(/href\s?=/)) continue;
// if this is a DOM match, it's probably a false positive?
if (warningLabel.includes('DOM'))
falseMatches++;
}
if (matches && matches == falseMatches) {
console.warn("FP? " + warningLabel + ": " + warningText);
return;
}
// print the warning to the console
console.warn("%c" + warningLabel + "%c: " + warningText, "color: Red", "color: Black");
// tell content script that we found something
thisWindow.parent.dispatchEvent(new CustomEvent('addWarning', { detail: thisWindow.location.origin + " " + warningLabel + ": " + warningText }));
};
// concatenate arguments to form a string
function argsToString(args) {
var argString = "";
for (arg of args) { argString += (argString.length ? ", " : "") + arg; }
return argString;
}
// create a tainted version of a URL
var discoveredKeywords = [];
thisWindow.myDecodeURIComponent = thisWindow.decodeURIComponent;
function addTaintToUrl(u) {
var url = new URL(u);
var taintedUrl = new URL(url.origin);
// taint the pathname
if (thisWindow.location.href != "about:blank") {
taintedUrl.pathname = "//" + taintString + ".path";
}
taintedUrl.pathname += url.pathname;
// decline navigation from /abc#123 to /123
if (thisWindow.location.hash.length > 2) {
if (url.pathname.slice(1).toLowerCase() == thisWindow.location.hash.slice(1).toLowerCase())
{
console.log("Not changing URL " + thisWindow.location.href + " to " + url);
return thisWindow.location.href;
}
}
// add the original parameters, if any
var search = "";
try {
search = thisWindow.myDecodeURIComponent(url.search);
} catch (e) {
console.log("Couldn't decode search string '" + url.search + "': " + e);
search = url.search;
}
// if no tainted parameters, add one
if (!taintRegex.test(search)) {
if (search.length != 0)
search += "&";
search += taintString + ".param=" + taintString + ".value" + filterString;
}
for (var word of discoveredKeywords) {
search += "&" + word + "=" + taintString + ".disco" + filterString;
}
taintedUrl.search = search + "&a=?&b";
// if old hash values exist, extract keywords
var hash = "";
for (var h of thisWindow.myDecodeURIComponent(url.hash).split(/[#&]/)) {
var p = h.split('=');
if (p.length > 1) {
if (!discoveredKeywords.includes(p[0]))
discoveredKeywords.push(p[0]);
} else if (h.length != 0) {
if (hash.length != 0)
hash += "#";
hash += h;
}
}
// add any discovered keywords to hash
for (var word of discoveredKeywords) {
if (hash.length != 0)
hash += "#";
hash += word + "=" + taintString + ".hash." + word + filterString;
}
// if nothing tainted in hash, add something
if (!taintRegex.test(hash)) {
hash += "#!//" + taintString + ".hash" + filterString;
}
taintedUrl.hash = hash;
//console.log("Original href: " + location.href);
//console.warn("Tainted href: " + taintedUrl.toString());
return taintedUrl.toString();
}
//
// Search script source for keywords to taint
//
const scanScriptsForKeywords = async() => {
console.log('Scanning script source for new keywords.');
function extractKeywords(text) {
for (let regEx of [ ///indexOf\\(\\s*['"]([a-z0-9_-]{0,20}[a-z0-9])=?['"]/ig,
///location.{4,32}\\(\\s*['"]([a-z0-9_-]{0,20}[a-z0-9])=?['"]/ig
/(\\(|=)\\s*['"]([a-z0-9_-]{0,20}[a-z0-9])=?['"]/ig
])
{
var match;
while (match = regEx.exec(text)) {
if (!discoveredKeywords.includes(match[2])) {
discoveredKeywords.push(match[2]);
}
}
}
}
// examine inline scripts
for (script of thisWindow.document.querySelectorAll("script:not([src])")) {
extractKeywords(script.textContent);
}
// fetch all other scripts
for (script of thisWindow.document.querySelectorAll("script[src]")) {
try {
console.log("Fetching " + script.src);
const response = await fetch(script.src);
const body = await response.text();
extractKeywords(body);
} catch (e) {
console.log("Couldn't fetch " + script.src + ": " + e);
}
}
console.log("Found new keywords: " + discoveredKeywords.toString());
// re-taint window location
var newUrl = addTaintToUrl(thisWindow.location.href);
if (unescape(newUrl) != unescape(thisWindow.location.href)) {
console.log("Setting new href: " + newUrl);
thisWindow.location.href = newUrl;
//thisWindow.location.reload();
}
}
thisWindow.addEventListener('scanPage', scanScriptsForKeywords);
//
// Add taint sources to the page
//
//
// Taint the location without reloading the page
//
if (thisWindow.top == thisWindow) {
var taintedHref = addTaintToUrl(thisWindow.location.href);
thisWindow.history.replaceState(null, "", taintedHref);
}
//
// taint the window name
//
Object.defineProperty(thisWindow, 'name', {
value: thisWindow.name + taintString + ".window.name" + filterString,
writable: false
});
//
// taint the referrer (maybe this should be a valid URL to detect SSRF?)
//
Object.defineProperty(thisWindow.document, 'referrer', {
value: "https://" + taintString + ".example.com/"+ taintString + filterString + ".referrer" + window.location.search + window.location.hash,
writable: false
});
//
// taint the cookie data
// TODO: actually taint existing cookies?
//
thisWindow.document.orgCookie = thisWindow.document.cookie + "; " + taintString + ".cookie.name=" + taintString + ".cookie.value" + filterString;
try {
Object.defineProperty(thisWindow.document, 'cookie', {
set: function() {
thisWindow.document.orgCookie += "; " + arguments[0];
var argString = argsToString(arguments);
//console.log("document.cookie set for " + thisWindow.document.origin + ": " + argString);
},
get: function() {
var argString = argsToString(arguments);
//console.log("document.cookie get for " + thisWindow.document.origin);
return thisWindow.document.orgCookie
}
});
} catch (e) {
console.error( "Couldn't hook document.cookie: " + e);
}
//
// taint the location.pathname element
//
//thisWindow.history.replaceState(0,0,location.origin+"//"+taintString+".path"+location.pathname+(location.search===""?"?":location.search)+"&a=?&b"+location.hash);
//
// Inspect History changes
//
try {
thisWindow.History.prototype.orgPushState = thisWindow.History.prototype.pushState;
thisWindow.History.prototype.pushState = function(){
// mostly because I am curious
if (arguments[0] && JSON.stringify(arguments[0]) != '{}')
console.warn("History.pushState: " + JSON.stringify(arguments[0]) + ", " + arguments[1] + ", " + arguments[2]);
// intercept and taint any one-page navigation
if (thisWindow.autoTaintEnabled)
arguments[2] = addTaintToUrl(arguments[2].startsWith("http") ? arguments[2] : location.origin + arguments[2]);
//console.log("History.pushState: " + JSON.stringify(arguments[0]) + ", " + arguments[1] + ", " + arguments[2]);
return thisWindow.History.prototype.orgPushState.apply(this, arguments);
};
thisWindow.History.prototype.orgReplaceState = thisWindow.History.prototype.replaceState;
thisWindow.History.prototype.replaceState = function(){
// mostly because I am curious
if (arguments[0] && JSON.stringify(arguments[0]) != '{}')
console.warn("History.replaceState: " + JSON.stringify(arguments[0]) + ", " + arguments[1] + ", " + arguments[2]);
// intercept and taint any one-page navigation
if (thisWindow.autoTaintEnabled)
arguments[2] = addTaintToUrl(arguments[2].startsWith("http") ? arguments[2] : location.origin + arguments[2]);
//console.log("History.replaceState: " + JSON.stringify(arguments[0]) + ", " + arguments[1] + ", " + arguments[2]);
return thisWindow.History.prototype.orgReplaceState.apply(this, arguments);
};
} catch (e) {
console.error( "Couldn't hook History.pushState/replaceState: " + e);
}
//
// Inspect sinks for tainted data
//
//
// Inspect URLs passed to XMLHttpRequest
// TODO: also hook setHeaders ?
//
try {
thisWindow.XMLHttpRequest.prototype.orgOpen = thisWindow.XMLHttpRequest.prototype.open;
thisWindow.XMLHttpRequest.prototype.open = function(){
// only match on origin and path
var url = arguments[1];
var pos = url.indexOf('?');
if (pos == -1) pos = url.indexOf('#');
if (pos != -1)
url = url.substring(0, pos);
// warn if URL is tainted
if (taintRegex.test(url))
{
var argString = argsToString(arguments);
addWarning("XHR.open", argString);
}
return this.orgOpen.apply( this, arguments );
}
} catch (e) {
console.error( "Couldn't hook XMLHttpRequest.open: " + e);
}
//
// Inspect HTMLElement attribute setters
//
try {
thisWindow.HTMLElement.prototype.orgSetAttribute = thisWindow.HTMLElement.prototype.setAttribute;
thisWindow.HTMLElement.prototype.setAttribute = function(){
var name = this.localName + (this.id ? "#" + this.id : "");
var argString = argsToString(arguments);
if (taintRegex.test(argString))
{
// don't warn on orphaned a.href (used for "URL parsing" by eg. Angular)
if (this.parentNode || this.tagName != "A" || arguments[0] != "href") {
addWarning("DOM setAttribute", name + "." + argString);
}
}
return thisWindow.HTMLElement.prototype.orgSetAttribute.apply(this, arguments);
}
} catch (e) {
console.error( "Couldn't hook HTMLElement.setAttribute: " + e);
}
//
// Inspect calls to document.write()
//
try {
thisWindow.HTMLDocument.prototype.orgWrite = thisWindow.HTMLDocument.prototype.write;
thisWindow.HTMLDocument.prototype.write = function () {
var argString = argsToString(arguments);
if (taintRegex.test(argString))
addWarning("DOMXSS document.write", argString);
return thisWindow.HTMLDocument.prototype.orgWrite.apply(this, arguments);
}
thisWindow.HTMLDocument.prototype.orgWriteln = thisWindow.HTMLDocument.prototype.writeln;
thisWindow.HTMLDocument.prototype.writeln = function () {
var argString = argsToString(arguments);
if (taintRegex.test(argString))
addWarning("DOMXSS document.writeln", argString);
return thisWindow.HTMLDocument.prototype.orgWriteln.apply(this, arguments);
}
} catch (e) {
console.error( "Couldn't hook document.write/writeln: " + e);
}
//
// Inspect setTimeout / setInterval calls
//
try {
thisWindow.orgSetTimeout = thisWindow.setTimeout;
thisWindow.setTimeout = function(){
var argString = argsToString(arguments);
if (taintRegex.test(argString))
addWarning("DOMXSS setTimeout", argString);
return thisWindow.orgSetTimeout.apply( this, arguments );
}
thisWindow.orgSetInterval = thisWindow.setInterval;
thisWindow.setInterval = function(){
var argString = argsToString(arguments);
if (taintRegex.test(argString))
addWarning("DOMXSS setInterval", argString);
return thisWindow.orgSetInterval.apply( this, arguments );
}
} catch (e) {
console.error( "Couldn't hook window.setTimeout/setInterval: " + e);
}
//
// Inspect JavaScript unescape/decode calls
//
/* // This is just creates a lot of noise and isn't very helpful
try {
thisWindow.orgUnescape = thisWindow.unescape;
thisWindow.unescape = function(){
var argString = argsToString(arguments);
if (taintRegex.test(argString)) {
console.warn("unescape: " + argString);
}
return thisWindow.orgUnescape.apply(this, arguments);
}
thisWindow.orgDecodeURI = thisWindow.decodeURI;
thisWindow.decodeURI = function(){
var argString = argsToString(arguments);
if (taintRegex.test(argString)) {
console.warn("decodeURI: " + argString);
}
return thisWindow.orgDecodeURI.apply(this, arguments);
}
thisWindow.orgDecodeURIComponent = thisWindow.decodeURIComponent;
thisWindow.decodeURIComponent = function(){
var argString = argsToString(arguments);
if (taintRegex.test(argString)) {
console.warn("decodeURIComponent: " + argString);
}
return thisWindow.orgDecodeURIComponent.apply(this, arguments);
}
} catch (e) {
console.error( "Couldn't hook window.unescape/decodeURI: " + e);
}
*/
//
// Inspect JavaScript eval() calls
//
try {
thisWindow.orgEval = thisWindow.eval;
thisWindow.eval = function () {
var argString = argsToString(arguments);
if (taintRegex.test(argString)) {
addWarning("DOMXSS eval", argString);
}
// catch any execution errors in eval()
var ret = undefined;
try {
// Known bug:
// calling eval indirectly like this will change scope in sloppy mode
// see http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.2
//
ret = thisWindow.orgEval.apply( thisWindow, arguments );
} catch (e) {
// error will now be caught by our eventListener
e.filename = "eval()";
throw e;
}
return ret;
}
} catch (e) {
console.error( "Couldn't hook window.eval: " + e);
}
//
// Catch and report JavaScript errors in page
//
thisWindow.addEventListener('error', function(e) {
var name = e.error ? e.error.filename : (e.filename ? e.filename : "unknown");
var text = e.error ? e.error.message : e.message;
if (name == 'eval()' || taintRegex.test(text)) {
addWarning("JavaScript Error in " + name, text);
}
});
//
// Inspect window messages
//
thisWindow.orgPostMessage = thisWindow.postMessage;
thisWindow.postMessage = function () {
var argString = argsToString(arguments);
if (taintRegex.test(argString)) {
addWarning("Tainted postMessage call", argString);
} else {
console.log( "postMessage: " + argString);
}
}
thisWindow.addEventListener('message', function(msg) {
if (taintRegex.test(msg.data)) {
addWarning("Tainted message event", msg.data);
}
});
//
// Inspect all assignments to innerHTML/outerHTML
//
var originalInnerHTML = Element.prototype.__lookupSetter__('innerHTML');
var originalOuterHTML = Element.prototype.__lookupSetter__('outerHTML');
try {
Element.prototype.__defineSetter__('innerHTML', function () {
var argString = argsToString(arguments);
if (taintRegex.test(argString)) {
var name = "[" + this.localName + (this.id ? "#" + this.id : "") + "]";
addWarning("DOMXSS " + name + ".innerHTML", argString);
}
return originalInnerHTML.apply(this, arguments);
});
Element.prototype.__defineSetter__('outerHTML', function () {
var argString = argsToString(arguments);
if (taintRegex.test(argString)) {
var name = "[" + this.localName + (this.id ? "#" + this.id : "") + "]";
addWarning("DOMXSS " + name + ".outerHTML", argString);
}
return originalOuterHTML.apply(this, arguments);
});
} catch (e) {
console.error( "Couldn't hook innerHTML/outerHTML: " + e);
}
//
// create a MutationObserver to monitor the DOM for changes
//
var observer = new MutationObserver( function (mutationsList, observer) {
for(var mutation of mutationsList) {
var name = mutation.target.localName + (mutation.target.id ? "#" + mutation.target.id : "");
if (mutation.type == 'childList') {
var text = "";
for (var i=0; i<mutation.addedNodes.length; i++){
var node = mutation.addedNodes[i];
text += node.outerHTML;
}
if (taintRegex.test(text)) {
addWarning("DOM child node added to '" + name + "'", text );
}
} else if (mutation.type == 'attributes'){
try {
if ( taintRegex.test(mutation.attributeName)
|| ( mutation.target.attributes[mutation.attributeName]
&& taintRegex.test(mutation.target.attributes[mutation.attributeName].nodeValue) )
)
{
// ignore tainted links
if (mutation.target.tagName != "A" || mutation.attributeName != "href") {
addWarning("DOM attribute '" + mutation.attributeName + "' was modified on '" + name + "'", mutation.target.attributes[mutation.attributeName].nodeValue);
}
}
} catch (e) {
console.error("Problem reporting tainted attribute: " + e);
}
} else if (mutation.type == 'characterData'){
if (taintRegex.test(mutation.target.textContent)) {
addWarning("DOM tainted characterData added to '" + name + "'", mutation.target.textContent);
}
} else {
console.group("Unknown MutationEvent");
console.dir(mutation);
console.groupEnd();
}
}
});
observer.observe(thisWindow.document, { attributes: true, childList: true, characterData: true, subtree: true });
//
// Stuff related to navigation, only for top frame
//
if (thisWindow.top == thisWindow) {
//
// re-trigger location tainting on single-page navigation
//
function taintChangedHash(e) {
console.log("onhashchange: " + thisWindow.location.hash);
if (thisWindow.autoTaintEnabled) {
var taintedHref = addTaintToUrl(e.newURL);
if (unescape(taintedHref) != unescape(e.newURL)) {
//thisWindow.location.hash = (new URL(taintedHref)).hash;
thisWindow.location.href = taintedHref;
}
}
}
thisWindow.addEventListener('hashchange', taintChangedHash);
//
// automatically scan for keywords after loading the page
//
thisWindow.addEventListener("load", function () {
if (thisWindow.autoTaintEnabled) {
scanScriptsForKeywords();
}
});
}
console.log("Added hooks to " + thisWindow.origin);
}
addTaintHooks(window);
`;
// inject our script code into the page
var s = document.createElement('script');
s.textContent = injectedCode;
(document.head||document.documentElement).appendChild(s);
s.remove();
// listen for warnings from the injected script
window.addEventListener("addWarning", function(data) {
chrome.runtime.sendMessage({op: "addWarning", message: data.detail});
});
// listen for messages from the background script
chrome.runtime.onMessage.addListener( function(req, sender, sendResponse) {
if (req.op == "scanPage") {
if (window === window.top) {
window.dispatchEvent(new Event('scanPage'));
}
}
});