-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinject.js
More file actions
278 lines (267 loc) · 8.16 KB
/
inject.js
File metadata and controls
278 lines (267 loc) · 8.16 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
const script = document.createElement("script");
function core(e, window) {
var globalConfig = e;
console.log("inject start!", e);
if (e["config-hook-debugger"]) {
function Closure(injectFunction) {
return function () {
if (!arguments.length) return injectFunction.apply(this, arguments);
arguments[arguments.length - 1] = arguments[
arguments.length - 1
].replace(/debugger/g, "");
return injectFunction.apply(this, arguments);
};
}
var oldFunctionConstructor = window.Function.prototype.constructor;
window.Function.prototype.constructor = Closure(oldFunctionConstructor);
//fix native function
window.Function.prototype.constructor.toString = oldFunctionConstructor.toString.bind(
oldFunctionConstructor
);
var oldFunction = Function;
window.Function = Closure(oldFunction);
//fix native function
window.Function.toString = oldFunction.toString.bind(oldFunction);
var oldEval = eval;
window.eval = Closure(oldEval);
//fix native function
window.eval.toString = oldEval.toString.bind(oldEval);
// hook GeneratorFunction
var oldGeneratorFunctionConstructor = Object.getPrototypeOf(function* () {})
.constructor;
var newGeneratorFunctionConstructor = Closure(
oldGeneratorFunctionConstructor
);
newGeneratorFunctionConstructor.toString = oldGeneratorFunctionConstructor.toString.bind(
oldGeneratorFunctionConstructor
);
Object.defineProperty(
oldGeneratorFunctionConstructor.prototype,
"constructor",
{
value: newGeneratorFunctionConstructor,
writable: false,
configurable: true,
}
);
// hook Async Function
var oldAsyncFunctionConstructor = Object.getPrototypeOf(
async function () {}
).constructor;
var newAsyncFunctionConstructor = Closure(oldAsyncFunctionConstructor);
newAsyncFunctionConstructor.toString = oldAsyncFunctionConstructor.toString.bind(
oldAsyncFunctionConstructor
);
Object.defineProperty(
oldAsyncFunctionConstructor.prototype,
"constructor",
{
value: newAsyncFunctionConstructor,
writable: false,
configurable: true,
}
);
// hook dom
var oldSetAttribute = window.Element.prototype.setAttribute;
window.Element.prototype.setAttribute = function (name, value) {
if (typeof value == "string") value = value.replace(/debugger/g, "");
// 向上调用
oldSetAttribute.call(this, name, value);
};
var oldContentWindow = Object.getOwnPropertyDescriptor(
HTMLIFrameElement.prototype,
"contentWindow"
).get;
Object.defineProperty(window.HTMLIFrameElement.prototype, "contentWindow", {
get() {
var newV = oldContentWindow.call(this);
if (!newV.inject) {
newV.inject = true;
core.call(newV, globalConfig, newV);
}
return newV;
},
});
}
if (e["config-hook-pushState"]) {
// hook pushState
var oldHistoryPushState = history.pushState;
var pushState = {};
history.pushState = function () {
// anti-shake filtering high frequency operation
if (new Date() - pushState.lastTime > 200) {
pushState.count = 0;
}
pushState.count++;
if (pushState.count > 3) return;
return oldHistoryPushState.apply(this, arguments);
};
history.pushState.toString = oldHistoryPushState.toString.bind(
oldHistoryPushState
);
}
if (e["config-hook-regExp"]) {
// hook RegExp
var oldRegExp = RegExp;
RegExp = new Proxy(RegExp, {
apply(target, thisArgument, argumentsList) {
console.log("Some codes are setting RegExp...");
debugger;
// prevent detection of formatting
if (argumentsList[0] == `\\w+ *\\(\\) *{\\w+ *['|"].+['|"];? *}`) {
return Reflect.apply(target, thisArgument, [""]);
}
return Reflect.apply(target, thisArgument, argumentsList);
},
});
RegExp.toString = oldRegExp.toString.bind(oldRegExp);
}
if (e["config-hook-console"]) {
// hook console
var oldConsole = [
"debug",
"error",
"info",
"log",
"warn",
"dir",
"dirxml",
"table",
"trace",
"group",
"groupCollapsed",
"groupEnd",
"clear",
"count",
"countReset",
"assert",
"profile",
"profileEnd",
"time",
"timeLog",
"timeEnd",
"timeStamp",
"context",
"memory",
].map((key) => {
var old = console[key];
console[key] = function () {};
console[key].toString = old.toString.bind(old);
return old;
});
}
if (e["config-hook-setcookie"]) {
(function () {
"use strict";
var pre = document.cookie;
Object.defineProperty(document, "cookie", {
get: function () {
console.log("Getting document.cookie");
return pre;
},
set: function (val) {
console.log("Setting document.cookie", val);
debugger;
pre = val;
},
});
})();
}
if (e["config-fuck-sojson"]) {
(function () {
var setInterval_ = setInterval;
setInterval = function () {};
setInterval.toString = function () {
console.log("有函数正在检测setInterval是否被hook");
return setInterval_.toString();
};
})();
(function () {
var _RegExp = RegExp;
RegExp = function (pattern, modifiers) {
if (
pattern ==
decodeURIComponent(
"%5Cw%2B%20*%5C(%5C)%20*%7B%5Cw%2B%20*%5B'%7C%22%5D.%2B%5B'%7C%22%5D%3B%3F%20*%7D"
) ||
pattern == decodeURIComponent("function%20*%5C(%20*%5C)") ||
pattern ==
decodeURIComponent(
"%5C%2B%5C%2B%20*(%3F%3A_0x(%3F%3A%5Ba-f0-9%5D)%7B4%2C6%7D%7C(%3F%3A%5Cb%7C%5Cd)%5Ba-z0-9%5D%7B1%2C4%7D(%3F%3A%5Cb%7C%5Cd))"
) ||
pattern ==
decodeURIComponent("(%5C%5C%5Bx%7Cu%5D(%5Cw)%7B2%2C4%7D)%2B")
) {
pattern = ".*?";
console.log("发现sojson检测特征,已帮您处理。");
}
if (modifiers) {
console.log("疑似最后一个检测...已帮您处理。");
console.log("已通过全部检测,请手动处理debugger后尽情调试吧!");
return _RegExp(pattern, modifiers);
} else {
return _RegExp(pattern);
}
};
RegExp.toString = function () {
return _RegExp.toString();
};
})();
}
if (e["config-hook-setInterval"]) {
(function () {
setInterval_ = setInterval;
console.log("原函数已被重命名为setInterval_");
setInterval = function () {};
setInterval.toString = function () {
console.log("有函数正在检测setInterval是否被hook");
return setInterval_.toString();
};
})();
}
if (e["config-hook-stringify"]) {
var rstringiyf = JSON.stringify;
var stringify_key = prompt("请输入你要监视的关键字:");
JSON.stringify = function (a) {
console.log("Detect JSON.stringify 被转换的数据为: => ", a);
for (const key in a) {
if (a.hasOwnProperty(key)) {
const element = a[key];
if (element == stringify_key) {
console.log("发现有代码正在将关键字对象转换为字符串...");
debugger;
}
}
}
return rstringiyf(a);
};
JSON.stringify.toString = function () {
return rstringiyf.toString();
};
//JSON.parse
var strparse = JSON.parse;
JSON.parse = function (b) {
console.log("Detect JSON.Parse");
return strparse(b);
};
}
}
chrome.storage.sync.get(
[
"config-hook-console",
"config-hook-debugger",
"config-hook-regExp",
"config-hook-pushState",
"config-hook-setcookie",
"config-fuck-sojson",
"config-hook-setInterval",
"config-hook-stringify",
],
function (result) {
script.text = `(${core.toString()})(${JSON.stringify(result)},window)`;
script.onload = () => {
script.parentNode.removeChild(script);
};
(document.head || document.documentElement).appendChild(script);
}
);