Вот исправленный код для Tampermonkey в Chrome:
// ==UserScript==
// @name Anti-Static Analysis Protection
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Anti-debugging and static analysis protection
// @author You
// @match *://*/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// Встроенный генератор имен для обфускации
class AdvancedNameGenerator {
constructor() {
this.usedNames = new Set();
this.chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';
}
generateObfuscatedName() {
let name;
do {
name = '';
const length = Math.floor(Math.random() * 8) + 3;
for (let i = 0; i < length; i++) {
name += this.chars[Math.floor(Math.random() * this.chars.length)];
}
} while (this.usedNames.has(name) || !this.isValidIdentifier(name));
this.usedNames.add(name);
return name;
}
isValidIdentifier(str) {
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(str);
}
}
class AntiStaticAnalysis {
constructor() {
this.nameGen = new AdvancedNameGenerator();
}
generateDynamicEval() {
const evalVar = this.nameGen.generateObfuscatedName();
const constructorVar = this.nameGen.generateObfuscatedName();
const funcVar = this.nameGen.generateObfuscatedName();
return `
var ${evalVar} = (function() {
var ${constructorVar} = 'constru' + 'ctor';
var ${funcVar} = 'Fun' + 'ction';
return (function() {}).constructor[${constructorVar}][${funcVar}];
})();
var dynamicEval = function(code) {
return ${evalVar}('retu' + 'rn ' + code)();
};`;
}
generateAntiDebugger() {
const names = {
check: this.nameGen.generateObfuscatedName(),
timer: this.nameGen.generateObfuscatedName(),
action: this.nameGen.generateObfuscatedName(),
err: this.nameGen.generateObfuscatedName(),
i: this.nameGen.generateObfuscatedName()
};
const reactions = [
'while(1){}',
'location.reload()',
`for(var k in window){ try{ if(typeof window[k] === 'object') window[k] = null; } catch(e){} }`
];
const selectedReaction = reactions[Math.floor(Math.random() * reactions.length)];
return `
var ${names.action} = function() { ${selectedReaction} };
var ${names.check} = function() {
var ${names.timer} = Date.now();
(function() { debugger; })();
if (Date.now() - ${names.timer} > 100) {
${names.action}();
}
try {
if (console.log.toString().indexOf('[native code]') === -1) {
${names.action}();
}
} catch(${names.err}) {}
try {
throw new Error();
} catch (${names.err}) {
if (${names.err}.stack && ${names.err}.stack.includes('debugger')) {
${names.action}();
}
}
};
if (typeof setInterval !== 'undefined') {
setInterval(${names.check}, ${Math.floor(Math.random() * 1000) + 500});
}
try { ${names.check}(); } catch(e) {}
`;
}
generateCodeIntegrityCheck(originalCode) {
const names = {
checkFunc: this.nameGen.generateObfuscatedName(),
hash: this.nameGen.generateObfuscatedName(),
expected: this.nameGen.generateObfuscatedName(),
code: this.nameGen.generateObfuscatedName(),
};
const expectedHash = this.calculateHash(originalCode);
return `
(function ${names.checkFunc}() {
var ${names.hash} = 0;
var ${names.code} = ${names.checkFunc}.toString();
var ${names.expected} = ${expectedHash};
if (${names.hash} !== ${names.expected}) {
while(1){}
}
})();`;
}
calculateHash(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash + str.charCodeAt(i)) & 0xffffffff;
}
return hash;
}
// Метод для запуска всех защит
activateProtection() {
// Выполняем анти-отладчик
eval(this.generateAntiDebugger());
// Выполняем динамический eval
eval(this.generateDynamicEval());
// Пример использования динамического eval
setTimeout(() => {
if (typeof dynamicEval !== 'undefined') {
console.log('Protection activated');
}
}, 1000);
}
}
// Активируем защиту
const protector = new AntiStaticAnalysis();
protector.activateProtection();
// Дополнительная защита от DevTools
(function() {
const devtools = {
isOpen: false,
orientation: null
};
const threshold = 160;
const emitEvent = (isOpen, orientation) => {
if (isOpen) {
// Анти-отладка активирована
debugger;
setInterval(() => {
debugger;
}, 50);
}
};
const main = () => {
const widthThreshold = window.outerWidth - window.innerWidth > threshold;
const heightThreshold = window.outerHeight - window.innerHeight > threshold;
const orientation = widthThreshold ? 'vertical' : 'horizontal';
if (
!(heightThreshold && widthThreshold) &&
((window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized) || widthThreshold || heightThreshold)
) {
if (!devtools.isOpen || devtools.orientation !== orientation) {
emitEvent(true, orientation);
}
devtools.isOpen = true;
devtools.orientation = orientation;
} else {
if (devtools.isOpen) {
emitEvent(false, null);
}
devtools.isOpen = false;
devtools.orientation = null;
}
};
main();
window.addEventListener('resize', main);
})();
})();