13.8 — Project: make and crack your own crackme
A crackme is a small program that asks for a secret, made on purpose to be reversed. Let's make our own and crack it in three levels.
Level 1: the password in plain sight#
#include <iostream>#include <string> int main(){ std::cout << "Password: "; std::string guess {}; std::cin >> guess; if (guess == "c0ffee42") std::cout << "Access granted!\n"; else std::cout << "Wrong password.\n"; return 0;}Compile it, then pretend you've forgotten the password. Run strings on the program:
strings ./crackme1...Password:c0ffee42Access granted!Wrong password....Cracked in one second! The password sits right next to the messages.
Never store secrets as plain text inside a program. Anyone can read them.
Level 2: hide the password#
Let's store the password scrambled with XOR (^), so strings can't see it:
#include <iostream>#include <string> // "c0ffee42" with every byte XOR-ed with 0x5Aconstexpr unsigned char hidden[] { 0x39, 0x6a, 0x3c, 0x3c, 0x3f, 0x3f, 0x6e, 0x68 };constexpr unsigned char key { 0x5A }; bool check(const std::string& guess){ if (guess.size() != sizeof(hidden)) return false; for (std::size_t i { 0 }; i < guess.size(); ++i) { if ((static_cast<unsigned char>(guess[i]) ^ key) != hidden[i]) return false; } return true;} int main(){ std::cout << "Password: "; std::string guess {}; std::cin >> guess; if (check(guess)) std::cout << "Access granted!\n"; else std::cout << "Wrong password.\n"; return 0;}A bit operation (^ in C++). Doing x ^ key scrambles a byte, and doing ^ key again un-scrambles it. Simple to use, but also simple to undo once you spot it.
Now strings shows the messages, but not the password. Time for real tools.
Level 3: crack it with Ghidra#
- Open the program in Ghidra and find
main. - The decompiler shows a call to a function that is checked before "Access granted!" — that's
check(or an unnamedFUN_...if names were stripped). - Open it. You'll find a loop that XORs each character with
0x5aand compares it to bytes in a table. - Double-click the table to see its bytes:
39 6a 3c 3c 3f 3f 6e 68. - XOR each one with
0x5a(by hand, or with a tiny C++ program) to get the password back.
Here's that tiny program:
#include <iostream> int main(){ const unsigned char hidden[] { 0x39, 0x6a, 0x3c, 0x3c, 0x3f, 0x3f, 0x6e, 0x68 }; for (unsigned char b : hidden) std::cout << static_cast<char>(b ^ 0x5A); std::cout << '\n'; return 0;}c0ffee42Level 4: change the program's mind (patching)#
Instead of finding the password, you can change the program — this is called patching. Do this only on your own programs and crackmes.
- Open your program in x64dbg (Windows) or gdb (Linux).
- Find the string reference "Access granted!" and look just above it.
- You'll see the result of
checkbeing tested, then a conditional jump (jeorjne) that skips to "Wrong password". - Change that jump to the opposite one (or to
nops). Save the patched copy. - Now any password is accepted.
This is why real software never trusts checks inside the program alone. Important checks (like licences or logins) must also happen on a server the user can't change.
Ideas to make your crackme harder: compute the key instead of storing it, check the length last, or split the check across several functions. Then swap with a friend and crack each other's!
Tip: press ← / → to move between lessons, / to search.