pcap: preserve capture and protocol source history

Port relevant PCAP implementation and regression snapshots from Craft Anything 6cbc66984c518bf4af7ed261594431eceba54e95.
This commit is contained in:
Afloat authored and Dylan M. Taylor committed 2026-11-17 08:37:50 -05:00
1 parent 9ecd830f68
commit f4ddb7234d
2 files changed
+96 -11

No files matched your search

+25 -11
View File
@@ -1037,7 +1037,7 @@ fn tls(p: &[u8], d: &mut Decoded) {
0x0302 => "TLS 1.1".into(),
0x0303 => "TLS 1.2".into(),
0x0304 => "TLS 1.3".into(),
v if v & 0x0f0f == 0x0a0a => "GREASE".into(),
v if v & 0x0f0f == 0x0a0a && v >> 8 == v & 0xff => "GREASE".into(),
v => format!("{v:#06x}"),
};
let mut hello = TlsHello {
@@ -1057,18 +1057,24 @@ fn tls(p: &[u8], d: &mut Decoded) {
let hello_end = handshake_end.min(record_end_available);
let r = Reader::new(p.get(..hello_end).unwrap_or(&[]));
// Every variable-length field must fit inside this handshake, not merely the packet.
let skip8 = |off: usize| {
let skip8 = |off: usize, min: usize, max: usize| {
let n = usize::from(r.u8(off)?);
if !(min..=max).contains(&n) {
return None;
}
r.slice(off + 1, n)?;
Some(off + 1 + n)
};
let skip16 = |off: usize| {
let cipher_suites_end = |off: usize| {
let n = usize::from(r.u16_be(off)?);
if n < 2 || n % 2 != 0 {
return None;
}
r.slice(off + 2, n)?;
Some(off + 2 + n)
};
// Record header (5) + handshake header (4) + client_version (2) + random (32).
let Some(off) = skip8(5 + 4 + 2 + 32).and_then(skip16).and_then(skip8) else {
let Some(off) = skip8(5 + 4 + 2 + 32, 0, 32).and_then(cipher_suites_end).and_then(|p| skip8(p, 1, 255)) else {
hello.partial = true;
d.tls = Some(hello);
return;
@@ -1087,6 +1093,7 @@ fn tls(p: &[u8], d: &mut Decoded) {
hello.partial |= declared_end != handshake_end;
let end = declared_end.min(hello_end);
let extensions = Reader::new(p.get(..end).unwrap_or(&[]));
let mut seen_extensions = BTreeSet::new();
let mut e = off + 2;
while e + 4 <= end {
let (Some(t), Some(l)) = (extensions.u16_be(e), extensions.u16_be(e + 2)) else { break };
@@ -1095,12 +1102,17 @@ fn tls(p: &[u8], d: &mut Decoded) {
hello.partial = true;
break;
};
if !seen_extensions.insert(t) {
hello.partial = true;
e = body + usize::from(l);
continue;
}
let dr = Reader::new(data);
match t {
0 => {
if let Some(len) = dr.u16_be(0) {
let list_end = 2 + usize::from(len);
hello.partial |= list_end != data.len();
hello.partial |= len == 0 || list_end != data.len();
let names = Reader::new(data.get(..list_end.min(data.len())).unwrap_or(&[]));
let mut a = 2;
while a < list_end.min(data.len()) {
@@ -1113,9 +1125,9 @@ fn tls(p: &[u8], d: &mut Decoded) {
break;
};
if kind == 0 {
if name.is_empty() {
if name.is_empty() || hello.sni.is_some() {
hello.partial = true;
} else if hello.sni.is_none() {
} else {
hello.sni = Some(name.iter().map(|&c| if c.is_ascii_graphic() { char::from(c) } else { '?' }).collect());
}
}
@@ -1128,10 +1140,10 @@ fn tls(p: &[u8], d: &mut Decoded) {
16 => {
if let Some(len) = dr.u16_be(0) {
let list_end = 2 + usize::from(len);
hello.partial |= list_end != data.len();
hello.partial |= len == 0 || list_end != data.len();
let protocols = Reader::new(data.get(..list_end.min(data.len())).unwrap_or(&[]));
let mut a = 2usize;
while a < list_end.min(data.len()) && hello.alpn.len() < 16 {
while a < list_end.min(data.len()) {
let Some(n) = protocols.u8(a).filter(|&n| n > 0) else {
hello.partial = true;
break;
@@ -1140,7 +1152,9 @@ fn tls(p: &[u8], d: &mut Decoded) {
hello.partial = true;
break;
};
hello.alpn.push(String::from_utf8_lossy(proto).into_owned());
if hello.alpn.len() < 16 {
hello.alpn.push(String::from_utf8_lossy(proto).into_owned());
}
a += 1 + usize::from(n);
}
} else {
@@ -1150,7 +1164,7 @@ fn tls(p: &[u8], d: &mut Decoded) {
43 => {
if let Some(len) = dr.u8(0) {
let list_end = 1 + usize::from(len);
hello.partial |= list_end != data.len() || len % 2 != 0;
hello.partial |= len < 2 || list_end != data.len() || len % 2 != 0;
let versions = Reader::new(data.get(..list_end.min(data.len())).unwrap_or(&[]));
let mut a = 1usize;
while let Some(v) = versions.u16_be(a) {
@@ -100,3 +100,74 @@ fn complete_hello_metadata_is_unchanged() {
assert_eq!(hello.supported_versions, ["TLS 1.3", "TLS 1.2"]);
assert!(!hello.partial);
}
fn hello_with_vectors(session: &[u8], suites: &[u8], compression: &[u8], extensions: &[(u16, &[u8])]) -> craft_anything_formats::pcap::TlsHello {
let mut record = client_hello("unused.test");
record.truncate(43);
record.push(session.len() as u8);
record.extend(session);
record.extend((suites.len() as u16).to_be_bytes());
record.extend(suites);
record.push(compression.len() as u8);
record.extend(compression);
let mut ext = Vec::new();
for (kind, bytes) in extensions {
ext.extend(kind.to_be_bytes());
ext.extend((bytes.len() as u16).to_be_bytes());
ext.extend(*bytes);
}
record.extend((ext.len() as u16).to_be_bytes());
record.extend(ext);
let len = record.len();
record[3..5].copy_from_slice(&((len - 5) as u16).to_be_bytes());
record[6..9].copy_from_slice(&((len - 9) as u32).to_be_bytes()[1..]);
decoded(&record).tls.unwrap()
}
#[test]
fn hello_vectors_must_have_protocol_lengths() {
assert!(!hello_with_vectors(&[0; 32], &[0, 47], &[0], &[]).partial);
assert!(hello_with_vectors(&[0; 33], &[0, 47], &[0], &[]).partial);
for suites in [&[][..], &[0, 47, 0][..]] {
assert!(hello_with_vectors(&[], suites, &[0], &[]).partial);
}
assert!(hello_with_vectors(&[], &[0, 47], &[], &[]).partial);
for (kind, empty) in [(0, &[0, 0][..]), (16, &[0, 0][..]), (43, &[0][..])] {
assert!(hello_with_vectors(&[], &[0, 47], &[0], &[(kind, empty)]).partial);
}
}
#[test]
fn only_registered_grease_patterns_are_removed() {
let hello = hello_with_vectors(&[], &[0, 47], &[0], &[(43, &[6, 0x1a, 0x2a, 0x4a, 0x4a, 3, 4])]);
assert!(!hello.partial);
assert_eq!(hello.supported_versions, ["0x1a2a", "TLS 1.3"]);
}
#[test]
fn duplicate_extensions_and_repeated_host_names_are_incomplete() {
let hello = hello_with_vectors(&[], &[0, 47], &[0], &[(43, &[2, 3, 4]), (43, &[2, 3, 3])]);
assert!(hello.partial);
assert_eq!(hello.supported_versions, ["TLS 1.3"]);
let hello = hello_with_vectors(&[], &[0, 47], &[0], &[(0, &[0, 8, 0, 0, 1, b'a', 0, 0, 1, b'b'])]);
assert!(hello.partial);
assert_eq!(hello.sni.as_deref(), Some("a"));
}
#[test]
fn the_alpn_display_limit_does_not_hide_a_damaged_tail() {
let mut protocols = vec![0, 33];
for _ in 0..16 {
protocols.extend([1, b'x']);
}
protocols.push(3);
let hello = hello_with_vectors(&[], &[0, 47], &[0], &[(16, &protocols)]);
assert_eq!(hello.alpn.len(), 16);
assert!(hello.partial);
protocols[1] = 34;
protocols.pop();
protocols.extend([1, b'y']);
let hello = hello_with_vectors(&[], &[0, 47], &[0], &[(16, &protocols)]);
assert_eq!(hello.alpn.len(), 16);
assert!(!hello.partial);
}