-
Notifications
You must be signed in to change notification settings - Fork 7
/
anti-anti-debug.js
200 lines (177 loc) · 6.08 KB
/
anti-anti-debug.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
!(() => {
console.log("Anti-anti-debug loaded! Happy debugging!")
const Proxy = window.Proxy;
const Object = window.Object;
const Array = window.Array;
/**
* Save original methods before we override them
*/
const Originals = {
createElement: document.createElement,
log: console.log,
warn: console.warn,
table: console.table,
clear: console.clear,
functionConstructor: window.Function.prototype.constructor,
setInterval: window.setInterval,
createElement: document.createElement,
toString: Function.prototype.toString,
addEventListener: window.addEventListener
}
/**
* Cutoffs for logging. After cutoff is reached, will no longer log anti debug warnings.
*/
const cutoffs = {
table: {
amount: 5,
within: 5000
},
clear: {
amount: 5,
within: 5000
},
redactedLog: {
amount: 5,
within: 5000
},
debugger: {
amount: 10,
within: 10000
},
debuggerThrow: {
amount: 10,
within: 10000
}
}
/**
* Decides if anti debug warnings should be logged
*/
function shouldLog(type) {
const cutoff = cutoffs[type];
if (cutoff.tripped) return false;
cutoff.current = cutoff.current || 0;
const now = Date.now();
cutoff.last = cutoff.last || now;
if (now - cutoff.last > cutoff.within) {
cutoff.current = 0;
}
cutoff.last = now;
cutoff.current++;
if (cutoff.current > cutoff.amount) {
Originals.warn("Limit reached! Will now ignore " + type)
cutoff.tripped = true;
return false;
}
return true;
}
window.console.log = wrapFn((...args) => {
// Keep track of redacted arguments
let redactedCount = 0;
// Filter arguments for detectors
const newArgs = args.map((a) => {
// Don't print functions.
if (typeof a === 'function') {
redactedCount++;
return "Redacted Function";
}
// Passthrough if primitive
if (typeof a !== 'object' || a === null) return a;
// For objects, scan properties
var props = Object.getOwnPropertyDescriptors(a)
for (var name in props) {
// Redact custom getters
if (props[name].get !== undefined) {
redactedCount++;
return "Redacted Getter";
}
// Also block toString overrides
if (name === 'toString') {
redactedCount++;
return "Redacted Str";
}
}
// Defeat Performance Detector
// https://github.com/theajack/disable-devtool/blob/master/src/detector/sub-detector/performance.ts
if (Array.isArray(a) && a.length === 50 && typeof a[0] === "object") {
redactedCount++;
return "Redacted LargeObjArray";
}
return a;
});
// If most arguments are redacted, its probably spam
if (redactedCount >= Math.max(args.length - 1, 1)) {
if (!shouldLog("redactedLog")) {
return;
}
}
return Originals.log.apply(console, newArgs)
}, Originals.log);
window.console.table = wrapFn((obj) => {
if (shouldLog("table")) {
Originals.warn("Redacted table");
}
}, Originals.table);
window.console.clear = wrapFn(() => {
if (shouldLog("table")) {
Originals.warn("Prevented clear");
}
}, Originals.clear);
let debugCount = 0;
window.Function.prototype.constructor = wrapFn((...args) => {
const originalFn = Originals.functionConstructor.apply(this, args);
var fnContent = args[0];
if (fnContent) {
if (fnContent.includes('debugger')) { // An anti-debugger is attempting to stop debugging
if (shouldLog("debugger")) {
Originals.warn("Prevented debugger");
}
debugCount++;
if (debugCount > 100) {
if (shouldLog("debuggerThrow")) {
Originals.warn("Debugger loop detected! Throwing error to stop execution");
}
throw new Error("You bad!");
} else {
setTimeout(() => {
debugCount--;
}, 1);
}
const newArgs = args.slice(0);
newArgs[0] = args[0].replaceAll("debugger", ""); // remove debugger statements
return new Proxy(Originals.functionConstructor.apply(this, newArgs),{
get: function (target, prop) {
if (prop === "toString") {
return originalFn.toString;
}
return target[prop];
}
});
}
}
return originalFn;
}, Originals.functionConstructor);
document.createElement = wrapFn((el, o) => {
var string = el.toString();
var element = Originals.createElement.apply(document, [string, o]);
if (string.toLowerCase() === "iframe") {
element.addEventListener("load", () => {
try {
element.contentWindow.window.console = window.console;
} catch (e) {
}
});
}
return element;
}, Originals.createElement);
function wrapFn(newFn, old) {
return new Proxy(newFn, {
get: function (target, prop) {
const callMethods = ['apply', 'bind', 'call'];
if (callMethods.includes(prop)) {
return target[prop];
}
return old[prop];
}
});
}
})()