Github Stackoverflow Email

Software developer notes

Once you have a readable binary in hand — unpacked, dumped, rebuilt — you still have to actually read it. Let's take Ghidra, the NSA's open-source reverse engineering suite: how to feed Ghidra a binary, how to drive it headless, and how to turn a few megabytes of disassembly into an understanding of the algorithms inside.
A honest disclaimer up front: Ghidra is a static tool. It will not unpack a protected binary for you, and it will not defeat a VM. Workflow is always "dynamic first, static second" — use an emulator (see the companion Unicorn article) to unpack and dump the payload, then bring the result into Ghidra to read it. Ghidra is where understanding happens; it is not where unpacking happens.

Why Ghidra

There are many disassemblers, but Ghidra hits a sweet spot:
  1. It is free and open source. No license server, no dongle, no "demo limited to 30 days". You can install it on every machine you own and script it without a care.
  2. The decompiler is genuinely good. It turns x86 into readable C-like pseudocode, and it keeps getting better. For reading algorithms — which is 90% of the job — pseudocode is many times faster to digest than assembly.
  3. It is scriptable and headless. Everything the GUI does can be done from a script, in batch, on a server, with no display. This matters enormously for large binaries (more below).
  4. It scales. The sample I worked with decompiled to roughly 14,000 functions. Ghidra chewed through it and stayed navigable.

Step one: give Ghidra something it can eat

Ghidra imports PE, ELF, Mach-O, raw binaries and more. The catch with a protected binary is that the file on disk is not the program — it is an encrypted container with a stub. So before Ghidra, you unpack: run the thing in an emulator, let it decrypt itself, and dump the populated memory image. I then rebuilt a loadable PE from that dump (fixing up section headers so the tool would accept it) and imported that.
A rebuilt dump is never a perfect PE, and it is worth knowing the usual scars:
  • The import table is empty or fake — the protector resolved APIs by hash at runtime, so Ghidra will not know which addresses are kernel32!CreateFile. You recover names by matching the runtime-resolved addresses against your emulator's API log.
  • The entry point is a stub, not the real OEP, unless you set it correctly during the rebuild.
  • References may be stale if the dump was taken at a different base than the headers claim — get the image base right or every absolute address is off.
None of these are deal-breakers. The code is all there; you just have to tell Ghidra a little about the context the loader would normally provide.

Headless analysis: analyzeHeadless

For a big binary, the GUI is the slow way to do the initial analysis. Ghidra ships a headless mode — analyzeHeadless — that imports a file, runs the full auto-analysis, and saves a project, all from the command line with no display:
# analyze a binary into a project, no GUI
analyzeHeadless /path/to/projects MyProject \
    -import /path/to/unpacked.exe \
    -overwrite

# later, open the project in the GUI to browse the results
ghidraRun   # then File -> Open Project -> MyProject
Why headless matters:
  1. It is repeatable. The same command on the same binary gives the same project. When you re-dump a slightly different version, you re-run one command instead of clicking through import dialogs.
  2. It runs on a server. No X11, no desktop — throw it at a build machine or a VM and come back when it is done. Auto-analysis of a multi-megabyte binary takes minutes; you do not want to babysit a GUI for that.
  3. It scripts. You can attach a post-analysis script (Java or Jython) that runs right after auto-analysis to rename functions, apply types, export data, or run batch queries — turning Ghidra into an analysis pipeline rather than an interactive tool.
Ghidra headless workflow

Reading the result: the decompiler is home base

Once analysis finishes, open the project and live in the decompiler. The workflow that actually gets you to understanding:
  1. Start from a name, a string, or an entry point. You rarely read top-to-bottom. You jump to something you recognize — a string ("Internal Error"), an imported API call, the entry point — and work outward.
  2. Read pseudocode, not assembly. The decompiler view shows C-like code. Most algorithms are obvious at this level: a CRC loop is a CRC loop, a key schedule is a key schedule.
  3. Rename as you understand. Every time you figure out what a function does, rename it (FUN_00401234 → decrypt_record). Every name you give makes every caller and callee easier to read. This is the single highest-leverage habit in reverse engineering.
  4. Retype variables. When you recognize a structure (a record header, a context block), give the pointer a real type. The decompiler immediately re-renders *(int *)(param_1 + 8) as rec->size, and a wall of offsets becomes a readable structure.
  5. Follow cross-references. "Who calls this?" and "what does this call?" are one keystroke away (x on a symbol). Xrefs are how you climb from a leaf function to the logic that drives it.
