r/MalwareAnalysis • u/inkman97 • 3h ago
unflat: a tool to undo control-flow flattening and map bytecode VMs in x64 PE/ELF binaries
Hi all, I wrote a small tool while reversing WardenStealer, whose main code was control-flow flattened to the point Ghidra couldn't follow it. I cleaned it up and put it on GitHub, and I've just released v0.4.0, which adds support for memory-indirect and call-based dispatchers.
What it does:
unflat scan: static and fast. For each function it counts indirect dispatchers (jmp reg,jmp [mem], andcall reg/call [mem]when acmovCC/setCCpicks the target), how many are preceded by a selector,ud2opaque predicates, and byte-fetch + pointer-table patterns typical of VM dispatch. It also flags protector section names and high-entropy sections, and tells you which command to run next.unflat flat: every dispatcher is emulated with Unicorn, forcing thecmov/setCCboth ways with two random sets of inputs: if the target doesn't change, it's resolved. When a block depends on earlier state (state index, base register, table pointer spilled to the stack), it walks the function from its entry carrying known values. Branches intoud2/int3are treated as opaque predicates. The tail becomesjcc+jmp; when flags would be clobbered or there isn't enough room, the code moves to a small cave (a new.unflatsection in PE, a newPT_LOADin ELF).unflat vm: emulates a VM-protected function and reports the dispatch sites, the handlers with a guess of what each does (based on values read and written), the bytecode range, the opcodes and the virtual program line by line.- Formats: PE and ELF x86-64, including PIE, static-pie and stripped binaries, and memory dumps without
.pdata.
New in v0.4.0:
jmp [mem](jmp [rdx + rcx*8],jmp [rip + slot]): the target is read from the emulated memory at the jump, so tables filled or decrypted at runtime are seen as they really are. Loader-filled slots (IAT, GOT, ifunc) and VM-style opcode fetches are skipped.call reg/call [mem]: a call used as a dispatcher also pushes a return address, so it checks what the target does with it:- if every target throws it away right away (
lea rsp,[rsp+8],add rsp,8,popinto a dead register), the call is really a jump and gets patched as one; - otherwise the patch keeps the exact same return address on the stack (position-independent, flags and registers untouched), so helpers that
retback still work.
- if every target throws it away right away (
- Three hand-flattened test programs (
jmp reg,jmp [mem],call reg) give the same output before and after patching, as PIE, non-PIE and static-pie ELF, and as PE.
Results on WardenStealer (v0.3.0): 285/286 reachable dispatchers resolved in stage 1, 1664/1674 in stage 2. Stage 1's main went from 23 lines of C in Ghidra to 1845.
Limits, so nobody is disappointed:
- x86-64 only
- calls are only treated as dispatchers when a
cmov/setCCpicks the target (or the register is loaded with a constant in the same block); plain function-pointer calls are left alone - values only known at runtime are reported, not guessed
vmmaps the VM but doesn't devirtualize it- commercial protectors like VMProtect or Themida add layers it doesn't handle on its own
Repo: https://github.com/inkman97/unflat
Feedback and samples that break it are very welcome.