use crate::rules::{COMPILED, CompiledRule, SALTED_COMPILED};
#[derive(Debug, Clone, PartialEq)]
pub struct Candidate {
pub mode: u32,
pub name: &'static str,
pub confidence: f32,
}
#[derive(Clone, Copy, Default)]
pub struct Opts {
pub all: bool,
pub strict: bool,
pub salted: bool,
}
fn push_matches(compiled: &[CompiledRule], s: &str, out: &mut Vec<Candidate>) {
for c in compiled {
if c.regex.is_match(s) {
for &mode in c.rule.modes {
out.push(Candidate {
mode,
name: c.rule.name,
confidence: c.rule.base_confidence,
});
}
}
}
}
fn fast_prefix(s: &str) -> Option<Vec<Candidate>> {
if let Some(rest) = s.strip_prefix("$2")
&& rest.len() >= 58
&& matches!(rest.as_bytes()[0], b'a' | b'b' | b'y' | b'x')
&& rest.as_bytes()[1] == b'$'
{
return Some(vec![Candidate {
mode: 3200,
name: "bcrypt (Blowfish, Unix) $2*$",
confidence: 0.99,
}]);
}
if let Some(rest) = s.strip_prefix("$1$")
&& rest.contains('$')
&& rest.len() >= 24
{
return Some(vec![Candidate {
mode: 500,
name: "md5crypt / MD5(Unix) / Cisco-IOS $1$",
confidence: 0.99,
}]);
}
if let Some(rest) = s.strip_prefix("$6$")
&& rest.contains('$')
&& rest.len() >= 88
{
return Some(vec![Candidate {
mode: 1800,
name: "sha512crypt / SHA512(Unix) $6$",
confidence: 0.99,
}]);
}
if let Some(rest) = s.strip_prefix("$5$")
&& rest.contains('$')
&& rest.len() >= 45
{
return Some(vec![Candidate {
mode: 7400,
name: "sha256crypt / SHA256(Unix) $5$",
confidence: 0.99,
}]);
}
if (s.starts_with("$P$") || s.starts_with("$H$")) && s.len() == 34 {
return Some(vec![Candidate {
mode: 400,
name: "phpass (WordPress / phpBB3 / Joomla)",
confidence: 0.99,
}]);
}
None
}
pub fn identify(input: &str, all: bool) -> Vec<Candidate> {
identify_with(
input,
Opts {
all,
..Opts::default()
},
)
}
pub fn identify_with(input: &str, opts: Opts) -> Vec<Candidate> {
let s = input.trim();
if !opts.salted
&& let Some(mut cs) = fast_prefix(s)
{
cs.sort_by(|a, b| {
b.confidence
.partial_cmp(&a.confidence)
.unwrap_or(std::cmp::Ordering::Equal)
.then(a.mode.cmp(&b.mode))
});
return cs;
}
let mut out: Vec<Candidate> = Vec::new();
push_matches(&COMPILED, s, &mut out);
if opts.salted {
push_matches(&SALTED_COMPILED, s, &mut out);
}
if !opts.strict {
out.extend(crate::hex::classify(s, opts.all));
}
out.sort_by(|a, b| {
b.confidence
.partial_cmp(&a.confidence)
.unwrap_or(std::cmp::Ordering::Equal)
.then(a.mode.cmp(&b.mode))
});
out
}
#[cfg(test)]
mod tests {
use super::*;
fn top_mode(hash: &str) -> u32 {
let c = identify(hash, false);
assert!(!c.is_empty(), "no candidate for: {hash}");
c[0].mode
}
#[test]
fn crypt_family() {
assert_eq!(top_mode("$1$28772684$iEwNOgGugqO9.bIz5sk8k/"), 500);
assert_eq!(top_mode("$apr1$71850310$gh9m4xcAn3MGxogwX/ztb."), 1600);
assert_eq!(
top_mode("$5$rounds=5000$GX7BopJZJxPc/KEK$le16UF8I2Anb.rOrn22AUPWvzUETDGefUmAV8AZkGcD"),
7400
);
assert_eq!(
top_mode(
"$6$52450745$k5ka2p8bFuSmoVT1tzOyyuaREkkKBcCNqoDKzYiJL9RaE8yMnPgh2XzzF0NDrUhgrcLwg78xs1w5pJiypEdFX/"
),
1800
);
assert_eq!(
top_mode("$2a$05$LhayLxezLhK1LhWvKxCyLOj0j1u.Kj0jZ0pEmm134uzrQlFvQJLF6"),
3200
);
assert_eq!(
top_mode("$sha1$40000$jtNX3nZ2$hBNaIXkt4wBI2o5rsi8KejSjNqIq"),
15100
);
}
#[test]
fn web_app_formats() {
assert_eq!(top_mode("$P$984478476IagS59wHZvyQMArzfx58u."), 400);
assert_eq!(top_mode("$H$984478476IagS59wHZvyQMArzfx58u."), 400);
assert_eq!(
top_mode("$S$C33783772bRXEnVExYFsyhWMzewYNI46xzUUKkHkJmuqBUp4tHUx"),
7900
);
assert_eq!(
top_mode(
"pbkdf2_sha256$20000$H0dPx8NeajVu$GiC4k5kqbbR9qWBlsRgDywNqC2vd9kqfk7zdorEnNas="
),
10000
);
assert_eq!(top_mode("{SHA}W6ph5Mm5Pz8GgiULbPgzG37mj9g="), 101);
assert_eq!(top_mode("{SSHA}0MF7YJUwXd4hCyMOFmyM/HcZesM2FQ4l"), 111);
}
#[test]
fn cisco() {
assert_eq!(
top_mode("$8$TnGX/fE4KGHOVU$pEhnEvxrEcqYKXGH3aFkYaC3B2MtqhkAKSl4WGWmc9E"),
9200
);
assert_eq!(
top_mode("$9$2MJBozw/9R3UsU$2lFhcKvpghcyw8deP25GOfyZaagyUOGBymkryvOdfo6"),
9300
);
}
#[test]
fn kerberos() {
assert_eq!(
top_mode(
"$krb5tgs$23$*user$realm$test/spn*$63386d22d359fe42230300d56852c9eb$891ad31d09ab89c6b3b8c5e5de6c06a7f49fd559d7a9a3c32576c8fdf6"
),
13100
);
assert_eq!(
top_mode("$krb5tgs$17$user$realm$ae8434177efd09be5bc2eff8$90b4ce5b"),
19600
);
assert_eq!(
top_mode("$krb5tgs$18$user$realm$8efd91bb01cc69dd07e46009$1090aa"),
19700
);
assert_eq!(
top_mode("$krb5asrep$23$user@domain.com:3e156ada591263b8aab0965f5aebd837$334ac"),
18200
);
assert_eq!(top_mode("$krb5pa$23$user$realm$salt$6f9ef1af5"), 7500);
}
#[test]
fn office_keepass_dpapi() {
assert_eq!(
top_mode("$office$*2007*20*128*16*411a51284e0d0200b131a8949aa8e07b*f1"),
9400
);
assert_eq!(
top_mode("$office$*2010*100000*128*16*77233201017277d426048818b9819cb8*b4"),
9500
);
assert_eq!(
top_mode("$office$*2013*100000*256*16*7dd611d7eb4c899f74816d1dec817b3b*948d"),
9600
);
assert_eq!(
top_mode("$keepass$*2*6000*0*4a3f8b6c2d*1c*7b1e*ac*ff"),
13400
);
assert_eq!(
top_mode("$DPAPImk$1*1*S-15-21-11*des3*sha1*100*abc*208*def"),
15300
);
assert_eq!(
top_mode("$DPAPImk$2*1*S-15-21-11*aes256*sha512*8000*abc*288*def"),
15900
);
}
#[test]
fn archives() {
assert_eq!(
top_mode("$7z$0$19$0$salt$8$1122334455667788$123$60$deadbeef"),
11600
);
assert_eq!(
top_mode("$RAR3$*0*45109af8ab5f297a*adc9c4300c1c5be0"),
12500
);
assert_eq!(
top_mode("$rar5$16$74575567518807622265582327032280$15$d33c8"),
13000
);
assert_eq!(
top_mode("$zip2$*0*3*0*ab5f297a*1122*0*0*deadbeef*$/zip2$"),
13600
);
let pdf = identify(
"$pdf$2*3*128*-1028*1*16*da42ee15d4b3e08fe5b9ecea0e02ad0f*32* deadbeef",
false,
);
assert!(pdf.iter().any(|c| c.mode == 10500));
}
#[test]
fn jwt() {
assert_eq!(
top_mode(
"eyJhbGciOiJIUzI1NiJ9.eyJpbmZvIjoibXkgc2VjcmV0In0.chURrO4W3up84rQFIQmOCFCUXKtoOJvY6qJ0LzjaQao"
),
16500
);
}
#[test]
fn netntlm() {
assert_eq!(
top_mode(
"u4-netntlm::kNS:338d08f8e26de93300000000000000000000000000000000:9526fb8c23a90751cdd619b6cea564742e1e4bf33006ba41:cb8086049ec4736c"
),
5500
);
assert_eq!(
top_mode(
"admin::N46iSNekpT:08ca45b7d7ea58ee:88dcbe4446168966a153a0064958dac6:5c7830315c7830310000000000000b45c67103d07d7b95acd12ffa11230e0000000052920b85f78d013c31cdb3b92f5d765c783030"
),
5600
);
}
#[test]
fn ad_cached_creds_and_wpa() {
assert_eq!(
top_mode("$DCC2$10240#tom#e4e938d12fe5974dc42a90120bd9c90f"),
2100
);
assert_eq!(
top_mode(
"WPA*01*4d4fe7aac3a2cecab195321ceb99a7d0*fc690c158264*f4747f87f9f4*686173686361742d6573736964***"
),
22000
);
}
#[test]
fn salted_family_is_opt_in_only() {
let salt = Opts {
salted: true,
..Opts::default()
};
assert!(identify("01dfae6e5d4d90d9892622325959afbe:7050461", false).is_empty());
assert!(identify("d41d8cd98f00b204e9800998ecf8427e:avatar.png", false).is_empty());
let c = identify_with("01dfae6e5d4d90d9892622325959afbe:7050461", salt);
assert_eq!(c[0].mode, 10);
assert!(c.iter().any(|x| x.mode == 20));
assert!(c.iter().any(|x| x.mode == 1100));
assert!(c[0].confidence < 0.85);
assert_eq!(
identify_with("2fc5a684737ce1bf7b3b239df432416e0dd07357:2014", salt)[0].mode,
110
);
assert_eq!(
identify_with(
"c73d08de890479518ed60cf670d17faa26a4a71f995c1dcc978165399401a6c4:1234",
salt
)[0]
.mode,
1410
);
}
#[test]
fn strict_suppresses_the_bare_hex_tier() {
let strict = Opts {
strict: true,
..Opts::default()
};
assert_eq!(
identify("a1b2c3d4e5f6789012345678901234567890abcd", false)[0].mode,
100
);
assert!(identify_with("a1b2c3d4e5f6789012345678901234567890abcd", strict).is_empty());
assert_eq!(
identify_with("$1$28772684$iEwNOgGugqO9.bIz5sk8k/", strict)[0].mode,
500
);
}
#[test]
fn bare_hex_is_ranked_never_certain() {
let c = identify("8743b52063cd84097a65d1633f5c74f5", false);
assert_eq!(c[0].mode, 0);
assert!(c.iter().any(|x| x.mode == 1000));
assert!(c.iter().any(|x| x.mode == 900));
assert!(c[0].confidence < 0.6, "bare hex must not read as certain");
}
#[test]
fn hex_lengths_pick_the_prevalent_mode() {
assert_eq!(top_mode("aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"), 100);
let s256 = identify(
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
false,
);
assert_eq!(s256[0].mode, 1400);
assert!(s256.iter().any(|c| c.mode == 17400));
assert_eq!(
top_mode(
"82a9dda829eb7f8ffe9fbe49e45d47d2dad9664fbb7adf72492e3c81ebd3e29134d9bc12212bf83c6840f10e8246b9db54a4859b7ccd0123d86e5872c1e5082f"
),
1700
);
}
#[test]
fn all_flag_reveals_rare_modes() {
let hash = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
assert!(identify(hash, true).len() > identify(hash, false).len());
assert!(identify(hash, true).iter().any(|c| c.mode == 6900)); }
#[test]
fn structured_match_excludes_the_hex_tier() {
let c = identify("$1$28772684$iEwNOgGugqO9.bIz5sk8k/", false);
assert_eq!(c.len(), 1);
assert_eq!(c[0].mode, 500);
}
#[test]
fn whitespace_is_trimmed() {
assert_eq!(top_mode(" $1$28772684$iEwNOgGugqO9.bIz5sk8k/\n"), 500);
}
}