-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhtml-guard.js
More file actions
217 lines (159 loc) · 6.57 KB
/
html-guard.js
File metadata and controls
217 lines (159 loc) · 6.57 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
// https://github.com/DosX-dev/HTML-Guard
// Protect your HTML pages!
(() => {
let scriptSrc = document.createElement("script");
scriptSrc.innerText = 'for (var i = 111; i < 222; i++) { console.error("%c[" + i.toString(16) + "] HTML Guard sees you!", "font-size: 25px; text-shadow: 0 0 1px black; color: red;") }';
document.head.appendChild(scriptSrc);
})();
const HtmlGuard = {
protections: {
antiDevTools() {
function isNative(func) { // Designed for functions returning values
return typeof func === "function" && (
window.eval.toString().includes("return") === false &&
window.eval.toString().includes("[native code]") &&
window.eval.toString().length < 40
)
}
// DevTools detector
let antiDevToolsInterval = setInterval(() => {
// Antispoof
if (!isNative(Date.now) ||
!isNative(window.eval) ||
window.eval("2+2") !== 4) {
alert("Do not spoof functions!");
document.head.innerHTML = "";
document.body.innerHTML = "";
location.reload();
clearInterval(antiDevToolsInterval);
}
let started = Date.now(),
end;
window.eval("// The use of DevTools is prohibited in this web application\n" + "debugger");
end = Date.now();
if ((end - started) > 50) {
alert("DevTools not allowed!");
document.head.innerHTML = "";
document.body.innerHTML = "";
location.reload();
clearInterval(antiDevToolsInterval);
}
}, 150);
},
blockContextMenu() {
document.oncontextmenu = () => {
return false;
};
},
blockDrag() {
document.ondragstart = () => {
return false;
};
},
blockSelection() {
document.onselectstart = () => {
return false;
};
},
blockConsoleOutput() {
["log", "debug", "warn", "error", "dir", "dirxml", "assert", "table"].forEach(funcName => {
console[funcName] = () => { return null; };
});
}
},
loader: {
loadStyleByRef(stylePath) {
let styleLink = document.createElement("link");
styleLink.rel = "stylesheet";
styleLink.href = stylePath;
document.head.appendChild(styleLink);
},
loadScriptBySrc(scriptPath) {
let scriptSrc = document.createElement("script");
scriptSrc.src = scriptPath;
document.head.appendChild(scriptSrc);
},
loadScriptBySrc_ContentLoaded(scriptPath) {
document.addEventListener("DOMContentLoaded", () => {
HtmlGuard.loader.loadScriptBySrc(scriptPath);
});
}
}
};
(() => {
if (Math.random() == Math.random() == Math.random()) {
document.head.innerHTML = "";
document.body.innerHTML = "";
location.reload();
}
document.onkeydown = (e) => {
if (event.keyCode == 123) { return false; } // F12
if (e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) { return false; } // Ctrl + Shift + I
if (e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)) { return false; } // Ctrl + Shift + J
if (e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)) { return false; } // Ctrl + U
}
document.addEventListener("DOMContentLoaded", () => {
obfuscatePageSource();
setInterval(() => {
obfuscatePageSource();
}, 2000);
function obfuscatePageSource() {
const
attributeName = "html-guard-attribute",
notGuardedSelector = ":not([" + attributeName + "])";
// Junk comments
for (const element of document.querySelectorAll("*" + notGuardedSelector)) {
for (var i = 0; i < randomByRange(5, 15); i++) {
let comment = "";
for (var o = 0; o < randomByRange(10, 20); o++) {
comment += generateRandomString(1, 5) + "\n";
}
element.parentNode.insertBefore(document.createComment(comment), element);
}
}
// Add fake IDs
for (const element of document.querySelectorAll(":not([id])" + notGuardedSelector)) {
element.id = generateRandomString(5, 15);
}
// Protected attributes
for (const element of document.querySelectorAll("*")) {
const attributes = element.attributes;
for (const attribute of attributes) {
if (attribute.name.startsWith("_")) {
element.setAttribute(attribute.name.substring(1), attribute.value);
element.removeAttribute(attribute.name);
}
}
}
for (const element of document.querySelectorAll("*" + notGuardedSelector)) {
// Junk classes
for (let i = 0; i < randomByRange(1, 8); i++) {
element.classList.add(generateRandomString(6, 20));
}
// Junk attributes
for (let i = 0; i < randomByRange(10, 55); i++) {
element.setAttribute(generateRandomString(6, 12), (randomByRange(0, 1) == 1) ? generateRandomString(1, 5) : "");
}
element.setAttribute(attributeName, "");
}
}
function generateRandomString(min, max) {
if (min > max) {
throw new RangeError("min > max");
}
const length = Math.floor(Math.random() * (max - min + 1)) + min;
const characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let randomString = "";
for (let i = 0; i < length; i++) {
randomString += characters.charAt(Math.floor(Math.random() * characters.length));
}
return randomString;
}
function randomByRange(min, max) {
if (min > max) {
throw new RangeError("min > max");
}
return Math.floor(Math.random() * (max - min + 1)) + min;
}
});
})();