From bytes to pseudocode

Reading Delphi code specifically

Some notes about code written in Delphi, which has its own flavor that is worth knowing:
  • The RTL is everywhere. A huge fraction of the 14,000 functions are Delphi's runtime library (string handling, memory management, the VCL). They are noise for your purposes — learn to recognize and skip them. The actual application logic is a small island in a sea of RTL.
  • Strings are length-prefixed. Delphi's AnsiString/UnicodeString carry a length and a reference count in a header before the character data, so string manipulation looks different from C's char* world. Once you recognize the pattern, you can spot string operations instantly.
  • Objects use VMTs. Delphi classes dispatch through a Virtual Method Table — a table of function pointers at the start of the class. A call like call [eax+0x14] is a virtual method; the offset into the VMT tells you which one. Finding the VMTs is a fast way to map out the class hierarchy.
  • Exceptions are Delphi-flavored. try/except/finally compiles to RTL calls rather than raw SEH, which actually makes the high-level structure easier to follow in pseudocode.

Small techniques that paid off

  1. Search for constants, not just code. A magic number (a CRC polynomial, an AES S-box, a protocol constant) is often the fastest way to locate an algorithm. Ghidra's "Search → For Scalars" finds every reference to a value; one hit on a CRC table led me straight to the integrity-checking code.
  2. Search memory for strings. Even in a stripped binary, error messages, format strings and registry paths are landmarks. "Internal Error" and "cannot be run in a virtual" were the breadcrumbs that located the protector's bail-out routine.
  3. Use the function graph. For a tangled function, the graph view (basic blocks and edges) reveals the loop structure and the suicide branches far better than linear disassembly.
  4. Bookmark and comment aggressively. You will forget why an address mattered. Future-you is grateful for every ; key for the next record comes from CRC of this body comment.
  5. Script the boring parts. When I needed to check hundreds of import slots or export a list of every function over a size threshold, a short Jython post-script beat an afternoon of clicking.

Know the limits

Ghidra is brilliant at reading code that is actually there, and genuinely not the right tool for code that is not:
  • It does not unpack. If the payload is encrypted, Ghidra sees ciphertext. Unpack first (emulator, debugger, or a known unpacker), then import.
  • It does not defeat a VM. A bytecode-VM function decompiles to the interpreter, not the logic. To understand the VM you either reverse its instruction set by hand or trace it dynamically — Ghidra alone just shows you the dispatcher loop.
  • Obfuscated control flow confuses the decompiler. Junk jumps and opaque predicates (the protector loves jmp-over-garbage) produce ugly or wrong pseudocode. I ended up writing a small custom deobfuscator for the worst of it and feeding Ghidra cleaner bytes.
  • It is static. It cannot tell you what a value is at runtime, only how it is computed. For "what is actually in this buffer", you go back to the emulator's watchpoints.
The healthy mental split: emulator for dynamics, Ghidra for statics. The emulator answers "what happens"; Ghidra answers "how does it work". Used together, a protected binary stops being a black box and becomes a (large, occasionally annoying) open book.

Conclusion

Ghidra's combination of a strong decompiler, real scalability and scriptable headless analysis makes it the backbone of my static workflow. The craft is not in any single feature but in the loop: navigate to a landmark, read the pseudocode, rename and retype until it reads like source, follow the xrefs one level up, repeat. Fourteen thousand functions sounds like an ocean — but you only ever need to understand the few dozen that matter, and Ghidra is very good at helping you find exactly those.

Reverse-Engineering

Leave a comment




Solve the puzzle

About me

Hi! I am Alex, the author of this blog. Here are my technical (in the majority) thoughts and stories. I will be hoping that you find this site interesting and fun. Feel free to leave comments under the articles.