Ghidra for Beginners: First CTF Reverse in 20 Minutes part 2

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
790
Reaction score
1,510
Parsing CTF tasks reverse: from the line "Wrong" to the flag

The algorithm for solving an Easy-level problem is a repeatable pattern. Here it is in full.

Step 1 - terminal reconnaissance. file→ format and architecture. strings→ suspicious lines. ltrace→ is the password leaking? strcmpIf ltrace shows the standard, the task is solved without Ghidra.

Step 2: Import. Project, file import, auto-analysis with default settings.

Step 3 — Defined Strings. Window → Defined Strings. Look for the marker strings: "Wrong", "Correct", "flag", "password"Double-click → right-click → References → Show References to Address. Navigate to the target function by xref.

Step 4 - Decompiler. Read the pseudocode of the function. Look for three constructs: data input ( scanf, fgets, gets, argv[1]), input transformation (XOR, shifts, arithmetic) and comparison with the reference ( strcmp, memcmp, straight ==).

Step 5 – Extraction or Bypass. Two options: compute the correct input from the logic (pure solution) or patch the conditional branch JNE → JE(Dirty, but fast). Both approaches count in CTF. Patching in Ghidra: right-click on the instruction in Listing → Patch Instruction → change the mnemonic. Then File → Export Program → Original File → save the patched binary.

The dirty way isn't shameful. The shameful thing is tinkering with an algorithm for four hours when you could have inverted a single byte.
XOR pattern in reverse CTF problems

The most common obfuscation technique at the entry and intermediate levels is to XOR each character of a password with a fixed key. In MITRE ATT&CK terminology, this is Obfuscated Files or Information (T1027, Defense Evasion tactic): data is obfuscated by a simple transformation. The reverse operation is Deobfuscate/Decode Files or Information (T1140).

In a decompiler, the XOR pattern looks characteristic: a cycle within which each byte of the array is xored with a constant, and the result is compared with the reference.

for (i = 0; i < 0xc; i++) {
if ((user_input ^ 0x37) != encoded) {
puts("Wrong!");
return 1;
}
}
puts("Correct!");

Here 0x37— XOR key, encoded— an array of encrypted bytes. To get the password, XOR each byte. encodedwith a key. Values encodedGet it from Ghidra: double-click the array address in Listing, read the bytes in hex format. A one-liner in Python is enough to decrypt it: ''.join(chr(b ^ 0x37) for b in encoded_bytes).

In more complex problems, the key isn't fixed—it could be an array or a calculation result. But the pattern remains the same: find the XOR operation in the decompiler, determine the key and the reference, and reverse the operation.
Stripped Binaries: When main Disappears from the Symbol Tree

At mid-level CTFs, binaries are often devoid of symbols - a function mainis not visible by name in the Functions tree. In MITRE ATT&CK terminology, these are Stripped Payloads (T1027.008, Defense Evasion Tactic): removing debug symbols makes static analysis more difficult.

Ghidra detects the entry point automatically - it appears in the Symbol Tree as entry. Double-clicking takes you to the startup code. In ELF binaries for x86-64, the entry point calls __libc_start_main, passing the address in the first argument mainIn the decompiler it looks like this __libc_start_main(FUN_00401196, ...)— address FUN_00401196and there is yours mainDouble click and you're inside.

Rename FUN_00401196 V mainRight-click → Rename Function — Ghidra will apply the new name to the entire analysis database. Navigation will work the same as for binary files with symbols.

For PE files the principle is similar: the entry point leads to CRT initialization ( _mainCRTStartup), which causes main or WinMainGhidra often detects these wrappers automatically - check the Functions branch for them. main or _main.
Packed Binaries: Recognize and Unpack

Packed binaries are another common obstacle. The packer compresses or encrypts a section of code and decompresses it into memory upon execution. In MITRE ATT&CK terminology, this is Software Packing (T1027.002, Defense Evasion tactic). Ghidra views packed code as a collection of meaningless data: the decompiler will show nothing useful, and the Defined Strings will be almost empty.

How to recognize:

stringsreturns suspiciously few rows - one or two instead of dozens.
The first bytes of the file show the packer signatures: UPX!for UPX, ASPackfor ASPack.
Ghidra may warn you about non-standard sections during import—pay attention to the dialog box.

The most common packer in CTF is UPX. Unpacking is trivial: upx -d crackmein the terminal. After unzipping, import the file into Ghidra again—the decompiler will display normal pseudocode.

For less common packers, a single command isn't enough: perform a dynamic analysis with a memory dump (run under a debugger, wait for unpacking, then dump) or manually analyze the unpacker in Ghidra. Exotic packers are rare in initial CTF tasks—but if you do, know you've been promoted from Easy to Medium.
Table of scenarios and limitations
Scenario What does file show? Actions in Ghidra
Unstripped ELF x86-64 not stripped main is visible in the Symbol Tree, standard workflow
Stripped ELF x86-64 stripped entry → __libc_start_main→ first argument = main
PE 32/64 (Windows) PE32 / PE32+ Enable Windows API propagation; search for _main
Statically linked statically linked Long list of features; ltraceuseless
Packaged (UPX) Few terms, signature UPX! upx -dbefore import
ARM / MIPS Architecture in file output Ghidra supports; decompiler output is less readable

Ghidra for Beginners works the same way in all these scenarios - only the first step changes: define through file And strings, what exactly are you dealing with?
Mistakes that cost hours

Skipping reconnaissance. The temptation to download the binary directly to Ghidra is understandable. But strings And ltraceThey save from minutes to hours. A habit is formed in three to four tasks.

Working with raw variable names. Decompiler with names iVar1 And local_28It reads like a ciphertext. Renaming two or three key variables radically changes the picture. Thirty seconds—and the code is clear.

Ignoring the Function Graph. Window → Function Graph displays the control flow graph—a visual map of branching. By default, it opens at a reduced scale, making it difficult to see. Right-clicking → Properties → Start Fully Zoomed In solves the problem. The graph helps you see the structure. if-elseand cycles faster than flat listing.

Fear of assembler. A decompiler is the primary tool when starting out. The assembly code in the Listing is for reference purposes. Don't try to read the entire listing: click on a line in the Decompiler to see the corresponding assembler. This way, mnemonics are understood contextually, not as an abstract table.

Incorrect export during patching. After the Patch Instruction, export via File → Export Program → Original File. Other formats may result in a non-working binary. During one analysis, a newbie exported to Binary and then spent an hour trying to figure out why the patched crackme wouldn't launch.

Most reverse engineering tutorials for beginners start with processor architecture, mnemonics MOV And JMP, registers RAX And RSPSounds right—assembler is the foundation. In practice, beginners who spend three weeks cramming x86-64 ISA before opening Ghidra burn out before they even solve their first crackme. Experience from dozens of mentoring sessions shows that the opposite approach works better. Open the decompiler, read the pseudocode, rename variables, solve the problem—and along the way, you notice how if (iVar1 == 0)turns into CMP + JNZAssembler is learned through context, not memorization. After solving
 
Top Bottom