hashglass 0.1.0

Fast pentester-focused hash identifier — mode + cost + ready hashcat command. Handles secretsdump / containers / mixed dumps. Identification only.
Documentation
//! Slice 8 — messy-input tolerance.
//!
//! Real dumps arrive with CRLF, UTF-8 BOM, quotes, indentation, trailing
//! commas, and often the hash is EMBEDDED — inside a JSON value, a CSV cell,
//! a `password: "..."` line, `NT: <hex>`. Clean identifiers refuse all of that.
//! This module strips the surface junk and lifts hash-shaped substrings out of
//! wrappers, feeding them through the normal rule set so we never invent a
//! second, less-tested identification path.
//!
//! Rejection discipline: an extract candidate is only returned if it looks
//! hash-shaped on its own (prefixed magic, hex ≥16, `$…$…` structure). We do
//! NOT return raw wordlike tokens — that would re-open the false-positive
//! class the strict rail (fix #2) exists to close.

/// Strip UTF-8 BOM, CRLF, indentation, wrapping quotes, and a trailing comma.
/// Byte-safe: leaves inner content untouched.
pub fn clean(line: &str) -> &str {
    let mut s = line;
    // UTF-8 BOM at the very start of the file's first line.
    if let Some(rest) = s.strip_prefix('\u{feff}') {
        s = rest;
    }
    s = s.trim_matches(|c: char| c.is_whitespace() || c == ',');
    // Strip a matched pair of wrapping quotes only.
    if s.len() >= 2 {
        let b = s.as_bytes();
        let first = b[0];
        let last = b[b.len() - 1];
        if (first == b'"' && last == b'"') || (first == b'\'' && last == b'\'') {
            s = &s[1..s.len() - 1];
        }
    }
    s
}

/// If the line has a `KEY: value` or `KEY = value` shape where KEY is a known
/// hash-context label, return just the value. `None` for anything else.
///
/// Recognized labels (case-insensitive): `nt`, `ntlm`, `lm`, `hash`, `password`,
/// `pass`, `pwd`, `md5`, `sha1`, `sha256`, `sha512`, `bcrypt`, `crypt`, `nthash`.
pub fn strip_label(s: &str) -> Option<&str> {
    let (k, v) = s.split_once([':', '='])?;
    let k = k
        .trim()
        .trim_matches('"')
        .trim_matches('\'')
        .to_ascii_lowercase();
    if !matches!(
        k.as_str(),
        "nt" | "ntlm"
            | "lm"
            | "hash"
            | "password"
            | "pass"
            | "pwd"
            | "md5"
            | "sha1"
            | "sha256"
            | "sha512"
            | "bcrypt"
            | "crypt"
            | "nthash"
    ) {
        return None;
    }
    let v = v
        .trim()
        .trim_matches(',')
        .trim_matches('"')
        .trim_matches('\'');
    if v.is_empty() {
        return None;
    }
    Some(v)
}

/// Extract candidate hash tokens embedded in a messy line: hex substrings of
/// hash-plausible length (16/32/40/48/56/64/96/128) and `$…$…` structured
/// hashes. Returns raw substrings for the caller to feed through `identify`.
///
/// Bounded discipline: only shapes that would identify on their own qualify.
/// A CSV field with a random word doesn't survive.
pub fn extract_candidates(s: &str) -> Vec<String> {
    let mut out = Vec::new();

    // 1. `$xxx$...` structured hash embedded anywhere in the line. Terminate
    //    on whitespace, quote, comma, or closing bracket.
    let bytes = s.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'$' {
            let start = i;
            i += 1;
            while i < bytes.len()
                && !matches!(
                    bytes[i],
                    b' ' | b'\t' | b'"' | b'\'' | b',' | b'}' | b']' | b'\r' | b'\n'
                )
            {
                i += 1;
            }
            if i - start >= 6 {
                out.push(s[start..i].to_string());
            }
        } else {
            i += 1;
        }
    }

    // 2. Hex substrings of hash-plausible lengths. Walk the string once, track
    //    hex runs, emit any run whose length matches a known hash bucket.
    let plausible = |n: usize| matches!(n, 16 | 32 | 40 | 48 | 56 | 64 | 96 | 128);
    let mut run_start: Option<usize> = None;
    for (idx, b) in bytes.iter().enumerate() {
        let is_hex = b.is_ascii_hexdigit();
        match (is_hex, run_start) {
            (true, None) => run_start = Some(idx),
            (false, Some(start)) => {
                let n = idx - start;
                if plausible(n) {
                    out.push(s[start..idx].to_string());
                }
                run_start = None;
            }
            _ => {}
        }
    }
    if let Some(start) = run_start {
        let n = bytes.len() - start;
        if plausible(n) {
            out.push(s[start..bytes.len()].to_string());
        }
    }

    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn clean_strips_bom_crlf_and_wrapping_quotes() {
        assert_eq!(clean("\u{feff}hello\r\n"), "hello");
        assert_eq!(clean("  \tindented \r"), "indented");
        assert_eq!(clean("\"quoted\""), "quoted");
        assert_eq!(clean("'single-quoted'"), "single-quoted");
        assert_eq!(clean("trailing,"), "trailing");
        // Doesn't strip a lone terminal quote (unbalanced), only matched pairs.
        assert_eq!(clean("\"unbalanced"), "\"unbalanced");
    }

    #[test]
    fn strip_label_handles_common_shapes() {
        assert_eq!(
            strip_label("NT: 8846f7eaee8fb117ad06bdd830b7586c"),
            Some("8846f7eaee8fb117ad06bdd830b7586c")
        );
        assert_eq!(
            strip_label("password = $2a$05$abcdef"),
            Some("$2a$05$abcdef")
        );
        assert_eq!(
            strip_label("\"nthash\": \"8846f7eaee8fb117ad06bdd830b7586c\","),
            Some("8846f7eaee8fb117ad06bdd830b7586c")
        );
        // Unknown key → don't strip
        assert!(strip_label("filename: config.yaml").is_none());
        // No delimiter → not a label line
        assert!(strip_label("just-a-hash").is_none());
    }

    #[test]
    fn extract_pulls_structured_hashes_out_of_wrappers() {
        let line = r#"{"user": "bob", "creds": "$2a$05$LhayLxezLhK1LhWvKxCyLOj0j1u.Kj0jZ0pEmm134uzrQlFvQJLF6"}"#;
        let cs = extract_candidates(line);
        assert!(
            cs.iter().any(|c| c.starts_with("$2a$05$")),
            "structured hash not extracted"
        );
    }

    #[test]
    fn extract_pulls_hex_of_plausible_lengths() {
        let line = "user,bob,hash,8846f7eaee8fb117ad06bdd830b7586c,extra";
        let cs = extract_candidates(line);
        assert!(cs.iter().any(|c| c == "8846f7eaee8fb117ad06bdd830b7586c"));
    }

    #[test]
    fn extract_does_not_return_short_or_wrong_length_hex() {
        // 8 chars, 15 chars, 33 chars — none are hash buckets, must be silent.
        let cs = extract_candidates("deadbeef abc123ef0987654 ffffffffffffffffffffffffffffffffff");
        assert!(cs.is_empty(), "leaked: {cs:?}");
    }

    #[test]
    fn extract_from_hashcat_show_output() {
        // `hashcat -m 0 --show` output shape: `hash:password`. We should
        // extract the hash but not treat the password as one.
        let cs = extract_candidates("5f4dcc3b5aa765d61d8327deb882cf99:password123");
        assert_eq!(cs.len(), 1);
        assert_eq!(cs[0], "5f4dcc3b5aa765d61d8327deb882cf99");
    }
}