use std::net::SocketAddr;
use std::time::Duration;
use sha2::{Digest, Sha256};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::time::timeout;
use super::analyzer::{Analyzer, PortContext};
use super::model::{Evidence, SourceId};
use super::response::{Collected, ResponseSet};
use crate::config::ServiceDetection;
use crate::model::port::Protocol;
use crate::protocols::tls;
const PROBE_TIMEOUT: Duration = Duration::from_secs(4);
const MAX_REPLY_BYTES: usize = 8192;
const EMPTY_HASH: &str = "00000000000000000000000000000000000000000000000000000000000000";
const SUITES_ALL: &[[u8; 2]] = &[
[0x00, 0x16],
[0x00, 0x33],
[0x00, 0x67],
[0xc0, 0x9e],
[0xc0, 0xa2],
[0x00, 0x9e],
[0x00, 0x39],
[0x00, 0x6b],
[0xc0, 0x9f],
[0xc0, 0xa3],
[0x00, 0x9f],
[0x00, 0x45],
[0x00, 0xbe],
[0x00, 0x88],
[0x00, 0xc4],
[0x00, 0x9a],
[0xc0, 0x08],
[0xc0, 0x09],
[0xc0, 0x23],
[0xc0, 0xac],
[0xc0, 0xae],
[0xc0, 0x2b],
[0xc0, 0x0a],
[0xc0, 0x24],
[0xc0, 0xad],
[0xc0, 0xaf],
[0xc0, 0x2c],
[0xc0, 0x72],
[0xc0, 0x73],
[0xcc, 0xa9],
[0x13, 0x02],
[0x13, 0x01],
[0xcc, 0x14],
[0xc0, 0x07],
[0xc0, 0x12],
[0xc0, 0x13],
[0xc0, 0x27],
[0xc0, 0x2f],
[0xc0, 0x14],
[0xc0, 0x28],
[0xc0, 0x30],
[0xc0, 0x60],
[0xc0, 0x61],
[0xc0, 0x76],
[0xc0, 0x77],
[0xcc, 0xa8],
[0x13, 0x05],
[0x13, 0x04],
[0x13, 0x03],
[0xcc, 0x13],
[0xc0, 0x11],
[0x00, 0x0a],
[0x00, 0x2f],
[0x00, 0x3c],
[0xc0, 0x9c],
[0xc0, 0xa0],
[0x00, 0x9c],
[0x00, 0x35],
[0x00, 0x3d],
[0xc0, 0x9d],
[0xc0, 0xa1],
[0x00, 0x9d],
[0x00, 0x41],
[0x00, 0xba],
[0x00, 0x84],
[0x00, 0xc0],
[0x00, 0x07],
[0x00, 0x04],
[0x00, 0x05],
];
const SUITES_NO_TLS13: &[[u8; 2]] = &[
[0x00, 0x16],
[0x00, 0x33],
[0x00, 0x67],
[0xc0, 0x9e],
[0xc0, 0xa2],
[0x00, 0x9e],
[0x00, 0x39],
[0x00, 0x6b],
[0xc0, 0x9f],
[0xc0, 0xa3],
[0x00, 0x9f],
[0x00, 0x45],
[0x00, 0xbe],
[0x00, 0x88],
[0x00, 0xc4],
[0x00, 0x9a],
[0xc0, 0x08],
[0xc0, 0x09],
[0xc0, 0x23],
[0xc0, 0xac],
[0xc0, 0xae],
[0xc0, 0x2b],
[0xc0, 0x0a],
[0xc0, 0x24],
[0xc0, 0xad],
[0xc0, 0xaf],
[0xc0, 0x2c],
[0xc0, 0x72],
[0xc0, 0x73],
[0xcc, 0xa9],
[0xcc, 0x14],
[0xc0, 0x07],
[0xc0, 0x12],
[0xc0, 0x13],
[0xc0, 0x27],
[0xc0, 0x2f],
[0xc0, 0x14],
[0xc0, 0x28],
[0xc0, 0x30],
[0xc0, 0x60],
[0xc0, 0x61],
[0xc0, 0x76],
[0xc0, 0x77],
[0xcc, 0xa8],
[0xcc, 0x13],
[0xc0, 0x11],
[0x00, 0x0a],
[0x00, 0x2f],
[0x00, 0x3c],
[0xc0, 0x9c],
[0xc0, 0xa0],
[0x00, 0x9c],
[0x00, 0x35],
[0x00, 0x3d],
[0xc0, 0x9d],
[0xc0, 0xa1],
[0x00, 0x9d],
[0x00, 0x41],
[0x00, 0xba],
[0x00, 0x84],
[0x00, 0xc0],
[0x00, 0x07],
[0x00, 0x04],
[0x00, 0x05],
];
const CODED_SUITES: &[[u8; 2]] = &[
[0x00, 0x04],
[0x00, 0x05],
[0x00, 0x07],
[0x00, 0x0a],
[0x00, 0x16],
[0x00, 0x2f],
[0x00, 0x33],
[0x00, 0x35],
[0x00, 0x39],
[0x00, 0x3c],
[0x00, 0x3d],
[0x00, 0x41],
[0x00, 0x45],
[0x00, 0x67],
[0x00, 0x6b],
[0x00, 0x84],
[0x00, 0x88],
[0x00, 0x9a],
[0x00, 0x9c],
[0x00, 0x9d],
[0x00, 0x9e],
[0x00, 0x9f],
[0x00, 0xba],
[0x00, 0xbe],
[0x00, 0xc0],
[0x00, 0xc4],
[0xc0, 0x07],
[0xc0, 0x08],
[0xc0, 0x09],
[0xc0, 0x0a],
[0xc0, 0x11],
[0xc0, 0x12],
[0xc0, 0x13],
[0xc0, 0x14],
[0xc0, 0x23],
[0xc0, 0x24],
[0xc0, 0x27],
[0xc0, 0x28],
[0xc0, 0x2b],
[0xc0, 0x2c],
[0xc0, 0x2f],
[0xc0, 0x30],
[0xc0, 0x60],
[0xc0, 0x61],
[0xc0, 0x72],
[0xc0, 0x73],
[0xc0, 0x76],
[0xc0, 0x77],
[0xc0, 0x9c],
[0xc0, 0x9d],
[0xc0, 0x9e],
[0xc0, 0x9f],
[0xc0, 0xa0],
[0xc0, 0xa1],
[0xc0, 0xa2],
[0xc0, 0xa3],
[0xc0, 0xac],
[0xc0, 0xad],
[0xc0, 0xae],
[0xc0, 0xaf],
[0xcc, 0x13],
[0xcc, 0x14],
[0xcc, 0xa8],
[0xcc, 0xa9],
[0x13, 0x01],
[0x13, 0x02],
[0x13, 0x03],
[0x13, 0x04],
[0x13, 0x05],
];
#[derive(Debug, Clone, Copy)]
struct Probe {
version: Version,
suites: Suites,
order: Order,
grease: bool,
alpn: Alpn,
supported: Supported,
extension_order: Order,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Version {
Tls11,
Tls12,
Tls13,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Suites {
All,
NoTls13,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Order {
Forward,
Reverse,
TopHalf,
BottomHalf,
MiddleOut,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Alpn {
All,
Rare,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Supported {
None,
Tls12,
Tls13,
}
const PROBES: [Probe; 10] = [
Probe {
version: Version::Tls12,
suites: Suites::All,
order: Order::Forward,
grease: false,
alpn: Alpn::All,
supported: Supported::Tls12,
extension_order: Order::Reverse,
},
Probe {
version: Version::Tls12,
suites: Suites::All,
order: Order::Reverse,
grease: false,
alpn: Alpn::All,
supported: Supported::Tls12,
extension_order: Order::Forward,
},
Probe {
version: Version::Tls12,
suites: Suites::All,
order: Order::TopHalf,
grease: false,
alpn: Alpn::All,
supported: Supported::None,
extension_order: Order::Forward,
},
Probe {
version: Version::Tls12,
suites: Suites::All,
order: Order::BottomHalf,
grease: false,
alpn: Alpn::Rare,
supported: Supported::None,
extension_order: Order::Forward,
},
Probe {
version: Version::Tls12,
suites: Suites::All,
order: Order::MiddleOut,
grease: true,
alpn: Alpn::Rare,
supported: Supported::None,
extension_order: Order::Reverse,
},
Probe {
version: Version::Tls11,
suites: Suites::All,
order: Order::Forward,
grease: false,
alpn: Alpn::All,
supported: Supported::None,
extension_order: Order::Forward,
},
Probe {
version: Version::Tls13,
suites: Suites::All,
order: Order::Forward,
grease: false,
alpn: Alpn::All,
supported: Supported::Tls13,
extension_order: Order::Reverse,
},
Probe {
version: Version::Tls13,
suites: Suites::All,
order: Order::Reverse,
grease: false,
alpn: Alpn::All,
supported: Supported::Tls13,
extension_order: Order::Forward,
},
Probe {
version: Version::Tls13,
suites: Suites::NoTls13,
order: Order::Forward,
grease: false,
alpn: Alpn::All,
supported: Supported::Tls13,
extension_order: Order::Forward,
},
Probe {
version: Version::Tls13,
suites: Suites::All,
order: Order::MiddleOut,
grease: true,
alpn: Alpn::All,
supported: Supported::Tls13,
extension_order: Order::Reverse,
},
];
fn rearrange<T: Copy>(items: &[T], order: Order) -> Vec<T> {
let len = items.len();
match order {
Order::Forward => items.to_vec(),
Order::Reverse => items.iter().rev().copied().collect(),
Order::BottomHalf => match len % 2 {
1 => items[len / 2 + 1..].to_vec(),
_ => items[len / 2..].to_vec(),
},
Order::TopHalf => {
let mut out = Vec::with_capacity(len.div_ceil(2));
if len % 2 == 1 {
out.push(items[len / 2]);
}
out.extend(rearrange(
&rearrange(items, Order::Reverse),
Order::BottomHalf,
));
out
}
Order::MiddleOut => {
let middle = len / 2;
let mut out = Vec::with_capacity(len);
if len % 2 == 1 {
out.push(items[middle]);
for step in 1..=middle {
out.push(items[middle + step]);
out.push(items[middle - step]);
}
} else {
for step in 1..=middle {
out.push(items[middle - 1 + step]);
out.push(items[middle - step]);
}
}
out
}
}
}
fn grease() -> [u8; 2] {
const VALUES: [u8; 16] = [
0x0a, 0x1a, 0x2a, 0x3a, 0x4a, 0x5a, 0x6a, 0x7a, 0x8a, 0x9a, 0xaa, 0xba, 0xca, 0xda, 0xea,
0xfa,
];
let pick = VALUES[rand::random_range(0..VALUES.len())];
[pick, pick]
}
enum Entropy {
Live,
#[cfg(test)]
Fixed { fill: u8, grease: u8 },
}
impl Entropy {
fn fill(&self) -> [u8; 32] {
match self {
Entropy::Live => rand::random(),
#[cfg(test)]
Entropy::Fixed { fill, .. } => [*fill; 32],
}
}
fn grease(&self) -> [u8; 2] {
match self {
Entropy::Live => grease(),
#[cfg(test)]
Entropy::Fixed { grease, .. } => [*grease; 2],
}
}
}
fn hello(probe: &Probe, host: &str, entropy: &Entropy) -> Vec<u8> {
let (record_version, body_version): ([u8; 2], [u8; 2]) = match probe.version {
Version::Tls11 => ([0x03, 0x02], [0x03, 0x02]),
Version::Tls12 => ([0x03, 0x03], [0x03, 0x03]),
Version::Tls13 => ([0x03, 0x01], [0x03, 0x03]),
};
let mut body = Vec::with_capacity(512);
body.extend_from_slice(&body_version);
body.extend_from_slice(&entropy.fill());
body.push(32);
body.extend_from_slice(&entropy.fill());
let offered = suites(probe, entropy);
body.extend_from_slice(&(offered.len() as u16).to_be_bytes());
body.extend_from_slice(&offered);
body.push(0x01);
body.push(0x00);
body.extend_from_slice(&extensions(probe, host, entropy));
let mut handshake = Vec::with_capacity(body.len() + 4);
handshake.push(0x01);
handshake.push(0x00);
handshake.extend_from_slice(&(body.len() as u16).to_be_bytes());
handshake.extend_from_slice(&body);
let mut record = Vec::with_capacity(handshake.len() + 5);
record.push(0x16);
record.extend_from_slice(&record_version);
record.extend_from_slice(&(handshake.len() as u16).to_be_bytes());
record.extend_from_slice(&handshake);
record
}
fn suites(probe: &Probe, entropy: &Entropy) -> Vec<u8> {
let list = match probe.suites {
Suites::All => SUITES_ALL,
Suites::NoTls13 => SUITES_NO_TLS13,
};
let mut ordered = rearrange(list, probe.order);
if probe.grease {
ordered.insert(0, entropy.grease());
}
ordered.concat()
}
fn extensions(probe: &Probe, host: &str, entropy: &Entropy) -> Vec<u8> {
let mut all = Vec::with_capacity(256);
if probe.grease {
all.extend_from_slice(&entropy.grease());
all.extend_from_slice(&[0x00, 0x00]);
}
all.extend_from_slice(&server_name(host));
all.extend_from_slice(&[0x00, 0x17, 0x00, 0x00]); all.extend_from_slice(&[0x00, 0x01, 0x00, 0x01, 0x01]); all.extend_from_slice(&[0xff, 0x01, 0x00, 0x01, 0x00]); all.extend_from_slice(&[
0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x18, 0x00, 0x19,
]); all.extend_from_slice(&[0x00, 0x0b, 0x00, 0x02, 0x01, 0x00]); all.extend_from_slice(&[0x00, 0x23, 0x00, 0x00]); all.extend_from_slice(&alpn(probe));
all.extend_from_slice(&[
0x00, 0x0d, 0x00, 0x14, 0x00, 0x12, 0x04, 0x03, 0x08, 0x04, 0x04, 0x01, 0x05, 0x03, 0x08,
0x05, 0x05, 0x01, 0x08, 0x06, 0x06, 0x01, 0x02, 0x01,
]); all.extend_from_slice(&key_share(probe.grease, entropy));
all.extend_from_slice(&[0x00, 0x2d, 0x00, 0x02, 0x01, 0x01]);
if probe.version == Version::Tls13 || probe.supported == Supported::Tls12 {
all.extend_from_slice(&supported_versions(probe, entropy));
}
let mut out = Vec::with_capacity(all.len() + 2);
out.extend_from_slice(&(all.len() as u16).to_be_bytes());
out.extend_from_slice(&all);
out
}
fn server_name(host: &str) -> Vec<u8> {
let name = host.as_bytes();
let mut out = vec![0x00, 0x00];
out.extend_from_slice(&((name.len() + 5) as u16).to_be_bytes());
out.extend_from_slice(&((name.len() + 3) as u16).to_be_bytes());
out.push(0x00);
out.extend_from_slice(&(name.len() as u16).to_be_bytes());
out.extend_from_slice(name);
out
}
fn alpn(probe: &Probe) -> Vec<u8> {
const HTTP_0_9: &[u8] = b"\x08http/0.9";
const HTTP_1_0: &[u8] = b"\x08http/1.0";
const HTTP_1_1: &[u8] = b"\x08http/1.1";
const SPDY_1: &[u8] = b"\x06spdy/1";
const SPDY_2: &[u8] = b"\x06spdy/2";
const SPDY_3: &[u8] = b"\x06spdy/3";
const H2: &[u8] = b"\x02h2";
const H2C: &[u8] = b"\x03h2c";
const HQ: &[u8] = b"\x02hq";
let list: Vec<&[u8]> = match probe.alpn {
Alpn::All => vec![
HTTP_0_9, HTTP_1_0, HTTP_1_1, SPDY_1, SPDY_2, SPDY_3, H2, H2C, HQ,
],
Alpn::Rare => vec![HTTP_0_9, HTTP_1_0, SPDY_1, SPDY_2, SPDY_3, H2C, HQ],
};
let protocols: Vec<u8> = rearrange(&list, probe.extension_order).concat();
let mut out = vec![0x00, 0x10];
out.extend_from_slice(&((protocols.len() + 2) as u16).to_be_bytes());
out.extend_from_slice(&(protocols.len() as u16).to_be_bytes());
out.extend_from_slice(&protocols);
out
}
fn key_share(with_grease: bool, entropy: &Entropy) -> Vec<u8> {
let mut shares = Vec::with_capacity(64);
if with_grease {
shares.extend_from_slice(&entropy.grease());
shares.extend_from_slice(&[0x00, 0x01, 0x00]);
}
shares.extend_from_slice(&[0x00, 0x1d]); shares.extend_from_slice(&[0x00, 0x20]);
shares.extend_from_slice(&entropy.fill());
let mut out = vec![0x00, 0x33];
out.extend_from_slice(&((shares.len() + 2) as u16).to_be_bytes());
out.extend_from_slice(&(shares.len() as u16).to_be_bytes());
out.extend_from_slice(&shares);
out
}
fn supported_versions(probe: &Probe, entropy: &Entropy) -> Vec<u8> {
let list: Vec<[u8; 2]> = match probe.supported {
Supported::Tls12 => vec![[0x03, 0x01], [0x03, 0x02], [0x03, 0x03]],
_ => vec![[0x03, 0x01], [0x03, 0x02], [0x03, 0x03], [0x03, 0x04]],
};
let mut versions = Vec::with_capacity(16);
if probe.grease {
versions.extend_from_slice(&entropy.grease());
}
versions.extend_from_slice(&rearrange(&list, probe.extension_order).concat());
let mut out = vec![0x00, 0x2b];
out.extend_from_slice(&((versions.len() + 1) as u16).to_be_bytes());
out.push(versions.len() as u8);
out.extend_from_slice(&versions);
out
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct Answer {
cipher: String,
version: String,
alpn: String,
extensions: String,
}
fn read_answer(reply: &[u8]) -> Answer {
if reply.first() != Some(&0x16) || reply.get(5) != Some(&0x02) {
return Answer::default();
}
let Some(end) = tls::server_hello_end(reply) else {
return Answer::default();
};
let Some(&session_id_len) = reply.get(43) else {
return Answer::default();
};
let counter = session_id_len as usize;
let (Some(cipher), Some(version)) = (reply.get(counter + 44..counter + 46), reply.get(9..11))
else {
return Answer::default();
};
let hello_length = match reply.get(3..5) {
Some(bytes) => u16::from_be_bytes([bytes[0], bytes[1]]) as usize,
None => return Answer::default(),
};
let Some((alpn, extensions)) = read_extensions(reply, counter, hello_length, end) else {
return Answer::default();
};
Answer {
cipher: hex(cipher),
version: hex(version),
alpn,
extensions,
}
}
fn read_extensions(
reply: &[u8],
counter: usize,
hello_length: usize,
end: usize,
) -> Option<(String, String)> {
let empty = Some((String::new(), String::new()));
if reply.get(counter + 47) == Some(&11)
|| reply.get(counter + 50..counter + 53) == Some(&[0x0e, 0xac, 0x0b][..])
|| reply.get(82..85) == Some(&[0x0f, 0xf0, 0x0b][..])
|| counter + 42 >= hello_length
{
return empty;
}
let Some(stated) = reply.get(counter + 47..counter + 49) else {
return empty;
};
let length = u16::from_be_bytes([stated[0], stated[1]]) as usize;
let mut at = counter + 49;
if at + length > end {
return None;
}
let hello = reply.get(..end)?;
let maximum = length + at - 1;
let mut types = Vec::new();
let mut alpn = String::new();
while at < maximum {
let kind = hello.get(at..at + 2)?;
let size = hello.get(at + 2..at + 4)?;
let size = u16::from_be_bytes([size[0], size[1]]) as usize;
if kind == [0x00, 0x10]
&& let Some(value) = hello.get(at + 4 + 3..at + 4 + size)
{
alpn = String::from_utf8_lossy(value).into_owned();
}
types.push(hex(kind));
at += size + 4;
}
Some((alpn, types.join("-")))
}
fn hash(answers: &[Answer]) -> String {
if answers.iter().all(|answer| answer == &Answer::default()) {
return EMPTY_HASH.to_string();
}
let mut fuzzy = String::with_capacity(62);
let mut spelled = String::new();
for answer in answers {
fuzzy.push_str(&cipher_code(&answer.cipher));
fuzzy.push(version_code(&answer.version));
spelled.push_str(&answer.alpn);
spelled.push_str(&answer.extensions);
}
let digest = Sha256::digest(spelled.as_bytes());
fuzzy.push_str(&hex(&digest)[..32]);
fuzzy
}
fn cipher_code(cipher: &str) -> String {
if cipher.is_empty() {
return "00".to_string();
}
let place = CODED_SUITES
.iter()
.position(|suite| hex(suite) == cipher)
.map_or(CODED_SUITES.len() + 1, |index| index + 1);
format!("{place:02x}")
}
fn version_code(version: &str) -> char {
const LETTERS: [char; 6] = ['a', 'b', 'c', 'd', 'e', 'f'];
version
.chars()
.nth(3)
.and_then(|digit| digit.to_digit(16))
.and_then(|index| LETTERS.get(index as usize).copied())
.unwrap_or('0')
}
fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}
pub async fn fingerprint(addr: SocketAddr, host: &str) -> Option<String> {
let mut answers = Vec::with_capacity(PROBES.len());
for probe in &PROBES {
answers.push(match exchange(addr, probe, host).await {
Some(reply) => read_answer(&reply),
None => Answer::default(),
});
}
let found = hash(&answers);
(found != EMPTY_HASH).then_some(found)
}
async fn exchange(addr: SocketAddr, probe: &Probe, host: &str) -> Option<Vec<u8>> {
let mut stream = timeout(PROBE_TIMEOUT, super::analyzer_connect(addr))
.await
.ok()?
.ok()?;
let hello = hello(probe, host, &Entropy::Live);
timeout(PROBE_TIMEOUT, stream.write_all(&hello))
.await
.ok()?
.ok()?;
let mut reply = vec![0u8; MAX_REPLY_BYTES];
let mut filled = 0;
let _ = timeout(PROBE_TIMEOUT, async {
while filled < reply.len() && wants_more(&reply[..filled]) {
match stream.read(&mut reply[filled..]).await {
Ok(0) | Err(_) => break,
Ok(read) => filled += read,
}
}
})
.await;
reply.truncate(filled);
(!reply.is_empty()).then_some(reply)
}
fn wants_more(reply: &[u8]) -> bool {
if tls::server_hello_end(reply).is_some() {
return false;
}
match tls::record_length(reply) {
Some(total) => reply.len() < total,
None => reply.len() < tls::RECORD_HEADER_LEN,
}
}
pub struct JarmAnalyzer;
#[async_trait::async_trait]
impl Analyzer for JarmAnalyzer {
fn id(&self) -> SourceId {
SourceId::Jarm
}
fn interested(&self, ctx: &PortContext) -> bool {
ctx.protocol == Protocol::Tcp
&& ctx.addr.is_some()
&& ctx.detection >= ServiceDetection::Thorough
}
async fn collect(&self, ctx: &PortContext, responses: &ResponseSet) -> Collected {
if responses.tls.is_none() {
return Collected::default();
}
let Some(addr) = ctx.addr else {
return Collected::default();
};
let host = addr.ip().to_string();
match fingerprint(addr, &host).await {
Some(found) => Collected::from_frames(vec![found.into_bytes()]),
None => Collected::default(),
}
}
fn analyze(
&self,
_ctx: &PortContext,
_responses: &ResponseSet,
collected: &Collected,
) -> Vec<Evidence> {
let Some(frame) = collected.frames.first() else {
return Vec::new();
};
let Ok(found) = std::str::from_utf8(frame) else {
return Vec::new();
};
super::db::SignatureDb::global()
.identify_jarm(found)
.map(|evidence| vec![as_handshake_reading(evidence)])
.unwrap_or_default()
}
}
fn as_handshake_reading(mut evidence: Evidence) -> Evidence {
evidence.source = SourceId::Jarm;
if evidence.extrainfo.is_none() {
evidence = evidence.with_extrainfo("TLS fingerprint".to_string());
}
evidence
}
#[cfg(test)]
mod tests {
use super::*;
const FIXED: Entropy = Entropy::Fixed {
fill: 0xab,
grease: 0x5a,
};
const VECTOR_HOST: &str = "192.168.64.4";
fn sha256_hex(bytes: &[u8]) -> String {
hex(&Sha256::digest(bytes))
}
#[test]
fn every_hello_matches_the_reference_implementation() {
const VECTORS: [(&str, usize, &str); 10] = [
(
"tls1_2_forward",
426,
"5000b1706a88d1009fb1247de87295804dba8099e8d72d61beea912c5a84ec29",
),
(
"tls1_2_reverse",
426,
"a62c054d4aff4afa6895fb1744f7cef2c5c343c2ac9ad5f50ca28349ef9cc886",
),
(
"tls1_2_top_half",
347,
"88d7cf577b0e938c221e0a1257984502a645165e2c8067e4204821167f8ce9bd",
),
(
"tls1_2_bottom_half",
333,
"210d2b8b1276ba0225bfee3bcb4d8fac834a00171bd7b679ca439e4313abe9be",
),
(
"tls1_2_middle_out",
414,
"2cb29950f961ba34ce7cd61e18ee03c915946415be76598a890b15a0e1db6941",
),
(
"tls1_1_middle_out",
415,
"19da4c2c0142b37772088d23a78883f7e436311c7c6431c08622e4256e8c97c2",
),
(
"tls1_3_forward",
428,
"6bc238b720a46538d34944da9995ba8532d93ace35dcb68726ad05380ec752f9",
),
(
"tls1_3_reverse",
428,
"4faef2f68ec0f294509f5780f87b329208e3618c32d32a27a676b9f617db6e40",
),
(
"tls1_3_invalid",
418,
"9c627ca140aa5f451720c3b7bca51cc20caa5c588f169131d0f0abfea9959914",
),
(
"tls1_3_middle_out",
441,
"ce7e02b6e208db87617b4254d55d05b9f3d58f75269c02c61be547f50efbc492",
),
];
assert_eq!(
PROBES.len(),
VECTORS.len(),
"the ten probes and the ten vectors are the same list"
);
for (probe, (name, length, digest)) in PROBES.iter().zip(VECTORS) {
let built = hello(probe, VECTOR_HOST, &FIXED);
assert_eq!(built.len(), length, "{name}: record length");
assert_eq!(sha256_hex(&built), digest, "{name}: record bytes");
}
}
#[test]
fn rearrange_deals_the_list_five_ways() {
let list = [1, 2, 3, 4, 5, 6, 7, 8];
assert_eq!(rearrange(&list, Order::Forward), list);
assert_eq!(rearrange(&list, Order::Reverse), [8, 7, 6, 5, 4, 3, 2, 1]);
assert_eq!(rearrange(&list, Order::MiddleOut), [5, 4, 6, 3, 7, 2, 8, 1]);
assert_eq!(rearrange(&list, Order::TopHalf), [4, 3, 2, 1]);
assert_eq!(rearrange(&list, Order::BottomHalf), [5, 6, 7, 8]);
let odd = [1, 2, 3, 4, 5];
assert_eq!(rearrange(&odd, Order::TopHalf), [3, 2, 1]);
assert_eq!(rearrange(&odd, Order::BottomHalf), [4, 5]);
assert_eq!(rearrange(&odd, Order::MiddleOut), [3, 4, 2, 5, 1]);
let empty: [u8; 0] = [];
for order in [
Order::Forward,
Order::Reverse,
Order::TopHalf,
Order::BottomHalf,
Order::MiddleOut,
] {
assert!(rearrange(&empty, order).is_empty());
}
}
#[test]
fn the_hash_is_thirty_cipher_characters_and_thirty_two_digest_characters() {
let answered = Answer {
cipher: "c02f".into(),
version: "0303".into(),
alpn: "h2".into(),
extensions: "002b-0033".into(),
};
let answers: Vec<Answer> = (0..10).map(|_| answered.clone()).collect();
let found = hash(&answers);
assert_eq!(found.len(), 62);
assert!(found.is_char_boundary(30));
assert!(
found.chars().all(|c| c.is_ascii_hexdigit()),
"the whole hash is hex: {found}"
);
}
#[test]
fn silence_is_not_a_fingerprint() {
let silent: Vec<Answer> = (0..10).map(|_| Answer::default()).collect();
assert_eq!(hash(&silent), EMPTY_HASH);
assert_eq!(EMPTY_HASH.len(), 62);
}
#[test]
fn a_probe_contributes_two_cipher_characters_and_one_version_character() {
assert_eq!(cipher_code(""), "00");
assert_eq!(version_code(""), '0');
assert_eq!(cipher_code("0016"), "05");
assert_eq!(cipher_code("c02f"), "29");
assert_eq!(cipher_code("c030"), "2a");
assert_eq!(cipher_code("dead"), "46");
assert_eq!(version_code("0301"), 'b');
assert_eq!(version_code("0302"), 'c');
assert_eq!(version_code("0303"), 'd');
assert_eq!(version_code("0304"), 'e');
}
#[test]
fn a_server_hello_reads_back_as_its_four_parts() {
let read = read_answer(&server_hello(false));
assert_eq!(read.cipher, "c02f");
assert_eq!(read.version, "0303");
assert_eq!(read.alpn, "", "the server agreed no protocol");
assert_eq!(read.extensions, "ff01-0017");
}
#[test]
fn a_server_hello_reads_whole_or_not_at_all() {
let whole = server_hello(false);
assert_ne!(read_answer(&whole), Answer::default());
for cut in 0..whole.len() {
assert_eq!(
read_answer(&whole[..cut]),
Answer::default(),
"the first {cut} of {} bytes",
whole.len()
);
}
}
#[test]
fn a_refusal_reads_back_empty() {
let alert = [0x15, 0x03, 0x03, 0x00, 0x02, 0x02, 0x28];
assert_eq!(read_answer(&alert), Answer::default());
assert_eq!(read_answer(&[]), Answer::default());
assert_eq!(read_answer(&[0x16, 0x03, 0x03]), Answer::default());
let truncated = [0x16, 0x03, 0x03, 0x00, 0x40, 0x02, 0x00, 0x00, 0x3c];
assert_eq!(read_answer(&truncated), Answer::default());
}
#[test]
fn a_published_hash_reaches_the_corpus() {
let db = super::super::db::SignatureDb::global();
let found = db
.identify_jarm("07d14d16d21d21d07c42d41d00041d24a458a375eef0c576d23a7bab9a9fb1")
.expect("a published hash names a product");
assert_eq!(found.product.as_deref(), Some("Cobalt Strike Listener"));
let reported = as_handshake_reading(found);
assert_eq!(reported.source, SourceId::Jarm);
assert_eq!(reported.extrainfo.as_deref(), Some("TLS fingerprint"));
let unpublished = "00112233445566778899aabbccddeeff00112233445566778899aabbccddee";
assert!(db.identify_jarm(unpublished).is_none());
assert_eq!(
db.identify_field(unpublished)
.and_then(|stray| stray.service),
Some("isakmp".to_string()),
"the whole corpus does answer, which is the reason for the index"
);
}
#[test]
fn the_analyzer_asks_only_at_the_thorough_level() {
let addr: std::net::SocketAddr = "127.0.0.1:443".parse().expect("an address");
let at = |level, protocol| {
PortContext::new(443, protocol)
.with_addr(Some(addr))
.with_detection(level)
};
assert!(JarmAnalyzer.interested(&at(ServiceDetection::Thorough, Protocol::Tcp)));
assert!(!JarmAnalyzer.interested(&at(ServiceDetection::Probe, Protocol::Tcp)));
assert!(!JarmAnalyzer.interested(&at(ServiceDetection::Banner, Protocol::Tcp)));
assert!(!JarmAnalyzer.interested(&at(ServiceDetection::Off, Protocol::Tcp)));
assert!(!JarmAnalyzer.interested(&at(ServiceDetection::Thorough, Protocol::Udp)));
assert!(!JarmAnalyzer.interested(
&PortContext::new(443, Protocol::Tcp).with_detection(ServiceDetection::Thorough)
));
}
#[tokio::test]
async fn a_plaintext_port_is_not_dialled() {
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:0").await.expect("binds");
let addr = listener.local_addr().expect("has an address");
let dialled = Arc::new(AtomicUsize::new(0));
let counted = Arc::clone(&dialled);
tokio::spawn(async move {
while listener.accept().await.is_ok() {
counted.fetch_add(1, Ordering::SeqCst);
}
});
let ctx = PortContext::new(80, Protocol::Tcp)
.with_addr(Some(addr))
.with_detection(ServiceDetection::Thorough);
let plaintext = ResponseSet::from_banners(vec!["HTTP/1.1 200 OK".to_string()]);
assert!(
JarmAnalyzer
.collect(&ctx, &plaintext)
.await
.frames
.is_empty()
);
assert_eq!(dialled.load(Ordering::SeqCst), 0, "no handshake, no probes");
let handshaken = ResponseSet::default().with_tls(super::super::TlsInfo::new(Vec::new()));
let _ = JarmAnalyzer.collect(&ctx, &handshaken).await;
assert_eq!(dialled.load(Ordering::SeqCst), PROBES.len(), "ten hellos");
}
async fn stub_server(refuse: bool) -> SocketAddr {
answering(server_hello(refuse), None).await
}
async fn answering(reply: Vec<u8>, split_at: Option<usize>) -> SocketAddr {
use tokio::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:0").await.expect("binds");
let addr = listener.local_addr().expect("has an address");
tokio::spawn(async move {
while let Ok((mut stream, _)) = listener.accept().await {
let mut hello = vec![0u8; 4096];
if stream.read(&mut hello).await.is_err() {
continue;
}
let (head, tail) = reply.split_at(split_at.unwrap_or(reply.len()));
let _ = stream.write_all(head).await;
if !tail.is_empty() {
tokio::time::sleep(Duration::from_millis(20)).await;
let _ = stream.write_all(tail).await;
}
}
});
addr
}
fn server_hello(refuse: bool) -> Vec<u8> {
if refuse {
return vec![0x15, 0x03, 0x03, 0x00, 0x02, 0x02, 0x28];
}
let mut record = vec![
0x16, 0x03, 0x03, 0x00, 0x55, 0x02, 0x00, 0x00, 0x51, 0x03, 0x03, ];
record.extend_from_slice(&[0x11; 32]); record.push(0x20);
record.extend_from_slice(&[0x22; 32]); record.extend_from_slice(&[0xc0, 0x2f]); record.push(0x00); record.extend_from_slice(&[0x00, 0x09]); record.extend_from_slice(&[0xff, 0x01, 0x00, 0x01, 0x00]); record.extend_from_slice(&[0x00, 0x17, 0x00, 0x00]); record
}
#[tokio::test]
async fn ten_answers_assemble_into_a_hash() {
let addr = stub_server(false).await;
let found = fingerprint(addr, "127.0.0.1")
.await
.expect("a server that answered has a fingerprint");
assert_eq!(&found[..30], "29d29d29d29d29d29d29d29d29d29d");
assert_eq!(found.len(), 62);
assert_eq!(
fingerprint(addr, "127.0.0.1").await.as_deref(),
Some(found.as_str()),
"the same server answers the same way twice"
);
}
#[tokio::test]
async fn a_server_hello_arriving_in_pieces_hashes_as_it_does_whole() {
let whole = stub_server(false).await;
let expected = fingerprint(whole, "127.0.0.1")
.await
.expect("a server that answered has a fingerprint");
let pieces = answering(server_hello(false), Some(85)).await;
assert_eq!(
fingerprint(pieces, "127.0.0.1").await.as_deref(),
Some(expected.as_str())
);
}
#[tokio::test]
async fn a_server_that_refuses_everything_has_no_fingerprint() {
let addr = stub_server(true).await;
assert_eq!(fingerprint(addr, "127.0.0.1").await, None);
}
#[test]
fn the_registry_carries_the_analyzer() {
assert!(
super::super::analyzers()
.iter()
.any(|analyzer| analyzer.id() == SourceId::Jarm),
"a scan consults the set, not this module"
);
}
#[test]
fn nothing_collected_is_nothing_claimed() {
let ctx = PortContext::new(443, Protocol::Tcp);
let responses = ResponseSet::default();
assert!(
JarmAnalyzer
.analyze(&ctx, &responses, &Collected::default())
.is_empty()
);
let invalid = Collected::from_frames(vec![vec![0xff, 0xfe]]);
assert!(JarmAnalyzer.analyze(&ctx, &responses, &invalid).is_empty());
}
}