Skip to the content ForoHackers
Log inJoin

Spotting djb2 inside a practice crackme (my cat helped)

CTFReverse engineeringSecurity education
#1031 · cr4ckm3_c4t · · Report

Notes from a crackme I solve for practice, one of the beginner ones made to be solved. My cat walked across the keyboard twice. The second time it found a bug in my notes, so I count her as a reviewer.

The check function, after I tidied the decompiler output:

int check(const char *s) {
    unsigned int h = 5381;
    while (*s) {
        h = h * 33 + (unsigned char)*s;
        s++;
    }
    return h == 0x0002b5e3;
}

Two constants give it away: 5381 as the starting value and 33 as the multiplier. That is the djb2 string hash (h = h * 33 + c). Recognising common constants is half of reverse engineering: they tell you what an algorithm is without reading every instruction. Others I look for are 0x67452301 (MD5/SHA-1 init), 0x9e3779b9 (a golden-ratio constant used in TEA and hash mixing), and the CRC32 polynomial 0xedb88320.

The lesson for the crackme: the program does not store the password, only a 32-bit hash of it. A hash this small has many inputs that give the same value, so "the" password is not unique. That is a weakness of this design, and a nice reminder for real code: never use a 32-bit non-cryptographic hash to check a secret.

I solved it on my own machine, offline, with the challenge's own binary. Cat approves.

#1501 · hexdump_hermit · · Report

Correct. And 0x0002b5e3 is small enough that the loop probably never overflowed for a short input, so you can reason about the length too. Nice detail.

#1502 · n0p_sl3d · · Report

The "constants as fingerprints" trick is the one I teach first in CTF write-ups. People expect magic, and it is a lookup table in your head. Add the AES S-box start 63 7c 77 7b to the list.

#1503 · g0blin_r00t · · Report

Lab rules, as always: own binaries, own VM, snapshots before you run anything. Good write-up. The cat deserves a flag in the credits.

#1504 · cr4ckm3_c4t · · Report

She has been informed. She is unmoved.

Log in or join to reply.