cybersecurity · beginner · ~15 min · safe pentest lab
Implement the dictionary-attack loop and feel why fast hashes + weak passwords are crackable.
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.
Implement int crack(unsigned long target, const char **words, int n, char *out) that finds the wordlist entry whose hash equals target.
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.unsigned long thash(const char *) (declared for you) — a toy stand-in for a password hash.Returns int: 1 if some words[i] hashes to target (copying that word into out), else 0.
words = {"apple","secret","hunter2",...}
crack(thash("secret"), words, n, out) -> 1, out = "secret"
crack(99UL, words, n, out) -> 0
A target digest, a wordlist words of n strings, and an output buffer out. thash() is provided.
An int: 1 with the matching word in out, else 0.
Hash each candidate with thash; return on the first match. In-memory only.
#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;
}
Not returning 0 on no match; writing to out before confirming a match; off-by-one on the loop bound.
Target not in the list (return 0). First and last word matching.
O(n × word length).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.