Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions src/backend/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<String> {
attack
.split_whitespace()
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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(&[]);
Expand Down
34 changes: 24 additions & 10 deletions src/backend/hashcat/argv.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::path::Path;

use crate::backend::paths::absolute;
use crate::backend::paths::resolve_file_token;

use super::HashcatCracker;

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,
Expand All @@ -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
Expand Down Expand Up @@ -133,6 +129,24 @@ 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"]);
Expand Down
141 changes: 140 additions & 1 deletion src/backend/paths.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,148 @@
//! 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=<absolute path>`
/// (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"));
}
}
Loading