-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathAntiAnti-Hook.js
More file actions
162 lines (143 loc) · 6.63 KB
/
Copy pathAntiAnti-Hook.js
File metadata and controls
162 lines (143 loc) · 6.63 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
// ==UserScript==
// @name AntiAnti Hook
// @namespace https://github.com/0xsdeo/Hook_JS
// @version 0.2
// @description 反Hook检测
// @author 0xsdeo
// @match https://www.zhipin.com/*
// @run-at document-start
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// ==/UserScript==
/**
* 脚本来源:https://github.com/0xsdeo/Hook_JS
*/
(function () {
'use strict';
// 必须返回随时间递增的值,否则会被时序检测(t1===t2)判定为 hook 并触发内存炸弹
const navStart = (typeof performance !== 'undefined' && performance.timing)
? performance.timing.navigationStart
: Date.now();
console.table = function () {};
performance.now = function () {
return Date.now() - navStart;
};
const methods = ['console.table', 'performance.now'];
function initAntiAntiHook() {
try {
const hookedFunctions = new Map();
for (let methodPath of methods) {
let ref = window;
let parts = methodPath.split('.');
try {
for (let i = 0; i < parts.length - 1; i++) {
ref = ref[parts[i]];
if (!ref) break;
}
if (ref) {
const fn = ref[parts[parts.length - 1]];
if (typeof fn === 'function') {
hookedFunctions.set(fn, parts[parts.length - 1]);
}
}
} catch (e) {}
}
if (hookedFunctions.size === 0) return;
let temp_toString = Function.prototype.toString;
Function.prototype.toString = function () {
if (this === Function.prototype.toString) {
return 'function toString() { [native code] }';
} else if (this === Function.prototype.constructor &&
hookedFunctions.has(Function.prototype.constructor)) {
return 'function Function() { [native code] }';
} else if (hookedFunctions.has(this)) {
const funcName = hookedFunctions.get(this);
return `function ${funcName}() { [native code] }`;
}
return temp_toString.apply(this, arguments);
};
const hookedMethodNames = new Map();
methods.forEach(path => {
let ref = window;
let parts = path.split('.');
try {
for (let i = 0; i < parts.length - 1; i++) {
ref = ref[parts[i]];
if (!ref) break;
}
if (ref) {
const fn = ref[parts[parts.length - 1]];
if (typeof fn === 'function') {
hookedMethodNames.set(path, fn);
}
}
} catch (e) {}
});
const objectHooksMap = new Map();
methods.forEach(path => {
const parts = path.split('.');
const rootParts = parts[0] === 'window' ? parts.slice(1) : parts;
if (rootParts.length < 2) return;
const rootName = rootParts[0];
const remaining = rootParts.slice(1);
const fn = hookedMethodNames.get(path);
if (!fn) return;
if (!objectHooksMap.has(rootName)) objectHooksMap.set(rootName, []);
objectHooksMap.get(rootName).push({ remaining, fn });
});
if (!objectHooksMap.has('Function')) objectHooksMap.set('Function', []);
const existsToString = objectHooksMap.get('Function')
.some(({ remaining }) => remaining.join('.') === 'prototype.toString');
if (!existsToString) {
objectHooksMap.get('Function').push({
remaining: ['prototype', 'toString'],
fn: Function.prototype.toString
});
}
let property_accessor = Object.getOwnPropertyDescriptor(HTMLIFrameElement.prototype, "contentWindow");
let get_accessor = property_accessor.get;
Object.defineProperty(HTMLIFrameElement.prototype, "contentWindow", {
get: function () {
let iframe_window = get_accessor.apply(this);
iframe_window = new Proxy(iframe_window, {
get: function (target, property, receiver) {
if (typeof property === 'string') {
for (const [fullPath, fn] of hookedMethodNames.entries()) {
if (fullPath.endsWith('.' + property) ||
fullPath === property) {
return fn;
}
}
if (objectHooksMap.has(property)) {
const obj = Reflect.get(target, property, target);
if (obj !== null && (typeof obj === 'object' || typeof obj === 'function')) {
objectHooksMap.get(property).forEach(({ remaining, fn }) => {
let ref = obj;
try {
for (let i = 0; i < remaining.length - 1; i++) {
ref = ref[remaining[i]];
if (!ref) return;
}
ref[remaining[remaining.length - 1]] = fn;
} catch (e) {}
});
}
return obj;
}
}
const value = Reflect.get(target, property, target);
if (typeof value === 'function') {
return value.bind(target);
}
return value;
},
});
return iframe_window;
}
});
} catch (e) {
console.error('AntiAnti_Hook: 初始化失败', e);
}
}
initAntiAntiHook();
})();