Reverse-engineering crackme in Ghidra: writing a keygen part 2

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
790
Reaction score
1,510
How to write a keygen in Python

A keygen is a program that generates a valid serial number or password. For an XOR-based crack, writing a keygen in Python is trivial if the verification algorithm is recovered from a decompiler.
Reversing the Algorithm: From Verification to Generation

The key property of XOR is that the operation is invertible to itself. If result = input ^ key, That input = result ^ key. Arrays xor_key And expectedalready extracted from the Ghidra decompiler. All that's left is to XOR them. expectedwith xor_key:

# Кейген: обращаем XOR-проверку серийного номера
xor_key = [0x5f, 0x4a, 0x7d, 0x2b, 0x38, 0x6e]
expected = [0x3a, 0x2f, 0x1c, 0x4e, 0x5b, 0x0d]
serial = ''.join(chr(e ^ k) for e, k in zip(expected, xor_key))
print(f"Valid serial: {serial}")

Five lines of code. Run it and you get a valid serial number. Enter "Key is Correct!" into crackme. The problem was solved not by bypassing the check, but by understanding the algorithm.

Where to get specific array bytes: In Ghidra, double-click on the array name (e.g. DAT_00409480) in the decompiler window. Ghidra will take you to the Listing window at the array address, where the byte sequence will be displayed. Write them down or copy them by right-clicking → Copy Special → Byte String.

For crackme with arithmetic transformations (addition, multiplication, bitwise shifts), the principle is similar: reconstruct the formula from pseudocode, apply the inverse operation. Addition → subtraction. Left shift → right shift. Multiplication → division (taking into account integer arithmetic and possible overflow—be careful here).

A special case is crackme with a username binding (name-serial). The program takes a name and serial number, calculates a hash from the name, and compares it with the serial number. The keygen for such crackme takes the name as input and calculates the serial number using the same formula. There's no need to reverse the hash here—you need to reproduce the direct calculation. Essentially, you copy the algorithm from the decompiler into Python and run it with the desired name.

If a cryptographic hash function (CRC32, MD5, SHA) is present in the transformation chain, it cannot be inverted algebraically. Brute-force attack is possible for CRC32, as the space of 32-bit values is finite. Brute-force attack is impractical for MD5/SHA, and the problem is reduced to a different approach: collision detection or limiting the input space. This is rare at the entry-level crackme levels.
Bypassing License Verification: Patching vs. Keygen

An alternative to a keygen is binary patching: changing bytes so that the program accepts any input. A classic reverse engineering technique is replacing a conditional jump. JNE (Jump if Not Equal, опкод 0x75) on JE (Jump if Equal, опкод 0x74) or two NOP(No Operation, opcode 0x90). One or two bytes - and the check is inverted or cut out completely.

In Ghidra: right-click on the assembly instruction in Listing → Patch Instruction. Change the mnemonics and export via File → Export Program. In x64dbg: set a breakpoint (F2) on the conditional jump after TEST EAX, EAX or CMP EAX, 0When triggered, double-click and replace the instruction. A change made through the debugger lasts until a restart; a permanent patch is created by editing bytes in the file.

Patching solves the "bypass check" problem. But it doesn't solve the reverse engineering problem.

In CTFs, the flag is often generated by the program from a valid serial number. A patch removes the check but doesn't supply a valid input—the flag won't be calculated. For crackme with a name-serial binding, the patch will return "Correct!" for anything, but won't generate the correct serial number for a specific name. Skills in algorithm recovery and keygen writing are what are actually tested in CTFs and asked about in interviews.

Patching is useful as an intermediate step: inverting the condition to confirm you've found the correct location in the code. But the ultimate goal of crackme analysis in Ghidra is the keygen.
Disassembling stripped and packed crackme

Two cases where the standard "strings → XREF → function" approach starts to slip.

Stripped binaries. Function names have been removed (Stripped Payloads, T1027.008 according to MITRE ATT&CK): instead mainyou will see FUN_00401000, instead of check_password — FUN_00401234. String literals remain in the section. .rodata(ELF) or .rdata(PE) — strip doesn't touch them. The approach remains the same: Defined Strings → XREF → jump to function. The only difference is that all functions will have to be renamed manually.

To search mainin stripped ELF: open entryIn the Symbol Tree, find the challenge __libc_start_main- its first argument contains the address mainIn PE binaries mainusually called from entrythrough 1-2 intermediate CRT initialization functions. Look for the function that calls GetCommandLineAor contains an input processing loop.

Packed binaries. When using packers ( Software Packing, T1027.002 according to MITRE ATT&CK ), Ghidra will not find any strings or functions other than entryCharacteristic feature: sections UPX0, UPX1in the Program Tree instead of the standard ones .text, .dataThe binary is compressed with UPX. Solution: unpack it before analysis with the command upx -d crackmeAfter unpacking, standard import and automatic analysis in Ghidra.

Custom packers (not UPX) require dynamic analysis: running under a debugger, waiting for self-extraction, and a memory dump. At the entry-level levels of crackmes.one and in CTF for beginners, UPX covers the vast majority of cases. Incidentally, Windows Defender can be triggered by obfuscated binaries—another reason to run in an isolated VM.

A special case is encrypted strings ( Obfuscated Files or Information, T1027 under MITRE ATT&CK ). If the crackme author omitted the strings "Try Again!" and "Correct!" via XOR or base64 before compiling, Defined Strings won't show them. Hint: look in the function list for calls that accept a byte array and return a string. Double-clicking such a function often reveals a simple decryptor. The decrypted strings are then routed to the verification function via the standard XREF path.
 
Top Bottom