C:\Seyi\Portfolio\writeups\keygenme_trial_ Y:0000PX
...\writeups\keygenme_trial Y:0000PX

< back to write-ups

CTF // MEDIUM keygenme_trial.md

// 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 — 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).()[i]. username_trial = .
Read the check order — Positions 4, 5, 3, 6, 2, 7, 1, 8.

// Deduction

  1. enter_license() only passes input along → the decision happens in check_key() → read that function.
  2. The key is compared against values computed at runtime → the key is derived, not stored → recompute the derivation.
  3. Each character is hexdigest()[i] → the of MILLER is the character pool, and the code sets which positions to take → both the hash and the order are needed.
  4. 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

  1. Run:
    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]))"
    Result: the 8 dynamic characters.
  2. Assemble: academy{1n_7h3_kk3y_of_ + the 8 characters + }.
    Result: the flag.
  3. Enter the key at menu option (c).
    Result: the key validates, then the program decrypts and writes keygenme.py (; see decrypt_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() 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 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 "" 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

  1. 1Source codeThe human-readable text a program is written in. Here, Python.
  2. 2Hex digestThe hash written as 64 characters using 0–9 and a–f.
  3. 3Bytes (b"...")Text as raw data. A hash works on bytes, so the exact letters and case matter.
  4. 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.
  5. 5FernetA standard method for encrypting and decrypting data with a key.
  6. 6Index [i]A position in a string, counted from 0. [4] is the fifth character.
  7. 7Asymmetric signatureA check that uses two keys: a private one that creates keys and a public one that can only verify them.
  8. 8KeygenA tool or method that produces valid license keys by copying the program's own key recipe.