Spaces:
Running
Running
| import crypto from "crypto"; | |
| /** | |
| * Code Obfuscation Engine for "Ghost Mode" | |
| * Provides multiple obfuscation techniques to make code harder to detect | |
| */ | |
| export class ObfuscationEngine { | |
| /** | |
| * Simple variable name obfuscation | |
| */ | |
| static obfuscateVariableNames(code: string): string { | |
| const variableMap: Record<string, string> = {}; | |
| let counter = 0; | |
| // Replace variable names with encoded names | |
| const obfuscated = code.replace(/\b([a-zA-Z_][a-zA-Z0-9_]*)\b/g, (match) => { | |
| if (!variableMap[match] && !this.isKeyword(match)) { | |
| variableMap[match] = `_${this.encodeToHex(match).substring(0, 8)}`; | |
| } | |
| return variableMap[match] || match; | |
| }); | |
| return obfuscated; | |
| } | |
| /** | |
| * String encoding to hex | |
| */ | |
| static encodeStringsToHex(code: string): string { | |
| return code.replace(/'([^']*)'/g, (match, str) => { | |
| const hex = Buffer.from(str).toString("hex"); | |
| return `bytes.fromhex('${hex}').decode()`; | |
| }); | |
| } | |
| /** | |
| * Add junk code to increase complexity | |
| */ | |
| static addJunkCode(code: string): string { | |
| const junkSnippets = [ | |
| "import random; _ = random.randint(0, 1)", | |
| "try: pass\nexcept: pass", | |
| "x = 1 + 1 - 1", | |
| ]; | |
| const lines = code.split("\n"); | |
| const junk = junkSnippets[Math.floor(Math.random() * junkSnippets.length)]; | |
| // Insert junk at random position | |
| const insertPos = Math.floor(Math.random() * lines.length); | |
| lines.splice(insertPos, 0, junk); | |
| return lines.join("\n"); | |
| } | |
| /** | |
| * Full obfuscation pipeline | |
| */ | |
| static fullObfuscate(code: string, level: "low" | "medium" | "high" = "medium"): string { | |
| let result = code; | |
| if (level === "low" || level === "medium" || level === "high") { | |
| result = this.obfuscateVariableNames(result); | |
| } | |
| if (level === "medium" || level === "high") { | |
| result = this.encodeStringsToHex(result); | |
| result = this.addJunkCode(result); | |
| } | |
| if (level === "high") { | |
| result = this.addJunkCode(result); | |
| result = this.addJunkCode(result); | |
| } | |
| return result; | |
| } | |
| private static isKeyword(word: string): boolean { | |
| const keywords = ["def", "class", "if", "else", "for", "while", "import", "return", "pass", "True", "False", "None"]; | |
| return keywords.includes(word); | |
| } | |
| private static encodeToHex(str: string): string { | |
| return Buffer.from(str).toString("hex"); | |
| } | |
| } | |
| /** | |
| * Payload Library Management | |
| */ | |
| export class PayloadLibrary { | |
| private payloads: Map<string, { code: string; score: number; tags: string[] }> = new Map(); | |
| addPayload(id: string, code: string, score: number, tags: string[]) { | |
| this.payloads.set(id, { code, score, tags }); | |
| } | |
| getPayload(id: string) { | |
| return this.payloads.get(id); | |
| } | |
| searchByTag(tag: string) { | |
| return Array.from(this.payloads.values()).filter(p => p.tags.includes(tag)); | |
| } | |
| getTopPayloads(limit: number = 10) { | |
| return Array.from(this.payloads.values()) | |
| .sort((a, b) => b.score - a.score) | |
| .slice(0, limit); | |
| } | |
| obfuscatePayload(id: string, level: "low" | "medium" | "high" = "medium"): string | null { | |
| const payload = this.payloads.get(id); | |
| if (!payload) return null; | |
| return ObfuscationEngine.fullObfuscate(payload.code, level); | |
| } | |
| } | |