cybersecurity · beginner · ~15 min · safe pentest lab

Dictionary attack against a hash

Implement the dictionary-attack loop and feel why fast hashes + weak passwords are crackable.

Challenge

Implement the core loop of an offline dictionary attack against a hash — the mechanic that makes fast hashes plus weak passwords instantly crackable, which is why slow KDFs exist.

Task

Implement int crack(unsigned long target, const char **words, int n, char *out) that finds the wordlist entry whose hash equals target.

Input

  • target: the target digest to match.
  • words, n: a fixed wordlist of n candidate strings and its length, supplied by the grader.
  • out: a buffer to receive the matching word.
  • The grader also provides unsigned long thash(const char *) (declared for you) — a toy stand-in for a password hash.

Output

Returns int: 1 if some words[i] hashes to target (copying that word into out), else 0.

Example

words = {"apple","secret","hunter2",...}
crack(thash("secret"), words, n, out)   ->   1, out = "secret"
crack(99UL, words, n, out)              ->   0

Edge cases

  • Return 0 when the target is not in the list.
  • On the first match, copy and return immediately.

Rules

  • Operates on an in-memory wordlist and a toy hash only — no real credentials, no I/O.

Input format

A target digest, a wordlist words of n strings, and an output buffer out. thash() is provided.

Output format

An int: 1 with the matching word in out, else 0.

Constraints

Hash each candidate with thash; return on the first match. In-memory only.

Starter code

#include <stddef.h>

/* Provided by the grader: a (toy) hash of a NUL-terminated string. */
unsigned long thash(const char *s);

int crack(unsigned long target, const char **words, int n, char *out) {
    /* TODO: hash each word; on match, copy to out and return 1; else 0. */
    (void)target;(void)words;(void)n; out[0]='\0'; return 0;
}

Common mistakes

Not returning 0 on no match; writing to out before confirming a match; off-by-one on the loop bound.

Edge cases to handle

Target not in the list (return 0). First and last word matching.

Complexity

O(n × word length).

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.