-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.js
91 lines (66 loc) · 2.43 KB
/
contentScript.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
function init() {
if (document.body && document.head) {
patchXhr();
} else {
requestIdleCallback(init);
}
}
function patchXhr() {
console.log("patchXhr");
let shellcode = document.createElement("script");
shellcode.type = "text/javascript";
shellcode.innerHTML = `
function hookSetter(obj, key, fun) {
let descriptor = Object.create(null);
descriptor.enumerable = true;
descriptor.configurable = true;
let value;
descriptor.get = () => {
return value;
};
descriptor.set = function(newValue) {
value = fun.call(this, newValue);
};
Object.defineProperty(
obj, key,
descriptor
);
}
(function() {
console.log("<shellcode>");
let xhr = XMLHttpRequest.prototype;
let xhrOpen = xhr.open;
xhr.open = function(method, url) {
this.__contentScript__url = url;
console.log("open " + url);
return xhrOpen.apply(this, arguments);
};
let xhrAddEventListener = xhr.addEventListener;
xhr.addEventListener = function(event, callback) {
console.log("addEventListener");
if (event != "load") {
return xhrAddEventListener.apply(this, arguments);
}
return xhrAddEventListener.call(this, event, function() {
console.log(arguments[0]);
return callback.apply(this, arguments);
});
};
let xhrSetOnreadystatechange = xhr.__lookupSetter__("onreadystatechange");
hookSetter(xhr, "onreadystatechange", function(callback) {
console.log("set onreadystatechange");
let newCallback = function() {
console.log("onreadystatechange");
if (this.readyState == XMLHttpRequest.DONE && this.status == 200) {
console.log(this.responseText);
}
return callback.apply(this, arguments);
};
xhrSetOnreadystatechange.call(this, newCallback);
return newCallback;
});
})();
`;
document.head.prepend(shellcode);
}
requestIdleCallback(init);