// Keygenme Trial
Reverse Engineering · Medium · CyLab Security Academy (picoCTF)
// Summary
A trial program asks for a license key. The key is not stored anywhere in the program. Instead, the program builds the correct key from the username MILLER each time, using a fixed recipe, and compares it with what was typed. The recipe is inside the program, so reading it and following it by hand produces the key. Comparable to a lock shipped with the instructions for cutting its own key taped to the back.
// Objective
Given: keygenme-trial.py, a Python trial program that asks for a license key.
Asked: the valid key, which is the flag.
Type: stated.
// Recon
Read the source1 — Most of it is menus and a star-distance table. enter_license() passes input to check_key().
Read check_key() — Key = key_part_static1_trial + 8 dynamic characters + }. The prefix academy{1n_7h3_kk3y_of_ is in plain text.
Read the dynamic checks — Each check compares one key character to hashlib.sha256(username_trial).hexdigest2()[i]. username_trial = b"MILLER"3.
Read the check order — Positions 4, 5, 3, 6, 2, 7, 1, 8.
// Deduction
enter_license()only passes input along → the decision happens incheck_key()→ read that function.- The key is compared against values computed at runtime → the key is derived, not stored → recompute the derivation.
- Each character is
hexdigest()[i]→ the hash4 ofMILLERis the character pool, and the code sets which positions to take → both the hash and the order are needed. - The order (4, 5, 3, 6, 2, 7, 1, 8) has no pattern and starts from 1, not 0 → copy it exactly from the source.
// Solve
- Run:
Result: the 8 dynamic characters.python3 -c "import hashlib; h=hashlib.sha256(b'MILLER').hexdigest(); print(''.join(h[i] for i in [4,5,3,6,2,7,1,8]))" - Assemble:
academy{1n_7h3_kk3y_of_+ the 8 characters +}.
Result: the flag. - Enter the key at menu option (c).
Result: the key validates, then the program decrypts and writeskeygenme.py(Fernet5; seedecrypt_full_version).
// Answer
Plain: the flag is the license key itself. It has three parts: a fixed opening written in the program, eight characters picked out of a scrambled code made from the name MILLER, and a closing bracket.
Technical: the dynamic segment is sha256(b"MILLER").hexdigest() indexed6 at [4,5,3,6,2,7,1,8], concatenated between the static prefix and }. check_key() accepts exactly that string.
// Root cause
Flaw: the validation logic and the secret it depends on (username + derivation) ship to the user.
CWE: CWE-602, client-side enforcement of server-side security; CWE-656, reliance on security through obscurity.
Fix: validate keys on a server, or with asymmetric signatures7 where the client holds only a public key.
Real-world impact: anyone with the program can make valid keys, so paid features are free to everyone. This is how software "keygens8" are built.
// Transfer
- A program checks a key but never stores it → find the comparison, recompute the right side.
hexdigest()[i]→ indexing into a hex string. Index from 0; copy positions exactly.
// Index
- 1Source codeThe human-readable text a program is written in. Here, Python.
- 2Hex digestThe hash written as 64 characters using 0–9 and a–f.
- 3Bytes (
b"...")Text as raw data. A hash works on bytes, so the exact letters and case matter. - 4Hash (SHA-256)A function that turns any input into a fixed-length scrambled code. The same input always gives the same code; the code cannot be turned back into the input.
- 5FernetA standard method for encrypting and decrypting data with a key.
- 6Index
[i]A position in a string, counted from 0.[4]is the fifth character. - 7Asymmetric signatureA check that uses two keys: a private one that creates keys and a public one that can only verify them.
- 8KeygenA tool or method that produces valid license keys by copying the program's own key recipe.