Documentation Index
Fetch the complete documentation index at: https://mintlify.com/pw4k/ironbrew-2/llms.txt
Use this file to discover all available pages before exploring further.
Overview
IronBrew 2 implements a virtual machine (VM) based obfuscation approach that transforms standard Lua 5.1 bytecode into a custom VM that executes the code at runtime. This makes reverse engineering significantly more difficult, as the original Lua opcodes are replaced with custom virtual instructions.How VM-Based Obfuscation Works
The Transformation Pipeline
- Deserialization: Lua bytecode is deserialized into an intermediate representation (IR)
- Virtual Opcode Mapping: Each standard Lua opcode is mapped to a custom virtual opcode (VOpcode)
- VM Generation: A custom Lua VM is generated that can execute these virtual opcodes
- Serialization: The modified bytecode is serialized and embedded into the VM
VM Generation Process
The VM generation is handled by theGenerator class (Generator.cs:15).
Virtual Opcode Discovery
The generator discovers all virtual opcodes needed for the chunk:- Scans all types that inherit from
VOpcode - Checks which opcodes are actually used in the bytecode (Generator.cs:22)
- Creates instances only for used opcodes to minimize VM size
Instruction Mapping
When an opcode is detected as used, it’s mapped:Instruction gets a CustomData field that stores the virtual opcode mapping.
VM Structure
The generated VM consists of several components:1. Bytecode Embedding
The serialized bytecode is embedded as a string (Generator.cs:411-444):2. Deserializer Function
A custom deserializer is generated that reads the bytecode format (Generator.cs:459-518):3. VM Executor
The VM executor uses binary search for opcode dispatch (Generator.cs:547-583):Obfuscation Context
TheObfuscationContext (ObfuscationContext.cs:37) manages the obfuscation state:
Random Ordering
Chunk steps and constant types are shuffled (ObfuscationContext.cs:55-65):- The order in which chunk data is deserialized
- How constant types are encoded (nil, boolean, number, string)
XOR Encryption
Bytecode is XOR-encrypted with random keys (ObfuscationContext.cs:67-71):Execution Flow
When obfuscated code runs:- VM Initialization: The VM deserializes the embedded bytecode
- Chunk Loading: Creates chunk structures with instructions, constants, and functions
- Instruction Dispatch: Binary search finds the correct opcode handler
- Execution: The custom opcode implementation executes the operation
- Stack Management: Maintains Lua stack state across operations
Key Design Decisions
Only Used Opcodes
The VM only includes opcodes that are actually used in the bytecode (Generator.cs:22-38). This:- Reduces VM size
- Makes analysis harder (fewer patterns to recognize)
- Improves performance
Binary Search Dispatch
Instead of a simple if-else chain or table lookup, binary search is used (Generator.cs:547-583). This:- Provides O(log n) opcode lookup
- Obscures the dispatch mechanism
- Balances performance and obfuscation
Randomized Structure
Every obfuscation produces a different VM:- Chunk step order is randomized
- Constant type mapping is randomized
- Opcode indices are shuffled (Generator.cs:404)
- XOR keys are random
Related Topics
- Bytecode - Understanding the bytecode format
- Obfuscation Techniques - Additional obfuscation layers
- Control Flow - Control flow obfuscation