r/MalwareAnalysis • • 3h ago

unflat: a tool to undo control-flow flattening and map bytecode VMs in x64 PE/ELF binaries

3 Upvotes

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], and call reg / call [mem] when a cmovCC/setCC picks the target), how many are preceded by a selector, ud2 opaque 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 the cmov/setCC both 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 into ud2/int3 are treated as opaque predicates. The tail becomes jcc + jmp; when flags would be clobbered or there isn't enough room, the code moves to a small cave (a new .unflat section in PE, a new PT_LOAD in 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, pop into 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 ret back still work.
  • 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/setCC picks 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
  • vm maps 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.


r/MalwareAnalysis • • 3h ago

Reversed a Hijacked LokiBot Sample: Someone Patched In Their Own C2 Over the Original

2 Upvotes

Hi all, I reversed a LokiBot sample that turned out to be a hijacked build and wrote up the analysis. Everything is static, I never ran it.

Quick summary:

- 106 KB PE32 from 2016 with almost no imports. Every API is resolved at runtime by walking the PEB and the export tables and comparing a CRC32 hash. The CRC is modified: constant 0x4358AD54 and XOR before the shift, so the textbook version doesn't match anything

- The executable has an extra section called .x. Somebody replaced the last instruction of the function that calls CryptDecrypt with a jmp into it, and the stub overwrites the decrypted C2 with hxxp://brysonsqroup[.]com/five/fre.php. The original 4 C2s are still in .rdata, 3DES-encrypted, and never used. I decrypted them statically (alphastand[.]trade/win/top and kbfvzoboss[.]bid, all /alien/fre.php)

- HTTP header strings are encrypted with a modified RC4 (no swap in the key setup, index starts at 1), key KOSFKF. User-Agent is the classic "Mozilla/4.08 (Charon; Inferno)"

- I mapped the binary packet: version 0x12, type 0x28 (check-in) or 0x27 (credentials), the "ckav.ru" marker, user/computer/domain, screen size, admin flags, OS version and a bot ID that's the first 24 hex chars of MD5(MachineGuid). A local .hdb file keeps CRCs of reports already sent so nothing goes out twice

- For Firefox it borrows Firefox's own sqlite3/nss3 DLLs and calls PK11SDR_Decrypt. If Firefox is 64-bit it starts a hidden copy and injects ~2.8 KB of x64 code (built on the stack with mov instructions) that decrypts logins.json from inside

- C2 commands: download and run EXE, download and load DLL, resend everything, update, uninstall

- Persistence: copies itself to %APPDATA%\<6 chars of the bot ID>\<6 chars>.exe, hidden+system, plus a Run key whose path is itself 3DES-encrypted in the binary

I worked out the algorithms and used Claude to help write the Python scripts for the decryption and for rebuilding the injected x64 code.

Full writeup with IOCs: https://inkman97.github.io/lokibot-analysis/