Skip to main content
Static Analysis & Decompilation with Ghidra & IDA Concepts
Lesson 10 of 20

Static Analysis & Decompilation with Ghidra & IDA Concepts

Navigate stripped ELF and PE binaries in Ghidra and IDA, reconstruct types and control flow from disassembly and decompiled C, and verify pseudocode against machine instructions.

Advanced 32 min IoT & embedded
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Explain the difference between linear-sweep disassembly, recursive-descent disassembly, and P-Code or microcode decompilation
  • Locate program entry points, strings, imports, and cross-references in stripped ELF and PE binaries
  • Recover readable logic by renaming functions, retyping parameters, and defining structs in Ghidra or IDA
  • Identify common decompiler inaccuracies by cross-checking pseudocode against raw assembly and calling conventions

Recommended first: CPU Registers, Memory Addressing & Calling ConventionsAssembly Basics: x86/x64 & ARM Control FlowELF & PE Binary Formats: Headers, Sections & Imports

#Why this matters

When an incident responder encounters an unfamiliar executable or a reverse engineer opens a compiled CTF binary, running the program blindly is either unsafe or uninformative. Static analysis lets you inspect a binary's capabilities, control flow, and data structures without executing a single instruction.

In earlier lessons, you learned how ELF and PE headers organize .text, .rodata, and import tables, and how CPU registers carry function arguments. Tools such as Ghidra and IDA combine those structural clues with control-flow graphing and decompilation so you can read complex binaries in minutes rather than decoding hundreds of raw opcodes by hand.

#Disassembly strategies and decompilation

Disassemblers turn raw bytes from code sections into assembly mnemonics, but how they walk those bytes matters when data sits inside code sections (such as jump tables or alignment padding).

Technique How it works Strength Limitation
Linear sweep (objdump -d) Decodes .text sequentially from start to end Fast, zero setup Misinterprets inline data bytes as instructions
Recursive descent (IDA, Ghidra) Follows calls, branches, and known entry points Separates code from data and builds a CFG Misses targets computed only at runtime (indirect jumps)
Decompilation Lifts assembly into IR (Ghidra P-Code), recovers SSA variables, emits C Condenses register juggling into readable expressions Can hide subtle size, signedness, or stack-layout details

Modern decompilers do not translate assembly directly to C line by line. Ghidra lifts each machine instruction into P-Code (a register-transfer intermediate language), runs data-flow and type-propagation passes, collapses stack offsets into local variables, and emits structured if/while blocks. IDA Pro and IDA Free follow a similar multi-stage pipeline with microcode (though IDA Free restricts some features compared to the commercial edition).

  1. Triagefile, strings, imports
  2. LoadAuto-analyze ELF or PE in Ghidra or IDA
  3. AnchorFind main, imports, and string XREFs
  4. RefineRename symbols, retype args, apply structs
  5. VerifyCross-check pseudocode against assembly

Production binaries and reverse-engineering challenges are usually compiled with strip, which deletes the .symtab symbol table and DWARF debug info. Function names like check_license disappear and become FUN_001011a9 in Ghidra or sub_1400011A9 in IDA. However, three anchors survive stripping:

  1. Dynamic imports: Functions resolved from shared libraries (libc.so.6, kernel32.dll) remain named in .dynsym / PLT or the PE Import Address Table (IAT).
  2. String literals: Messages such as "Invalid key" or "Usage: %s <token>" stay in .rodata or .rdata.
  3. Startup stubs: The binary's entry point (_start) still hands main to the C runtime. On classic System V x86-64 Linux binaries, _start passes the address of main in RDI (the first argument) to __libc_start_main.

Once you open a binary, use the Strings window or Imports tree to pick a meaningful anchor, then follow Cross-References (XREFs) to see every function that references it. For statically linked binaries where printf or strlen were compiled straight into .text, enable Function ID (Ghidra) or FLIRT signatures (IDA) to label standard library functions automatically instead of reversing memcpy by hand.

#Reading and refining decompiled output

Out of the box, a decompiler guesses generic names (param_1, local_28, uVar1) and default integer widths. Reading raw decompiler output without cleaning it up slows you down on non-trivial functions.

Consider a stripped x86-64 ELF binary vault_check inspected in Ghidra after following an XREF from "Access granted\n":

c
undefined8 FUN_001011a9(long param_1)
{
  size_t sVar1;
  uint local_14;

  sVar1 = strlen((char *)param_1);
  if (sVar1 == 8) {
    for (local_14 = 0; (int)local_14 < 8; local_14 = local_14 + 1) {
      if ((byte)(*(char *)(param_1 + (int)local_14) ^ 0x2a) != DAT_00102010[local_14]) {
        return 0;
      }
    }
    return 1;
  }
  return 0;
}

Renaming FUN_001011a9 to verify_token, param_1 to user_input, sVar1 to len, local_14 to idx, and DAT_00102010 to expected_xor_bytes (with type byte[8]) immediately clarifies the contract: the function requires an 8-byte string where each character XORed with 0x2a matches the 8 bytes stored in .rodata at 0x102010.

When structures are passed by pointer, defining a struct in Ghidra's Data Type Manager (or IDA's Structures view) removes raw offset arithmetic. If *(int *)(param_1 + 0x10) appears across five functions, creating a Session struct with field role_id at offset 0x10 turns every reference into session->role_id.

Inspecting the target bytes in .rodata on the lab host
$ objdump -s -j .rodata ./vault_check | head -n 6

./vault_check:     file format elf64-x86-64

Contents of section .rodata:
 2000 01000200 41636365 73732067 72616e74  ....Access grant
 2010 791e4779 7f751a1b 00                 y.Gy.u...

With the 8 bytes at 0x2010 (79 1e 47 79 7f 75 1a 1b), XORing each byte with 0x2a recovers the exact 8-character password (S4mSU_01) directly from static inspection.

#Cross-checking pseudocode against assembly

Decompilers make mistakes when binary optimizations violate simple C assumptions. Always check the disassembly view when something looks strange:

  • Signed vs unsigned comparisons: Assembly differentiates JL (signed less-than) from JB (unsigned below). If a decompiler defaults a variable to int instead of uint, a large positive length check can look negative.
  • Wrong function signatures: Calling-convention mismatches (System V RDI, RSI, RDX, RCX vs Windows x64 RCX, RDX, R8, R9) or variadic functions (printf, ioctl) can cause the decompiler to hide an argument register or invent extra parameters.
  • Inlined helpers and tail calls: Compilers at -O2 or -O3 often replace return helper(x); with a direct JMP helper (tail call) or merge two small functions, making the call graph look flatter than the original source.

#Common mistakes

  • Trusting decompiled C blindly: Treating pseudocode casts, array bounds, or dead-code removal as ground truth without verifying the underlying cmp, test, or movzx/movsx instructions.
  • Starting at offset zero: Reading every FUN_* sequentially from the top of .text instead of pivoting from _start, imported APIs, and string XREFs.
  • Skipping annotations: Leaving dozens of functions named FUN_00401200 and variables named local_48 makes multi-function call chains impossible to track.
  • Ignoring architecture and ABI settings: Analyzing a bare-metal firmware blob or Windows DLL with the wrong calling convention or base address breaks every cross-reference and pointer cast.

#Summary

Static analysis combines binary headers, recursive-descent disassembly, cross-references, and intermediate-representation decompilation to explain program behavior without executing untrusted code. Anchoring on _start, dynamic imports, and .rodata strings lets you bypass missing symbols in stripped binaries, while renaming variables and defining structs turns raw pointer math into readable logic. Always validate critical checks, types, and loop bounds against the assembly listing before drawing conclusions.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge