internal static void Run(string path, Tasks task) { var bytes = File.ReadAllBytes(path); Console.Write(string.Format("[+] Checking {0}...", Path.GetFileName(path))); if (bytes[3] == 0x80) { Console.Write(" <Encrypted>\n"); if (task == Tasks.Encrypt) { Console.WriteLine(string.Format("[+] {0} is already encrypted... <Aborted>", Path.GetFileName(path))); return; } else if (task == Tasks.Decompile || task == Tasks.Recompile) { Console.WriteLine("[+] You cannot decompile an encrypted lua... <Aborted>"); return; } } else if (bytes[3] == 0x02) { Console.Write(" <Decrypted>\n"); if (task == Tasks.Decrypt) { Console.WriteLine(string.Format("[+] {0} is already decrypted... <Aborted>", Path.GetFileName(path))); return; } } else if (task != Tasks.Recompile) { Console.Write(" <Unknown>\n"); Console.WriteLine("[+] Not a valid or damaged lua file... <Aborted>"); return; } else { Console.Write(" <OK>\n"); } Console.Write($"[+] {(task == Tasks.Decrypt ? "Decrypting" : task == Tasks.Encrypt ? "Encrypting" : task == Tasks.Decompile ? "Decompiling" : "Recompiling")} {Path.GetFileName(path)}..."); var luaPath = Path.Combine(PathMgr.Environment(task == Tasks.Decrypt ? "Decrypted_lua" : task == Tasks.Encrypt ? "Encrypted_lua" : task == Tasks.Decompile ? "Decompiled_lua" : "Recompiled_lua"), Path.GetFileName(path)); if (File.Exists(luaPath)) { File.Delete(luaPath); } try { if (task == Tasks.Decrypt || task == Tasks.Encrypt) { using (var reader = new BinaryReader(new MemoryStream(bytes))) { var magic = reader.ReadBytes(3); var version = reader.ReadByte(); var bits = reader.ReadUleb128(); var is_stripped = ((bits & 2u) != 0u); if (!is_stripped) { var length = reader.ReadUleb128(); var name = Encoding.UTF8.GetString(reader.ReadBytes((int)length)); } while (reader.BaseStream.Position < reader.BaseStream.Length) { var size = reader.ReadUleb128(); if (size == 0) { break; } var next = reader.BaseStream.Position + size; bits = reader.ReadByte(); var arguments_count = reader.ReadByte(); var framesize = reader.ReadByte(); var upvalues_count = reader.ReadByte(); var complex_constants_count = reader.ReadUleb128(); var numeric_constants_count = reader.ReadUleb128(); var instructions_count = reader.ReadUleb128(); var start = (int)reader.BaseStream.Position; if (task == Tasks.Encrypt) { bytes[3] = 0x80; bytes = Lock(start, bytes, (int)instructions_count); } else { bytes[3] = 2; bytes = Unlock(start, bytes, (int)instructions_count); } reader.BaseStream.Position = next; } } File.WriteAllBytes(luaPath, bytes); } else if (task == Tasks.Decompile || task == Tasks.Recompile) { Utils.Command(task == Tasks.Decompile ? $"python main.py -f \"{path}\" -o \"{luaPath}\"" : $"luajit.exe -b \"{path}\" \"{luaPath}\""); } Program.isInvalid = false; } catch (Exception e) { Utils.ExceptionLogger($"Exception detected during {(task == Tasks.Decrypt ? "decrypting" : task == Tasks.Encrypt ? "encrypting" : task == Tasks.Decompile ? "decompiling" : "recompiling")} {Path.GetFileName(path)}", e); } finally { if (File.Exists(luaPath)) { SuccessCount++; Console.Write("<done>\n"); } else { Console.Write("<failed>\n"); FailedCount++; } } }
internal static void Initialize(string lua, Tasks tasks) { try { Console.Write($"[+] {(tasks == Tasks.Decrypt ? "Decrypting" : tasks == Tasks.Encrypt ? "Encrypting" : tasks == Tasks.Decompile ? "Decompiling" : "Recompiling")} {Path.GetFileName(lua)}..."); var luaPath = Path.Combine(PathMgr.Environment(tasks == Tasks.Decrypt ? "Decrypted_lua" : tasks == Tasks.Encrypt ? "Encrypted_lua" : tasks == Tasks.Decompile ? "Decompiled_lua" : "Recompiled_lua"), Path.GetFileName(lua)); if (tasks == Tasks.Decrypt || tasks == Tasks.Encrypt) { var bytes = File.ReadAllBytes(lua); var reader = new BinaryReader(new MemoryStream(bytes)); var magic = reader.ReadBytes(3); var version = reader.ReadByte(); var bits = reader.ReadUleb128(); var is_stripped = ((bits & 2u) != 0u); if (!is_stripped) { var length = reader.ReadUleb128(); var name = Encoding.UTF8.GetString(reader.ReadBytes((int)length)); } while (reader.BaseStream.Position < reader.BaseStream.Length) { var size = reader.ReadUleb128(); if (size == 0) { break; } var next = reader.BaseStream.Position + size; bits = reader.ReadByte(); var arguments_count = reader.ReadByte(); var framesize = reader.ReadByte(); var upvalues_count = reader.ReadByte(); var complex_constants_count = reader.ReadUleb128(); var numeric_constants_count = reader.ReadUleb128(); var instructions_count = reader.ReadUleb128(); var start = (int)reader.BaseStream.Position; start += 4; var v2 = 0; do { if (tasks == Tasks.Encrypt) { bytes[3] = 0x80; var v3 = bytes[start - 4]; start += 4; var v4 = bytes[start - 7] ^ v2++; bytes[start - 8] = (byte)(Properties.Resources.Lock[v3] ^ v4); } else { bytes[3] = 2; var v3 = bytes[start - 4]; start += 4; var v4 = bytes[start - 7] ^ v3 ^ (v2++ & 0xFF); bytes[start - 8] = Properties.Resources.Unlock[v4]; } }while (v2 != (int)instructions_count); reader.BaseStream.Position = next; } if (File.Exists(luaPath)) { File.Delete(luaPath); } File.WriteAllBytes(luaPath, bytes); } else if (tasks == Tasks.Decompile || tasks == Tasks.Recompile) { Utils.Command(tasks == Tasks.Decompile ? $"python main.py -f \"{lua}\" -o \"{luaPath}\"" : $"luajit.exe -b \"{lua}\" \"{luaPath}\""); } Console.Write("<done>"); Console.WriteLine(); } catch (Exception e) { Console.Write("<exception-detected>"); } }