//! Cracker attack modes and the worker threads driving them. //! //! Everything here runs in the UI process — the candidates and the GPU //! need no privileges and **no radio**: wordlist / brute-force candidates //! go through PBKDF2 (GPU when an adapter exists, CPU fallback), a rainbow //! table ([`crate::rainbow`]) skips PBKDF2 entirely and checks precomputed //! PMKs with one PTK derivation + MIC compare each. //! //! Worker contract: progress via `CrackProgress` (its `done` is an exact //! RAW stream position — the session v2 resume index, see [`RawPosIter`]), //! then exactly one terminator — `CrackFound` + `CrackStopped { found: //! true }` on success, `CrackStopped { found: false }` on stop, //! exhaustion, or setup failure, `CrackError { reason }` on backend //! failure (GPU device lost / dispatch timeout / worker panic: NOT //! exhaustion; the resume session is left in place). The rainbow *builder* //! job reports `RainbowProgress` and always ends with one `RainbowBuilt` //! (entries written so far; errors precede it as `Error` events). use std::collections::HashSet; use std::path::{Path, PathBuf}; use std::rc::Rc; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::{Duration, Instant}; use anyhow::{Context, Result, bail}; use crossbeam_channel::Sender; use crate::common::Event; use crate::crypto::{self, CrackTarget}; use crate::gpu::{self, GpuCracker}; use crate::rainbow; use crate::store; use crate::wordlist; /// Attack mode selected on the Cracker page. Serialized into the resume /// session file (see [`crate::store::CrackSession`]) exactly as entered. #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum Attack { /// Dictionary: stream a wordlist through PBKDF2. Wordlist(PathBuf), /// Dictionary × mutation rules (hashcat-subset ops, see [`parse_rules`]). Rules { wordlist: PathBuf, rules: PathBuf }, /// Dictionary × append-charset sweep: `word + charset^k` for k in `min..=max`. Hybrid { wordlist: PathBuf, charset: Vec, min: usize, max: usize, }, /// Exhaustive `charset^len` sweep for len in `min..=max` through PBKDF2. /// `freq_order` permutes the charset most-common-first (a bijection — /// space size and seek indices are unchanged). Bruteforce { charset: Vec, min: usize, max: usize, #[serde(default)] freq_order: bool, }, /// Precomputed PMK table for the target SSID — no PBKDF2 at run time. Rainbow(PathBuf), } impl Attack { /// One line for logs/status. pub fn describe(&self) -> String { match self { Attack::Wordlist(p) => format!("wordlist {}", p.display()), Attack::Rules { wordlist, rules } => { format!("rules {} × {}", wordlist.display(), rules.display()) } Attack::Hybrid { wordlist, charset, min, max, } => format!( "hybrid {} + charset({} bytes) suffix len {min}..={max}", wordlist.display(), charset.len() ), Attack::Bruteforce { charset, min, max, freq_order, } => format!( "bruteforce charset({} bytes, {:?}…{}) len {min}..={max}", charset.len(), String::from_utf8_lossy(&charset[..charset.len().min(16)]), if *freq_order { ", freq-order" } else { "" }, ), Attack::Rainbow(p) => format!("rainbow {}", p.display()), } } /// Fail-fast validation the UI runs BEFORE marking a run active, so a /// refused start never leaves stale running flags or a zombie thread. pub fn validate(&self) -> Result<()> { match self { Attack::Wordlist(p) => { std::fs::File::open(p) .with_context(|| format!("cannot open wordlist {}", p.display()))?; } Attack::Rules { wordlist, rules } => { std::fs::File::open(wordlist) .with_context(|| format!("cannot open wordlist {}", wordlist.display()))?; let parsed = parse_rules(rules) .with_context(|| format!("cannot parse rules file {}", rules.display()))?; if parsed.is_empty() { bail!("rules file {} has no rules", rules.display()); } } Attack::Hybrid { wordlist, charset, min, max, } => { std::fs::File::open(wordlist) .with_context(|| format!("cannot open wordlist {}", wordlist.display()))?; HybridIter::validate(charset, *min, *max)?; } Attack::Bruteforce { charset, min, max, freq_order, } => { BruteForce::new(charset.clone(), *min, *max, *freq_order).map(|_| ())?; } Attack::Rainbow(p) => { std::fs::File::open(p) .with_context(|| format!("cannot open PMK table {}", p.display()))?; } } Ok(()) } } // --------------------------------------------------------------------------- // Charset presets (Cracker page bruteforce chips) + custom input // --------------------------------------------------------------------------- /// Predefined charsets the UI offers as toggle chips; the effective /// brute-force charset is the union of the checked presets and the /// custom field. Keep SPECIAL to the commonly used symbols — exotic /// bytes belong in the custom field. pub mod charset { pub const LOWER: &[u8] = b"abcdefghijklmnopqrstuvwxyz"; pub const UPPER: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; pub const DIGITS: &[u8] = b"0123456789"; pub const SPECIAL: &[u8] = b"!@#$%^&*()-_=+[]{};:,.<>/?"; /// Union of the selected presets + `custom`, deduplicated. pub fn compose( lower: bool, upper: bool, digits: bool, special: bool, custom: &[u8], ) -> Vec { let mut out = Vec::new(); for src in [ (lower, LOWER), (upper, UPPER), (digits, DIGITS), (special, SPECIAL), ] { if src.0 { out.extend_from_slice(src.1); } } out.extend_from_slice(custom); out.sort_unstable(); out.dedup(); out } } // --------------------------------------------------------------------------- // Brute-force candidate generator // --------------------------------------------------------------------------- /// password-frequency heuristic: most-common characters first; the /// odometer still varies the right-most position fastest. const FREQ_ORDER: &[u8] = b"e1a0ir7o29nls38tc45um6!dpghwyb$vkfxqjz@EAOINSRHLTCDUMPFGYKWBVJXQZ#._-"; /// Streaming `charset^len` generator for len in min..=max, clamped to the /// WPA passphrase range 8..=63. Odometer order: right-most position cycles /// fastest, words of one length exhaust before the next length starts. /// With `freq_order` the charset is permuted by [`FREQ_ORDER`] rank /// (chars absent from it keep codepoint order, appended after) — a /// bijection, so `space()` size and `seek()` indices are unchanged. pub struct BruteForce { charset: Vec, /// Clamped minimum length (the space starts here). min_len: usize, /// Current word length. len: usize, max_len: usize, /// Odometer digits (indices into charset), same length as the word. idx: Vec, exhausted: bool, /// Candidates yielded so far = raw stream position (the resume seek /// index of the next unyielded candidate). [`BruteForce::seek`] sets it. pos: u64, } impl BruteForce { /// `Err` when the charset is empty or the clamped length range is empty /// (nothing 8..=63-compatible to try). pub fn new(charset: Vec, min: usize, max: usize, freq_order: bool) -> Result { let mut charset = charset; if freq_order { let rank = |b: u8| { FREQ_ORDER .iter() .position(|&c| c == b) .unwrap_or(usize::MAX) }; charset.sort_by_key(|&b| (rank(b), b)); charset.dedup(); } else { charset.dedup(); } if charset.is_empty() { bail!("bruteforce: charset is empty"); } // Intersect with the WPA passphrase range instead of clamping // both bounds: 64..=70 must be rejected (empty intersection), not // silently turned into 63..=63. let lo = min.max(8); let hi = max.min(63); if lo > hi { bail!("bruteforce: length range {min}..={max} has no WPA-legal lengths (8..=63)"); } Ok(BruteForce { charset, min_len: lo, len: lo, max_len: hi, idx: vec![0; lo], exhausted: false, pos: 0, }) } /// Total candidate count over the whole length range (may exceed u64; /// used for logs only). pub fn space(&self) -> u128 { let base = self.charset.len() as u128; (self.len..=self.max_len).map(|l| base.pow(l as u32)).sum() } /// Jump the odometer so the NEXT [`next`] yields candidate number `n` /// (0-based) of the full space — the resume path. `n` beyond the space /// exhausts the generator. pub fn seek(&mut self, n: u64) { let base = self.charset.len() as u128; let mut rem = n as u128; let mut len = self.min_len; loop { let size = base.pow(len as u32); if rem < size || len == self.max_len { break; } rem -= size; len += 1; } if len == self.max_len && rem >= base.pow(len as u32) { self.exhausted = true; self.pos = n; return; } // Odometer order: right-most digit cycles fastest → `rem` is the // index in little-endian base-|charset|. self.len = len; self.idx = vec![0; len]; for pos in (0..len).rev() { self.idx[pos] = (rem % base) as usize; rem /= base; } self.exhausted = false; self.pos = n; } } impl RawPosIter for BruteForce { fn raw_pos(&self) -> u64 { self.pos } } impl Iterator for BruteForce { type Item = Vec; fn next(&mut self) -> Option> { if self.exhausted { return None; } let word: Vec = self.idx.iter().map(|&d| self.charset[d]).collect(); self.pos += 1; // Advance the odometer; carry beyond the left-most digit either // grows the word length or exhausts the space. let mut pos = self.idx.len(); loop { if pos == 0 { if self.len == self.max_len { self.exhausted = true; } else { self.len += 1; self.idx = vec![0; self.len]; } break; } pos -= 1; self.idx[pos] += 1; if self.idx[pos] < self.charset.len() { break; } self.idx[pos] = 0; } Some(word) } } // --------------------------------------------------------------------------- // Raw stream positions (exact resume, session format v2) // --------------------------------------------------------------------------- /// Candidate iterators that know their exact RAW stream position: the /// index of the next unyielded candidate in the addressing scheme the /// mode's resume seek uses (flattened index for rules/hybrid, plain /// candidate number otherwise). Raw positions count EVERY slot — /// candidates filtered out below (rules/hybrid length rejects) or skipped /// above ([`CacheSkipIter`]) consume raw positions too. This makes a saved /// position an exact resume point that is INDEPENDENT of the PMK cache's /// contents: a mutated cache changes which candidates get skipped, never /// the addressing. trait RawPosIter: Iterator> { /// Raw index of the NEXT unyielded candidate (absolute — seek /// functions preset it, so `raw_pos() - yields_since_seek` is not /// meaningful; use the value directly). fn raw_pos(&self) -> u64; } impl RawPosIter for Box { fn raw_pos(&self) -> u64 { (**self).raw_pos() } } /// Adds raw (yield-)positions to any plain `Vec` candidate stream /// whose resume is a plain `skip_n` (wordlist files, arcane tables: their /// stream addressing already IS the yield sequence). `at` is the absolute /// position of the next yield (resume offset). struct CountIter>> { inner: I, pos: u64, } impl>> CountIter { fn at(inner: I, pos: u64) -> Self { CountIter { inner, pos } } } impl>> Iterator for CountIter { type Item = Vec; fn next(&mut self) -> Option> { let out = self.inner.next()?; self.pos += 1; Some(out) } } impl>> RawPosIter for CountIter { fn raw_pos(&self) -> u64 { self.pos } } /// FIFO of raw positions: one entry per candidate DELIVERED to the crack /// backend (post-cache-skip), popped once the backend confirms processing /// via a progress tick. The pop count aligns with the progress event's /// cumulative `done`, mapping it to an exact raw position. Bounded by the /// backend's in-flight window (≤ a couple of batches). type PosMap = std::rc::Rc>>; /// Wraps the final (post-cache-skip) stream delivered to a crack backend /// and records the raw position after each yield for [`PosMap`]. struct PosFeed { inner: Box, map: PosMap, } impl PosFeed { fn new(inner: Box) -> (PosFeed, PosMap) { let map: PosMap = Default::default(); ( PosFeed { inner, map: map.clone(), }, map, ) } } impl Iterator for PosFeed { type Item = Vec; fn next(&mut self) -> Option> { let out = self.inner.next()?; let raw = self.inner.raw_pos(); self.map.borrow_mut().push_back(raw); Some(out) } } /// Translate a backend progress count (`done` = candidates processed so /// far THIS run) into the absolute raw position of the last processed /// candidate, popping confirmed entries out of `map`. Returns `None` when /// the map underflows (a found-candidate early return, or a partial batch /// the backend never counted — in both cases the last known position is /// still the last reported one). fn raw_position_of(map: &PosMap, acked: &mut u64, done: u64) -> Option { let mut raw = None; let mut dq = map.borrow_mut(); while *acked < done { match dq.pop_front() { Some(p) => { raw = Some(p); *acked += 1; } None => break, } } raw } // --------------------------------------------------------------------------- // Mutation rules (hashcat-subset) — wordlist × rules attack // --------------------------------------------------------------------------- /// One hashcat-style single-letter op (supported subset). `$X`/`^X` carry /// the literal byte; `sXY` carries (from, to). #[derive(Clone, Debug, PartialEq, Eq)] pub enum Op { Noop, Lower, Upper, Capitalize, InvertCapitalize, Toggle, Reverse, Duplicate, Append(u8), Prepend(u8), DeleteFirst, DeleteLast, Replace(u8, u8), } /// One rule = a line's ops, applied left to right (`c $1 $9` etc.). pub type Rule = Vec; /// Apply `ops` to `word`, left to right. `None` when the result falls /// outside the WPA passphrase range 8..=63 — filtered like WordIter, but /// the flattened index CONSUMES that candidate (resume positions line up /// with the raw stream). fn apply_rule(ops: &Rule, word: &[u8]) -> Option> { let mut w = word.to_vec(); for op in ops { match op { Op::Noop => {} Op::Lower => w.make_ascii_lowercase(), Op::Upper => w.make_ascii_uppercase(), Op::Capitalize => { w.make_ascii_lowercase(); if let Some(first) = w.first_mut() { first.make_ascii_uppercase(); } } Op::InvertCapitalize => { w.make_ascii_uppercase(); if let Some(first) = w.first_mut() { first.make_ascii_lowercase(); } } Op::Toggle => { for b in &mut w { if b.is_ascii_lowercase() { *b = b.to_ascii_uppercase(); } else if b.is_ascii_uppercase() { *b = b.to_ascii_lowercase(); } } } Op::Reverse => w.reverse(), Op::Duplicate => { let tail = w.clone(); w.extend_from_slice(&tail); } Op::Append(b) => w.push(*b), Op::Prepend(b) => w.insert(0, *b), Op::DeleteFirst => { if !w.is_empty() { w.remove(0); } } Op::DeleteLast => { w.pop(); } Op::Replace(x, y) => { for b in &mut w { if b == x { *b = *y; } } } } } (8..=63).contains(&w.len()).then_some(w) } /// Parse one rules line into ops. The grammar is byte-oriented: `$`/`^` /// take the next byte as the literal to append/prepend, `s` takes the two /// following bytes. Fails with a positioned message on unknown ops or /// truncated literals. fn parse_rule_line(line: &[u8], lineno: usize) -> Result { let mut ops = Vec::new(); let mut i = 0usize; while i < line.len() { let c = line[i]; i += 1; let op = match c { // Space separates ops in hashcat files ("c $1 $!" etc.) and is // itself a do-nothing op there; skip it. Appending a literal // space still works ("$ " — the space is $'s literal). b' ' => continue, b':' => Op::Noop, b'l' => Op::Lower, b'u' => Op::Upper, b'c' => Op::Capitalize, b'C' => Op::InvertCapitalize, b't' => Op::Toggle, b'r' => Op::Reverse, b'd' => Op::Duplicate, b'[' => Op::DeleteFirst, b']' => Op::DeleteLast, b'$' | b'^' => { let Some(&lit) = line.get(i) else { bail!("rules line {lineno}: `{c}` at column {i} is missing its character"); }; i += 1; if c == b'$' { Op::Append(lit) } else { Op::Prepend(lit) } } b's' => { let Some(&x) = line.get(i) else { bail!( "rules line {lineno}: `s` at column {i} is missing its replacement chars" ); }; let Some(&y) = line.get(i + 1) else { bail!( "rules line {lineno}: `s` at column {i} is missing its replacement chars" ); }; i += 2; Op::Replace(x, y) } _ => bail!( "rules line {lineno}: unsupported op `{}` (column {})", c as char, i - 1 ), }; ops.push(op); } Ok(ops) } /// Parse a hashcat-subset rules file (the classic best64.rule family): /// blank lines and lines starting with `#` are skipped, every other line /// is one rule. The "::"-style noop lines parse to a single [`Op::Noop`] /// per `:`. pub fn parse_rules(path: &Path) -> Result> { use std::io::BufRead; let f = std::fs::File::open(path) .with_context(|| format!("opening rules file {}", path.display()))?; let mut reader = std::io::BufReader::with_capacity(1 << 16, f); let mut rules = Vec::new(); let mut line = String::new(); let mut lineno = 0usize; loop { line.clear(); let n = reader .read_line(&mut line) .with_context(|| format!("reading rules file {}", path.display()))?; if n == 0 { break; } lineno += 1; let trimmed = line.trim_end_matches(['\r', '\n']); if trimmed.is_empty() || trimmed.starts_with('#') { continue; } rules.push(parse_rule_line(trimmed.as_bytes(), lineno)?); } Ok(rules) } /// Count of real rules (non-blank, non-comment lines) — the per-word /// multiplier for the rules attack's flattened space. Consumed by the /// cracker-page estimator (W4 UI surface). #[allow(dead_code)] pub fn rule_count(path: &Path) -> Result { use std::io::BufRead; let f = std::fs::File::open(path) .with_context(|| format!("opening rules file {}", path.display()))?; let mut count = 0u64; for line in std::io::BufReader::with_capacity(1 << 16, f).lines() { let line = line.with_context(|| format!("reading rules file {}", path.display()))?; let trimmed = line.trim_end_matches(['\r', '\n']); if !trimmed.is_empty() && !trimmed.starts_with('#') { count += 1; } } Ok(count) } /// wordlist × rules generator, WORD-MAJOR / rule-minor: rules are applied /// to word 0 in file order, then to word 1, and so on. The flattened /// candidate index of `(word_idx, rule_idx)` is `word_idx * rules_len + /// rule_idx`, so a resume position is a plain `skip_n` over this stream — /// even though rule outputs outside 8..=63 are filtered (they still /// consume a flattened index, matching [`apply_rule`]'s contract). pub struct RuleIter { words: Box> + Send>, rules: Rc>, cur: Option>, ri: usize, /// Flattened slots consumed so far (`word_idx * rules_len + ri`, /// absolute): the resume seek index of the next unconsumed slot. flat: u64, } impl RuleIter { pub fn new(words: Box> + Send>, rules: Rc>) -> Self { if rules.is_empty() { // Degenerate case: keep the stream empty rather than panic. return RuleIter { words: Box::new(std::iter::empty()), rules, cur: None, ri: 0, flat: 0, }; } RuleIter { words, rules, cur: None, ri: 0, flat: 0, } } /// Position the generator at flattened index `n` (exact resume): skip /// `n / rules_len` words and continue at rule `n % rules_len` of the /// next word. `n` beyond the space leaves the stream at its end. pub fn seek_flat(&mut self, n: u64) { let rules_len = self.rules.len() as u64; if rules_len == 0 { // Degenerate stream: always empty; positions are moot. self.flat = n; return; } skip_n(&mut self.words, n / rules_len); let ri = (n % rules_len) as usize; // cur = None makes the first next() LOAD the word — which resets // ri to 0; pre-fill `cur` instead so the primed rule index // survives (a wordlist failure/exhaustion just empties the // stream). self.cur = self.words.next(); self.ri = if self.cur.is_some() { ri } else { 0 }; self.flat = n; } } impl RawPosIter for RuleIter { fn raw_pos(&self) -> u64 { self.flat } } impl Iterator for RuleIter { type Item = Vec; fn next(&mut self) -> Option> { loop { if self.cur.is_none() { self.cur = Some(self.words.next()?); self.ri = 0; } let cur = self.cur.clone().expect("just filled"); while self.ri < self.rules.len() { let r = &self.rules[self.ri]; self.ri += 1; self.flat += 1; if let Some(out) = apply_rule(r, &cur) { return Some(out); } // Filtered: the index slot is consumed (documented above). } self.cur = None; } } } // --------------------------------------------------------------------------- // Hybrid: wordlist × append-charset sweep (word + charset^k) // --------------------------------------------------------------------------- /// wordlist × append-charset generator: for each word yields /// `word + charset^k` for k in `min..=max`, rightmost-fastest odometer /// like [`BruteForce`]. Suffix lengths `min`/`max` are clamped to 1..=4 /// (the bruteforce-ish complement of the wordlist — longer suffixes /// belong to plain bruteforce). Candidates longer than 63 bytes are /// skipped AFTER consuming their flattened index (like [`RuleIter`]). /// /// Flattened index of `(word_idx, inner)` is `word_idx * per_word + /// inner` with `per_word = Σ_{k=min..=max} |charset|^k`; `inner` walks /// k-blocks in ascending order with little-endian odometer order inside a /// block (same convention as [`BruteForce`]). `total = line_count(wordlist) /// * per_word` is an upper bound (length-skipping only shrinks it). pub struct HybridIter { words: Box> + Send>, charset: Vec, min: usize, max: usize, cur_word: Option>, /// Suffix length currently emitted for `cur_word`. k: usize, /// Odometer digits for the current suffix length. odo: Vec, /// Set by `seek_flat`: the odometer was primed externally and the next /// word load must NOT reset it. primed: bool, /// Flattened slots consumed so far (absolute): the resume seek index /// of the next unconsumed slot. flat: u64, /// Slots consumed for the CURRENT word (0..=per_word). inner_used: u64, } impl HybridIter { /// `Err` when the charset is empty or the clamped suffix range is /// inverted (`min` > `max` after clamping: `min` 4 with `max` 1 etc.). pub fn new( words: Box> + Send>, charset: Vec, min: usize, max: usize, ) -> Result { let (min, max) = Self::clamped_range(min, max)?; let mut charset = charset; charset.sort_unstable(); charset.dedup(); if charset.is_empty() { bail!("hybrid: charset is empty"); } Ok(HybridIter { words, charset, min, max, cur_word: None, k: min, odo: vec![0; min], primed: false, flat: 0, inner_used: 0, }) } /// Flag validation for [`Attack::Hybrid`] (no word stream needed). pub fn validate(charset: &[u8], min: usize, max: usize) -> Result<()> { let mut cs = charset.to_vec(); cs.sort_unstable(); cs.dedup(); if cs.is_empty() { bail!("hybrid: charset is empty"); } Self::clamped_range(min, max).map(|_| ()) } fn clamped_range(min: usize, max: usize) -> Result<(usize, usize)> { let lo = min.clamp(1, 4); let hi = max.clamp(1, 4); if lo > hi { bail!("hybrid: suffix length range {min}..={max} is inverted (clamped 1..=4)"); } Ok((lo, hi)) } /// Candidates emitted per wordlist word: Σ_{k=min..=max} |charset|^k /// (saturated to u64::MAX). pub fn per_word(&self) -> u64 { let base = self.charset.len() as u128; let s: u128 = (self.min..=self.max).map(|k| base.pow(k as u32)).sum(); s.min(u64::MAX as u128) as u64 } /// Position the generator at flattened index `n` (resume): skips /// `n / per_word` words and primes the suffix odometer to emit the /// remainder `n % per_word`. An `n` beyond the space leaves the stream /// at its natural end. pub fn seek_flat(&mut self, n: u64) { let per_word = self.per_word(); let word_idx = n.checked_div(per_word).unwrap_or(n); let inner = if per_word == 0 { 0 } else { n % per_word }; skip_n(&mut self.words, word_idx); self.set_inner(inner as u128); // The skipped words leave cur_word empty; next() loads the word at // word_idx (or the stream ends) — WITHOUT clobbering the primed // odometer (one word only). self.cur_word = None; self.primed = true; self.flat = n; self.inner_used = inner; } /// Consume the rest of the current word's slots without emitting /// (overlong-word early exit: every remaining candidate is provably /// >63 bytes, i.e. "tried" by construction). fn consume_word_rest(&mut self) { let rest = self.per_word().saturating_sub(self.inner_used); self.flat += rest; self.inner_used = self.per_word(); self.cur_word = None; } /// Prime the odometer so the next calls yield suffix-block index /// `inner` (ascending k-blocks, little-endian odometer per block). fn set_inner(&mut self, mut rem: u128) { let base = self.charset.len() as u128; let mut k = self.min; loop { let size = base.pow(k as u32); if rem < size || k == self.max { break; } rem -= size; k += 1; } // rem can exceed the last block only when `inner` pointed past the // whole per-word space (callers never produce that), but stay // defensive: exhaust the stream at the first next(). let mut odo = vec![0usize; k]; for pos in (0..k).rev() { odo[pos] = (rem % base) as usize; rem /= base; } if rem > 0 { // Sentinel: out-of-range digit exhausts at the first next(). self.k = k; self.odo = vec![self.charset.len(); k]; return; } self.k = k; self.odo = odo; } } impl RawPosIter for HybridIter { fn raw_pos(&self) -> u64 { self.flat } } impl Iterator for HybridIter { type Item = Vec; fn next(&mut self) -> Option> { loop { if self.cur_word.is_none() { self.cur_word = Some(self.words.next()?); // seek_flat-primed state survives the first word load; // afterwards the odometer restarts at zero per word. if !self.primed { self.k = self.min; self.odo = vec![0; self.k]; self.inner_used = 0; } self.primed = false; } let word = self.cur_word.clone().expect("just filled"); // Out-of-range sentinel from set_inner: exhaust the word. if self.odo.iter().any(|&d| d >= self.charset.len()) { self.consume_word_rest(); continue; } let mut cand = word.clone(); for d in &self.odo { cand.push(self.charset[*d]); } self.flat += 1; self.inner_used += 1; // Advance the odometer: rightmost fastest; carry grows k until // max is exhausted, which moves on to the next word. let mut pos = self.odo.len(); loop { if pos == 0 { if self.k == self.max { self.cur_word = None; } else { self.k += 1; self.odo = vec![0; self.k]; } break; } pos -= 1; self.odo[pos] += 1; if self.odo[pos] < self.charset.len() { break; } self.odo[pos] = 0; } // >63-byte candidates are skipped but consumed the index. if cand.len() <= 63 { return Some(cand); } // word exhausted check: when the suffix made it not fit at k, // larger k will not either — consume the word's remaining // slots (all provably too long) and jump to the next word. if self.cur_word.is_some() && word.len() + self.k + 1 > 63 { self.consume_word_rest(); } } } } // --------------------------------------------------------------------------- // PMK-cache coverage filter // --------------------------------------------------------------------------- /// Wraps a candidate stream, dropping candidates already covered by the /// per-SSID PMK cache consult (they were shown not to match). Skipped /// candidates consume their RAW positions like any other (the cache pass /// did the work), so saved positions stay exact no matter how the cache /// changes between save and resume — see [`RawPosIter`]. pub struct CacheSkipIter>> { inner: I, covered: HashSet>, } impl>> CacheSkipIter { pub fn new(inner: I, covered: HashSet>) -> Self { CacheSkipIter { inner, covered } } } impl>> Iterator for CacheSkipIter { type Item = Vec; fn next(&mut self) -> Option> { self.inner .by_ref() .find(|cand| !self.covered.contains(cand.as_slice())) } } /// After a yield, the wrapped iterator's raw position reflects every slot /// pulled to produce it — including the cache-skipped ones: positions /// stay in RAW stream coordinates (cache contents never shift them). impl> + RawPosIter> RawPosIter for CacheSkipIter { fn raw_pos(&self) -> u64 { self.inner.raw_pos() } } // --------------------------------------------------------------------------- // Workers // --------------------------------------------------------------------------- fn send(tx: &Sender, ev: Event) { let _ = tx.send(ev); } fn log(tx: &Sender, line: impl Into) { send(tx, Event::Log(line.into())); } fn log_err(tx: &Sender, line: impl Into) { send(tx, Event::Error(line.into())); } /// Run-scoped crack context shared by the words-family backends: target /// identity, raw-position map, worst-case total, event sink, stop flag /// and the CPU-only override. Bundled so [`word_crack`] stays under the /// argument-count lint. struct CrackCtx<'a> { target_desc: &'a str, target: &'a CrackTarget, pos_map: &'a PosMap, total: Option, tx: &'a Sender, stop: &'a Arc, force_cpu: bool, } /// GPU (or CPU-fallback) PBKDF2 crack over a word stream — the selection /// logic the cracker has always used. `total` is the worst-case candidate /// count (worst-case ETA = (total - done) / hps) when the source is /// countable. `pos_map` carries raw stream positions recorded by /// [`PosFeed`], one per candidate DELIVERED to the backend; progress /// events map backend-confirmed counts onto them, so `CrackProgress.done` /// is always an exact raw position (session v2 resume index), never a /// post-filter yield count. `force_cpu` skips the GPU entirely (CLI /// `--cpu`); it also disables the PMK-cache sink so a forced-CPU client /// run stays side-effect-free. Otherwise a PURE-CPU run (no adapter, or a /// key_ver the shader cannot express) populates the per-SSID PMK cache /// while it cracks — every candidate's PBKDF2 result lands in /// `store::cache_file(essid)` in rainbow format. /// /// `Err` means the GPU backend itself failed (device lost, dispatch /// timeout): NOT exhaustion, NOT user stop — the caller reports /// [`Event::CrackError`] and the session stays resumable. fn word_crack( ctx: &CrackCtx<'_>, words: &mut dyn Iterator>, ) -> Result, gpu::GpuError> { let CrackCtx { target_desc, target, pos_map, total, tx, stop, force_cpu, } = *ctx; if let Some(t) = total { log(tx, format!("cracker: worst case: {t} candidates")); } let mut progress = { let tx = tx.clone(); let map = pos_map.clone(); let mut acked = 0u64; move |done: u64, hps: f64| { if let Some(raw) = raw_position_of(&map, &mut acked, done) { send( &tx, Event::CrackProgress { done: raw, hps, total, }, ); } } }; // SHA1-family targets (handshake key_ver 1|2, PMKID) crack fully // on-device (init + chain + MIC epilogue); key_ver 3 / overlong // ESSIDs stay on the CPU cracker even when a GPU exists. let adapter = if !force_cpu && gpu::gpu_verifiable(target) { GpuCracker::try_new() } else { None }; match adapter { Some(g) => { log( tx, format!( "cracker: GPU backend: {} — cracking {target_desc}", g.device_name() ), ); // GPU runs never populate the cache: the hit-slot readback // pipeline has no PMK readback plumbed through it. g.crack(target, words, &mut progress, stop) } None => { log( tx, if force_cpu { format!("cracker: CPU backend (forced) — cracking {target_desc}") } else { format!( "cracker: CPU backend (no adapter or non-SHA1 target) — cracking {target_desc}" ) }, ); let mut sink = if force_cpu { None } else { let path = store::cache_file(target.essid()); match rainbow::append_open(&path, target.essid()) { Ok(w) => Some(gpu::CpuPmkSink::new(w, { let tx = tx.clone(); Box::new(move |line: String| log(&tx, line)) })), Err(e) => { log( tx, format!( "pmk cache: cannot open cache file for writing ({e:#}) — continuing without" ), ); None } } }; let found = gpu::cpu_crack(target, words, &mut progress, stop, sink.as_mut()); if let Some(s) = sink { match s.finish() { Ok(n) => log( tx, format!( "pmk cache: {n} PMKs cached → {}", store::cache_file(target.essid()).display() ), ), Err(e) => log(tx, format!("pmk cache: flush failed ({e:#})")), } } Ok(found) } } } /// Rainbow run: precomputed PMKs, one PTK derivation + MIC compare per /// entry (no PBKDF2). Progress ticks are time-based (~4/s) so a fast disk /// is not reporting-bound. pub fn rainbow_crack( target: &CrackTarget, entries: &mut dyn Iterator)>, progress: &mut dyn FnMut(u64, f64), stop: &Arc, ) -> Option { let prepared = gpu::prepare_target(target); let mut done = 0u64; let started = Instant::now(); let mut last_tick = started; for (pmk, pass) in entries { done += 1; if gpu::prepared_matches(&prepared, &pmk) { progress( done, done as f64 / started.elapsed().as_secs_f64().max(1e-6), ); return String::from_utf8(pass).ok(); } if last_tick.elapsed() > Duration::from_millis(250) { last_tick = Instant::now(); progress( done, done as f64 / started.elapsed().as_secs_f64().max(1e-6), ); } if done.is_multiple_of(4096) && stop.load(Ordering::Relaxed) { return None; } } // Exhaustion: a table smaller than one tick interval would otherwise // end with zero progress ever reported (the word path ticks per // batch — match it). progress( done, done as f64 / started.elapsed().as_secs_f64().max(1e-6), ); None } /// Drop the first `n` items of a candidate stream (resume). The stream /// simply runs out early when fewer than `n` remain — the caller's crack /// loop then reports "exhausted" on the empty remainder. fn skip_n(it: &mut dyn Iterator, mut n: u64) { while n > 0 && it.next().is_some() { n -= 1; } } /// Result of consulting the per-SSID PMK cache before a wordlist-family run. enum CacheConsult { /// Cache contained the PSK — report as a normal find, skip the main run. Hit(String), /// Readable cache, no match: the covered passphrases to skip in the /// main iterator. Miss(HashSet>), /// No cache, or unreadable — run everything. Skip, } /// Per-SSID PMK cache consult (`~/wifi_lab/cache/.pmk`, rainbow /// text format): if the file exists and is readable, run a quick /// PBKDF2-free pass ([`rainbow_crack`]) over its entries BEFORE the main /// attack. On a miss the caller wraps the main iterator in /// [`CacheSkipIter`] so cached-and-ruled-out passwords are not recomputed. /// Unreadable caches (open/parse/header-SSID mismatch) produce exactly /// one `Log` and fall through to a full run. fn consult_cache( target: &CrackTarget, attack_desc: &str, tx: &Sender, stop: &Arc, ) -> CacheConsult { let path = store::cache_file(target.essid()); if !path.is_file() { return CacheConsult::Skip; } let mut entries = match rainbow::open(&path) { Ok((ssid, entries)) => match rainbow::check_ssid(ssid.as_deref(), target.essid()) { Ok(()) => entries, Err(e) => { log( tx, format!("pmk cache: unreadable ({e:#}), proceeding without"), ); return CacheConsult::Skip; } }, Err(e) => { log( tx, format!("pmk cache: unreadable ({e:#}), proceeding without"), ); return CacheConsult::Skip; } }; let total = wordlist::line_count(&path).ok().map(|n| n.max(1)); let mut covered: HashSet> = HashSet::new(); // Tee the password out of each entry while rainbow_crack consumes. let mut teed = std::iter::from_fn(|| { entries.next().map(|(pmk, pass)| { covered.insert(pass.clone().into_boxed_slice()); (pmk, pass) }) }); let mut progress = { let tx = tx.clone(); move |done: u64, hps: f64| { send(&tx, Event::CrackProgress { done, hps, total }); } }; log(tx, "pmk cache: file exists — checking cached PMKs first"); if let Some(pass) = rainbow_crack(target, &mut teed, &mut progress, stop) { return CacheConsult::Hit(pass); } if stop.load(Ordering::Relaxed) { // Stopped mid-consult: treat as a miss; the main run's stop checks // produce the canonical "stopped" terminator. return CacheConsult::Miss(covered); } log( tx, format!( "pmk cache: {} entries, no hit — continuing {attack_desc}", covered.len() ), ); CacheConsult::Miss(covered) } /// Start a crack run on a background thread. Always returns the stop flag; /// spawn failure is reported as events instead of a thread. `resume_from` /// is a RAW stream position (session v2): the candidate index the run /// continues at — a flattened slot index for rules/hybrid (see /// [`RuleIter::seek_flat`]/[`HybridIter::seek_flat`]), a plain skip count /// for wordlist tables, an odometer index for bruteforce. Progress events /// report raw positions of the FULL space from zero. `force_cpu` pins the /// CPU backend (headless CLI `--cpu`) and disables PMK-cache writes. /// Wordlist-family attacks first consult the per-SSID PMK cache /// (`store::cache_file`): hit → the normal found-terminator without a /// main run; miss → cached passwords are skipped via [`CacheSkipIter`] /// WITHOUT shifting raw positions. /// /// Terminators: found → `CrackFound` + `CrackStopped { found: true }`; /// user stop / true exhaustion / setup failure → `CrackStopped { found: /// false }`; GPU backend failure or worker panic → `CrackError { reason }` /// (the session file stays resumable). pub fn spawn_crack( target_desc: String, target: CrackTarget, attack: Attack, resume_from: u64, tx: Sender, force_cpu: bool, ) -> Arc { let stop = Arc::new(AtomicBool::new(false)); let stop2 = stop.clone(); let err_tx = tx.clone(); let panic_tx = tx.clone(); let result = std::thread::Builder::new() .name("wifi_lab-crack".into()) .spawn(move || { let run = || { if resume_from > 0 { log(&tx, format!("cracker: resuming at candidate {resume_from}")); } // Per-SSID PMK cache consult (wordlist-family only). let wordlist_family = matches!( attack, Attack::Wordlist(_) | Attack::Rules { .. } | Attack::Hybrid { .. } | Attack::Bruteforce { .. } ); let mut covered: Option>> = None; if wordlist_family { match consult_cache(&target, &attack.describe(), &tx, &stop2) { CacheConsult::Hit(password) => { log(&tx, format!("cracker: PASSWORD found: {password}")); send(&tx, Event::CrackFound { password }); send(&tx, Event::CrackStopped { found: true }); return; } CacheConsult::Miss(c) => covered = Some(c), CacheConsult::Skip => {} } } // Wrap a raw-positioned candidate stream with the coverage // filter from the cache consult (when one happened). Coverage // sits BETWEEN the raw stream and the backend: positions stay // raw, so a cache that changed since the session was saved // shifts only WHICH candidates get skipped, never the resume // address. fn with_cache( it: I, covered: &mut Option>>, ) -> Box { match covered.take() { Some(c) => Box::new(CacheSkipIter::new(it, c)), None => Box::new(it), } } // Setup failures (missing files, bad rules): the error is // already logged; the run ends with the standard // not-found terminator (NOT CrackError: nothing backend died). fn setup_fail(tx: &Sender, e: &anyhow::Error) -> Result, gpu::GpuError> { log_err(tx, format!("cracker: {e:#}")); Ok(None) } // Snapshot the input skip counters so the run can surface // lines the iterators silently dropped (cache consult above // is excluded — it owns its own log lines). let wl_skipped_at_start = wordlist::skipped_lines(); let rb_skipped_at_start = rainbow::skipped_lines(); let found: Result, gpu::GpuError> = match &attack { Attack::Wordlist(path) => match wordlist::open(path) { Ok(mut words) => { // Line count is an upper bound (length-filtering // only shrinks the space — exactly the "worst // case" the ETA promises). let total = wordlist::line_count(path).ok(); skip_n(&mut words, resume_from); let raw = CountIter::at(words, resume_from); let stream = with_cache(raw, &mut covered); let (mut feed, map) = PosFeed::new(stream); word_crack( &CrackCtx { target_desc: &target_desc, target: &target, pos_map: &map, total, tx: &tx, stop: &stop2, force_cpu, }, &mut feed, ) } Err(e) => setup_fail(&tx, &e), }, Attack::Rules { wordlist: wl_path, rules: rules_path } => { match (wordlist::open(wl_path), parse_rules(rules_path)) { (Ok(words), Ok(rules)) => { let n_rules = rules.len() as u128; let lines = wordlist::line_count(wl_path).ok(); // Worst case: every word × every rule. let total = lines.map(|l| (l as u128).saturating_mul(n_rules).min(u64::MAX as u128) as u64); let mut it = RuleIter::new(words, Rc::new(rules)); // Exact flattened-index resume. it.seek_flat(resume_from); let stream = with_cache(it, &mut covered); let (mut feed, map) = PosFeed::new(stream); word_crack( &CrackCtx { target_desc: &target_desc, target: &target, pos_map: &map, total, tx: &tx, stop: &stop2, force_cpu, }, &mut feed, ) } (Err(e), _) => setup_fail(&tx, &e), (_, Err(e)) => setup_fail(&tx, &e), } } Attack::Hybrid { wordlist: wl_path, charset, min, max } => { match wordlist::open(wl_path) { Ok(words) => { let mut it = HybridIter::new(words, charset.clone(), *min, *max) .expect("validated"); let lines = wordlist::line_count(wl_path).ok(); let total = lines.map(|l| { (l as u128).saturating_mul(it.per_word() as u128).min(u64::MAX as u128) as u64 }); // Exact flattened-index resume. it.seek_flat(resume_from); let stream = with_cache(it, &mut covered); let (mut feed, map) = PosFeed::new(stream); word_crack( &CrackCtx { target_desc: &target_desc, target: &target, pos_map: &map, total, tx: &tx, stop: &stop2, force_cpu, }, &mut feed, ) } Err(e) => setup_fail(&tx, &e), } } Attack::Bruteforce { charset, min, max, freq_order } => { // validate() runs before spawn; new() cannot fail here. let mut brute = BruteForce::new(charset.clone(), *min, *max, *freq_order).expect("validated"); let total = Some(brute.space().min(u64::MAX as u128) as u64); // Exact seek (no skipping): O(1) odometer math. brute.seek(resume_from); let stream = with_cache(brute, &mut covered); let (mut feed, map) = PosFeed::new(stream); word_crack( &CrackCtx { target_desc: &target_desc, target: &target, pos_map: &map, total, tx: &tx, stop: &stop2, force_cpu, }, &mut feed, ) } Attack::Rainbow(path) => match rainbow::open(path) { Ok((ssid, mut entries)) => { if let Err(e) = rainbow::check_ssid(ssid.as_deref(), target.essid()) { setup_fail(&tx, &e) } else { let total = wordlist::line_count(path).ok(); skip_n(&mut entries, resume_from); // Rainbow entries have no cache coverage layer; // raw position = resume_from + pulls, recorded // per delivered item like the word paths. let map: PosMap = Default::default(); let pull_map = map.clone(); let mut pulls = resume_from; let mut feed = std::iter::from_fn(move || { let out = entries.next()?; pulls += 1; pull_map.borrow_mut().push_back(pulls); Some(out) }); let mut acked = 0u64; let mut progress = { let tx = tx.clone(); move |done: u64, hps: f64| { if let Some(raw) = raw_position_of(&map, &mut acked, done) { send(&tx, Event::CrackProgress { done: raw, hps, total }); } } }; log( &tx, format!("cracker: rainbow run (precomputed PMKs) — {target_desc}"), ); Ok(rainbow_crack(&target, &mut feed, &mut progress, &stop2)) } } Err(e) => setup_fail(&tx, &e), }, }; // One summary line per run when the input iterator silently // dropped lines (out-of-range or undecodable bytes). let skipped = if matches!(attack, Attack::Rainbow(_)) { rainbow::skipped_lines() - rb_skipped_at_start } else { wordlist::skipped_lines() - wl_skipped_at_start }; if skipped > 0 { log( &tx, format!( "wordlist: {skipped} line(s) skipped (out-of-range or undecodable bytes)" ), ); } match found { Err(e) => { // Backend failure (device lost, dispatch timeout, // unsupported mid-run): NOT exhaustion, NOT user stop. // The session file stays — the run may be resumed. log_err(&tx, format!("cracker: backend failed: {e} — run aborted, session resumable")); send(&tx, Event::CrackError { reason: e.to_string() }); } Ok(Some(password)) => { log(&tx, format!("cracker: PASSWORD found: {password}")); send(&tx, Event::CrackFound { password }); send(&tx, Event::CrackStopped { found: true }); } Ok(None) => { if stop2.load(Ordering::Relaxed) { log(&tx, "cracker: stopped"); } else { log(&tx, "cracker: candidates exhausted, password not found"); } send(&tx, Event::CrackStopped { found: false }); } } }; if let Err(payload) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(run)) { // A worker panic is a backend failure too: surface it as // CrackError (the session file stays for a resume) instead // of the historical silent "exhausted" mis-report. let msg = if let Some(s) = payload.downcast_ref::<&str>() { (*s).to_string() } else if let Some(s) = payload.downcast_ref::() { s.clone() } else { "unknown panic payload".to_string() }; log_err(&panic_tx, format!("cracker: worker panicked: {msg}")); send(&panic_tx, Event::CrackError { reason: format!("worker panicked: {msg}") }); } }); if let Err(e) = result { send(&err_tx, Event::Error(format!("cracker: spawn failed: {e}"))); send(&err_tx, Event::CrackStopped { found: false }); } stop } /// Build a rainbow PMK table for `ssid` from a wordlist (PBKDF2 once — /// GPU when available), so later runs against that SSID are PBKDF2-free. /// Always ends with exactly one `RainbowBuilt` (entries written so far), /// preceded by `Error` events on failure. pub fn spawn_rainbow_build( ssid: String, wordlist: PathBuf, out: PathBuf, tx: Sender, ) -> Arc { let stop = Arc::new(AtomicBool::new(false)); let stop2 = stop.clone(); let err_tx = tx.clone(); let out2 = out.clone(); let result = std::thread::Builder::new() .name("wifi_lab-rainbow-build".into()) .spawn(move || { let entries = build_table(&ssid, &wordlist, &out2, &tx, &stop2); send( &tx, Event::RainbowBuilt { path: out2.display().to_string(), entries, }, ); }); if let Err(e) = result { send(&err_tx, Event::Error(format!("rainbow: spawn failed: {e}"))); send( &err_tx, Event::RainbowBuilt { path: out.display().to_string(), entries: 0, }, ); } stop } /// Batch PMK computer: raw passphrases in, one PMK per word out (GPU /// kernel or test fake). type PmkBatchFn<'a> = dyn FnMut(&[Vec]) -> Result, gpu::GpuError> + 'a; /// Compute PMKs for EVERY word of `batch` over the GPU batch kernel. /// `pmk_batch_gpu` may return only a PREFIX after its internal /// halving-retry — the kernel was invoked on the head, the tail was never /// attempted; loop it until the whole batch is covered (each retry itself /// may return a shorter prefix). An empty `Ok` on a non-empty slice would /// spin forever, so it is treated as failure. `Err` aborts the build /// (the caller keeps the partial table). fn gpu_pmk_batch_full( compute: &mut PmkBatchFn<'_>, batch: &[Vec], ) -> Result, gpu::GpuError> { let mut out: Vec<[u8; 32]> = Vec::with_capacity(batch.len()); while out.len() < batch.len() { let tail = &batch[out.len()..]; let mut got = compute(tail)?; if got.is_empty() { // Not per the kernel's contract — bail rather than spin. return Err(gpu::GpuError::Unsupported( "batch kernel returned zero PMKs for a non-empty input".to_string(), )); } // Defensive: never accept more PMKs than requested words. got.truncate(tail.len()); out.append(&mut got); } Ok(out) } /// The build loop: wordlist batches → PMKs (GPU batch or per-word CPU) → /// table file. Returns entries actually written. fn build_table( ssid: &str, wordlist_path: &Path, out: &Path, tx: &Sender, stop: &Arc, ) -> u64 { let mut words = match wordlist::open(wordlist_path) { Ok(w) => w, Err(e) => { log_err(tx, format!("rainbow: {e:#}")); return 0; } }; let mut table = match rainbow::create(out, ssid) { Ok(t) => t, Err(e) => { log_err(tx, format!("rainbow: {e:#}")); return 0; } }; let gpu = GpuCracker::try_new(); log( tx, match &gpu { Some(g) => format!( "rainbow: building via GPU ({}) → {}", g.device_name(), out.display() ), None => format!("rainbow: building via CPU → {}", out.display()), }, ); const BATCH: usize = 4096; let salt = ssid.as_bytes(); let total = wordlist::line_count(wordlist_path).ok(); let mut written = 0u64; let started = Instant::now(); let mut last_tick = started; 'outer: loop { let mut batch: Vec> = Vec::with_capacity(BATCH); while batch.len() < BATCH { match words.next() { Some(w) => batch.push(w), None => break, } } if batch.is_empty() { break; } let pmks = match &gpu { Some(g) => { match gpu_pmk_batch_full(&mut |slice| g.pmk_batch_gpu(slice, salt), &batch) { Ok(p) => p, // Device hiccup even after the internal halving: give up, // the partial table stays usable. Err(e) => { log_err( tx, format!("rainbow: GPU batch failed ({e}) — table left partial"), ); break 'outer; } } } None => batch.iter().map(|w| crypto::pmk(w, salt)).collect(), }; debug_assert_eq!(pmks.len(), batch.len(), "every batch word must have a PMK"); for (pmk, w) in pmks.iter().zip(batch.iter()) { if let Err(e) = table.push(pmk, w) { log_err(tx, format!("rainbow: write failed: {e:#}")); break 'outer; } written += 1; } if last_tick.elapsed() > Duration::from_millis(250) { last_tick = Instant::now(); let secs = started.elapsed().as_secs_f64().max(1e-6); send( tx, Event::RainbowProgress { done: written, hps: written as f64 / secs, total, }, ); } if stop.load(Ordering::Relaxed) { log(tx, "rainbow: build stopped — partial table kept"); break; } } let skipped_at_start = wordlist::skipped_lines(); let skipped = wordlist::skipped_lines() - skipped_at_start; if skipped > 0 { log( tx, format!("wordlist: {skipped} line(s) skipped (out-of-range or undecodable bytes)"), ); } match table.finish() { Ok(n) => n, Err(e) => { log_err(tx, format!("rainbow: flush failed: {e:#}")); written } } } // --------------------------------------------------------------------------- // Benchmark — measured H/s for the crack-time estimator // --------------------------------------------------------------------------- /// CPU half of the benchmark: time `crypto::pmk` over candidate-sized /// batches and report hashes/sec. Runs at least one batch; honors `stop` /// between batches. fn bench_cpu(secs: f64, stop: Option<&AtomicBool>) -> f64 { const BATCH: u64 = 512; let word = b"benchmark1"; let salt = b"wifi_lab-bench"; let started = Instant::now(); let mut done = 0u64; loop { for _ in 0..BATCH { std::hint::black_box(crypto::pmk(std::hint::black_box(word), salt)); } done += BATCH; let elapsed = started.elapsed().as_secs_f64(); if elapsed >= secs { return done as f64 / elapsed.max(1e-6); } if let Some(s) = stop && s.load(Ordering::Relaxed) { return done as f64 / elapsed.max(1e-6); } } } /// GPU half: batches of 4096 fixed words through the PMK kernel for /// `secs`; `None` when no adapter exists (or the first dispatch fails — /// the CPU number is still meaningful then). fn bench_gpu(secs: f64, stop: Option<&AtomicBool>) -> Option { let g = GpuCracker::try_new()?; const BATCH: usize = 4096; let batch = vec![b"benchmark1".to_vec(); BATCH]; let salt = b"wifi_lab-bench"; let started = Instant::now(); let mut done = 0u64; loop { // A mid-bench backend failure just ends the GPU half (the CPU // number is still meaningful). done += g.pmk_batch_gpu(&batch, salt).ok()?.len() as u64; let elapsed = started.elapsed().as_secs_f64(); if elapsed >= secs { return Some(done as f64 / elapsed.max(1e-6)); } if let Some(s) = stop && s.load(Ordering::Relaxed) { return Some(done as f64 / elapsed.max(1e-6)); } } } /// Measure crack throughput on this machine for `seconds` per backend: /// returns `(gpu_hps, cpu_hps)` where the GPU half is `None` without an /// adapter. Honoring `stop` between batches; `None` stop = run to time. /// Synchronous — wrap it in [`spawn_benchmark`] for UI use. #[allow(dead_code)] pub fn benchmark(seconds: u32) -> (Option, f64) { let secs = seconds.max(1) as f64; (bench_gpu(secs, None), bench_cpu(secs, None)) } /// Benchmark on a background thread ("wifi_lab-benchmark"); terminates /// with exactly one `Event::BenchmarkDone { gpu_hps, cpu_hps }` (on spawn /// failure: an `Error` plus a zeroed `BenchmarkDone`). Honors the stop /// flag between batches. The UI guards against running it alongside a /// crack or rainbow build. #[allow(dead_code)] pub fn spawn_benchmark(seconds: u32, tx: Sender) -> Arc { let stop = Arc::new(AtomicBool::new(false)); let stop2 = stop.clone(); let err_tx = tx.clone(); let result = std::thread::Builder::new() .name("wifi_lab-benchmark".into()) .spawn(move || { log( &tx, format!("benchmark: measuring crack speed ({seconds}s per backend)"), ); let secs = seconds.max(1) as f64; let gpu_hps = bench_gpu(secs, Some(&stop2)); let cpu_hps = bench_cpu(secs, Some(&stop2)); send(&tx, Event::BenchmarkDone { gpu_hps, cpu_hps }); }); if let Err(e) = result { send( &err_tx, Event::Error(format!("benchmark: spawn failed: {e}")), ); send( &err_tx, Event::BenchmarkDone { gpu_hps: None, cpu_hps: 0.0, }, ); } stop } #[cfg(test)] mod tests { use super::*; use crate::common::{HandshakeCapture, MacAddr}; fn synthetic_target(ssid: &str, pass: &[u8]) -> CrackTarget { let ap: MacAddr = "aa:bb:cc:dd:ee:ff".parse().unwrap(); let sta: MacAddr = "11:22:33:44:55:66".parse().unwrap(); let anonce = [0x5a; 32]; let snonce = [0xc3; 32]; let key = crypto::pmk(pass, ssid.as_bytes()); let dk = crypto::ptk(&key, &ap, &sta, &anonce, &snonce); // Minimal M2 frame (see gpu::tests::build_eapol_m2 — same layout). let mut f = Vec::with_capacity(99); f.push(0x02); f.push(0x03); f.extend_from_slice(&95u16.to_be_bytes()); f.push(0x02); let info: u16 = 0x0100 | 0x0080 | 2; f.extend_from_slice(&info.to_be_bytes()); f.extend_from_slice(&16u16.to_be_bytes()); f.extend_from_slice(&1u64.to_be_bytes()); f.extend_from_slice(&snonce); f.extend_from_slice(&[0u8; 16]); f.extend_from_slice(&[0u8; 8]); f.extend_from_slice(&[0u8; 8]); f.extend_from_slice(&[0u8; 16]); f.extend_from_slice(&0u16.to_be_bytes()); let m = crypto::mic_on_zeroed(&dk, &crypto::zeroed_eapol(&f), 2); CrackTarget::Handshake(HandshakeCapture::new( ap, sta, ssid.to_string(), anonce, snonce, m, f, 2, )) } #[test] fn bruteforce_order_and_exhaustion() { let bf = BruteForce::new(b"ab".to_vec(), 8, 8, false).unwrap(); let got: Vec> = bf.collect(); assert_eq!(got.len(), 256); assert_eq!(got[0], b"aaaaaaaa"); assert_eq!(got[1], b"aaaaaaab"); assert_eq!(got[2], b"aaaaaaba"); assert_eq!(got[255], b"bbbbbbbb"); } #[test] fn bruteforce_length_growth_and_clamp() { // Length grows only after the shorter space is exhausted; range is // clamped into 8..=63. let mut bf = BruteForce::new(b"z".to_vec(), 1, 100, false).unwrap(); assert_eq!(bf.next().unwrap(), b"zzzzzzzz"); assert_eq!(bf.next().unwrap(), b"zzzzzzzzz"); let mut bf = BruteForce::new(b"z".to_vec(), 1, 9, false).unwrap(); assert_eq!(bf.next().unwrap(), b"zzzzzzzz"); assert_eq!(bf.next().unwrap(), b"zzzzzzzzz"); assert_eq!(bf.next(), None); } #[test] fn bruteforce_rejects_bad_input() { assert!(BruteForce::new(Vec::new(), 8, 12, false).is_err()); assert!(BruteForce::new(b"a".to_vec(), 64, 70, false).is_err()); assert!(BruteForce::new(b"a".to_vec(), 12, 8, false).is_err()); } #[test] fn bruteforce_seek_lands_exactly() { // seek(j) must equal collecting j items — across a length boundary. let reference: Vec> = BruteForce::new(b"abcde".to_vec(), 8, 9, false) .unwrap() .take(5usize.pow(8) + 40) .collect(); for j in [0u64, 1, 17, 5u64.pow(8) - 1, 5u64.pow(8), 5u64.pow(8) + 39] { let mut bf = BruteForce::new(b"abcde".to_vec(), 8, 9, false).unwrap(); bf.seek(j); let got: Vec> = bf.by_ref().take(3).collect(); let want = &reference[j as usize..(j as usize + 3).min(reference.len())]; assert_eq!(&got[..want.len()], want, "j={j}"); } // Beyond the space: exhausted. let mut bf = BruteForce::new(b"ab".to_vec(), 8, 8, false).unwrap(); bf.seek(256); assert_eq!(bf.next(), None); } #[test] fn bruteforce_space_matches_stream_count() { let bf = BruteForce::new(b"abcd".to_vec(), 8, 9, false).unwrap(); assert_eq!(bf.space(), 4u128.pow(8) + 4u128.pow(9)); assert_eq!(bf.count() as u128, 4u128.pow(8) + 4u128.pow(9)); } #[test] fn rainbow_crack_finds_pass() { let pass = b"sup3r-secret"; let target = synthetic_target("testnet", pass); let entries: Vec<([u8; 32], Vec)> = vec![ (crypto::pmk(b"nope-nope", b"testnet"), b"nope-nope".to_vec()), (crypto::pmk(pass, b"testnet"), pass.to_vec()), ]; let mut it = entries.into_iter(); let stop = Arc::new(AtomicBool::new(false)); let found = rainbow_crack(&target, &mut it, &mut |_, _| {}, &stop); assert_eq!(found.as_deref(), Some("sup3r-secret")); } #[test] fn build_then_crack_end_to_end() { // The exact files the worker threads touch: wordlist in, table out, // crack via the table — no GPU required. let dir = std::env::temp_dir(); let wl = dir.join(format!("wifi_lab-attack-wl-{}", std::process::id())); let tbl = dir.join(format!("wifi_lab-attack-tbl-{}", std::process::id())); std::fs::write(&wl, "nope-nope\nsup3r-secret\nalsonope!\n").unwrap(); let (etx, erx) = crossbeam_channel::unbounded(); let stop = Arc::new(AtomicBool::new(false)); let written = build_table("testnet", &wl, &tbl, &etx, &stop); assert_eq!(written, 3); let built: Vec = erx.try_iter().collect(); assert!(!built.iter().any(|e| matches!(e, Event::Error(_)))); let target = synthetic_target("testnet", b"sup3r-secret"); let (ssid, mut entries) = rainbow::open(&tbl).unwrap(); rainbow::check_ssid(ssid.as_deref(), target.essid()).unwrap(); let found = rainbow_crack(&target, &mut entries, &mut |_, _| {}, &stop); assert_eq!(found.as_deref(), Some("sup3r-secret")); // Wrong-ssid table must be refused, not silently exhausted. let target2 = synthetic_target("othernet", b"sup3r-secret"); assert!(rainbow::check_ssid(ssid.as_deref(), target2.essid()).is_err()); let _ = std::fs::remove_file(&wl); let _ = std::fs::remove_file(&tbl); } #[test] fn spawn_crack_emits_progress_and_terminator() { // The full worker contract over the channel: starting a run MUST // surface progress and exactly one terminator (the historical bug // was a run that looked alive at 0 H/s forever). CPU fallback is // fine — the GPU path shares the same word_crack wrapper. let dir = std::env::temp_dir(); let wl = dir.join(format!("wifi_lab-spawn-wl-{}", std::process::id())); std::fs::write(&wl, "wrong-pass1\nsup3r-secret\nwrong-pass2\n").unwrap(); let target = synthetic_target("testnet", b"sup3r-secret"); let (etx, erx) = crossbeam_channel::unbounded(); // force_cpu: deterministic CPU run AND no PMK-cache writes into // the real ~/wifi_lab/cache from a test. let _stop = spawn_crack( target.describe(), target, Attack::Wordlist(wl.clone()), 0, etx, true, ); let mut found = None; let mut stopped = false; let deadline = std::time::Instant::now() + Duration::from_secs(60); while !(stopped) && std::time::Instant::now() < deadline { match erx.recv_timeout(Duration::from_secs(5)) { Ok(Event::CrackFound { password }) => found = Some(password), Ok(Event::CrackStopped { found: true }) => stopped = true, Ok(Event::CrackStopped { found: false }) => { panic!("run ended without finding the planted pass") } Ok(Event::Error(line)) => panic!("worker error: {line}"), Ok(_) => {} // Log/CrackProgress Err(_) => panic!("channel silent — no events, no terminator"), } } assert!(stopped, "no terminator within deadline"); assert_eq!(found.as_deref(), Some("sup3r-secret")); let _ = std::fs::remove_file(&wl); } #[test] #[ignore = "smoke: user's real stored capture + GPU; run on demand"] fn smoke_real_stored_capture() { let caps = crate::store::load_captures().expect(" ~/wifi_lab/captures.jsonl"); let target = caps .iter() .find_map(|c| match c { crate::store::StoredCapture::Handshake(h) => { Some(CrackTarget::Handshake(h.clone())) } _ => None, }) .expect("a stored handshake"); let ssid = target.essid().to_string(); eprintln!("smoke target: {}", target.describe()); let dir = std::env::temp_dir(); let wl = dir.join("wifi_lab-smoke-wl.txt"); std::fs::write(&wl, "password123\nletmein123\nqwertyuiop1\n").unwrap(); let tbl = dir.join("wifi_lab-smoke.pmk"); let drain = |erx: &crossbeam_channel::Receiver, what: &str| { let mut saw_progress = false; let deadline = std::time::Instant::now() + Duration::from_secs(180); loop { assert!( std::time::Instant::now() < deadline, "{what}: no terminator" ); match erx.recv_timeout(Duration::from_secs(10)) { Ok( Event::CrackProgress { done, hps, .. } | Event::RainbowProgress { done, hps, .. }, ) => { if done > 0 { saw_progress = true; assert!(hps > 0.0, "{what}: progress with 0 H/s"); } } Ok(Event::CrackStopped { found }) => { assert!(!found, "{what}: unexpected password match"); break; } Ok(Event::RainbowBuilt { entries, .. }) => { assert!(entries > 0, "{what}: empty table"); break; } Ok(Event::Error(l)) => panic!("{what}: {l}"), Ok(_) => {} Err(_) => panic!("{what}: channel silence"), } } assert!( saw_progress || what.contains("build"), "{what}: zero progress events" ); }; // 1. wordlist run (GPU): the exact stuck path from the bug report. let (tx, rx) = crossbeam_channel::unbounded(); let _s1 = spawn_crack( target.describe(), target.clone(), Attack::Wordlist(wl.clone()), 0, tx, false, ); drain(&rx, "wordlist run"); // 2. rainbow build then run (GPU for the build). let (tx, rx) = crossbeam_channel::unbounded(); let _s2 = spawn_rainbow_build(ssid, wl.clone(), tbl.clone(), tx); drain(&rx, "rainbow build"); let (tx, rx) = crossbeam_channel::unbounded(); let _s3 = spawn_crack( target.describe(), target, Attack::Rainbow(tbl.clone()), 0, tx, false, ); drain(&rx, "rainbow run"); let _ = std::fs::remove_file(&wl); let _ = std::fs::remove_file(&tbl); } #[test] fn resume_from_skips_checked_candidates() { // wordlist positions: 0 wrong, 1 wrong, 2 TARGET, 3 wrong. let dir = std::env::temp_dir(); let wl = dir.join(format!("wifi_lab-resume-wl-{}", std::process::id())); std::fs::write(&wl, "wrong-pass1\nwrong-pass2\nsup3r-secret\nwrong-pass3\n").unwrap(); let target = synthetic_target("testnet", b"sup3r-secret"); let run = |resume_from: u64| -> (Option, bool) { let (etx, erx) = crossbeam_channel::unbounded(); // force_cpu: keeps the run CPU-deterministic and the real // per-SSID PMK cache untouched. let _s = spawn_crack( target.describe(), target.clone(), Attack::Wordlist(wl.clone()), resume_from, etx, true, ); let mut pass = None; let deadline = std::time::Instant::now() + Duration::from_secs(60); loop { assert!(std::time::Instant::now() < deadline, "no terminator"); match erx.recv_timeout(Duration::from_secs(5)) { Ok(Event::CrackFound { password }) => pass = Some(password), Ok(Event::CrackStopped { found }) => return (pass, found), Ok(Event::Error(l)) => panic!("worker error: {l}"), Ok(_) => {} Err(_) => panic!("channel silent"), } } }; // Resume BEFORE the target's position: found. assert_eq!(run(2), (Some("sup3r-secret".to_string()), true)); // Resume past it: exhausted without a match. assert_eq!(run(3), (None, false)); let _ = std::fs::remove_file(&wl); } #[test] fn validate_catches_missing_files() { assert!( Attack::Wordlist(PathBuf::from("/definitely/not/here.lst")) .validate() .is_err() ); assert!( Attack::Rainbow(PathBuf::from("/definitely/not/here.pmk")) .validate() .is_err() ); assert!( Attack::Bruteforce { charset: b"01".to_vec(), min: 8, max: 10, freq_order: false } .validate() .is_ok() ); assert!( Attack::Rules { wordlist: PathBuf::from("/definitely/not/here.lst"), rules: PathBuf::from("/definitely/not/here.rule"), } .validate() .is_err() ); assert!( Attack::Hybrid { wordlist: PathBuf::from("/definitely/not/here.lst"), charset: b"01".to_vec(), min: 1, max: 2, } .validate() .is_err() ); // Inverted (clamped) hybrid suffix range is refused. assert!(HybridIter::validate(b"01", 4, 1).is_err()); assert!(HybridIter::validate(&[], 1, 2).is_err()); } #[test] fn rule_ops_golden_table() { // One row per op, plus a compound multi-op line ("c $1 $9"-style). let cases: &[(&str, &[u8], &[u8])] = &[ (":", b"password1", b"password1"), ("l", b"PassWord1", b"password1"), ("u", b"PassWord1", b"PASSWORD1"), ("c", b"pASSWORD1", b"Password1"), ("C", b"Password1", b"pASSWORD1"), ("t", b"PassWord1", b"pASSwORD1"), ("r", b"password", b"drowssap"), ("d", b"password", b"passwordpassword"), ("$1", b"password", b"password1"), ("^1", b"password", b"1password"), ("[", b"password1", b"assword1"), ("]", b"password1", b"password"), ("so0", b"password05", b"passw0rd05"), // Compound: capitalize, append "1", append "!" — the classic // "Password1!" family from an 8-char base word. ("c $1 $!", b"password", b"Password1!"), ]; for (rule_text, input, want) in cases { let ops = parse_rule_line(rule_text.as_bytes(), 0).unwrap(); assert_eq!( apply_rule(&ops, input).as_deref(), Some(&want[..]), "rule {rule_text:?} on {input:?}" ); } // Outputs outside 8..=63 are filtered (but consume the index). let ops = parse_rule_line(b"]", 0).unwrap(); assert_eq!(apply_rule(&ops, b"12345678"), None); // 7 < 8 let ops = parse_rule_line(b"d d d", 0).unwrap(); assert_eq!(apply_rule(&ops, b"12345678901234"), None); // 14*8 = 112 > 63 } #[test] fn parse_rules_skips_comments_and_blanks() { let dir = std::env::temp_dir(); let path = dir.join(format!("wifi_lab-rules-{}", std::process::id())); std::fs::write(&path, "# comment\n\n:\nl\n$1\n# another\nso0\n").unwrap(); let rules = parse_rules(&path).unwrap(); assert_eq!(rules.len(), 4); assert_eq!(rule_count(&path).unwrap(), 4); let _ = std::fs::remove_file(&path); } #[test] fn rule_iter_flatten_and_resume_continuation() { // 3 words × 2 rules: flattened order is word-major / rule-minor. let words: Vec> = vec![ b"password".to_vec(), b"letmein1".to_vec(), b"hunter22".to_vec(), ]; let rules = std::rc::Rc::new(vec![ parse_rule_line(b":", 0).unwrap(), parse_rule_line(b"$1", 0).unwrap(), ]); let collect_from = |it: &mut dyn Iterator>| -> Vec> { it.collect() }; let mut full = RuleIter::new(Box::new(words.clone().into_iter()), rules.clone()); let got = collect_from(&mut full); assert_eq!( got, vec![ b"password".to_vec(), b"password1".to_vec(), b"letmein1".to_vec(), b"letmein11".to_vec(), b"hunter22".to_vec(), b"hunter221".to_vec(), ] ); // Resume: skip_n(3) must continue exactly with candidate #3. let mut resumed = RuleIter::new(Box::new(words.into_iter()), rules); skip_n(&mut resumed, 3); assert_eq!( collect_from(&mut resumed), vec![ b"letmein11".to_vec(), b"hunter22".to_vec(), b"hunter221".to_vec() ] ); } #[test] fn hybrid_space_math_and_order() { // charset {0,1}, k in 1..=2: per-word = 2 + 4 = 6. let words: Vec> = vec![b"password".to_vec()]; let mut it = HybridIter::new(Box::new(words.into_iter()), b"01".to_vec(), 1, 2).unwrap(); assert_eq!(it.per_word(), 6); let got: Vec> = it.by_ref().collect(); assert_eq!( got, vec![ b"password0".to_vec(), b"password1".to_vec(), b"password00".to_vec(), b"password01".to_vec(), b"password10".to_vec(), b"password11".to_vec(), ], "k-blocks ascending, odometer little-endian within a block" ); } #[test] fn hybrid_seek_flat_continuation() { // 2 words, per_word 6: full space is 12 candidates. let words: Vec> = vec![b"password".to_vec(), b"hunter22".to_vec()]; let reference: Vec> = HybridIter::new(Box::new(words.clone().into_iter()), b"ab".to_vec(), 1, 2) .unwrap() .collect(); assert_eq!(reference.len(), 12); for n in [0u64, 1, 5, 6, 7, 11] { let mut it = HybridIter::new(Box::new(words.clone().into_iter()), b"ab".to_vec(), 1, 2).unwrap(); it.seek_flat(n); let got: Vec> = it.collect(); assert_eq!(got, reference[n as usize..], "seek_flat({n})"); } // Past the end: empty. let mut it = HybridIter::new(Box::new(words.into_iter()), b"ab".to_vec(), 1, 2).unwrap(); it.seek_flat(12); assert_eq!(it.next(), None); } #[test] fn hybrid_skips_overlong_but_consumes_index() { // A 63-byte word + any suffix exceeds 63 → candidates filtered, // but flattened slots still consumed: seeking past the first // word's 6 slots lands at the second word's first candidate. let long_word = vec![b'x'; 63]; let words: Vec> = vec![long_word.clone(), b"password".to_vec()]; let mut it = HybridIter::new(Box::new(words.clone().into_iter()), b"ab".to_vec(), 1, 2).unwrap(); it.seek_flat(6); assert_eq!(it.next().unwrap(), b"passworda"); // EXACT semantics: running through the overlong word consumes all // of its 6 slots, so the STREAM position after the run equals the // position seek_flat(6) addresses — resume can use the live // position directly, no drift. let mut run = HybridIter::new(Box::new(words.into_iter()), b"ab".to_vec(), 1, 2).unwrap(); assert_eq!(run.next().unwrap(), b"passworda"); assert_eq!(run.raw_pos(), 7, "six filtered slots + one yielded"); let mut resumed = HybridIter::new( Box::new(vec![long_word, b"password".to_vec()].into_iter()), b"ab".to_vec(), 1, 2, ) .unwrap(); resumed.seek_flat(7); assert_eq!(resumed.next().unwrap(), b"passwordb"); } #[test] fn hybrid_raw_pos_exact_through_overlong_word() { // The overlong word contributes NOTHING but still consumes its // full flattened block — raw positions jump from 0 to 6 (per_word) // to 12 without any yield, then the second word emits at 6..11. let long_word = vec![b'x'; 63]; let words: Vec> = vec![long_word, b"hunter22".to_vec()]; let mut it = HybridIter::new(Box::new(words.into_iter()), b"ab".to_vec(), 1, 2).unwrap(); let mut positions = Vec::new(); let mut out = Vec::new(); while let Some(c) = it.next() { positions.push(it.raw_pos()); out.push(c); } assert_eq!(out.len(), 6, "only the 8-byte word yields"); assert_eq!(positions, vec![7, 8, 9, 10, 11, 12]); // Resuming at each recorded raw position must continue exactly. for (i, &pos) in positions.iter().enumerate() { let words: Vec> = vec![vec![b'x'; 63], b"hunter22".to_vec()]; let mut r = HybridIter::new(Box::new(words.into_iter()), b"ab".to_vec(), 1, 2).unwrap(); r.seek_flat(pos); let rest: Vec> = r.collect(); assert_eq!(rest, out[i + 1..], "resume at raw {pos}"); } } #[test] fn rule_iter_raw_pos_and_seek_flat_exact() { // Positions count filter-consumed slots; seek_flat(pos) continues // exactly where a run at raw position `pos` would. let words: Vec> = vec![b"password".to_vec(), b"letmein1".to_vec()]; let rules = Rc::new(vec![ parse_rule_line(b"]", 0).unwrap(), // 8-byte word → 7 bytes: FILTERED parse_rule_line(b":", 0).unwrap(), parse_rule_line(b"$!", 0).unwrap(), ]); let mut it = RuleIter::new(Box::new(words.clone().into_iter()), rules.clone()); let mut seen = Vec::new(); while let Some(c) = it.next() { seen.push((it.raw_pos(), c)); } // 2 words × 3 rules = 6 slots; the two `]` outputs (7 bytes) are // filtered, so 4 yields at raw positions after each slot. assert_eq!( seen, vec![ (2, b"password".to_vec()), (3, b"password!".to_vec()), (5, b"letmein1".to_vec()), (6, b"letmein1!".to_vec()), ] ); // Resume from every recorded position plus interesting slots // (including a filtered one) continues exactly. let reference: Vec> = seen.iter().map(|(_, c)| c.clone()).collect(); for start in 0..=6u64 { let mut r = RuleIter::new(Box::new(words.clone().into_iter()), rules.clone()); r.seek_flat(start); let rest: Vec> = r.collect(); let want: Vec> = seen .iter() .filter(|(pos, _)| *pos > start) .map(|(_, c)| c.clone()) .collect(); assert_eq!(rest, want, "seek_flat({start}) of {reference:?}"); } } #[test] fn pos_feed_progress_maps_to_raw_positions() { // Cache-covered candidates are dropped between raw stream and // backend, and the backend reports counts in POST-skip terms; the // progress translation must still yield raw positions. let words: Vec> = (0..6u8).map(|i| vec![b'0' + i; 8]).collect(); let covered: HashSet> = [ vec![b'1'; 8].into_boxed_slice(), vec![b'3'; 8].into_boxed_slice(), ] .into_iter() .collect(); let raw = CountIter::at(words.into_iter(), 0); let stream: Box = Box::new(CacheSkipIter::new(raw, covered)); let (mut feed, map) = PosFeed::new(stream); // Backend pulls its first batch (all 4 surviving candidates), then // reports progress in two chunks. let pulled: Vec> = feed.by_ref().collect(); assert_eq!(pulled.len(), 4); let mut acked = 0u64; assert_eq!(raw_position_of(&map, &mut acked, 1), Some(1)); assert_eq!( raw_position_of(&map, &mut acked, 2), Some(3), "skipped raw 1,2" ); assert_eq!( raw_position_of(&map, &mut acked, 4), Some(6), "skipped raw 3,4" ); // Map drained: every confirmed candidate consumed exactly its raw // position; resume at 6 continues past '0'..'5'. assert!(map.borrow().is_empty()); // Underflow (a progress tick beyond recorded yields) is a no-op. assert_eq!(raw_position_of(&map, &mut acked, 5), None); } #[test] fn gpu_pmk_batch_tail_is_computed() { // Fake kernel: succeeds only with a PREFIX — first call returns // half, subsequent calls again shorter prefixes — the loop must // keep asking until every word has its PMK. let batch: Vec> = (0..5u8).map(|i| vec![b'a' + i; 8]).collect(); let salt = b"testnet"; let mut calls = 0usize; let pmks = gpu_pmk_batch_full( &mut |slice: &[Vec]| { calls += 1; let n = if slice.len() == 1 { 1 } else { slice.len() / 2 }; Ok(slice[..n].iter().map(|w| crypto::pmk(w, salt)).collect()) }, &batch, ) .unwrap(); assert_eq!(pmks.len(), batch.len(), "tail words must not vanish"); for (pmk, w) in pmks.iter().zip(batch.iter()) { assert_eq!(pmk, &crypto::pmk(w, salt)); } assert!( calls >= 4, "prefix returns force several tail calls ({calls})" ); } #[test] fn gpu_pmk_batch_err_propagates_and_empty_aborts() { let batch: Vec> = vec![b"password1".to_vec(), b"password2".to_vec()]; let err = gpu_pmk_batch_full(&mut |_| Err(gpu::GpuError::Lost), &batch).unwrap_err(); assert_eq!(err, gpu::GpuError::Lost); // A zero-PMK Ok would spin forever without the guard. let err = gpu_pmk_batch_full(&mut |_| Ok(Vec::new()), &batch).unwrap_err(); assert!(matches!(err, gpu::GpuError::Unsupported(_))); } #[test] fn spawn_crack_worker_panic_becomes_crack_error() { // An invalid attack that BYPASSES validate() (empty hybrid // charset) panics inside the worker at the `.expect("validated")` // — the terminator must be CrackError, not a fake "exhausted". let target = synthetic_target("testnet", b"sup3r-secret"); let wl = std::env::temp_dir().join(format!("wifi_lab-panic-wl-{}", std::process::id())); std::fs::write(&wl, "whatever1\n").unwrap(); let (etx, erx) = crossbeam_channel::unbounded(); let _stop = spawn_crack( "test".to_string(), target, Attack::Hybrid { wordlist: wl.clone(), charset: Vec::new(), min: 1, max: 2, }, 0, etx, true, ); let deadline = std::time::Instant::now() + Duration::from_secs(30); let got_error = loop { assert!( std::time::Instant::now() < deadline, "no terminator after worker panic" ); match erx.recv_timeout(Duration::from_secs(5)) { Ok(Event::CrackError { reason }) => break reason, Ok(Event::CrackStopped { found }) => { panic!("panic was mis-reported as CrackStopped {{ found: {found} }}") } Ok(Event::Error(_)) => {} // the panic's log line Ok(_) => {} Err(_) => panic!("channel silent — no terminator"), } }; assert!(got_error.contains("panicked")); let _ = std::fs::remove_file(&wl); } #[test] fn rainbow_resume_position_exact() { // Build a 3-entry table, resume at 1 and at 3: the position is a // raw entry index — 1 finds the planted pass, 3 exhausts. let dir = std::env::temp_dir(); let wl = dir.join(format!("wifi_lab-rbresume-wl-{}", std::process::id())); let tbl = dir.join(format!("wifi_lab-rbresume-tbl-{}", std::process::id())); std::fs::write(&wl, "wrong-pass1\nsup3r-secret\nwrong-pass2\n").unwrap(); let (etx, _erx) = crossbeam_channel::unbounded(); let stop = Arc::new(AtomicBool::new(false)); assert_eq!(build_table("testnet", &wl, &tbl, &etx, &stop), 3); let target = synthetic_target("testnet", b"sup3r-secret"); let run = |resume_from: u64| -> (Option, bool) { let (etx, erx) = crossbeam_channel::unbounded(); let _s = spawn_crack( target.describe(), target.clone(), Attack::Rainbow(tbl.clone()), resume_from, etx, true, ); let mut pass = None; let deadline = std::time::Instant::now() + Duration::from_secs(60); loop { assert!(std::time::Instant::now() < deadline, "no terminator"); match erx.recv_timeout(Duration::from_secs(5)) { Ok(Event::CrackFound { password }) => pass = Some(password), Ok(Event::CrackStopped { found }) => return (pass, found), Ok(Event::CrackError { reason }) => panic!("backend error: {reason}"), Ok(Event::Error(l)) => panic!("worker error: {l}"), Ok(_) => {} Err(_) => panic!("channel silent"), } } }; assert_eq!(run(1), (Some("sup3r-secret".to_string()), true)); assert_eq!(run(3), (None, false)); let _ = std::fs::remove_file(&wl); let _ = std::fs::remove_file(&tbl); } #[test] fn resume_progress_reports_raw_positions() { // End-to-end position semantics at the event level: a resumed // wordlist run reports progress as RAW stream positions — the // first tick after a resume at 1 with a 512-item CPU batch is raw // 513 (1 + 512 confirmed), not the old post-filter yield count. let dir = std::env::temp_dir(); let wl = dir.join(format!("wifi_lab-rawpos-wl-{}", std::process::id())); let mut lines: Vec = (0..700).map(|i| format!("filler-{i:04}")).collect(); lines[650] = "sup3r-secret".to_string(); std::fs::write(&wl, lines.join("\n") + "\n").unwrap(); // A test-unique SSID keeps the real per-SSID PMK cache out of the // run (consult proceeds straight to a miss). let target = synthetic_target("rawpos-test-ssid", b"sup3r-secret"); let (etx, erx) = crossbeam_channel::unbounded(); let _s = spawn_crack( target.describe(), target, Attack::Wordlist(wl.clone()), 1, etx, true, ); let mut positions = Vec::new(); let mut found = false; let deadline = std::time::Instant::now() + Duration::from_secs(120); loop { assert!(std::time::Instant::now() < deadline, "no terminator"); match erx.recv_timeout(Duration::from_secs(30)) { Ok(Event::CrackProgress { done, .. }) => positions.push(done), Ok(Event::CrackFound { password }) => { assert_eq!(password, "sup3r-secret"); found = true; } Ok(Event::CrackStopped { found: true }) => break, Ok(Event::CrackError { reason }) => panic!("backend error: {reason}"), Ok(Event::Error(l)) => panic!("worker error: {l}"), Ok(_) => {} Err(_) => panic!("channel silent"), } } assert!(found); // Resume(1) + first full batch of 512 confirmed = raw 513; then // the find short-circuits batch 2 before its tick. assert_eq!(positions, vec![513]); let _ = std::fs::remove_file(&wl); } #[test] fn freq_order_puts_common_first_and_seeks() { // Charset with members ranked differently in FREQ_ORDER: 'e' // (rank 0) precedes 'z' (absent→end) regardless of input order. let bf = BruteForce::new(b"ze".to_vec(), 8, 8, true).unwrap(); let first: Vec> = bf.take(4).collect(); assert_eq!(first[0], b"eeeeeeee"); assert_eq!(first[1], b"eeeeeeez"); assert_eq!(first[2], b"eeeeeeze"); assert_eq!(first[3], b"eeeeeezz"); // Sanitizer: the space is still a bijection (size + seek match // the lexicographic construction). let freq: Vec> = BruteForce::new(b"wze1".to_vec(), 8, 8, true) .unwrap() .collect(); let mut plain: Vec> = BruteForce::new(b"wze1".to_vec(), 8, 8, false) .unwrap() .collect(); plain.sort(); let mut freq_sorted = freq.clone(); freq_sorted.sort(); assert_eq!(plain, freq_sorted, "freq_order must be a permutation"); // seek(n) then iterate == skip n from start. for n in [0u64, 3, 255] { let mut s = BruteForce::new(b"wze1".to_vec(), 8, 8, true).unwrap(); s.seek(n); let got: Vec> = s.by_ref().take(3).collect(); assert_eq!( got, freq[n as usize..(n as usize + 3).min(freq.len())], "seek({n})" ); } } #[test] fn cache_skip_iter_filters_covered() { let words: Vec> = vec![ b"password".to_vec(), b"letmein1".to_vec(), b"hunter22".to_vec(), ]; let covered: HashSet> = [b"letmein1".as_slice().into(), b"hunter22".as_slice().into()] .into_iter() .collect(); let it = CacheSkipIter::new(words.into_iter(), covered); assert_eq!(it.collect::>(), vec![b"password".to_vec()]); // Empty coverage = pass-through. let words2: Vec> = vec![b"password".to_vec()]; let it2 = CacheSkipIter::new(words2.into_iter(), HashSet::new()); assert_eq!(it2.count(), 1); } #[test] fn benchmark_cpu_half_returns_positive_hps() { // A ~0.1 s micro-run MUST return a positive throughput number // without needing a GPU. let hps = bench_cpu(0.1, None); assert!(hps > 0.0, "cpu benchmark returned {hps}"); } #[test] fn spawn_benchmark_terminates_with_benchmark_done() { // Contract: exactly one BenchmarkDone terminator arrives. A GPU // adapter (when present) may need a while for the first dispatch, // so wait generously before declaring a hang. let (tx, rx) = crossbeam_channel::unbounded(); let stop = spawn_benchmark(1, tx); let deadline = Instant::now() + Duration::from_secs(120); let mut done_seen = false; while Instant::now() < deadline { match rx.recv_timeout(deadline.duration_since(Instant::now())) { Ok(Event::BenchmarkDone { cpu_hps, .. }) => { assert!(cpu_hps > 0.0); done_seen = true; break; } Ok(Event::Error(e)) => panic!("benchmark error: {e}"), Ok(_) => {} // Log line Err(_) => break, } } assert!(done_seen, "no BenchmarkDone within 120 s"); drop(stop); } }