From b2cb2e9887fb46801a282d0ef96ce5a70e940a3e Mon Sep 17 00:00:00 2001 From: Four Random Common Words <304938239+four-random-common-words@users.noreply.github.com> Date: Mon, 12 Oct 2026 10:18:12 +0200 Subject: [PATCH 1/2] Resolve file paths in key=value attack arguments --- src/backend/generic/mod.rs | 40 ++++++++--- src/backend/hashcat/argv.rs | 33 ++++++--- src/backend/paths.rs | 138 +++++++++++++++++++++++++++++++++++- 3 files changed, 190 insertions(+), 21 deletions(-) diff --git a/src/backend/generic/mod.rs b/src/backend/generic/mod.rs index 027fb62..d49eafb 100644 --- a/src/backend/generic/mod.rs +++ b/src/backend/generic/mod.rs @@ -77,7 +77,7 @@ pub mod status; use std::path::{Path, PathBuf}; use crate::backend::args::Args; -use crate::backend::paths::absolute; +use crate::backend::paths::resolve_file_token; use crate::cracker::{ChunkOutcome, Cracker, CrackerError, HealthCheckOutcome, ProgressSink}; use crate::model::{Chunk, CrackerId, ResolvedPreprocessor, TaskAssignment}; @@ -101,8 +101,8 @@ impl GenericCracker { /// Resolve the attack for a crack or benchmark run: swap the hashlist alias /// for the absolute hashlist path and resolve any token naming a file under - /// the files dir to its absolute path. `-a` and every other token pass - /// through + /// the files dir to its absolute path, including a `key=value` token whose + /// value names a file. `-a` and every other token pass through fn resolve_attack(&self, attack: &str, alias: &str, hashlist: &Path) -> Vec { attack .split_whitespace() @@ -136,13 +136,7 @@ impl GenericCracker { } fn resolve_file_token(&self, token: &str) -> String { - let candidate = self.paths.files.join(token); - - if candidate.is_file() { - path_arg(&absolute(candidate)) - } else { - token.to_owned() - } + resolve_file_token(&self.paths.files, token) } } @@ -272,6 +266,32 @@ mod tests { ); } + #[test] + fn crack_attack_resolves_key_value_file_tokens() { + let (dir, cracker) = cracker_with_files(&["hint.hcstat2"]); + let hashlist = PathBuf::from("/data/hashlists/1"); + + let args = cracker.resolve_attack("-a #HL# hintfile=hint.hcstat2", ALIAS, &hashlist); + + assert_eq!( + args, + [ + "-a".to_owned(), + "/data/hashlists/1".to_owned(), + format!("hintfile={}", seeded(&dir, "hint.hcstat2")), + ] + ); + } + + #[test] + fn keyspace_attack_resolves_key_value_file_tokens() { + let (dir, cracker) = cracker_with_files(&["hint.hcstat2"]); + + let args = cracker.resolve_keyspace_attack("-a #HL# hintfile=hint.hcstat2", ALIAS); + + assert_eq!(args, [format!("hintfile={}", seeded(&dir, "hint.hcstat2"))]); + } + #[test] fn crack_attack_passes_unknown_tokens_through_in_order() { let (_dir, cracker) = cracker_with_files(&[]); diff --git a/src/backend/hashcat/argv.rs b/src/backend/hashcat/argv.rs index 4e8c272..dc7ff96 100644 --- a/src/backend/hashcat/argv.rs +++ b/src/backend/hashcat/argv.rs @@ -1,6 +1,6 @@ use std::path::Path; -use crate::backend::paths::absolute; +use crate::backend::paths::resolve_file_token; use super::HashcatCracker; @@ -8,8 +8,10 @@ impl HashcatCracker { /// Split the server's attack string into argv tokens, resolving each: the /// hashlist alias becomes `hashlist`, or is dropped entirely when `None` (a /// keyspace measure runs without the hashlist). Any token naming a file under - /// the files dir becomes that file's absolute path; everything else passes - /// through unchanged. No quoting — argv carries spaces natively. + /// the files dir becomes that file's absolute path, including a `key=value` + /// token whose value names a file (hashcat's `-a 4` PCFG hint files use this + /// form); everything else passes through unchanged. No quoting — argv carries + /// spaces natively. pub(super) fn resolve_attack( &self, attack: &str, @@ -27,13 +29,7 @@ impl HashcatCracker { continue; } - let candidate = self.paths.files.join(token); - - if candidate.is_file() { - resolved.push(path_arg(&absolute(candidate))); - } else { - resolved.push(token.to_owned()); - } + resolved.push(resolve_file_token(&self.paths.files, token)); } resolved @@ -133,6 +129,23 @@ mod tests { assert_eq!(args, ["-a", "0", "missing.txt", "?d?d"]); } + #[test] + fn key_value_file_tokens_resolve_to_absolute_paths() { + let (dir, cracker) = cracker_with_files(&["hint.hcstat2"]); + let hashlist = PathBuf::from("/data/hashlists/1"); + + let args = cracker.resolve_attack("#HL# -a 4 hintfile=hint.hcstat2", ALIAS, Some(&hashlist)); + + let expected = vec![ + "/data/hashlists/1".to_owned(), + "-a".to_owned(), + "4".to_owned(), + format!("hintfile={}", seeded(&dir, "hint.hcstat2")), + ]; + + assert_eq!(args, expected); + } + #[test] fn preserves_order_with_alias_mid_command() { let (dir, cracker) = cracker_with_files(&["wl.txt"]); diff --git a/src/backend/paths.rs b/src/backend/paths.rs index 44d4916..d55d2c0 100644 --- a/src/backend/paths.rs +++ b/src/backend/paths.rs @@ -1,9 +1,145 @@ //! Path helpers shared across the cracker backends. -use std::path::PathBuf; +use std::path::{Path, PathBuf}; /// Make `path` absolute without touching the filesystem, falling back to the /// original on the (platform-specific) error paths. pub(crate) fn absolute(path: PathBuf) -> PathBuf { std::path::absolute(&path).unwrap_or(path) } + +/// Resolve a single attack-command token to its argv form, replacing a file +/// name with its absolute path when the token names a file under `files_dir`. +/// +/// Three forms are handled: +/// - a bare token that names a file becomes that file's absolute path; +/// - a `key=value` token whose value names a file becomes `key=` +/// (hashcat's `-a 4` PCFG hint files use this form), with matching surrounding +/// quotes stripped from the value first; +/// - anything else passes through unchanged. +pub(crate) fn resolve_file_token(files_dir: &Path, token: &str) -> String { + let candidate = files_dir.join(token); + + if candidate.is_file() { + return absolute(candidate).to_string_lossy().into_owned(); + } + + if let Some((key, value)) = token.split_once('=') { + let value = strip_matching_quotes(value); + + if !value.is_empty() { + let candidate = files_dir.join(value); + + if candidate.is_file() { + let path = absolute(candidate); + + return format!("{key}={}", path.to_string_lossy()); + } + } + } + + token.to_owned() +} + +/// Strip one layer of matching surrounding single or double quotes from `value`, +/// mirroring the way the server may quote a `key="value"` argument. +fn strip_matching_quotes(value: &str) -> &str { + let bytes = value.as_bytes(); + + let quoted = bytes.len() > 1 + && (bytes[0] == b'"' || bytes[0] == b'\'') + && bytes[bytes.len() - 1] == bytes[0]; + + if quoted { + &value[1..value.len() - 1] + } else { + value + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use tempfile::TempDir; + + fn files_with(names: &[&str]) -> TempDir { + let dir = tempfile::tempdir().unwrap(); + + for name in names { + std::fs::write(dir.path().join(name), b"x").unwrap(); + } + + dir + } + + fn seeded(dir: &TempDir, name: &str) -> String { + dir.path().join(name).to_string_lossy().into_owned() + } + + #[test] + fn bare_file_token_resolves_to_absolute_path() { + let dir = files_with(&["rockyou.txt"]); + + let resolved = resolve_file_token(dir.path(), "rockyou.txt"); + + assert_eq!(resolved, seeded(&dir, "rockyou.txt")); + } + + #[test] + fn key_value_token_resolves_the_value() { + let dir = files_with(&["hint.hcstat2"]); + + let resolved = resolve_file_token(dir.path(), "hintfile=hint.hcstat2"); + + assert_eq!(resolved, format!("hintfile={}", seeded(&dir, "hint.hcstat2"))); + } + + #[test] + fn quoted_key_value_token_resolves_the_value() { + let dir = files_with(&["hint file.hcstat2"]); + + let double = resolve_file_token(dir.path(), "hintfile=\"hint file.hcstat2\""); + let single = resolve_file_token(dir.path(), "hintfile='hint file.hcstat2'"); + + let expected = format!("hintfile={}", seeded(&dir, "hint file.hcstat2")); + assert_eq!(double, expected); + assert_eq!(single, expected); + } + + #[test] + fn key_value_token_with_missing_file_passes_through_unchanged() { + let dir = files_with(&[]); + + let resolved = resolve_file_token(dir.path(), "hintfile=missing.hcstat2"); + + assert_eq!(resolved, "hintfile=missing.hcstat2"); + } + + #[test] + fn non_file_key_value_flag_passes_through_unchanged() { + let dir = files_with(&[]); + + let resolved = resolve_file_token(dir.path(), "--hash-type=0"); + + assert_eq!(resolved, "--hash-type=0"); + } + + #[test] + fn empty_value_passes_through_unchanged() { + let dir = files_with(&[]); + + let resolved = resolve_file_token(dir.path(), "hintfile="); + + assert_eq!(resolved, "hintfile="); + } + + #[test] + fn a_bare_file_takes_precedence_over_key_value_parsing() { + let dir = files_with(&["weird=name.txt"]); + + let resolved = resolve_file_token(dir.path(), "weird=name.txt"); + + assert_eq!(resolved, seeded(&dir, "weird=name.txt")); + } +} From cf93279bba4e1f3fa5b88dae91a60431670d3bc5 Mon Sep 17 00:00:00 2001 From: Four Random Common Words <304938239+four-random-common-words@users.noreply.github.com> Date: Mon, 12 Oct 2026 10:21:00 +0200 Subject: [PATCH 2/2] Apply rustfmt to key=value attack argument tests --- src/backend/hashcat/argv.rs | 3 ++- src/backend/paths.rs | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/backend/hashcat/argv.rs b/src/backend/hashcat/argv.rs index dc7ff96..e8dfa1b 100644 --- a/src/backend/hashcat/argv.rs +++ b/src/backend/hashcat/argv.rs @@ -134,7 +134,8 @@ mod tests { let (dir, cracker) = cracker_with_files(&["hint.hcstat2"]); let hashlist = PathBuf::from("/data/hashlists/1"); - let args = cracker.resolve_attack("#HL# -a 4 hintfile=hint.hcstat2", ALIAS, Some(&hashlist)); + let args = + cracker.resolve_attack("#HL# -a 4 hintfile=hint.hcstat2", ALIAS, Some(&hashlist)); let expected = vec![ "/data/hashlists/1".to_owned(), diff --git a/src/backend/paths.rs b/src/backend/paths.rs index d55d2c0..c4f930d 100644 --- a/src/backend/paths.rs +++ b/src/backend/paths.rs @@ -92,7 +92,10 @@ mod tests { let resolved = resolve_file_token(dir.path(), "hintfile=hint.hcstat2"); - assert_eq!(resolved, format!("hintfile={}", seeded(&dir, "hint.hcstat2"))); + assert_eq!( + resolved, + format!("hintfile={}", seeded(&dir, "hint.hcstat2")) + ); } #[test]