Reverse engineering is reading a program to understand what it does without
source code, usually to find how it validates a flag, hides a secret, or
transforms input. This skill covers the standard authorized CTF reversing
workflow: quick triage to classify the binary, static analysis in Ghidra to
read the logic, dynamic analysis in gdb to observe runtime values, handling
the obfuscation tricks CTF authors love, and writing a solver that
reproduces the flag. Everything here applies to CTF competitions, lab
binaries, and software you have explicit permission to analyze.
Quick Workflow
- Triage the file:
file, strings, checksec, and ltrace/strace
to classify it (native, packed, .NET, script bundled, VM).
- Look for the flag format or input prompt in strings; many easy
challenges end here.
- Open the binary in Ghidra, run auto-analysis, and find
main (via
entry -> __libc_start_main argument on Linux).
- Read the decompilation of the input-checking function; rename
variables and add comments as you understand them.
- Identify the transform: XOR loop, arithmetic, table lookup, hash
compare, or custom VM; note constants and expected values.
- If static reading stalls, run the binary under gdb, break at the
check function, and inspect registers and memory with real input.
- Write a Python solver that inverts the transform (reverse the XOR,
brute force the key, or reimplement the algorithm).
- Validate the solver output matches the expected flag format before
submitting.
1. Triage: file, strings, and behavior
Two minutes of triage often classifies the whole challenge.
file ./rev
# ELF 64-bit LSB executable, x86-64 ... not stripped -> native, symbols present
# ELF 64-bit ... stripped -> native, harder
# PE32 executable -> Windows binary (use wine or a VM)
strings -n 8 ./rev | head -40
# look for: flag{, "Enter password", "Correct", "Wrong", suspicious base64
checksec ./rev # protections matter less for reversing, but note PIE for gdb
# watch what it does without reading code
echo "test" | strace -f -e trace=read,write ./rev 2>&1 | head -30
ltrace -e strcmp,memcmp ./rev <<< "test" # reveals comparison calls and args
If strings shows UPX markers or section names like UPX0/UPX1, the
binary is packed: upx -d ./rev -o ./rev_unpacked and restart triage on
the unpacked file.
2. Ghidra static analysis essentials
Ghidra's auto-analysis plus decompiler view is the fastest path to
understanding.
- Import the binary, let analysis finish, open the CodeBrowser.
- In the Symbol Tree, find
main (or entry, then follow the first
argument of __libc_start_main).
- Read the decompiler panel: look for input functions (
scanf, fgets,
read), then the checking logic after them.
- Rename variables (press
L): local_28 becomes user_input,
local_10 becomes expected.
- Follow cross-references (right-click -> References) on interesting
strings like
"Correct!" to land on the check function.
- For data tables, hover to see bytes; right-click -> Data -> Array to
view a 256 byte substitution table properly.
Typical flag-check shape in decompilation:
fgets(user_input, 64, stdin);
for (i = 0; i < 32; i++) {
if ((user_input[i] ^ 0x42) != expected[i]) {
puts("Wrong!");
return 1;
}
}
puts("Correct!");
The solver inverts it: flag[i] = expected[i] ^ 0x42. Read expected
from Ghidra's listing (the .rodata bytes) and script the rest.
3. Dynamic analysis with gdb
When static reading is ambiguous (computed values, anti-debug timing),
watch the program run.
gdb -q ./rev
(gdb) break main
(gdb) run <<< "AAAAAAAAAAAAAAAA"
(gdb) break *0x5555555551a9 # address of the check loop from Ghidra
(gdb) continue
(gdb) x/32bx $rsi # inspect the buffer being checked
(gdb) info registers rax rdi # watch comparison results
(gdb) stepi / nexti # walk the transform instruction by instruction
Useful patterns: break on strcmp/memcmp and print arguments
(x/s $rdi, x/s $rsi) to see exactly what the program compares your
input against; this alone solves many challenges. For ptrace-based
anti-debugging, patch the check out (set $rax = 0 after the ptrace
call) or use Frida to hook instead of gdb.
4. Common transforms and solver patterns
Most CTF reversing challenges use a small set of transforms. Recognize the
shape, then invert it.
#!/usr/bin/env python3
# Pattern A: single-byte XOR with constant key
expected = bytes.fromhex("...") # copied from Ghidra .rodata
flag = bytes(b ^ 0x42 for b in expected)
# Pattern B: multi-byte repeating XOR key
key = b"secret"
flag = bytes(b ^ key[i % len(key)] for i, b in enumerate(expected))
# Pattern C: arithmetic (add/sub with wraparound) -> invert the operation
flag = bytes(((b - 7) & 0xff) for b in expected) # program did (c + 7) & 0xff
# Pattern D: position-dependent (i added to each byte)
flag = bytes(((b - i) & 0xff) for i, b in enumerate(expected))
# Pattern E: substitution table (256 byte S-box in .rodata)
sbox = bytes([...]) # copied from Ghidra
inv = bytes(sbox.index(b) for b in range(256))
flag = bytes(inv[b] for b in expected)
When the transform is not invertible by inspection (hash compare like
sha256(input) == digest), the intended path is usually brute force over
a small input space or a keygen-me where you reimplement the algorithm;
check the input length first.
5. Handling obfuscation
CTF authors add layers: packing, string encryption, control-flow
flattening, and custom VMs.
- Packed (UPX, custom): unpack first (
upx -d); for custom packers,
let it run to the original entry point in gdb, then dump memory
(gcore or dump binary memory).
- Encrypted strings: find the decryption routine (called early, loops
over
.rodata), break after it in gdb, and read the decrypted strings
from memory.
- Control-flow flattening: the decompiler shows one giant switch loop;
focus on the basic blocks that touch input and the success string,
ignore dispatcher noise.
- Custom VM: identify the opcode dispatch table and handlers, then either
reimplement the VM in Python or hook it with Frida to log execution.
Edge cases
- Stripped Go or Rust binaries: huge, unfamiliar control flow; search
strings for the prompt text and work backward from there instead of
reading
main.
- The binary is a PyInstaller bundle: extract with
pyinstxtractor,
then decompile the .pyc with uncompyle6 or pycdc.
- .NET assemblies: use
ilspyc or dnSpy-style decompilers, not Ghidra;
the logic is usually plain C#.
- Input is read from argv or a file, not stdin: check
main's argument
handling and any fopen calls before assuming interactive input.
- The check runs in a child process or thread: attach gdb with
set follow-fork-mode child or break on clone.
- Anti-debug
ptrace: the program exits under gdb; bypass by patching
the binary, using LD_PRELOAD to fake ptrace, or switching to Frida
instrumentation.
Escalation
- Ghidra decompilation is unreadable (flattened or virtualized): drop
to the disassembly view and trace data flow manually, or switch to
dynamic analysis and let the program show you the values.
- The transform uses unknown constants from memory: break in gdb right
before the check and dump the buffers; runtime values beat static
guessing.
- Solver output is close but not the flag format: check for off-by-one
in the loop bound, an extra newline in expected data, or a final
transform applied after the loop you reversed.
- Custom crypto you cannot invert: look for the encryption routine being
called with known plaintext (chosen-plaintext in the binary itself),
which often leaks the key schedule.
- Time is running out: search strings for partial flags, try the
challenge author's handle as XOR key, and check for
flag.txt
embedded as a resource.
Detection and defender view
Defenders encounter reversing from the other side: malware analysts use
exactly these techniques on hostile binaries, inside isolated sandboxes,
with network access cut. For software protection, defenders use the same
obfuscation (packing, control-flow flattening, white-box crypto) to raise
the cost of analysis, plus license checks and tamper detection. Detection
of malicious reversing activity focuses on debugging artifacts (unexpected
ptrace, debuggers attached to production processes) and on telemetry
from EDR tools watching process memory dumps. The durable lesson: never
embed secrets in client-side code; any check the client performs can be
read and inverted.
Key references
- MITRE ATT&CK T1027 (Obfuscated Files or Information) and T1140
(Deobfuscate/Decode Files or Information): the obfuscation half.
- CWE-798 (Use of Hard-coded Credentials): embedded secrets and keys.
- CWE-327 (Use of a Broken or Risky Cryptographic Algorithm): weak
custom transforms.
- Ghidra documentation: https://ghidra-sre.org/documentation.html
- GDB manual: https://sourceware.org/gdb/documentation/
- "Practical Malware Analysis" (Sikorski and Honig): chapters on static
and dynamic analysis transfer directly to CTF reversing.
See also
ghidra-basics: Use it instead for the Ghidra-only tool walkthrough, without the competition reversing workflow.
1---2name: ctf-reversing-basics3description: Use when competing in a CTF reverse engineering challenge,4 practicing in a lab, or analyzing a binary you are authorized to examine.5 Covers the reversing workflow, identifying file type and protections,6 triaging with strings and basic static analysis, navigating Ghidra7 decompilation to find the flag logic, dynamic debugging with gdb to watch8 values at runtime, defeating common obfuscation (packing, XOR, control9 flow flattening), and scripting keygen or decryption solvers in Python.10 Use when a challenge hands you a binary that checks input or transforms11 data, when strings show no flag, or when you need to understand what a12 program does before exploiting or bypassing it.13version: 1.0.014domain: ctf15lifecycle: stable16last_verified: "2026-10-23"17requires: []18extends: []19relates: [ghidra-basics, ctf-pwn-basics]20tools:21 - name: Ghidra22 version: "11.3"23 - name: GDB24 version: "15.2"25 - name: Python26 version: "3.12"27 - name: binutils28 version: "2.42"29 - name: Frida30 version: "16.5"31metadata:32 source: https://skillmd.com/skills/mizazhaider-ceh/ctf-reversing-basics33---3435Reverse engineering is reading a program to understand what it does without36source code, usually to find how it validates a flag, hides a secret, or37transforms input. This skill covers the standard authorized CTF reversing38workflow: quick triage to classify the binary, static analysis in Ghidra to39read the logic, dynamic analysis in gdb to observe runtime values, handling40the obfuscation tricks CTF authors love, and writing a solver that41reproduces the flag. Everything here applies to CTF competitions, lab42binaries, and software you have explicit permission to analyze.4344## Quick Workflow45461. Triage the file: `file`, `strings`, `checksec`, and `ltrace`/`strace`47 to classify it (native, packed, .NET, script bundled, VM).482. Look for the flag format or input prompt in strings; many easy49 challenges end here.503. Open the binary in Ghidra, run auto-analysis, and find `main` (via51 `entry` -> `__libc_start_main` argument on Linux).524. Read the decompilation of the input-checking function; rename53 variables and add comments as you understand them.545. Identify the transform: XOR loop, arithmetic, table lookup, hash55 compare, or custom VM; note constants and expected values.566. If static reading stalls, run the binary under gdb, break at the57 check function, and inspect registers and memory with real input.587. Write a Python solver that inverts the transform (reverse the XOR,59 brute force the key, or reimplement the algorithm).608. Validate the solver output matches the expected flag format before61 submitting.6263## 1. Triage: file, strings, and behavior6465Two minutes of triage often classifies the whole challenge.6667```bash68file ./rev69# ELF 64-bit LSB executable, x86-64 ... not stripped -> native, symbols present70# ELF 64-bit ... stripped -> native, harder71# PE32 executable -> Windows binary (use wine or a VM)7273strings -n 8 ./rev | head -4074# look for: flag{, "Enter password", "Correct", "Wrong", suspicious base647576checksec ./rev # protections matter less for reversing, but note PIE for gdb7778# watch what it does without reading code79echo "test" | strace -f -e trace=read,write ./rev 2>&1 | head -3080ltrace -e strcmp,memcmp ./rev <<< "test" # reveals comparison calls and args81```8283If `strings` shows `UPX` markers or section names like `UPX0`/`UPX1`, the84binary is packed: `upx -d ./rev -o ./rev_unpacked` and restart triage on85the unpacked file.8687## 2. Ghidra static analysis essentials8889Ghidra's auto-analysis plus decompiler view is the fastest path to90understanding.91921. Import the binary, let analysis finish, open the CodeBrowser.932. In the Symbol Tree, find `main` (or `entry`, then follow the first94 argument of `__libc_start_main`).953. Read the decompiler panel: look for input functions (`scanf`, `fgets`,96 `read`), then the checking logic after them.974. Rename variables (press `L`): `local_28` becomes `user_input`,98 `local_10` becomes `expected`.995. Follow cross-references (right-click -> References) on interesting100 strings like `"Correct!"` to land on the check function.1016. For data tables, hover to see bytes; right-click -> Data -> Array to102 view a 256 byte substitution table properly.103104Typical flag-check shape in decompilation:105106```c107fgets(user_input, 64, stdin);108for (i = 0; i < 32; i++) {109 if ((user_input[i] ^ 0x42) != expected[i]) {110 puts("Wrong!");111 return 1;112 }113}114puts("Correct!");115```116117The solver inverts it: `flag[i] = expected[i] ^ 0x42`. Read `expected`118from Ghidra's listing (the `.rodata` bytes) and script the rest.119120## 3. Dynamic analysis with gdb121122When static reading is ambiguous (computed values, anti-debug timing),123watch the program run.124125```bash126gdb -q ./rev127(gdb) break main128(gdb) run <<< "AAAAAAAAAAAAAAAA"129(gdb) break *0x5555555551a9 # address of the check loop from Ghidra130(gdb) continue131(gdb) x/32bx $rsi # inspect the buffer being checked132(gdb) info registers rax rdi # watch comparison results133(gdb) stepi / nexti # walk the transform instruction by instruction134```135136Useful patterns: break on `strcmp`/`memcmp` and print arguments137(`x/s $rdi`, `x/s $rsi`) to see exactly what the program compares your138input against; this alone solves many challenges. For `ptrace`-based139anti-debugging, patch the check out (set `$rax = 0` after the ptrace140call) or use Frida to hook instead of gdb.141142## 4. Common transforms and solver patterns143144Most CTF reversing challenges use a small set of transforms. Recognize the145shape, then invert it.146147```python148#!/usr/bin/env python3149# Pattern A: single-byte XOR with constant key150expected = bytes.fromhex("...") # copied from Ghidra .rodata151flag = bytes(b ^ 0x42 for b in expected)152153# Pattern B: multi-byte repeating XOR key154key = b"secret"155flag = bytes(b ^ key[i % len(key)] for i, b in enumerate(expected))156157# Pattern C: arithmetic (add/sub with wraparound) -> invert the operation158flag = bytes(((b - 7) & 0xff) for b in expected) # program did (c + 7) & 0xff159160# Pattern D: position-dependent (i added to each byte)161flag = bytes(((b - i) & 0xff) for i, b in enumerate(expected))162163# Pattern E: substitution table (256 byte S-box in .rodata)164sbox = bytes([...]) # copied from Ghidra165inv = bytes(sbox.index(b) for b in range(256))166flag = bytes(inv[b] for b in expected)167```168169When the transform is not invertible by inspection (hash compare like170`sha256(input) == digest`), the intended path is usually brute force over171a small input space or a keygen-me where you reimplement the algorithm;172check the input length first.173174## 5. Handling obfuscation175176CTF authors add layers: packing, string encryption, control-flow177flattening, and custom VMs.178179- Packed (UPX, custom): unpack first (`upx -d`); for custom packers,180 let it run to the original entry point in gdb, then dump memory181 (`gcore` or `dump binary memory`).182- Encrypted strings: find the decryption routine (called early, loops183 over `.rodata`), break after it in gdb, and read the decrypted strings184 from memory.185- Control-flow flattening: the decompiler shows one giant switch loop;186 focus on the basic blocks that touch input and the success string,187 ignore dispatcher noise.188- Custom VM: identify the opcode dispatch table and handlers, then either189 reimplement the VM in Python or hook it with Frida to log execution.190191## Edge cases192193- Stripped Go or Rust binaries: huge, unfamiliar control flow; search194 strings for the prompt text and work backward from there instead of195 reading `main`.196- The binary is a PyInstaller bundle: extract with `pyinstxtractor`,197 then decompile the `.pyc` with `uncompyle6` or `pycdc`.198- .NET assemblies: use `ilspyc` or dnSpy-style decompilers, not Ghidra;199 the logic is usually plain C#.200- Input is read from argv or a file, not stdin: check `main`'s argument201 handling and any `fopen` calls before assuming interactive input.202- The check runs in a child process or thread: attach gdb with203 `set follow-fork-mode child` or break on `clone`.204- Anti-debug `ptrace`: the program exits under gdb; bypass by patching205 the binary, using `LD_PRELOAD` to fake ptrace, or switching to Frida206 instrumentation.207208## Escalation209210- Ghidra decompilation is unreadable (flattened or virtualized): drop211 to the disassembly view and trace data flow manually, or switch to212 dynamic analysis and let the program show you the values.213- The transform uses unknown constants from memory: break in gdb right214 before the check and dump the buffers; runtime values beat static215 guessing.216- Solver output is close but not the flag format: check for off-by-one217 in the loop bound, an extra newline in expected data, or a final218 transform applied after the loop you reversed.219- Custom crypto you cannot invert: look for the encryption routine being220 called with known plaintext (chosen-plaintext in the binary itself),221 which often leaks the key schedule.222- Time is running out: search strings for partial flags, try the223 challenge author's handle as XOR key, and check for `flag.txt`224 embedded as a resource.225226## Detection and defender view227228Defenders encounter reversing from the other side: malware analysts use229exactly these techniques on hostile binaries, inside isolated sandboxes,230with network access cut. For software protection, defenders use the same231obfuscation (packing, control-flow flattening, white-box crypto) to raise232the cost of analysis, plus license checks and tamper detection. Detection233of malicious reversing activity focuses on debugging artifacts (unexpected234`ptrace`, debuggers attached to production processes) and on telemetry235from EDR tools watching process memory dumps. The durable lesson: never236embed secrets in client-side code; any check the client performs can be237read and inverted.238239## Key references240241- MITRE ATT&CK T1027 (Obfuscated Files or Information) and T1140242 (Deobfuscate/Decode Files or Information): the obfuscation half.243- CWE-798 (Use of Hard-coded Credentials): embedded secrets and keys.244- CWE-327 (Use of a Broken or Risky Cryptographic Algorithm): weak245 custom transforms.246- Ghidra documentation: https://ghidra-sre.org/documentation.html247- GDB manual: https://sourceware.org/gdb/documentation/248- "Practical Malware Analysis" (Sikorski and Honig): chapters on static249 and dynamic analysis transfer directly to CTF reversing.250251## See also252253- `ghidra-basics`: Use it instead for the Ghidra-only tool walkthrough, without the competition reversing workflow.