use crate::hex::is_hex;
use crate::identify::{Candidate, Opts, identify_with};
use crate::output::AMBIGUOUS_BELOW;
use std::collections::{BTreeMap, HashMap};
use std::fs::{self, File};
use std::io::{BufRead, BufWriter, Write};
use std::path::Path;
pub struct HashHit {
pub token: String,
pub label: Option<String>,
pub candidates: Vec<Candidate>,
}
pub struct Cfg {
pub all: bool,
pub json: bool,
pub summary_only: bool,
pub strict: bool,
pub salted: bool,
pub pot: Option<crate::potfile::Pot>,
}
impl Cfg {
fn opts(&self) -> Opts {
Opts {
all: self.all,
strict: self.strict,
salted: self.salted,
}
}
}
#[derive(Default)]
pub struct Summary {
pub lines: usize,
pub hits: usize,
pub unrecognized: usize,
pub skipped_pot: usize,
pub by_mode: BTreeMap<u32, (&'static str, usize)>,
}
const EMPTY_LM: &str = "aad3b435b51404eeaad3b435b51404ee";
pub fn analyze_line(line: &str, opts: Opts) -> Vec<HashHit> {
let t = crate::extract::clean(line);
if t.is_empty()
|| t.starts_with('#')
|| (t.starts_with('[') && t.len() >= 3 && matches!(t.as_bytes()[1], b'*' | b'+' | b'-') && t.as_bytes()[2] == b']')
{
return Vec::new();
}
if let Some(hits) = parse_pwdump(t) {
return hits;
}
let direct = identify_with(t, opts);
if !direct.is_empty() {
return vec![HashHit {
token: t.to_string(),
label: None,
candidates: direct,
}];
}
if let Some(v) = crate::extract::strip_label(t) {
let cs = identify_with(v, opts);
if !cs.is_empty() {
return vec![HashHit {
token: v.to_string(),
label: None,
candidates: cs,
}];
}
}
let mut hits = Vec::new();
for cand in crate::extract::extract_candidates(t) {
let cs = identify_with(&cand, opts);
if !cs.is_empty() {
hits.push(HashHit {
token: cand,
label: None,
candidates: cs,
});
}
}
if !hits.is_empty() {
return hits;
}
vec![HashHit {
token: t.to_string(),
label: None,
candidates: Vec::new(),
}]
}
fn parse_pwdump(line: &str) -> Option<Vec<HashHit>> {
if let Some(hits) = parse_kerberos_key(line) {
return Some(hits);
}
let f: Vec<&str> = line.split(':').collect();
if f.len() < 4 {
return None;
}
let (user_raw, rid, lm, nt) = (f[0], f[1], f[2], f[3]);
if rid.is_empty() || !rid.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
if lm.len() != 32 || !is_hex(lm) || nt.len() != 32 || !is_hex(nt) {
return None;
}
let (user, tag) = if let Some(stem) = user_raw.strip_suffix('$') {
(stem, Some("$MACHINE.ACC"))
} else if let Some((stem, hist)) = split_history_suffix(user_raw) {
(stem, Some(hist))
} else {
(user_raw, None)
};
let nt_label = match tag {
Some(t) => format!("NT: {user} [{t}]"),
None => format!("NT: {user}"),
};
let mut hits = vec![HashHit {
token: nt.to_string(),
label: Some(nt_label),
candidates: vec![Candidate {
mode: 1000,
name: "NTLM",
confidence: 0.99,
}],
}];
if !lm.eq_ignore_ascii_case(EMPTY_LM) {
let lm_label = match tag {
Some(t) => format!("LM: {user} [{t}]"),
None => format!("LM: {user}"),
};
hits.push(HashHit {
token: lm.to_string(),
label: Some(lm_label),
candidates: vec![Candidate {
mode: 3000,
name: "LM",
confidence: 0.99,
}],
});
}
Some(hits)
}
fn split_history_suffix(user: &str) -> Option<(&str, &'static str)> {
let stem_end = user.rfind("_history")?;
let tail = &user[stem_end + "_history".len()..];
if tail.len() == 1
&& tail
.chars()
.next()
.map(|c| c.is_ascii_digit())
.unwrap_or(false)
{
let label: &'static str = match tail {
"0" => "history 0",
"1" => "history 1",
"2" => "history 2",
"3" => "history 3",
_ => "history",
};
Some((&user[..stem_end], label))
} else {
None
}
}
fn parse_kerberos_key(line: &str) -> Option<Vec<HashHit>> {
let (user, rest) = line.split_once(':')?;
let (etype, hex) = rest.split_once(':')?;
if user.is_empty() || !is_hex(hex) {
return None;
}
let (mode, name, note): (u32, &'static str, &'static str) = match etype {
"aes256-cts-hmac-sha1-96" if hex.len() == 64 => {
(19700, "Kerberos 5 TGS-REP aes256 (key)", "aes256 key")
}
"aes128-cts-hmac-sha1-96" if hex.len() == 32 => {
(19600, "Kerberos 5 TGS-REP aes128 (key)", "aes128 key")
}
"des-cbc-md5" if hex.len() == 16 => return Some(Vec::new()),
_ => return None,
};
Some(vec![HashHit {
token: hex.to_string(),
label: Some(format!("Krb {note}: {user}")),
candidates: vec![Candidate {
mode,
name,
confidence: 0.99,
}],
}])
}
pub fn filter_only(
reader: impl BufRead,
mode: u32,
opts: Opts,
out: &mut impl Write,
) -> std::io::Result<usize> {
let mut n = 0usize;
for line in reader.lines() {
let line = line?;
for hit in analyze_line(&line, opts) {
if hit.candidates.first().map(|c| c.mode) == Some(mode) {
writeln!(out, "{}", hit.token)?;
n += 1;
}
}
}
Ok(n)
}
pub struct SplitReport {
pub dir: String,
pub by_mode: BTreeMap<u32, (&'static str, usize)>,
pub unknown: usize,
pub skipped_pot: usize,
}
fn create_owner_only(path: &Path) -> std::io::Result<File> {
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o600)
.open(path)
}
#[cfg(not(unix))]
{
File::create(path)
}
}
pub fn split(
reader: impl BufRead,
dir: &Path,
opts: Opts,
pot: Option<&crate::potfile::Pot>,
) -> std::io::Result<SplitReport> {
fs::create_dir_all(dir)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700));
}
let mut writers: HashMap<u32, BufWriter<File>> = HashMap::new();
let mut by_mode: BTreeMap<u32, (&'static str, usize)> = BTreeMap::new();
let mut unknown_writer: Option<BufWriter<File>> = None;
let mut unknown = 0usize;
let mut skipped_pot = 0usize;
for line in reader.lines() {
let line = line?;
for hit in analyze_line(&line, opts) {
if let Some(pot) = pot
&& pot.contains(&hit.token)
{
skipped_pot += 1;
continue;
}
match hit.candidates.first() {
Some(top) => {
let writer = match writers.entry(top.mode) {
std::collections::hash_map::Entry::Occupied(e) => e.into_mut(),
std::collections::hash_map::Entry::Vacant(e) => {
let f = create_owner_only(&dir.join(format!("{}.txt", top.mode)))?;
e.insert(BufWriter::new(f))
}
};
writeln!(writer, "{}", hit.token)?;
by_mode.entry(top.mode).or_insert((top.name, 0)).1 += 1;
}
None => {
if unknown_writer.is_none() {
unknown_writer =
Some(BufWriter::new(create_owner_only(&dir.join("unknown.txt"))?));
}
writeln!(unknown_writer.as_mut().unwrap(), "{}", hit.token)?;
unknown += 1;
}
}
}
}
for (_, mut w) in writers {
w.flush()?;
}
if let Some(mut w) = unknown_writer {
w.flush()?;
}
Ok(SplitReport {
dir: dir.to_string_lossy().into_owned(),
by_mode,
unknown,
skipped_pot,
})
}
pub fn render_split(rep: &SplitReport, out: &mut impl Write) -> std::io::Result<()> {
writeln!(out, "wrote {} mode(s) to {}/", rep.by_mode.len(), rep.dir)?;
let wl = crate::wordlist::substitute();
let mut rows: Vec<(&u32, &(&'static str, usize))> = rep.by_mode.iter().collect();
rows.sort_by(|a, b| b.1.1.cmp(&a.1.1).then(a.0.cmp(b.0)));
for (mode, (name, count)) in rows {
writeln!(
out,
" -m {:<6} {:<20} {:>7} -> hashcat -m {} {}/{}.txt {}",
mode,
preview(name, 20),
count,
mode,
rep.dir,
mode,
wl
)?;
let one = crate::cost::oneliner(*mode);
if !one.is_empty() {
writeln!(out, " {one}")?;
}
}
if rep.unknown > 0 {
writeln!(
out,
" {:<9} {:<20} {:>7} -> {}/unknown.txt (try: hashcat --identify)",
"unknown", "", rep.unknown, rep.dir
)?;
}
if rep.skipped_pot > 0 {
writeln!(
out,
" (skipped {} hash(es) already in hashcat potfile)",
rep.skipped_pot
)?;
}
let h = crate::wordlist::hint();
if !h.is_empty() {
writeln!(out, "{h}")?;
}
Ok(())
}
pub fn run(reader: impl BufRead, cfg: &Cfg, out: &mut impl Write) -> std::io::Result<Summary> {
let mut sum = Summary::default();
let mut line_no = 0usize;
let opts = cfg.opts();
for line in reader.lines() {
let line = line?;
line_no += 1;
let hits = analyze_line(&line, opts);
if hits.is_empty() {
continue; }
sum.lines += 1;
for hit in &hits {
if let Some(pot) = &cfg.pot
&& pot.contains(&hit.token)
{
sum.skipped_pot += 1;
continue;
}
if hit.candidates.is_empty() {
sum.unrecognized += 1;
} else {
sum.hits += 1;
let top = &hit.candidates[0];
sum.by_mode.entry(top.mode).or_insert((top.name, 0)).1 += 1;
}
if cfg.summary_only {
continue;
}
if cfg.json {
write_hit_json(out, line_no, hit)?;
} else {
write_hit_text(out, line_no, hit)?;
}
}
}
write_summary(out, cfg, &sum)?;
Ok(sum)
}
fn write_hit_text(out: &mut impl Write, line_no: usize, hit: &HashHit) -> std::io::Result<()> {
if hit.candidates.is_empty() {
return writeln!(
out,
" line {:<5} ?? unrecognized {}",
line_no,
preview(&hit.token, 40)
);
}
let top = &hit.candidates[0];
let more = hit.candidates.len() - 1;
let more_s = if more > 0 {
format!(" +{more}")
} else {
String::new()
};
let ambig = if top.confidence < AMBIGUOUS_BELOW {
" ?"
} else {
""
};
let typ = preview(&format!("{}{}{}", top.name, more_s, ambig), 22);
let tail = match &hit.label {
Some(l) => format!("[{l}]"),
None => preview(&hit.token, 40),
};
writeln!(
out,
" line {:<5} -m {:<6} {:<22} {}",
line_no, top.mode, typ, tail
)
}
fn write_hit_json(out: &mut impl Write, line_no: usize, hit: &HashHit) -> std::io::Result<()> {
let candidates: Vec<serde_json::Value> = hit
.candidates
.iter()
.map(|c| {
serde_json::json!({
"mode": c.mode,
"name": c.name,
"hashcat": format!("-m {}", c.mode),
"confidence": ((c.confidence as f64) * 100.0).round() / 100.0,
})
})
.collect();
let obj = serde_json::json!({
"line": line_no,
"token": hit.token,
"label": hit.label,
"candidates": candidates,
});
writeln!(out, "{}", serde_json::to_string(&obj).unwrap_or_default())
}
fn write_summary(out: &mut impl Write, cfg: &Cfg, sum: &Summary) -> std::io::Result<()> {
if cfg.json {
let by: serde_json::Map<String, serde_json::Value> = sum
.by_mode
.iter()
.map(|(mode, (_, count))| (mode.to_string(), serde_json::json!(count)))
.collect();
let obj = serde_json::json!({
"summary": {
"lines": sum.lines,
"hits": sum.hits,
"unrecognized": sum.unrecognized,
"skipped_pot": sum.skipped_pot,
"by_mode": by,
}
});
return writeln!(out, "{}", serde_json::to_string(&obj).unwrap_or_default());
}
writeln!(out)?;
writeln!(
out,
"summary: {} line(s), {} hit(s) identified, {} unrecognized",
sum.lines, sum.hits, sum.unrecognized
)?;
if sum.skipped_pot > 0 {
writeln!(
out,
" (skipped {} hash(es) already in hashcat potfile)",
sum.skipped_pot
)?;
}
let mut rows: Vec<(&u32, &(&'static str, usize))> = sum.by_mode.iter().collect();
rows.sort_by(|a, b| b.1.1.cmp(&a.1.1).then(a.0.cmp(b.0)));
for (mode, (name, count)) in rows {
writeln!(out, " -m {:<6} {:<22} {}", mode, preview(name, 22), count)?;
}
Ok(())
}
fn preview(s: &str, max: usize) -> String {
if s.chars().count() <= max {
s.to_string()
} else {
let head: String = s.chars().take(max.saturating_sub(1)).collect();
format!("{head}…")
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn blank_and_comment_lines_are_skipped() {
assert!(analyze_line("", Opts::default()).is_empty());
assert!(analyze_line(" ", Opts::default()).is_empty());
assert!(analyze_line("# a comment", Opts::default()).is_empty());
}
#[test]
fn secretsdump_line_pins_nt_and_suppresses_empty_lm() {
let hits = analyze_line(
"Administrator:500:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::",
Opts::default(),
);
assert_eq!(hits.len(), 1, "empty LM must be suppressed");
assert_eq!(hits[0].candidates[0].mode, 1000);
assert!(
hits[0].candidates[0].confidence > 0.9,
"context pins NT to NTLM"
);
assert_eq!(hits[0].label.as_deref(), Some("NT: Administrator"));
}
#[test]
fn secretsdump_line_emits_lm_when_present() {
let hits = analyze_line(
"bob:1001:e52cac67419a9a224a3b108f3fa6cb6d:8846f7eaee8fb117ad06bdd830b7586c:::",
Opts::default(),
);
assert_eq!(hits.len(), 2);
assert_eq!(hits[0].candidates[0].mode, 1000); assert_eq!(hits[1].candidates[0].mode, 3000); }
#[test]
fn machine_account_line_tags_the_label() {
let hits = analyze_line(
"SERVER$:1001:aad3b435b51404eeaad3b435b51404ee:8846f7eaee8fb117ad06bdd830b7586c:::",
Opts::default(),
);
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].candidates[0].mode, 1000);
let label = hits[0].label.as_deref().unwrap_or("");
assert!(label.contains("SERVER"), "want stem, got {label:?}");
assert!(label.contains("MACHINE"), "want machine tag, got {label:?}");
}
#[test]
fn history_suffix_is_labelled() {
let hits = analyze_line(
"bob_history0:1101:aad3b435b51404eeaad3b435b51404ee:8846f7eaee8fb117ad06bdd830b7586c:::",
Opts::default(),
);
assert_eq!(hits[0].candidates[0].mode, 1000);
let label = hits[0].label.as_deref().unwrap_or("");
assert!(label.contains("bob"));
assert!(label.contains("history 0"));
}
#[test]
fn kerberos_aes256_key_line_pins_to_19700() {
let hits = analyze_line(
"Administrator:aes256-cts-hmac-sha1-96:5c9d20d8c1c62c5476a1cee2ea8c8bd75d15edcf1d5c5a45b2b74d17b34ac47a",
Opts::default(),
);
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].candidates[0].mode, 19700);
let label = hits[0].label.as_deref().unwrap_or("");
assert!(label.contains("aes256"));
assert!(label.contains("Administrator"));
}
#[test]
fn kerberos_aes128_key_line_pins_to_19600() {
let hits = analyze_line(
"svc_web:aes128-cts-hmac-sha1-96:a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6",
Opts::default(),
);
assert_eq!(hits[0].candidates[0].mode, 19600);
}
#[test]
fn netntlmv2_line_is_not_mistaken_for_pwdump() {
let hits = analyze_line(
"admin::N46iSNekpT:08ca45b7d7ea58ee:88dcbe4446168966a153a0064958dac6:5c7830315c7830310000000000000b45c67103d07d7b95acd12ffa11230e0000000052920b85f78d013c31cdb3b92f5d765c783030",
Opts::default(),
);
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].candidates[0].mode, 5600);
}
#[test]
fn bare_hex_line_stays_ambiguous() {
let hits = analyze_line("8743b52063cd84097a65d1633f5c74f5", Opts::default());
assert_eq!(hits[0].candidates[0].mode, 0);
assert!(hits[0].candidates[0].confidence < 0.6);
}
#[test]
fn run_tallies_summary() {
let dump = "\
# dump
Administrator:500:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::
$1$28772684$iEwNOgGugqO9.bIz5sk8k/
garbage-not-a-hash-token
";
let cfg = Cfg {
all: false,
json: false,
summary_only: true,
strict: false,
salted: false,
pot: None,
};
let mut out = Vec::new();
let sum = run(Cursor::new(dump), &cfg, &mut out).unwrap();
assert_eq!(sum.lines, 3); assert_eq!(sum.hits, 2); assert_eq!(sum.unrecognized, 1); assert_eq!(sum.by_mode.get(&1000).map(|v| v.1), Some(1));
assert_eq!(sum.by_mode.get(&500).map(|v| v.1), Some(1));
}
#[test]
fn filter_only_emits_just_matching_tokens() {
let dump = "\
Administrator:500:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::
$1$28772684$iEwNOgGugqO9.bIz5sk8k/
bob:1001:e52cac67419a9a224a3b108f3fa6cb6d:8846f7eaee8fb117ad06bdd830b7586c:::
";
let mut out = Vec::new();
let n = filter_only(Cursor::new(dump), 1000, Opts::default(), &mut out).unwrap();
let text = String::from_utf8(out).unwrap();
assert_eq!(n, 2, "two NT hashes");
assert_eq!(
text,
"31d6cfe0d16ae931b73c59d7e0c089c0\n8846f7eaee8fb117ad06bdd830b7586c\n"
);
}
#[test]
fn split_writes_one_file_per_mode() {
let dir = std::env::temp_dir().join("hashglass_test_split_slice2");
let _ = fs::remove_dir_all(&dir);
let dump = "\
Administrator:500:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::
svc:1104:aad3b435b51404eeaad3b435b51404ee:e19ccf75ee54e06b06a5907af13cef42:::
$2a$05$LhayLxezLhK1LhWvKxCyLOj0j1u.Kj0jZ0pEmm134uzrQlFvQJLF6
notahash
";
let rep = split(Cursor::new(dump), &dir, Opts::default(), None).unwrap();
assert_eq!(rep.by_mode.get(&1000).map(|v| v.1), Some(2)); assert_eq!(rep.by_mode.get(&3200).map(|v| v.1), Some(1)); assert_eq!(rep.unknown, 1);
let nt = fs::read_to_string(dir.join("1000.txt")).unwrap();
assert_eq!(nt.lines().count(), 2);
assert!(dir.join("unknown.txt").exists());
let mut buf = Vec::new();
render_split(&rep, &mut buf).unwrap();
assert!(String::from_utf8(buf).unwrap().contains("hashcat -m 1000"));
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn run_ndjson_is_line_delimited_valid_json() {
let cfg = Cfg {
all: false,
json: true,
summary_only: false,
strict: false,
salted: false,
pot: None,
};
let mut out = Vec::new();
run(
Cursor::new("$1$28772684$iEwNOgGugqO9.bIz5sk8k/\n"),
&cfg,
&mut out,
)
.unwrap();
let text = String::from_utf8(out).unwrap();
for line in text.lines() {
serde_json::from_str::<serde_json::Value>(line).expect("each NDJSON line valid JSON");
}
}
}