Skip to content

Latest commit

 

History

History
173 lines (141 loc) · 5.98 KB

lua-bytecode-restore.md

File metadata and controls

173 lines (141 loc) · 5.98 KB

PUBG Mobile Lua bytecode format (reverse-engineered)

This document describes the on-disk Lua chunk format used by PUBG Mobile (ShadowTrackerExtra/Content/Lua/**/*.lua) and the exact transformation the third-party "restore" front-end applies before unluac can decompile it.

Everything below was recovered by differential analysis of 200 real pairs (original .lua file vs. the bytecode the front-end feeds to unluac) and is validated byte-for-byte: the Rust/Python reference implementation in .tmp/decr/restore.py reproduces the reference output for 200/200 pairs.

1. Container layout (both formats)

u8 magic[4] = "\x1bLua"
u8 version  = 0x53   (Lua 5.3)
u8 format   = 0x00
u8 luac_data[6] = 19 93 0d 0a 1a 0a
u8 int_size      = 0x04
u8 size_t_size   = 0x04
u8 instruction_size = 0x04
u8 integer_size  = 0x08
u8 float_size    = 0x08
u64 integer   = 0x5678       (little endian)
f64 float     = 370.5        (little endian)
u8 main_upvalues             (usually 1)
<function>

Function layout (recursive; identical in both formats up to the debug section):

string  source            # "" for nested functions, "@.\GameLua\..." for main
i32     linedefined
i32     lastlinedefined
u8      numparams
u8      is_vararg
u8      maxstacksize
u32     code_count
i32     code[code_count]
u32     const_count
        constants: u8 tag, payload
          0  nil
          1  boolean (u8 0/1)
          3  number   (f64, 8 bytes)
          4  string
          19 integer  (i64, 8 bytes)
          20 long string
u32     upvalue_count
        (u8 instack, u8 idx) * upvalue_count
u32     proto_count
        <function> * proto_count
<debug>

String encoding (both formats): one byte size where size == 0 means the empty string and otherwise size == len+1; 0xFF escapes to a 4-byte little-endian size. The NUL byte after the text is not stored (the reader consumes size-1 bytes); the byte that follows is the next field.

2. Original debug section (Lua 5.4-style compact form)

u32 line_count
i8  line_delta[line_count]         # signed, see below
u32 abs_count
    (u32 pc, u32 line) * abs_count # absolute line resets
u32 local_count
    (string name, i32 startpc, i32 endpc) * local_count
u32 upname_count
    string name * upname_count

Line reconstruction (validated):

current = linedefined
for pc in 0..line_count:
    if pc has an abslineinfo entry: current = absline[pc]   # delta byte is a sentinel (-128), ignored
    else:                           current += line_delta[pc]
    restored_lines[pc] = current

The restored format writes those absolute lines as u32 each (no abslineinfo table, no deltas), which is why the restored file is larger.

3. Restored debug section

u32 line_count
i32 line[line_count]     # absolute line numbers as computed above
u32 local_count
    (string, i32 startpc, i32 endpc)
u32 upname_count
    string

4. Transformations applied by restore (original -> unluac format)

4.1 String encryption

Every string payload (main source, string constants, local names, upvalue names) is XOR-encrypted with a 32-byte repeating key, indexed by the position inside the string:

key = 11 21 36 47 46 57 a7 8d 9d 84 90 d8 ab 00 8c 35
      26 1a f7 e4 58 05 b8 b3 15 07 d0 2c 1e 8f f6 c8
plain[i] = cipher[i] XOR key[i % 32]

The length bytes are not encrypted. The key was confirmed consistent for hundreds of thousands of bytes (string lengths up to several hundred chars).

4.2 Instruction opcode permutation

Only the low opcode byte changes; bytes 1-3 and all operands are identical. The permutation is applied to the low 6 bits and mirrored for bit 6 (0x40); bit 7 (part of operand A) is preserved.

Observed mapping (restored -> original), for x = opcode & 0x3F:

if x < 30:  mapped = (x + 17) mod 30
else:       mapped = x
original_opcode = mapped | (opcode & 0xC0)

so the restore direction (original -> restored) uses the inverse rotation:

if x < 30:  restored = (x + 13) mod 30
else:       restored = x

Validation: 113,782 instructions across 200 files, zero conflicts; 0 files where bytes 1-3 differ. The mapping for x >= 30 is identity on all observed values (opcodes 30..45 and 62..109 are common); a broader capture is planned to cover the remaining ranges before shipping.

4.3 Debug conversion

  • line numbers: compact deltas + abslineinfo -> absolute i32 array (rule in section 2); abslineinfo table is dropped.
  • locals and upvalue names: same section order, but string payloads are decrypted with the key above.
  • empty source strings (nested functions) are written as a single 0x00 byte; every other string uses len+1 (so an empty constant is 0x01).

5. Evidence / validation

  • 200 real pairs, byte-identical after transformation (all lengths match too).
  • Reference scripts: .tmp/decr/orig_chunk.py (original parser), .tmp/decr/chunk.py (restored parser), .tmp/decr/restore.py (transform).
  • Test fixtures committed under crates/yargi-core/tests/fixtures/lua/ (*.lua original, *.restored.luac expected output, manifest.json with sizes and SHA-256).

6. Implementation status

  1. Restore is implemented in Rust (yargi-core::lua::restore) and validated byte-identically against the reference front-end on captured chunks.
  2. Native decompilation is fully in-process (yargi-lua): restore -> chunk parser -> vendored/patched unluac crate -> Lua source. No JVM, no external tools, no temporary files. yargi.exe dropped from ~44 MB to ~20 MB.
  3. Recovery ladder for protos the strict pass cannot structure: retry with debug info ignored, then a permissive pass that keeps the rest of the chunk as normal source.
  4. Real-corpus result (27,562 PUBG Mobile chunks, 12 threads):
    • 27,562 / 27,562 decompiled (100%); 14 files used the recovery ladder;
    • full run ~6.8 minutes (reference pipeline: 2h20m);
    • luac 5.3 -p: 27,544 / 27,562 outputs recompile; the remaining 18 exceed Lua's own 200-active-local compiler limit (source is still generated).