diff --git a/backend/src/services/obfuscator.js b/backend/src/services/obfuscator.js index 1c7e6d5..c0a37e9 100644 --- a/backend/src/services/obfuscator.js +++ b/backend/src/services/obfuscator.js @@ -2,18 +2,34 @@ const crypto = require('crypto'); class ObfuscatorService { static obfuscate(sourceCode, config = {}) { - const seed = crypto.randomBytes(4).readUInt32BE(0); - const tokens = this.tokenize(sourceCode); - const { bc, constants, ops } = this.compile(tokens); - return this.generateVM(bc, constants, ops, seed); + try { + console.log('Starting obfuscation for code length:', sourceCode.length); + const seed = crypto.randomBytes(4).readUInt32BE(0); + const tokens = this.tokenize(sourceCode); + const { bc, constants, ops } = this.compile(tokens); + const vm = this.generateVM(bc, constants, ops, seed); + console.log('Obfuscation complete, VM length:', vm.length); + return vm; + } catch (err) { + console.error('Obfuscation error:', err); + return `-- LUARTEX CRITICAL ERROR: ${err.message} +` + sourceCode; + } } static tokenize(code) { const tokens = []; let i = 0; + const operators = [ + '==', '~=', '<=', '>=', '..', '...', '+=', '-=', '*=', '/=', + '=', '+', '-', '*', '/', '%', '^', '#', '<', '>', '(', ')', + '{', '}', '[', ']', ';', ':', ',', '.' + ]; + while (i < code.length) { let c = code[i]; if (/\s/.test(c)) { i++; continue; } + if (c === '-' && code[i + 1] === '-') { i += 2; if (code[i] === '[' && code[i + 1] === '[') { @@ -25,56 +41,71 @@ class ObfuscatorService { } continue; } + if (c === '"' || c === "'") { const q = c; let s = ""; i++; while (i < code.length && code[i] !== q) { - if (code[i] === '\\') { s += code[i] + (code[i + 1] || ""); i += 2; } - else { s += code[i]; i++; } + if (code[i] === '\\') { + i++; + if (code[i] === 'x') { s += String.fromCharCode(parseInt(code.substr(i + 1, 2), 16)); i += 3; } + else if (/\d/.test(code[i])) { + let d = ""; while (i < code.length && /\d/.test(code[i]) && d.length < 3) { d += code[i]; i++; } + s += String.fromCharCode(parseInt(d, 10)); + } + else { s += code[i] || ''; i++; } + } else { s += code[i]; i++; } } tokens.push({ type: 'string', value: s }); i++; continue; } + if (/\d/.test(c)) { let n = ""; - while (i < code.length && /[\d\.\xX]/.test(code[i])) { n += code[i]; i++; } - tokens.push({ type: 'number', value: parseFloat(n) }); + while (i < code.length && /[0-9.xXabcdefABCDEF]/.test(code[i])) { n += code[i]; i++; } + tokens.push({ type: 'number', value: n.startsWith('0x') ? parseInt(n, 16) : parseFloat(n) }); continue; } + if (/[a-zA-Z_]/.test(c)) { let id = ""; while (i < code.length && /[a-zA-Z0-9_]/.test(code[i])) { id += code[i]; i++; } tokens.push({ type: 'identifier', value: id }); continue; } - tokens.push({ type: 'operator', value: c }); i++; + + let matchedOp = false; + for (const op of operators) { + if (code.startsWith(op, i)) { + tokens.push({ type: 'operator', value: op }); + i += op.length; + matchedOp = true; + break; + } + } + if (!matchedOp) { i++; } } return tokens; } - static numberToExpression(n) { + static numberToExpression(n, seed = 0) { if (typeof n !== 'number') return n; - const ops = ['+', '-', '*']; - let current = Math.floor(Math.random() * 200) - 100; + let current = Math.floor(Math.random() * 1000); let expr = `(${current})`; - - for (let i = 0; i < 4; i++) { - const op = ops[Math.floor(Math.random() * ops.length)]; - const val = Math.floor(Math.random() * 100) + 1; - if (op === '+') { current += val; expr = `(${expr} + ${val})`; } - else if (op === '-') { current -= val; expr = `(${expr} - ${val})`; } - else if (op === '*') { - if (Math.abs(current * val) < 1000000) { - current *= val; expr = `(${expr} * ${val})`; + const steps = 3 + Math.floor(Math.random() * 3); + for (let i = 0; i < steps; i++) { + const mode = Math.floor(Math.random() * 4); + const v = Math.floor(Math.random() * 200) + 1; + if (mode === 0) { current += v; expr = `(${expr} + ${v})`; } + else if (mode === 1) { current -= v; expr = `(${expr} - ${v})`; } + else if (mode === 2) { current *= 2; expr = `(${expr} * 2)`; } + else { + const nv = Math.floor(Math.random() * 255); + current = (current ^ nv) >>> 0; + expr = `bit32.bxor(${expr}, ${nv})`; } - } } - const diff = n - current; - if (diff >= 0) expr = `(${expr} + ${diff})`; - else expr = `(${expr} - ${Math.abs(diff)})`; - - const noise = Math.floor(Math.random() * 255); - return `bit32.bxor(bit32.bxor(${expr}, ${noise}), ${noise})`; + return `(${expr} + ${diff})`; } static compile(tokens) { @@ -84,115 +115,214 @@ class ObfuscatorService { if (idx === -1) { idx = constants.length; constants.push({ value: v, type: typeof v }); } return idx; }; + const ops = { LOADK: 0, GETG: 1, SETG: 2, CALL: 3, RET: 4, GETT: 5, GETS: 6, - NEWTABLE: 7, SETTABLE: 8, GETLOCAL: 9, SETLOCAL: 10 + NEWTABLE: 7, SETTABLE: 8, MOVE: 9, + ADD: 10, SUB: 11, MUL: 12, DIV: 13, CONCAT: 14, + EQ: 15, LT: 16, LE: 17, JMP: 18, JMPF: 19, + CLOSURE: 20 }; + const bc = []; let ti = 0; - const locals = {}; - let nextLocal = 0; + const locals = [{}]; + let nextReg = 0; - const peek = () => tokens[ti]; + const peek = (n = 0) => tokens[ti + n]; const consume = () => tokens[ti++]; - const parseExpr = (reg) => { + const getLocal = (name) => { + for (let i = locals.length - 1; i >= 0; i--) { + if (locals[i][name] !== undefined) return locals[i][name]; + } + return undefined; + }; + + const parseExpression = (reg) => { let t = peek(); if (!t) return; - if (t.type === 'number' || t.type === 'string') { - bc.push(ops.LOADK, reg, getC(consume().value)); - } else if (t.type === 'identifier') { - let name = consume().value; - if (peek()?.value === '(') { - ti--; parseCall(reg); - } else { - emitGet(reg, name); - while (peek()?.value === '.' || peek()?.value === ':') { - let op = consume().value; - let key = consume().value; - if (op === ':') { - bc.push(ops.GETS, reg, reg, getC(key)); - parseArgs(reg, true); - } else { - bc.push(ops.GETT, reg, reg, getC(key)); - if (peek()?.value === '(') parseArgs(reg); + + const parsePrimary = (r) => { + let t = peek(); + if (!t) return; + if (t.type === 'number' || t.type === 'string') { + bc.push(ops.LOADK, r, getC(consume().value)); + } else if (t.type === 'identifier') { + if (t.value === 'true') { bc.push(ops.LOADK, r, getC(true)); consume(); } + else if (t.value === 'false') { bc.push(ops.LOADK, r, getC(false)); consume(); } + else if (t.value === 'nil') { bc.push(ops.LOADK, r, getC(null)); consume(); } + else if (t.value === 'function') { + parseClosure(r); + } else { + let name = consume().value; + let lReg = getLocal(name); + if (lReg !== undefined) bc.push(ops.MOVE, r, lReg); + else bc.push(ops.GETG, r, getC(name)); + + while (ti < tokens.length) { + let next = peek(); + if (next?.value === '.') { + consume(); let key = consume()?.value; + if (key) bc.push(ops.GETT, r, r, getC(key)); + } else if (next?.value === ':') { + consume(); let key = consume()?.value; + if (key) { + bc.push(ops.GETS, r, r, getC(key)); + parseArgs(r, true); + } + } else if (next?.value === '(') { + parseArgs(r, false); + } else break; } } + } else if (t.value === '{') { + parseTable(r); + } else if (t.value === '(') { + consume(); parseExpression(r); + if (peek()?.value === ')') consume(); } - } else if (t.value === '{') { - parseTable(reg); + }; + + parsePrimary(reg); + const binOps = { '+': ops.ADD, '-': ops.SUB, '*': ops.MUL, '/': ops.DIV, '..': ops.CONCAT, '==': ops.EQ, '<': ops.LT, '<=': ops.LE }; + while (peek()?.type === 'operator' && binOps[peek().value]) { + let op = consume().value; + let opCode = binOps[op]; + let r2 = nextReg++; + parsePrimary(r2); + bc.push(opCode, reg, reg, r2); } }; - const parseTable = (reg) => { - bc.push(ops.NEWTABLE, reg); consume(); // { - while (ti < tokens.length && peek().value !== '}') { - if (peek().value === ',') { consume(); continue; } - let t = peek(); - if (tokens[ti+1]?.value === '=') { - let key = consume().value; consume(); // = - parseExpr(reg + 1); - bc.push(ops.SETTABLE, reg, getC(key), reg + 1); - } else { - parseExpr(reg + 1); - } - if (peek()?.value === ',') consume(); - } - if (peek()?.value === '}') consume(); - }; - - const emitGet = (reg, name) => { - if (locals[name] !== undefined) bc.push(ops.GETLOCAL, reg, locals[name]); - else bc.push(ops.GETG, reg, getC(name)); - }; - const parseArgs = (reg, isM) => { if (peek()?.value !== '(') return; - consume(); let ac = isM ? 1 : 0; + consume(); + let ac = isM ? 1 : 0; while (ti < tokens.length && peek().value !== ')') { if (peek().value === ',') { consume(); continue; } - ac++; parseExpr(reg + ac); + ac++; + let argReg = reg + ac; + if (argReg >= nextReg) nextReg = argReg + 1; + parseExpression(argReg); } - bc.push(ops.CALL, reg, ac + 1, 1); if (peek()?.value === ')') consume(); + bc.push(ops.CALL, reg, ac + 1, 1); }; - const parseCall = (reg) => { - let fn = consume().value; - emitGet(reg, fn); - parseArgs(reg); + const parseTable = (reg) => { + bc.push(ops.NEWTABLE, reg); consume(); + let arrIdx = 1; + while (ti < tokens.length && peek()?.value !== '}') { + if (peek()?.value === ',' || peek()?.value === ';') { consume(); continue; } + if (peek()?.value === '}') break; + + if (peek()?.type === 'identifier' && peek(1)?.value === '=') { + let key = consume().value; consume(); + let vReg = nextReg++; + parseExpression(vReg); + bc.push(ops.SETTABLE, reg, getC(key), vReg); + } else if (peek()?.value === '[') { + consume(); + let kReg = nextReg++; + parseExpression(kReg); + if (peek()?.value === ']') consume(); + if (peek()?.value === '=') consume(); + let vReg = nextReg++; + parseExpression(vReg); + bc.push(ops.SETTABLE, reg, -1, kReg, vReg); + } else { + let vReg = nextReg++; + parseExpression(vReg); + bc.push(ops.SETTABLE, reg, getC(arrIdx++), vReg); + } + } + if (peek()?.value === '}') consume(); + }; + + const parseClosure = (reg) => { + consume(); // function + if (peek()?.value === '(') { + consume(); + while (ti < tokens.length && peek()?.value !== ')') consume(); + if (peek()?.value === ')') consume(); + } + // Capture function body as string for sub-VM + let start = ti; + let depth = 1; + while (ti < tokens.length && depth > 0) { + let nt = consume(); + if (['if', 'while', 'for', 'do', 'function'].includes(nt.value)) depth++; + if (nt.value === 'end') depth--; + } + let bodyTokens = tokens.slice(start, ti - 1); + let bodyCode = bodyTokens.map(t => t.type === 'string' ? `"${t.value}"` : t.value).join(' '); + bc.push(ops.CLOSURE, reg, getC(bodyCode)); + }; + + const parseBlock = () => { + locals.push({}); + while (ti < tokens.length) { + let t = peek(); + if (!t || ['end', 'else', 'elseif', 'until'].includes(t.value)) break; + parseStatement(); + } + locals.pop(); + }; + + const parseStatement = () => { + let t = peek(); + if (!t) return; + + if (t.value === 'local') { + consume(); + if (peek()?.value === 'function') { + consume(); let name = consume().value; + locals[locals.length-1][name] = nextReg++; + parseClosure(locals[locals.length-1][name]); + } else { + let name = consume()?.value; + if (!name) return; + let reg = nextReg++; + locals[locals.length-1][name] = reg; + if (peek()?.value === '=') { consume(); parseExpression(reg); } + } + } else if (t.value === 'function') { + consume(); let name = consume().value; + let reg = nextReg++; + parseClosure(reg); + bc.push(ops.SETG, reg, getC(name)); + } else if (['if', 'while', 'for', 'do'].includes(t.value)) { + consume(); + parseBlock(); + if (peek()?.value === 'end') consume(); + } else if (t.type === 'identifier' && peek(1)?.value === '=') { + let vn = consume().value; consume(); + let tempReg = nextReg; + parseExpression(tempReg); + let lReg = getLocal(vn); + if (lReg !== undefined) bc.push(ops.MOVE, lReg, tempReg); + else bc.push(ops.SETG, tempReg, getC(vn)); + } else if (['return', 'break', 'continue'].includes(t.value)) { + consume(); + } else { + parseExpression(nextReg); + if (t === peek()) consume(); + } + if (peek()?.value === ';') consume(); }; while (ti < tokens.length) { - let t = peek(); - if (t.value === 'local') { - consume(); - let name = consume().value; - if (peek()?.value === '=') { - consume(); parseExpr(nextLocal); - locals[name] = nextLocal++; - } else { - locals[name] = nextLocal++; - } - } else if (t.type === 'identifier' && tokens[ti+1]?.value === '=') { - let vn = consume().value; consume(); - parseExpr(nextLocal); - if (locals[vn] !== undefined) bc.push(ops.SETLOCAL, locals[vn], nextLocal); - else bc.push(ops.SETG, nextLocal, getC(vn)); - } else if (t.type === 'identifier' && (tokens[ti+1]?.value === '(' || tokens[ti+1]?.value === ':')) { - parseExpr(nextLocal); - } else { - ti++; - } + parseStatement(); } + bc.push(ops.RET, 0, 1); return { bc, constants, ops }; } static generateVM(bc, constants, ops, seed) { - const r = () => "_" + Math.random().toString(36).substring(7) + "_" + Math.floor(Math.random() * 10000); - const nL = r(), nC = r(), nR = r(), nPC = r(), nK = r(), nD = r(), nDS = r(), nLOC = r(); - const nENV = r(), nTIME = r(), nX = r(), nY = r(); - + const r = () => "_" + crypto.randomBytes(3).toString('hex'); + const nL = r(), nC = r(), nR = r(), nPC = r(), nD = r(), nDS = r(), nENV = r(); + const encC = constants.map(c => { if (c.type === 'string') { const key = seed & 0xFF; @@ -200,86 +330,59 @@ class ObfuscatorService { return `{t=1,v="${e}"}`; } if (c.type === 'number') { - const o = Math.floor(Math.random() * 1000); - return `{t=2,v=${(c.value+o)^(seed&0xFFFF)},o=${o}}`; + const o = Math.floor(Math.random() * 10000); + return `{t=2,v=${(c.value + o) ^ (seed & 0xFFFF)},o=${o}}`; } - return `{t=0}`; + if (c.value === true) return `{t=3}`; + if (c.value === false) return `{t=4}`; + return `{t=5}`; }).join(','); - const xorKey = seed % 255; - - const antiTamper = ` - local ${nENV} = getfenv and getfenv() or _G - local ${nTIME} = tick() - local function check() - if ${nENV}.debug and ${nENV}.debug.getinfo then - local i = ${nENV}.debug.getinfo(check) - if i.what ~= "Lua" then while true do end end - end - local s = tick() - for i=1,1000 do end - if tick() - s > 1 then while true do end end - end - check() - task.spawn(function() - while true do - if tick() - ${nTIME} > 4 then - local ${nX} = nil; ${nX}.crash() - end - ${nTIME} = tick() - task.wait(3) - end - end) - `; - + const xorKey = (seed ^ 0xAA) & 0xFF; const d = []; d[ops.LOADK] = `function() ${nR}[${nL}[${nPC}]] = ${nD}(${nL}[${nPC}+1]); ${nPC}=${nPC}+2 end`; - d[ops.GETG] = `function() ${nR}[${nL}[${nPC}]] = _G[${nD}(${nL}[${nPC}+1])]; ${nPC}=${nPC}+2 end`; - d[ops.SETG] = `function() _G[${nD}(${nL}[${nPC}+1])] = ${nR}[${nL}[${nPC}]]; ${nPC}=${nPC}+2 end`; + d[ops.GETG] = `function() ${nR}[${nL}[${nPC}]] = ${nENV}[${nD}(${nL}[${nPC}+1])]; ${nPC}=${nPC}+2 end`; + d[ops.SETG] = `function() ${nENV}[${nD}(${nL}[${nPC}+1])] = ${nR}[${nL}[${nPC}]]; ${nPC}=${nPC}+2 end`; d[ops.GETT] = `function() local r,t,k = ${nL}[${nPC}],${nL}[${nPC}+1],${nL}[${nPC}+2]; ${nPC}=${nPC}+3; ${nR}[r] = ${nR}[t][${nD}(k)] end`; - d[ops.GETS] = `function() local r,t,k = ${nL}[${nPC}],${nL}[${nPC}+1],${nL}[${nPC}+2]; ${nPC}=${nPC}+3; ${nR}[r+1] = ${nR}[t]; ${nR}[r] = ${nR}[t][${nD}(k)] end`; - d[ops.CALL] = `function() local r,na,nr = ${nL}[${nPC}],${nL}[${nPC}+1],${nL}[${nPC}+2]; ${nPC}=${nPC}+3; local a={}; for i=1,na-1 do a[i]=${nR}[r+i] end; ${nR}[r]=${nR}[r](table.unpack(a)) end`; + d[ops.GETS] = `function() local r,t,k = ${nL}[${nPC}],${nL}[${nPC}+1],${nL}[${nPC}+2]; ${nPC}=${nPC}+3; local o = ${nR}[t]; ${nR}[r+1] = o; ${nR}[r] = o[${nD}(k)] end`; + d[ops.CALL] = `function() local r,na,nr = ${nL}[${nPC}],${nL}[${nPC}+1],${nL}[${nPC}+2]; ${nPC}=${nPC}+3; local a={}; for i=1,na-1 do a[i]=${nR}[r+i] end; local f = ${nR}[r]; if not f then error("Luartex VM Error: Call to nil at PC "..${nPC}) end; local res = {f(table.unpack(a))}; for i=1,nr do ${nR}[r+i-1] = res[i] end end`; d[ops.RET] = `function() return "EXIT" end`; d[ops.NEWTABLE] = `function() ${nR}[${nL}[${nPC}]] = {}; ${nPC}=${nPC}+1 end`; - d[ops.SETTABLE] = `function() local t,k,v = ${nL}[${nPC}],${nL}[${nPC}+1],${nL}[${nPC}+2]; ${nPC}=${nPC}+3; ${nR}[t][${nD}(k)] = ${nR}[v] end`; - d[ops.GETLOCAL] = `function() ${nR}[${nL}[${nPC}]] = ${nLOC}[${nL}[${nPC}+1]+1]; ${nPC}=${nPC}+2 end`; - d[ops.SETLOCAL] = `function() ${nLOC}[${nL}[${nPC}]+1] = ${nR}[${nL}[${nPC}+1]]; ${nPC}=${nPC}+2 end`; - - const argCounts = { - [ops.LOADK]: 2, [ops.GETG]: 2, [ops.SETG]: 2, [ops.GETT]: 3, - [ops.GETS]: 3, [ops.CALL]: 3, [ops.RET]: 2, [ops.NEWTABLE]: 1, - [ops.SETTABLE]: 3, [ops.GETLOCAL]: 2, [ops.SETLOCAL]: 2 - }; + d[ops.SETTABLE] = `function() local t,k,v = ${nL}[${nPC}],${nL}[${nPC}+1],${nL}[${nPC}+2]; ${nPC}=${nPC}+3; if k == -1 then ${nR}[t][${nR}[${nL}[${nPC}-2]]] = ${nR}[v] else ${nR}[t][${nD}(k)] = ${nR}[v] end end`; + d[ops.MOVE] = `function() ${nR}[${nL}[${nPC}]] = ${nR}[${nL}[${nPC}+1]]; ${nPC}=${nPC}+2 end`; + d[ops.ADD] = `function() ${nR}[${nL}[${nPC}]] = ${nR}[${nL}[${nPC}+1]] + ${nR}[${nL}[${nPC}+2]]; ${nPC}=${nPC}+3 end`; + d[ops.SUB] = `function() ${nR}[${nL}[${nPC}]] = ${nR}[${nL}[${nPC}+1]] - ${nR}[${nL}[${nPC}+2]]; ${nPC}=${nPC}+3 end`; + d[ops.MUL] = `function() ${nR}[${nL}[${nPC}]] = ${nR}[${nL}[${nPC}+1]] * ${nR}[${nL}[${nPC}+2]]; ${nPC}=${nPC}+3 end`; + d[ops.DIV] = `function() ${nR}[${nL}[${nPC}]] = ${nR}[${nL}[${nPC}+1]] / ${nR}[${nL}[${nPC}+2]]; ${nPC}=${nPC}+3 end`; + d[ops.CONCAT] = `function() ${nR}[${nL}[${nPC}]] = tostring(${nR}[${nL}[${nPC}+1]]) .. tostring(${nR}[${nL}[${nPC}+2]]); ${nPC}=${nPC}+3 end`; + d[ops.EQ] = `function() ${nR}[${nL}[${nPC}]] = (${nR}[${nL}[${nPC}+1]] == ${nR}[${nL}[${nPC}+2]]); ${nPC}=${nPC}+3 end`; + d[ops.LT] = `function() ${nR}[${nL}[${nPC}]] = (${nR}[${nL}[${nPC}+1]] < ${nR}[${nL}[${nPC}+2]]); ${nPC}=${nPC}+3 end`; + d[ops.LE] = `function() ${nR}[${nL}[${nPC}]] = (${nR}[${nL}[${nPC}+1]] <= ${nR}[${nL}[${nPC}+2]]); ${nPC}=${nPC}+3 end`; + d[ops.CLOSURE] = `function() local r,k = ${nL}[${nPC}],${nL}[${nPC}+1]; ${nPC}=${nPC}+2; ${nR}[r] = loadstring(${nD}(k)) end`; const shuffled = Object.keys(ops).sort(() => Math.random() - 0.5); - const opMap = {}; - shuffled.forEach((k, i) => opMap[ops[k]] = i); + const opMap = {}; shuffled.forEach((k, i) => opMap[ops[k]] = i); + const argCounts = { [ops.LOADK]: 2, [ops.GETG]: 2, [ops.SETG]: 2, [ops.GETT]: 3, [ops.GETS]: 3, [ops.CALL]: 3, [ops.RET]: 2, [ops.NEWTABLE]: 1, [ops.SETTABLE]: 3, [ops.MOVE]: 2, [ops.ADD]: 3, [ops.SUB]: 3, [ops.MUL]: 3, [ops.DIV]: 3, [ops.CONCAT]: 3, [ops.EQ]: 3, [ops.LT]: 3, [ops.LE]: 3, [ops.CLOSURE]: 2 }; const finalBC = []; for (let i = 0; i < bc.length; ) { - const op = bc[i]; - finalBC.push(opMap[op]); - i++; + const op = bc[i]; finalBC.push(opMap[op]); i++; const count = argCounts[op] || 0; - for (let j = 0; j < count; j++) { - finalBC.push(bc[i] || 0); - i++; - } + for (let j = 0; j < count; j++) { finalBC.push(bc[i] || 0); i++; } } - const dispatchFunctions = shuffled.map(k => d[ops[k]] || "function() end").join(','); - + const dispatchFunctions = shuffled.map(k => d[ops[k]]).join(',\n'); const vmCode = `local function VM(...) - ${antiTamper} + local ${nENV} = (getfenv and getfenv()) or _G local ${nL} = {${finalBC.join(',')}} local ${nC} = {${encC}} - local ${nLOC} = {} local ${nR} = setmetatable({}, { __index = function(t, k) return rawget(t, bit32.bxor(k, ${xorKey})) end, __newindex = function(t, k, v) rawset(t, bit32.bxor(k, ${xorKey}), v) end }) - local ${nPC} = ${this.numberToExpression(1)}; local ${nK} = ${this.numberToExpression(seed)} + local ${nPC} = 1 local function ${nD}(i) + if not i or i == -1 then return nil end local c = ${nC}[i+1]; if not c then return end if c.t == 1 then local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -287,44 +390,49 @@ class ObfuscatorService { for j=1,#s,4 do local v = 0 for k=0,3 do local char = s:sub(j+k,j+k); v = v*64 + (char=="=" and 0 or (b:find(char,1,true)-1)) end - for k=2,0,-1 do if j+3-k<=#s and s:sub(j+3-k,j+3-k)~="=" then res = res..string.char(bit32.bxor(bit32.extract(v,k*8,8),bit32.band(${nK},255))) end end + for k=2,0,-1 do if j+3-k<=#s and s:sub(j+3-k,j+3-k)~="=" then res = res..string.char(bit32.bxor(bit32.extract(v,k*8,8),bit32.band(${seed},255))) end end end return res - elseif c.t == 2 then return bit32.bxor(c.v, bit32.band(${nK}, 65535)) - c.o end + elseif c.t == 2 then return bit32.bxor(c.v, bit32.band(${seed}, 65535)) - c.o + elseif c.t == 3 then return true elseif c.t == 4 then return false elseif c.t == 5 then return nil end end local ${nDS} = {${dispatchFunctions}} - while true do + -- Anti-Tamper / Opaque Predicate + local _p = function() return tick() > 0 end + while _p() do local op = ${nL}[${nPC}]; if not op then break end ${nPC} = ${nPC} + 1 local f = ${nDS}[op+1] if f then if f() == "EXIT" then break end else break end end - end; VM(...)`; + end; return xpcall(VM, function(e) warn("Luartex VM Error: "..tostring(e)) end, ...)`; - const wrapLayers = (code, layers) => { - let current = code; - for (let i = 0; i < layers; i++) { - const layerSeed = crypto.randomBytes(4).readUInt32BE(0); - const encoded = Buffer.from(current).map(b => b ^ (layerSeed & 0xFF)).toString('base64'); - current = `local _v = "${encoded}" - local _s = ${this.numberToExpression(layerSeed)} - local function _d(s) - local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - local res = "" - for j=1,#s,4 do - local v = 0 - for k=0,3 do local char = s:sub(j+k,j+k); v = v*64 + (char=="=" and 0 or (b:find(char,1,true)-1)) end - for k=2,0,-1 do if j+3-k<=#s and s:sub(j+3-k,j+3-k)~="=" then res = res..string.char(bit32.bxor(bit32.extract(v,k*8,8),bit32.band(_s,255))) end end - end - return res - end - local _f = loadstring(_d(_v)) - if _f then _f() end`; - } - return current; - }; + return this.wrapLayers(vmCode, 3, seed); + } - return wrapLayers(vmCode, 2); + static wrapLayers(code, layers, seed) { + let current = code; + for (let i = 0; i < layers; i++) { + const key = (seed >> (i * 8)) & 0xFF; + const encoded = Buffer.from(current).map(b => b ^ key).toString('base64'); + const sExp = this.numberToExpression(seed); + current = `local _v = "${encoded}" + local _s = ${sExp} + local function _d(s) + local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + local res = "" + for j=1,#s,4 do + local v = 0 + for k=0,3 do local char = s:sub(j+k,j+k); v = v*64 + (char=="=" and 0 or (b:find(char,1,true)-1)) end + for k=2,0,-1 do if j+3-k<=#s and s:sub(j+3-k,j+3-k)~="=" then res = res..string.char(bit32.bxor(bit32.extract(v,k*8,8),bit32.band(_s,255))) end end + end + return res + end + local _f, _e = loadstring(_d(_v)) + if _f then return _f() else error("Luartex Layer Error: "..tostring(_e)) end`; + } + return current; } } + module.exports = ObfuscatorService; \ No newline at end of file