Decompilers: Ghidra, IDA Free, Binary Ninja, radare2 and rizin
Reading disassembly line by line works for a function; it doesn't scale to a program with two thousand of them. Interactive disassemblers with decompilers do the bookkeeping: they find functions, build control-flow graphs, track cross-references ("who calls this?", "who reads this string?"), recover types, and turn each function into C-like pseudocode. The analyst's job becomes reading, renaming and annotating until the pseudocode reads like source. This lesson compares the main tools, shows what a decompiler does with one of the course's stripped toys, and practises the renaming workflow in Python.
The tools
| Tool | Licence and cost | Notes |
|---|---|---|
| Ghidra | open source (Apache 2.0), free; released by the NSA in 2019 | Java GUI; decompiler for many architectures (x86, ARM, MIPS, PowerPC, RISC-V...); processor definitions in SLEIGH; scripting in Java and Python; analyzeHeadless for batch analysis; collaborative server |
| IDA Free (Hex-Rays) | free for non-commercial use; IDA Home (non-commercial) and IDA Pro are paid | the long-time industry standard; the free edition includes a cloud-based decompiler for x86/x64 only; FLIRT library signatures; IDAPython and the SDK only in paid editions |
| Binary Ninja (Vector 35) | commercial; the free edition is for evaluation and education (fewer architectures, no plugins or Python API) | layered intermediate languages (LLIL, MLIL, HLIL) and a clean Python API; popular for automation |
| radare2 | open source (LGPL), free | command-line framework (r2); scriptable through r2pipe; decompilation through plugins (r2ghidra, r2dec) |
| rizin / Cutter | open source, free | a 2020 fork of radare2 with a more stable API; Cutter is its GUI with the Ghidra decompiler built in |
All of them do the same core analysis; choose by architecture support, budget, team habits and scripting needs. Many analysts keep two, because a second opinion catches the first tool's mistakes.
Finding main in a stripped binary
The session builds a stripped toy with a key-check function and does by hand what every decompiler does first: find main from the entry point, then find the function main calls. In a stripped glibc program, _start passes main's address to __libc_start_main in rdi.
cat > lic.c <<'SRC'
#include <stdio.h>
#include <string.h>
int validate(const char *s) {
size_t n = strlen(s);
if (n != 8) return 0;
unsigned sum = 0;
for (size_t i = 0; i < n; i++) sum = sum * 33 + (unsigned char)s[i];
return (sum & 0xffff) == 0x1234;
}
int main(int argc, char **argv) {
if (argc > 1 && validate(argv[1])) puts("valid");
else puts("invalid");
return 0;
}
SRC
gcc -O1 lic.c -o lic && strip lic
entry=$(readelf -h lic | awk '/Entry/{print $4}')
echo "entry point: $entry"
echo "== _start: the address in rdi before the call is main"
objdump -d -M intel --no-show-raw-insn --start-address=$entry lic | grep -E '^\s+[0-9a-f]+:' | sed -n '11,12p'
echo "== main (0x11ac) and the function it calls (0x1169)"
objdump -d -M intel --no-show-raw-insn --start-address=0x1169 --stop-address=0x11ec lic \
| grep -E '^\s+[0-9a-f]+:' | sed 's/^ *//'
echo "== the strings main prints"
objdump -s -j .rodata lic | grep -E '^ 2000'entry point: 0x1080
== _start: the address in rdi before the call is main
1098: lea rdi,[rip+0x10d] # 11ac <strlen@plt+0x13c>
109f: call QWORD PTR [rip+0x2f33] # 3fd8 <strlen@plt+0x2f68>
== main (0x11ac) and the function it calls (0x1169)
1169: endbr64
116d: push rbx
116e: mov rbx,rdi
1171: call 1070 <strlen@plt>
1176: mov rdx,rax
1179: mov eax,0x0
117e: cmp rdx,0x8
1182: jne 11aa <strlen@plt+0x13a>
1184: mov rdx,rbx
1187: lea rdi,[rbx+0x8]
118b: mov ecx,eax
118d: shl ecx,0x5
1190: add eax,ecx
1192: movzx ecx,BYTE PTR [rdx]
1195: add eax,ecx
1197: add rdx,0x1
119b: cmp rdx,rdi
119e: jne 118b <strlen@plt+0x11b>
11a0: cmp ax,0x1234
11a4: sete al
11a7: movzx eax,al
11aa: pop rbx
11ab: ret
11ac: endbr64
11b0: sub rsp,0x8
11b4: cmp edi,0x1
11b7: jle 11c6 <strlen@plt+0x156>
11b9: mov rdi,QWORD PTR [rsi+0x8]
11bd: call 1169 <strlen@plt+0xf9>
11c2: test eax,eax
11c4: jne 11dc <strlen@plt+0x16c>
11c6: lea rdi,[rip+0xe37] # 2004 <strlen@plt+0xf94>
11cd: call 1060 <puts@plt>
11d2: mov eax,0x0
11d7: add rsp,0x8
11db: ret
11dc: lea rdi,[rip+0xe23] # 2006 <strlen@plt+0xf96>
11e3: call 1060 <puts@plt>
11e8: jmp 11d2 <strlen@plt+0x162>
== the strings main prints
2000 01000200 696e7661 6c696400 ....invalid.
Two things to notice. objdump labels every address relative to the last symbol it knows (strlen@plt+0xf9), which in a stripped file is meaningless; ignore those labels. And main passes 0x2004 and 0x2006 to puts: the .rodata dump shows invalid\0 at 0x2004, so 0x2006 is valid, the tail of the same string. The linker merged the two literals (string tail merging), and a naive "string at this address" lookup would show the wrong text. Decompilers handle this; quick scripts often don't.
What a decompiler makes of it
This is a lab transcript: Ghidra 11's decompiler window for the function at 0x101169 of the same stripped lic (Ghidra loads PIE files at image base 0x100000), before any renaming. It is abridged, and the exact variable names and casts vary by version.
// Lab session: Ghidra, lic (the course's own toy), function FUN_00101169, before renaming
undefined4 FUN_00101169(char *param_1)
{
size_t sVar1;
char *pcVar2;
uint uVar3;
sVar1 = strlen(param_1);
uVar3 = 0;
if (sVar1 == 8) {
pcVar2 = param_1;
do {
uVar3 = uVar3 * 0x21 + (uint)(byte)*pcVar2;
pcVar2 = pcVar2 + 1;
} while (pcVar2 != param_1 + 8);
uVar3 = (uint)((short)uVar3 == 0x1234);
}
return uVar3;
}Compare with the listing: shl ecx, 5 plus add eax, ecx became uVar3 * 0x21 (x*32 + x = x*33), the pointer loop became a do/while, and cmp ax, 0x1234 became the (short) comparison of the low 16 bits. The decompiler has recovered the algorithm; it has no idea what it means. The same function in radare2, as a command-line lab transcript:
$ r2 -A lic # lab session: the course's own toy; -A runs the analysis
[0x00001080]> afl # list functions (abridged)
0x00001080 1 37 entry0
0x00001169 5 67 fcn.00001169
0x000011ac 5 64 main
[0x00001080]> axt @ 0x1169 # cross-references to fcn.00001169
main 0x11bd [CALL:--x] call fcn.00001169
[0x00001080]> afn validate_key @ 0x1169 # rename itRenaming and annotating
The workflow is the same in every tool: rename a function or variable as soon as you know what it is (N in IDA, L in Ghidra), set its type, add a comment with the evidence, and let the tool propagate the names to every caller. The example applies a rename map and annotations to the pseudocode above and produces the note an analyst would keep.
import re
PSEUDO = """undefined4 FUN_00101169(char *param_1)
{
sVar1 = strlen(param_1);
uVar3 = 0;
if (sVar1 == 8) {
pcVar2 = param_1;
do {
uVar3 = uVar3 * 0x21 + (uint)(byte)*pcVar2;
pcVar2 = pcVar2 + 1;
} while (pcVar2 != param_1 + 8);
uVar3 = (uint)((short)uVar3 == 0x1234);
}
return uVar3;
}"""
RENAMES = {"FUN_00101169": "validate_key", "param_1": "key", "sVar1": "len",
"pcVar2": "p", "uVar3": "hash", "undefined4": "int"}
COMMENTS = {"strlen": "key must be exactly 8 characters",
"* 0x21": "djb2-style hash: h = h*33 + c (no initial 5381)",
"0x1234": "accept when the low 16 bits of the hash are 0x1234"}
def apply(text):
for old, new in RENAMES.items():
text = re.sub(rf"\b{re.escape(old)}\b", new, text) # whole identifiers only
out = []
for line in text.splitlines():
note = next((c for k, c in COMMENTS.items() if k in line), None)
out.append(f"{line:<54}// {note}" if note else line)
return "\n".join(out)
print(apply(PSEUDO))
print()
print("finding: 0x1169 validate_key(key) accepts any 8-char key whose 16-bit hash is 0x1234;")
print(" many keys collide, so the check is weak by design (see the crackme lesson).")int validate_key(char *key)
{
len = strlen(key); // key must be exactly 8 characters
hash = 0;
if (len == 8) {
p = key;
do {
hash = hash * 0x21 + (uint)(byte)*p; // djb2-style hash: h = h*33 + c (no initial 5381)
p = p + 1;
} while (p != key + 8);
hash = (uint)((short)hash == 0x1234); // accept when the low 16 bits of the hash are 0x1234
}
return hash;
}
finding: 0x1169 validate_key(key) accepts any 8-char key whose 16-bit hash is 0x1234;
many keys collide, so the check is weak by design (see the crackme lesson).
After renaming, the function documents itself, and the weakness is visible: a 16-bit target for an 8-character input means many different keys pass. That is the kind of conclusion a decompiler can't draw for you.
How analysts catch decompiler mistakes
- Wrong function signatures: missing or extra parameters (from guessing the calling convention),
undefined8return types where nothing is returned. Fix the prototype; the pseudocode around every call improves. - Wrong types: a struct accessed as
*(long *)(param_1 + 0x10)until you define the struct (Structs and Virtual Calls). - Missing code: functions reached only through indirect jumps or tables, data decoded as code, or code hidden by anti-disassembly (objdump lesson). Check the disassembly whenever the pseudocode looks too simple or nonsensical.
- Optimisations undone or not: the decompiler may show
x * 0x21or(x << 5) + x; both are the same computation.
How the work is kept sound
- Keep the disassembly as the ground truth and the pseudocode as a reading aid.
- Record each rename with its evidence (as
COMMENTSabove) so the analysis can be reviewed and repeated; Ghidra and IDA project files and BinExport/BinDiff let teams share them. - Run analysis tools on untrusted files in an isolated VM: decompilers parse hostile input and have had their own vulnerabilities.
Pitfalls
- Trusting names a tool invented:
FUN_,fcn.,sub_prefixes are addresses, and auto-names such asstrlenfor a statically linked function come from signatures that can mismatch. - Forgetting the image base: Ghidra shows PIE addresses at
0x100000 +offset, objdump and radare2 at the file's virtual addresses, gdb at run-time addresses; convert before comparing. - Analysing only
main: constructors (.init_array), TLS callbacks on Windows and signal handlers run code before or outsidemain.
Practice
1. In a stripped glibc binary, how do you find main?
Disassemble the entry point (_start); the address loaded into rdi before the call to __libc_start_main (through the GOT) is main.
2. Why did puts(0x2006) print "valid" although the string in the dump at that line begins with "invalid"?
The linker merged "valid" into the tail of "invalid": 0x2004 is invalid, and two bytes later 0x2006 is valid.
3. Add a rename so that the comparison constant reads as a named value in the output.
Add "0x1234": "TARGET_HASH" to RENAMES (the regex escapes it); the comment rule keys on 0x1234, so change it to TARGET_HASH too.
Test yourself
What does a decompiler produce?
- The original source code
- C-like pseudocode reconstructed from the machine code, without original names or comments
- A memory dump
- A packet capture
shl ecx, 5 followed by add eax, ecx (with ecx = eax) computes:
- eax * 5
- eax * 33
- eax * 32
- eax + 5
Which of these tools is open source and was released by the NSA?
- IDA Free
- Binary Ninja
- Ghidra
- x64dbg
Why rename functions and variables as soon as you understand them?
- It makes the binary smaller
- Names propagate to every caller and turn the pseudocode into readable, reviewable analysis
- Decompilers require it
- It removes obfuscation