Ghidra Reverse Engineering Tutorial: Analyzing Binaries for Malware and Vulnerabilities

Ghidra is NSA’s open-source software reverse engineering (SRE) framework, released publicly in 2019. It provides a full disassembler and decompiler for x86, x64, ARM, MIPS, and other architectures. Security researchers use Ghidra to analyze malware, find vulnerabilities in closed-source software, and solve CTF binary challenges. This tutorial covers setting up Ghidra, navigating its interface, and performing practical reverse engineering tasks.

Installation

# Download from https://ghidra-sre.org
# Requires Java 17+
wget https://github.com/NationalSecurityAgency/ghidra/releases/download/Ghidra_11.1.2_build/ghidra_11.1.2_PUBLIC_20240709.zip
unzip ghidra_11.1.2_PUBLIC_20240709.zip
cd ghidra_11.1.2_PUBLIC

# Install Java
apt install default-jdk

# Launch Ghidra
./ghidraRun

Creating a Project and Importing a Binary

1. File → New Project → Non-Shared Project → Choose directory
2. File → Import File → Select your binary
3. Ghidra detects format automatically (ELF, PE, Mach-O, raw binary)
4. Double-click the imported file to open CodeBrowser
5. Click 'Yes' to auto-analyze the binary
6. Accept default analysis options (or add more for thorough analysis)

Key Interface Panels

Listing (Disassembly) - Assembly instructions with addresses
Decompiler             - Pseudo-C code (right panel, most important)
Symbol Tree            - Functions, labels, imports, exports
Program Tree           - Sections (.text, .data, .bss, etc.)
Data Type Manager      - Struct/typedef definitions
Console                - Script output and messages

Navigation Shortcuts

G          - Go to address
L          - Rename label/variable
T          - Set data type
;          - Add comment
Ctrl+F     - Search for string/instruction
Ctrl+Shift+E - Search for strings in binary
X or Ctrl+Shift+F - Find cross-references to address
Alt+←/→   - Navigate back/forward
F          - Follow function call
Q          - Open quick search
P          - Create function at cursor position

Finding the main() Function

# In Symbol Tree, expand 'Functions'
# Look for 'main', 'entry', or 'FUN_00401234'

# If stripped (no symbols), find entry point:
# 1. Go to entry() function
# 2. Look for the last argument pushed to __libc_start_main call
# 3. That's the address of main()

# In decompiler, typical __libc_start_main call:
__libc_start_main(main_function, argc, argv, init, fini, 0);
# The first argument is main()

Identifying Vulnerabilities

Buffer Overflow Detection

# Search for dangerous functions
Search → Program Text → Type: 'Instruction Mnemonic'
Window → Script Manager → Run GhidraScripts/Analysis/FindStrings.java

# In decompiler, look for:
strcpy(buffer, user_input);   // No bounds check
gets(input_buf);              // Always vulnerable
sprintf(buf, format, arg);    // Format string issue
scanf("%s", buffer);          // No length limit

Hardcoded Credentials and Secrets

# Search for interesting strings
Window → Defined Strings (Shift+Ctrl+E)
# Filter for: password, key, token, secret, admin

# Check string cross-references
Right-click string → References → Show References to Address
# Navigate to function using the string to understand context

Renaming and Annotating

# Rename function: Right-click in decompiler → Rename Function
# Or press L on function name

# Rename variables: Click variable in decompiler, press L

# Add comments: Press ; at any instruction
# End-of-line comment: Press ;
# Plate comment (above function): Press Shift+;

# Set data type: Click variable, press T
# This propagates type info through decompiled code

Analyzing Packed or Obfuscated Malware

# Check for packing
Entropy analysis: Analysis → One Shot Analysis → Entropy
High entropy sections (>7.0) suggest packing/encryption

# Common packer signatures in imports:
# UPX: UPX0, UPX1 sections
# Manual unpacking with x64dbg + dump + load in Ghidra

# Dynamic analysis approach:
# 1. Run sample in Cuckoo/CAPE sandbox to unpack in memory
# 2. Dump memory to .bin file
# 3. Load dump in Ghidra with 'raw binary' format

# Identify string deobfuscation routines:
# Strings encrypted → decrypted in memory at runtime
# Find loop that XORs bytes: common pattern for XOR encryption

Ghidra Scripting

# Automate analysis with Python scripts (Jython)
# Window → Script Manager → New Script

# Example: Find all calls to strcmp
from ghidra.program.model.symbol import RefType
references = currentProgram.getReferenceManager()
for ref in references.getReferencesTo(getSymbols('strcmp').next().getAddress()):
    print(ref.getFromAddress())

# Community scripts:
# https://github.com/ghidra-sre/ghidra/tree/master/Ghidra/Features/Python/ghidra_scripts

# ghidra2dwarf: Export Ghidra analysis to DWARF debug info
# BinDiff integration: Compare two binaries to find diff (useful for patch analysis)

CTF Workflow

1. Import binary → auto-analyze
2. Find main() via __libc_start_main or symbol tree
3. Rename functions as you understand them
4. Search strings for hints (flags, passwords, interesting text)
5. Follow XREFs from interesting strings back to functions
6. Use decompiler view to understand logic quickly
7. Identify input validation, comparison functions
8. For crypto: look for XOR loops, known constants (0x9e3779b9 = TEA)
9. Script repetitive analysis tasks in Python

Conclusion

Ghidra is a professional-grade reverse engineering tool that rivals IDA Pro for most tasks. The decompiler view dramatically speeds up analysis — start there, not in the raw disassembly. Rename functions and variables as you understand them, use cross-references to trace data flow, and leverage Ghidra’s scripting for repetitive tasks. For CTF challenges, focus on finding main(), tracing string comparisons, and understanding any custom encryption or encoding routines.

Related Articles

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top