| name | Binary Analysis and Reverse Engineering |
| description | Systematic approach to analyzing compiled binaries, understanding program behavior, and identifying vulnerabilities without source code access |
| when_to_use | When analyzing closed-source software, malware analysis, understanding proprietary protocols, or assessing compiled applications for security vulnerabilities |
| version | 1.0.0 |
| languages | assembly, c, python |
Binary Analysis and Reverse Engineering
Overview
Binary analysis examines compiled executables without access to source code. This skill combines static analysis (disassembly, decompilation) with dynamic analysis (debugging, tracing) to understand program behavior, identify vulnerabilities, and reverse engineer functionality.
Core principle: Combine static and dynamic analysis. Static reveals structure; dynamic reveals behavior.
When to Use
- Analyzing closed-source software for vulnerabilities
- Malware analysis and understanding
- Reverse engineering proprietary protocols
- Understanding third-party libraries or dependencies
- CTF challenges and security research
- Verifying security claims of binary-only software
Analysis Workflow
Phase 1: Initial Assessment
Goal: Understand what you're analyzing and gather basic information.
file binary
strings binary | less
ldd binary
checksec binary
readelf -h binary
objdump -p binary
Phase 2: Static Analysis
Goal: Understand program structure without execution.
Disassembly
objdump -d binary > disassembly.txt
Function Analysis
"""
$ r2 binary
[0x00001000]> aa # Analyze all
[0x00001000]> afl # List functions
[0x00001000]> pdf @ main # Disassemble main
[0x00001000]> VV @ main # Visual graph mode
"""
Control Flow Analysis
"""
Analyze program flow:
1. Identify entry point
2. Follow execution paths
3. Identify decision points (if/else, switch)
4. Map loops and recursion
5. Identify error handling
6. Find return points
Key questions:
- What are the main code paths?
- Where does user input enter?
- What validation occurs?
- Where are dangerous operations?
"""
Phase 3: Dynamic Analysis
Goal: Observe actual program behavior during execution.
Debugging with GDB
gdb ./binary
(gdb) break main
(gdb) break *0x401234
(gdb) run arg1 arg2
(gdb) info registers
(gdb) x/10x $rsp
(gdb) x/s 0x404000
(gdb) stepi
(gdb) nexti
(gdb) continue
(gdb) display/i $pc
(gdb) display/x $rax
Enhanced GDB with PEDA/GEF/pwndbg
git clone https://github.com/longld/peda.git ~/peda
echo "source ~/peda/peda.py" >> ~/.gdbinit
bash -c "$(curl -fsSL https://gef.blah.cat/sh)"
System Call Tracing
strace ./binary
strace -v -s 1024 ./binary
strace -e trace=open,read,write ./binary
ltrace ./binary
strace -f ./binary
Dynamic Instrumentation
import frida
import sys
def on_message(message, data):
print(f"[*] {message}")
session = frida.attach("target_process")
script_code = """
Interceptor.attach(Module.findExportByName(null, 'strcmp'), {
onEnter: function(args) {
console.log('[*] strcmp called');
console.log(' arg1: ' + Memory.readUtf8String(args[0]));
console.log(' arg2: ' + Memory.readUtf8String(args[1]));
},
onLeave: function(retval) {
console.log(' return: ' + retval);
}
});
"""
script = session.create_script(script_code)
script.on('message', on_message)
script.load()
sys.stdin.read()
Phase 4: Vulnerability Identification
Common Vulnerability Patterns:
Buffer Overflow
; Look for unsafe string operations
call strcpy ; No bounds checking
call gets ; Always unsafe
call sprintf ; No bounds checking
; Check for:
; - Fixed-size buffers
; - User-controlled input
; - No size validation
Format String
; User input directly to printf
mov rdi, [user_input]
call printf ; Dangerous if user_input has format specifiers
Use After Free
; Pattern:
call free ; Free memory
; ... later ...
mov rax, [ptr] ; Use freed pointer
Integer Overflow
; Look for:
; - Size calculations
; - Loop counters
; - Memory allocation sizes
imul eax, [count], 8 ; Can overflow
call malloc ; Allocates wrong size
Phase 5: Exploit Development
See skills/exploitation/exploit-dev-workflow for detailed exploitation process.
Tool Ecosystem
Disassemblers/Decompilers
Ghidra (Free)
IDA Pro (Commercial)
- Industry standard
- Best-in-class decompiler (Hex-Rays)
- Extensive plugin ecosystem
- IDA Free available with limitations
Binary Ninja (Commercial)
- Modern UI
- Medium-level IL
- Good API for automation
- Active development
radare2 (Free)
r2 binary
[0x00001000]> aa
[0x00001000]> afl
[0x00001000]> s main
[0x00001000]> pdf
[0x00001000]> VV
Debuggers
GDB with Extensions
- PEDA - Python Exploit Development Assistance
- GEF - GDB Enhanced Features
- pwndbg - Exploit development focused
WinDbg (Windows)
- Microsoft's debugger
- Kernel and user-mode debugging
- Essential for Windows analysis
x64dbg (Windows)
- Modern UI
- Plugin support
- Good for malware analysis
Dynamic Analysis
Frida
- Dynamic instrumentation
- JavaScript API
- Cross-platform
- Runtime modification
PIN/DynamoRIO
- Dynamic binary instrumentation frameworks
- Research-grade tools
- Performance analysis
Valgrind
valgrind --leak-check=full ./binary
valgrind --tool=massif ./binary
Common Analysis Scenarios
Scenario 1: Finding Hardcoded Credentials
strings binary | grep -i "password\|secret\|key"
Scenario 2: Understanding Network Protocol
strace -e trace=network ./binary
gdb ./binary
(gdb) break send
(gdb) break recv
tcpdump -i lo -w capture.pcap
wireshark capture.pcap
Scenario 3: Bypassing License Check
strings binary | grep -i "license\|trial\|expired"
Scenario 4: Extracting Encryption Keys
"""
Interceptor.attach(Module.findExportByName('libcrypto.so', 'AES_set_encrypt_key'), {
onEnter: function(args) {
console.log('[*] AES key:');
console.log(hexdump(args[0], { length: 32 }));
}
});
"""
Practical Tips
Naming Conventions
Cross-Reference Analysis
Identifying Compiler Artifacts
; Stack canary check (GCC)
mov rax, fs:0x28
mov [rbp-0x8], rax
; ... function body ...
mov rdx, [rbp-0x8]
xor rdx, fs:0x28
je .no_corruption
call __stack_chk_fail
; C++ name mangling
_ZN6Class14memberFunctionEi ; Class1::memberFunction(int)
Anti-Analysis Techniques
Detection:
- Debugger detection (ptrace, IsDebuggerPresent)
- Timing checks
- Code obfuscation
- Anti-disassembly tricks
Countermeasures:
(gdb) break ptrace
(gdb) return 0
Common Pitfalls
| Mistake | Impact | Solution |
|---|
| Only static analysis | Miss runtime behavior | Combine static + dynamic |
| Not documenting findings | Lose context | Take detailed notes |
| Analyzing without goal | Waste time | Define specific objectives |
| Ignoring cross-references | Miss important connections | Follow all references |
| Not checking compiler version | Misinterpret artifacts | Identify compiler/flags used |
Integration with Other Skills
- skills/analysis/zero-day-hunting - Finding vulnerabilities in binaries
- skills/exploitation/exploit-dev-workflow - Exploiting discovered flaws
- skills/analysis/static-vuln-analysis - Source code analysis if available
Legal and Ethical Considerations
Authorization Required:
- Only analyze authorized software
- Respect license agreements
- Don't distribute cracked software
- Follow responsible disclosure
Legitimate Use Cases:
- Security research with permission
- Malware analysis for defense
- Own software assessment
- Educational purposes with legal samples
Success Metrics
- Understanding program functionality
- Identifying security vulnerabilities
- Extracting useful intelligence
- Creating working exploits (if authorized)
- Comprehensive documentation
References and Further Reading
- "Practical Reverse Engineering" by Dang et al.
- "The IDA Pro Book" by Chris Eagle
- "Practical Binary Analysis" by Dennis Andriesse
- Ghidra documentation and training materials
- Malware analysis books (for dynamic analysis techniques)
- Assembly language references (Intel manuals, x86-64 ABI)
- CTF write-ups for practical examples