410 lines
16 KiB
PHP
410 lines
16 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if (!$input) {
|
|
$input = json_decode(file_get_contents('php://stdin'), true);
|
|
}
|
|
|
|
if (!$input || empty($input['code'])) {
|
|
echo json_encode(['success' => false, 'error' => 'No code provided']);
|
|
exit;
|
|
}
|
|
|
|
$code = $input['code'];
|
|
|
|
class LuartexHyperionV6_2 {
|
|
private $rawCode;
|
|
private $constants = [];
|
|
private $instructions = [];
|
|
private $keys = [];
|
|
private $opMap = [];
|
|
private $vm_id;
|
|
|
|
public function __construct($code) {
|
|
$this->rawCode = $code;
|
|
$this->vm_id = bin2hex(random_bytes(4));
|
|
for ($i = 0; $i < 32; $i++) {
|
|
$this->keys[] = rand(0, 255);
|
|
}
|
|
$this->setupOpcodes();
|
|
}
|
|
|
|
private function setupOpcodes() {
|
|
$ops = [
|
|
'LOADK', 'GETGLOBAL', 'SETGLOBAL', 'CALL', 'MOVE',
|
|
'ADD', 'SUB', 'MUL', 'DIV', 'MOD', 'POW',
|
|
'JMP', 'EQ', 'LT', 'LE', 'RETURN', 'GETTABLE', 'SETTABLE', 'NEWTABLE',
|
|
'CLOSURE', 'VARARG', 'FORPREP', 'FORLOOP', 'AND', 'OR', 'NOT', 'LEN',
|
|
'TFORLOOP', 'SETLIST', 'CLOSE', 'JUNK1', 'JUNK2', 'JUNK3', 'OPAQUE',
|
|
'SET_GLOBAL_CONST', 'MOVE_GLOBAL', 'CALL_GLOBAL_K', 'CALL_GLOBAL_V',
|
|
'RECURSIVE_CALL', 'TAMPER_CHECK', 'LAYER_ENTER', 'STACK_SHUFFLE'
|
|
];
|
|
shuffle($ops);
|
|
foreach ($ops as $op) {
|
|
$this->opMap[$op] = rand(1000, 99999);
|
|
}
|
|
}
|
|
|
|
private function genVar($len = 24) {
|
|
$sets = [
|
|
'l1Ii', 'O0Q', 'uvvw', 'nmM', 'il1I', 'oO0Q',
|
|
'S5s', 'Z2z', 'B8b', 'g9q', '01OI'
|
|
];
|
|
$res = '_';
|
|
for($i=0; $i<$len; $i++) {
|
|
$set = $sets[array_rand($sets)];
|
|
$res .= $set[rand(0, strlen($set)-1)];
|
|
}
|
|
return $res . bin2hex(random_bytes(3));
|
|
}
|
|
|
|
private function numToLuaExpr($n, $depth = 0) {
|
|
if (!is_numeric($n)) return $n;
|
|
$n = (float)$n;
|
|
|
|
if ($depth > 3) return $n;
|
|
|
|
$ops = ['+', '-', '*', 'xor', 'math'];
|
|
$op = $ops[array_rand($ops)];
|
|
|
|
switch($op) {
|
|
case '+':
|
|
$v1 = rand(1, 10000) / 10;
|
|
$v2 = $n - $v1;
|
|
return "(" . $this->numToLuaExpr($v1, $depth + 1) . " + " . $this->numToLuaExpr($v2, $depth + 1) . ")";
|
|
case '-':
|
|
$v1 = rand((int)$n + 1, (int)$n + 10000) / 10;
|
|
$v2 = $v1 - $n;
|
|
return "(" . $this->numToLuaExpr($v1, $depth + 1) . " - " . $this->numToLuaExpr($v2, $depth + 1) . ")";
|
|
case '*':
|
|
if ($n == 0) return "(0 * " . rand(1, 100) . ")";
|
|
for ($v1 = 2; $v1 <= 8; $v1++) {
|
|
if ($n != 0 && fmod($n, $v1) == 0) {
|
|
$v2 = $n / $v1;
|
|
return "(" . $this->numToLuaExpr($v1, $depth + 1) . " * " . $this->numToLuaExpr($v2, $depth + 1) . ")";
|
|
}
|
|
}
|
|
return "(" . $this->numToLuaExpr($n - 1, $depth + 1) . " + 1)";
|
|
case 'xor':
|
|
$v1 = rand(1, 65535);
|
|
$v2 = (int)$n ^ $v1;
|
|
return "bit32.bxor(" . $this->numToLuaExpr($v1, $depth + 1) . ", " . $this->numToLuaExpr($v2, $depth + 1) . ")";
|
|
case 'math':
|
|
return "(math.abs(" . $this->numToLuaExpr($n, $depth + 1) . "))";
|
|
}
|
|
return $n;
|
|
}
|
|
|
|
private function addConst($val) {
|
|
$idx = array_search($val, $this->constants);
|
|
if ($idx === false) {
|
|
$this->constants[] = $val;
|
|
$idx = count($this->constants) - 1;
|
|
}
|
|
return $idx;
|
|
}
|
|
|
|
private function genOpaquePredicate($type = 'true') {
|
|
$truePreds = [
|
|
"(math.pi > 3)",
|
|
"(type(math.abs) == 'function')",
|
|
"(math.floor(10.5) == 10)",
|
|
"(#('Hyperion') == 8)",
|
|
"(math.sin(0) == 0)",
|
|
"(not (1 == 0))",
|
|
"(math.abs(-1) == 1)",
|
|
"(math.ceil(5.1) == 6)",
|
|
"(bit32.bxor(123, 123) == 0)",
|
|
"(string.len('luartex') == 7)",
|
|
"(math.log10(100) == 2)"
|
|
];
|
|
$falsePreds = [
|
|
"(math.pi < 3)",
|
|
"(type(math.abs) == 'string')",
|
|
"(math.floor(10.5) == 11)",
|
|
"(#('Hyperion') == 9)",
|
|
"(math.sin(0) == 1)",
|
|
"(1 == 0)",
|
|
"(math.abs(-1) == 0)",
|
|
"(bit32.bxor(123, 123) ~= 0)",
|
|
"(math.sqrt(16) == 5)"
|
|
];
|
|
|
|
if ($type === 'true') {
|
|
return $truePreds[array_rand($truePreds)];
|
|
} else {
|
|
return $falsePreds[array_rand($falsePreds)];
|
|
}
|
|
}
|
|
|
|
private function foldInstructions() {
|
|
$folded = [];
|
|
$i = 0;
|
|
$count = count($this->instructions);
|
|
|
|
while ($i < $count) {
|
|
$inst = $this->instructions[$i];
|
|
|
|
if ($i + 2 < $count) {
|
|
$n1 = $this->instructions[$i + 1];
|
|
$n2 = $this->instructions[$i + 2];
|
|
|
|
if ($inst[0] === $this->opMap['GETGLOBAL'] && $inst[1] === 0 &&
|
|
$n1[0] === $this->opMap['LOADK'] && $n1[1] === 1 &&
|
|
$n2[0] === $this->opMap['CALL'] && $n2[1] === 0 && $n2[2] === 1) {
|
|
$folded[] = [$this->opMap['CALL_GLOBAL_K'], $inst[2], $n1[2]];
|
|
$i += 3;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
$folded[] = $inst;
|
|
$i++;
|
|
}
|
|
$this->instructions = $folded;
|
|
}
|
|
|
|
private function compile() {
|
|
$this->addConst("Hyperion V6.2 - Multi-Layer Recursive Virtualization");
|
|
|
|
$cleanCode = $this->rawCode;
|
|
$cleanCode = preg_replace('/--[[]*.*?[]]*--/s', '', $cleanCode);
|
|
$cleanCode = preg_replace('/--.*$/m', '', $cleanCode);
|
|
|
|
$tokens = preg_split('/[;\n]+/', $cleanCode);
|
|
|
|
foreach ($tokens as $token) {
|
|
$token = trim($token);
|
|
if (empty($token)) continue;
|
|
|
|
if (preg_match('/^([a-zA-Z_]\w*(?:[.:]\w*)*)\s*\((.*?)\)$/', $token, $m)) {
|
|
$this->emitCall($m[1], $m[2]);
|
|
}
|
|
elseif (preg_match('/^(?:local\s+)?([a-zA-Z_]\w*)\s*=\s*(.*)$/', $token, $m)) {
|
|
$this->emitAssignment($m[1], $m[2]);
|
|
}
|
|
}
|
|
|
|
$this->foldInstructions();
|
|
$this->instructions[] = [$this->opMap['RETURN'], 0, 0];
|
|
}
|
|
|
|
private function emitCall($funcName, $argStr) {
|
|
$fIdx = $this->addConst($funcName);
|
|
$this->instructions[] = [$this->opMap['GETGLOBAL'], 0, $fIdx];
|
|
|
|
$args = [];
|
|
if (!empty(trim($argStr))) {
|
|
$rawArgs = explode(',', $argStr);
|
|
foreach ($rawArgs as $idx => $arg) {
|
|
$arg = trim($arg);
|
|
$rIdx = $idx + 1;
|
|
if (preg_match('/^["\'](.*)["\']$/s', $arg, $m)) {
|
|
$vIdx = $this->addConst($m[1]);
|
|
$this->instructions[] = [$this->opMap['LOADK'], $rIdx, $vIdx];
|
|
} elseif (is_numeric($arg)) {
|
|
$vIdx = $this->addConst((float)$arg);
|
|
$this->instructions[] = [$this->opMap['LOADK'], $rIdx, $vIdx];
|
|
} else {
|
|
$vIdx = $this->addConst($arg);
|
|
$this->instructions[] = [$this->opMap['GETGLOBAL'], $rIdx, $vIdx];
|
|
}
|
|
$args[] = $rIdx;
|
|
}
|
|
}
|
|
$this->instructions[] = [$this->opMap['CALL'], 0, count($args)];
|
|
}
|
|
|
|
private function emitAssignment($var, $val) {
|
|
$val = trim($val);
|
|
if (preg_match('/^["\'](.*)["\']$/s', $val, $m)) {
|
|
$vIdx = $this->addConst($m[1]);
|
|
$this->instructions[] = [$this->opMap['LOADK'], 0, $vIdx];
|
|
} elseif (is_numeric($val)) {
|
|
$vIdx = $this->addConst((float)$val);
|
|
$this->instructions[] = [$this->opMap['LOADK'], 0, $vIdx];
|
|
} else {
|
|
$vIdx = $this->addConst($val);
|
|
$this->instructions[] = [$this->opMap['GETGLOBAL'], 0, $vIdx];
|
|
}
|
|
|
|
$sIdx = $this->addConst($var);
|
|
$this->instructions[] = [$this->opMap['SETGLOBAL'], 0, $sIdx];
|
|
}
|
|
|
|
private function serializeAll() {
|
|
$bin = "";
|
|
$bin .= pack("N", count($this->instructions));
|
|
foreach ($this->instructions as $inst) {
|
|
$op = (int)$inst[0];
|
|
$bin .= chr(($op >> 16) & 0xFF) . chr(($op >> 8) & 0xFF) . chr($op & 0xFF);
|
|
$bin .= pack("n", (int)$inst[1]);
|
|
$bin .= pack("n", (int)$inst[2]);
|
|
}
|
|
|
|
$bin .= pack("N", count($this->constants));
|
|
foreach ($this->constants as $c) {
|
|
if (is_string($c)) {
|
|
$bin .= chr(1);
|
|
$bin .= pack("N", strlen($c));
|
|
$bin .= $c;
|
|
} elseif (is_numeric($c)) {
|
|
$bin .= chr(2);
|
|
$sNum = (string)$c;
|
|
$bin .= pack("N", strlen($sNum));
|
|
$bin .= $sNum;
|
|
} else {
|
|
$bin .= chr(0);
|
|
}
|
|
}
|
|
|
|
$keyLen = count($this->keys);
|
|
$enc = "";
|
|
for ($i = 0; $i < strlen($bin); $i++) {
|
|
$enc .= chr(ord($bin[$i]) ^ $this->keys[$i % $keyLen] ^ (($i * 13) % 256));
|
|
}
|
|
|
|
return bin2hex($enc);
|
|
}
|
|
|
|
private function calculateChecksum($hex) {
|
|
$sum = 0;
|
|
for ($i = 0; $i < strlen($hex); $i++) {
|
|
$sum = ($sum + ord($hex[$i]) * ($i + 1)) % 4294967296;
|
|
}
|
|
return $sum;
|
|
}
|
|
|
|
public function build() {
|
|
$this->compile();
|
|
|
|
$k_v = $this->genVar(32); $b_v = $this->genVar(32); $e_v = $this->genVar(32);
|
|
$f_v = $this->genVar(32); $d_v = $this->genVar(32); $c_v = $this->genVar(32);
|
|
$cs_v = $this->genVar(32); $tm_v = $this->genVar(32);
|
|
$v_v = $this->genVar(32); $l2_v = $this->genVar(32);
|
|
$layers_v = $this->genVar(32);
|
|
$stack_v = $this->genVar(32);
|
|
|
|
$k_str = "";
|
|
foreach ($this->keys as $i => $k) {
|
|
$k_str .= ($i > 0 ? "," : "") . $this->numToLuaExpr($k);
|
|
}
|
|
|
|
$allHex = $this->serializeAll();
|
|
$checksum = $this->calculateChecksum($allHex);
|
|
|
|
$lua = "-- [[ Hyperion Engine V6.2 - Recursive Layered Architecture ]] --\n";
|
|
|
|
$lua .= "local function " . $tm_v . "() ";
|
|
$lua .= "local _G = (getgenv and getgenv()) or _G; ";
|
|
$lua .= "if debug and (debug.getinfo or debug.setupvalue) then return true end; ";
|
|
$lua .= "if getfenv and (type(getfenv) ~= 'function') then return true end; ";
|
|
$lua .= "local check = {string.char, table.concat, tonumber, pcall, bit32.bxor, math.abs}; ";
|
|
$lua .= "for i=1,#check do if type(check[i]) ~= 'function' then return true end end; ";
|
|
$lua .= "local s, e = pcall(function() return os.clock() end); if not s then return true end; ";
|
|
$lua .= "return false end; ";
|
|
$lua .= "if " . $tm_v . "() then while true do end end; ";
|
|
|
|
$lua .= "local " . $k_v . " = { " . $k_str . " }; ";
|
|
$lua .= "local " . $b_v . " = \"" . $allHex . "\"; ";
|
|
$lua .= "local " . $e_v . " = (getgenv and getgenv()) or (getfenv and getfenv(0)) or _G; ";
|
|
|
|
$lua .= "local function " . $cs_v . "(s) local h = 0; for i = 1, #s do h = (h + string.byte(s, i) * i) % " . $this->numToLuaExpr(4294967296) . " end return h end; ";
|
|
$lua .= "if " . $cs_v . "(" . $b_v . ") ~= " . $this->numToLuaExpr($checksum) . " then while true do end end; ";
|
|
|
|
$lua .= "local function _H(h) local b = {}; for i = 1, #h, 2 do b[#b+1] = tonumber(h:sub(i, i+1), 16) end return b end; ";
|
|
$lua .= "local function _D(b) local o = {}; for i = 1, #b do local k = ((i - 1) % 32) + 1; o[i] = bit32.bxor(b[i], " . $k_v . "[k], ((i - 1) * 13) % 256) end return o end; ";
|
|
$lua .= "local " . $d_v . " = _D(_H(" . $b_v . ")); ";
|
|
|
|
$lua .= "local function _R32(b, p) return b[p]*16777216 + b[p+1]*65536 + b[p+2]*256 + b[p+3] end; ";
|
|
$lua .= "local function _R16(b, p) return b[p]*256 + b[p+1] end; ";
|
|
|
|
$lua .= "local ic = _R32(" . $d_v . ", 1); ";
|
|
$lua .= "local co = 5 + ic * 7; ";
|
|
$lua .= "local cc = _R32(" . $d_v . ", co); ";
|
|
|
|
$lua .= "local " . $c_v . " = {}; ";
|
|
$lua .= "local cu = co + 4; ";
|
|
$lua .= "for i = 1, cc do ";
|
|
$lua .= "local t = " . $d_v . "[cu]; cu = cu + 1; ";
|
|
$lua .= "if t == 1 or t == 2 then ";
|
|
$lua .= "local l = _R32(" . $d_v . ", cu); cu = cu + 4; ";
|
|
$lua .= "local s = {}; for j = 1, l do s[j] = string.char(" . $d_v . "[cu]); cu = cu + 1 end; ";
|
|
$lua .= "s = table.concat(s); if t == 2 then " . $c_v . "[i] = tonumber(s) else " . $c_v . "[i] = s end; ";
|
|
$lua .= "else cu = cu + 1 end end; ";
|
|
|
|
$lua .= "local function " . $f_v . "(p) local o = 5 + (p - 1) * 7; ";
|
|
$lua .= "return " . $d_v . "[o]*65536 + " . $d_v . "[o+1]*256 + " . $d_v . "[o+2], _R16(" . $d_v . ", o+3), _R16(" . $d_v . ", o+5) end; ";
|
|
|
|
$lua .= "local " . $v_v . "; " . $v_v . " = function(spc, layer) ";
|
|
$lua .= "local " . $stack_v . " = {}; local pc = spc or 1; local cur_layer = layer or 1; ";
|
|
$lua .= "while true do local op, a, b = " . $f_v . "(pc); ";
|
|
|
|
$cases = [];
|
|
$op_getglobal = $this->opMap['GETGLOBAL'];
|
|
$cases[] = "elseif op == $op_getglobal then if " . $this->genOpaquePredicate('true') . " then local n = " . $c_v . "[b + 1]; local t = " . $e_v . "; for p in n:gmatch('[^.:]+') do t = t[p] end; " . $stack_v . "[a] = t; else pc = pc + " . rand(100, 1000) . " end ";
|
|
|
|
$op_loadk = $this->opMap['LOADK'];
|
|
$cases[] = "elseif op == $op_loadk then " . $stack_v . "[a] = " . $c_v . "[b + 1] ";
|
|
|
|
$op_call = $this->opMap['CALL'];
|
|
$cases[] = "elseif op == $op_call then local f = " . $stack_v . "[a]; local args = {}; for m = 1, b do args[m] = " . $stack_v . "[a + m] end; if f == " . $v_v . " then " . $v_v . "(" . $stack_v . "[a+1], cur_layer + 1) else f(unpack(args)) end ";
|
|
|
|
$op_setglobal = $this->opMap['SETGLOBAL'];
|
|
$cases[] = "elseif op == $op_setglobal then local n = " . $c_v . "[b + 1]; " . $e_v . "[n] = " . $stack_v . "[a] ";
|
|
|
|
$op_return = $this->opMap['RETURN'];
|
|
$cases[] = "elseif op == $op_return then return ";
|
|
|
|
$op_opaque = $this->opMap['OPAQUE'];
|
|
$cases[] = "elseif op == $op_opaque then if " . $tm_v . "() then while true do end else " . $stack_v . "[0] = math.sqrt(a*b) end ";
|
|
|
|
$op_recursive_call = $this->opMap['RECURSIVE_CALL'];
|
|
$cases[] = "elseif op == $op_recursive_call then " . $v_v . "(a, cur_layer + 1) ";
|
|
|
|
$op_tamper_check = $this->opMap['TAMPER_CHECK'];
|
|
$cases[] = "elseif op == $op_tamper_check then if " . $tm_v . "() then while true do end end ";
|
|
|
|
$op_layer_enter = $this->opMap['LAYER_ENTER'];
|
|
$cases[] = "elseif op == $op_layer_enter then if cur_layer < 10 then " . $v_v . "(pc + 1, cur_layer + 1) return end ";
|
|
|
|
$op_call_gk = $this->opMap['CALL_GLOBAL_K'];
|
|
$cases[] = "elseif op == $op_call_gk then local n = " . $c_v . "[a + 1]; local f = " . $e_v . "; for p in n:gmatch('[^.:]+') do f = f[p] end; f(" . $c_v . "[b + 1]) ";
|
|
|
|
$op_stack_shuffle = $this->opMap['STACK_SHUFFLE'];
|
|
$cases[] = "elseif op == $op_stack_shuffle then local t = " . $stack_v . "[a]; " . $stack_v . "[a] = " . $stack_v . "[b]; " . $stack_v . "[b] = t; ";
|
|
|
|
shuffle($cases);
|
|
|
|
$lua .= "local function " . $layers_v . "(op, a, b, pc) ";
|
|
$lua .= "if false then elseif op == -1 then return ";
|
|
$lua .= implode(" ", $cases);
|
|
$lua .= " end end; ";
|
|
|
|
$lua .= "local s, e = pcall(" . $layers_v . ", op, a, b, pc); ";
|
|
$lua .= "if not s then if " . $tm_v . "() then while true do end else error(e) end end; ";
|
|
$lua .= "pc = pc + 1; end end; ";
|
|
|
|
$lua .= "pcall(" . $v_v . ", 1, 1); ";
|
|
|
|
return [
|
|
'success' => true,
|
|
'protected_code' => $lua,
|
|
'stats' => [
|
|
'original_size' => strlen($this->rawCode),
|
|
'protected_size' => strlen($lua),
|
|
'vm_version' => '6.2-recursive-layered'
|
|
]
|
|
];
|
|
}
|
|
}
|
|
|
|
try {
|
|
$vm = new LuartexHyperionV6_2($code);
|
|
echo json_encode($vm->build());
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|