mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 08:15:29 +00:00
pcap: preserve capture and protocol source history
Port relevant PCAP implementation and regression snapshots from Craft Anything 901f99f8465b3dc26b4c1b047388bcd78579f7f5.
This commit is contained in:
1 parent
b9f792d293
commit
32ed24dc73
2 files changed
+167
-1
No files matched your search
+110
-1
@@ -826,6 +826,11 @@ pub fn qtype_name(t: u16) -> String {
|
||||
16 => "TXT".into(),
|
||||
28 => "AAAA".into(),
|
||||
33 => "SRV".into(),
|
||||
35 => "NAPTR".into(),
|
||||
39 => "DNAME".into(),
|
||||
44 => "SSHFP".into(),
|
||||
52 => "TLSA".into(),
|
||||
256 => "URI".into(),
|
||||
64 => "SVCB".into(),
|
||||
65 => "HTTPS".into(),
|
||||
257 => "CAA".into(),
|
||||
@@ -891,6 +896,103 @@ fn dns_name(msg: &[u8], mut off: usize, allow_compression: bool) -> Option<(Stri
|
||||
None
|
||||
}
|
||||
|
||||
/// Decode discovery and authentication records whose domain names prohibit compression.
|
||||
fn discovery_answer(kind: u16, rd: &[u8]) -> Option<String> {
|
||||
let r = Reader::new(rd);
|
||||
let text = |b: &[u8]| b.iter().flat_map(|v| std::ascii::escape_default(*v)).map(char::from).collect::<String>();
|
||||
let hex = |b: &[u8]| b.iter().map(|v| format!("{v:02x}")).collect::<String>();
|
||||
match kind {
|
||||
39 => dns_name(rd, 0, false).filter(|(_, after, _)| *after == rd.len()).map(|(name, _, _)| name),
|
||||
44 => {
|
||||
let (&algorithm, next) = rd.split_first()?;
|
||||
let (&hash, digest) = next.split_first()?;
|
||||
if digest.is_empty() || hash == 1 && digest.len() != 20 || hash == 2 && digest.len() != 32 {
|
||||
return None;
|
||||
}
|
||||
Some(format!("{algorithm} {hash} {}", hex(digest)))
|
||||
}
|
||||
52 => {
|
||||
let [usage, selector, matching, cert @ ..] = rd else { return None };
|
||||
if cert.is_empty() || *matching == 1 && cert.len() != 32 || *matching == 2 && cert.len() != 64 {
|
||||
return None;
|
||||
}
|
||||
Some(format!("{usage} {selector} {matching} {}", hex(cert)))
|
||||
}
|
||||
256 => {
|
||||
let target = rd.get(4..).filter(|s| !s.is_empty())?;
|
||||
Some(format!("{} {} \"{}\"", r.u16_be(0)?, r.u16_be(2)?, strip_userinfo(&text(target))))
|
||||
}
|
||||
35 => {
|
||||
let mut pos = 4;
|
||||
let mut value = format!("{} {}", r.u16_be(0)?, r.u16_be(2)?);
|
||||
for _ in 0..3 {
|
||||
let len = usize::from(r.u8(pos)?);
|
||||
value.push_str(&format!(" \"{}\"", text(r.slice(pos + 1, len)?)));
|
||||
pos += 1 + len;
|
||||
}
|
||||
let (replacement, _, _) = dns_name(rd, pos, false).filter(|(_, after, _)| *after == rd.len())?;
|
||||
Some(format!("{value} {replacement}"))
|
||||
}
|
||||
64 | 65 => {
|
||||
let priority = r.u16_be(0)?;
|
||||
let (target, mut pos, _) = dns_name(rd, 2, false)?;
|
||||
let mut value = format!("{priority} {target}");
|
||||
let mut previous = None;
|
||||
while pos < rd.len() {
|
||||
let key = r.u16_be(pos)?;
|
||||
let len = usize::from(r.u16_be(pos + 2)?);
|
||||
if key == u16::MAX || previous.is_some_and(|old| old >= key) {
|
||||
return None;
|
||||
}
|
||||
let param = r.slice(pos + 4, len)?;
|
||||
// Keep unknown keys lossless; interpreting them as text can fabricate an endpoint.
|
||||
let field = match key {
|
||||
1 if priority != 0 => {
|
||||
let mut remaining = param;
|
||||
let mut ids = Vec::new();
|
||||
while let Some((&n, rest)) = remaining.split_first() {
|
||||
if n == 0 {
|
||||
return None;
|
||||
}
|
||||
let (id, tail) = rest.split_at_checked(usize::from(n))?;
|
||||
ids.push(text(id).replace(',', "\\,"));
|
||||
remaining = tail;
|
||||
}
|
||||
if ids.is_empty() {
|
||||
return None;
|
||||
}
|
||||
format!("alpn={}", ids.join(","))
|
||||
}
|
||||
3 if priority != 0 => format!("port={}", u16::from_be_bytes(param.try_into().ok()?)),
|
||||
4 if priority != 0 => {
|
||||
let (addresses, tail) = param.as_chunks::<4>();
|
||||
if addresses.is_empty() || !tail.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let hints: Vec<_> = addresses.iter().map(|ip| Ipv4Addr::from(*ip).to_string()).collect();
|
||||
format!("ipv4hint={}", hints.join(","))
|
||||
}
|
||||
6 if priority != 0 => {
|
||||
let (addresses, tail) = param.as_chunks::<16>();
|
||||
if addresses.is_empty() || !tail.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let hints: Vec<_> = addresses.iter().map(|ip| Ipv6Addr::from(*ip).to_string()).collect();
|
||||
format!("ipv6hint={}", hints.join(","))
|
||||
}
|
||||
_ => format!("key{key}=0x{}", hex(param)),
|
||||
};
|
||||
value.push(' ');
|
||||
value.push_str(&field);
|
||||
previous = Some(key);
|
||||
pos += len + 4;
|
||||
}
|
||||
Some(value)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode a bounded set of DNS questions and answer/prerequisite records from one message.
|
||||
/// Preserve complete entries before malformed data and mark the message incomplete.
|
||||
fn dns(msg: &[u8], d: &mut Decoded, label: &'static str, mdns_wire_format: bool) {
|
||||
@@ -940,7 +1042,7 @@ fn dns(msg: &[u8], d: &mut Decoded, label: &'static str, mdns_wire_format: bool)
|
||||
let record_class = if mdns_wire_format { class & 0x7fff } else { class };
|
||||
let empty_prerequisite = rd.is_empty() && opcode == 5 && matches!(class, 254 | 255) && ttl == 0;
|
||||
let value = match (rt, rd.len()) {
|
||||
(1 | 28 | 6 | 33 | 257, 0) if empty_prerequisite => String::new(),
|
||||
(1 | 28 | 6 | 33 | 35 | 39 | 44 | 52 | 64 | 65 | 256 | 257, 0) if empty_prerequisite => String::new(),
|
||||
(1 | 28, _) if record_class != 1 => format!("<{} bytes>", rd.len()),
|
||||
(1, 4) => Ipv4Addr::new(
|
||||
rd.first().copied().unwrap_or(0),
|
||||
@@ -1042,6 +1144,13 @@ fn dns(msg: &[u8], d: &mut Decoded, label: &'static str, mdns_wire_format: bool)
|
||||
}
|
||||
txt
|
||||
}
|
||||
(35 | 39 | 44 | 52 | 64 | 65 | 256, _) if record_class == 1 => {
|
||||
let Some(value) = discovery_answer(rt, rd) else {
|
||||
out.incomplete = true;
|
||||
break;
|
||||
};
|
||||
value
|
||||
}
|
||||
(_, n) => format!("<{n} bytes>"),
|
||||
};
|
||||
if out.incomplete {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
use craft_anything_formats::pcap::{Dns, decode};
|
||||
use craft_anything_testkit::pcap::{dns_query, eth, ipv4_pkt, udp};
|
||||
|
||||
fn answer(kind: u16, rd: &[u8]) -> Dns {
|
||||
let mut msg = dns_query();
|
||||
msg[2..4].copy_from_slice(&0x8180u16.to_be_bytes());
|
||||
msg[6..8].copy_from_slice(&1u16.to_be_bytes());
|
||||
msg.extend([0xc0, 12]);
|
||||
msg.extend(kind.to_be_bytes());
|
||||
msg.extend([0, 1, 0, 0, 0, 60]);
|
||||
msg.extend((rd.len() as u16).to_be_bytes());
|
||||
msg.extend(rd);
|
||||
decode(1, ð(0x0800, &ipv4_pkt(17, [192, 0, 2, 2], [192, 0, 2, 1], &udp(53, 51000, &msg)))).dns.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn svc_binding_reports_target_and_port() {
|
||||
for kind in [64, 65] {
|
||||
let msg = answer(kind, b"\0\x01\x03api\0\0\x03\0\x02\x20\xfb");
|
||||
assert_eq!(msg.answers[0].value, "1 api port=8443");
|
||||
assert_eq!(answer(kind, b"\0\x01\0\0\x01\0\x03\x02h2\0\x04\0\x04\xc0\0\x02\x01").answers[0].value, "1 . alpn=h2 ipv4hint=192.0.2.1");
|
||||
assert!(answer(kind, b"\0\x01\0\0\x01\0\x01\x00").incomplete);
|
||||
assert!(answer(kind, b"\0\x01\xc0\x0c").incomplete);
|
||||
assert!(answer(kind, b"\0\x01\0\0\x03\0\x01\x00").incomplete);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn sshfp_preserves_algorithm_and_complete_digest() {
|
||||
let mut d = vec![4, 2];
|
||||
d.extend([0x5a; 32]);
|
||||
assert_eq!(answer(44, &d).answers[0].value, format!("4 2 {}", "5a".repeat(32)));
|
||||
assert!(answer(44, &d[..15]).incomplete);
|
||||
}
|
||||
#[test]
|
||||
fn tlsa_keeps_selector_and_certificate_association() {
|
||||
let mut d = vec![3, 1, 1];
|
||||
d.extend([0xaa; 32]);
|
||||
assert_eq!(answer(52, &d).answers[0].value, format!("3 1 1 {}", "aa".repeat(32)));
|
||||
assert!(answer(52, &d[..4]).incomplete);
|
||||
}
|
||||
#[test]
|
||||
fn naptr_keeps_order_preference_strings_and_replacement() {
|
||||
let rd = b"\0\x0a\0\x02\x01s\x03sip\0\x03srv\0";
|
||||
assert_eq!(answer(35, rd).answers[0].value, r#"10 2 "s" "sip" "" srv"#);
|
||||
assert!(answer(35, &rd[..rd.len() - 1]).incomplete);
|
||||
}
|
||||
#[test]
|
||||
fn uri_keeps_priority_weight_and_target() {
|
||||
assert_eq!(answer(256, b"\0\x02\0\x05https://x.test/s").answers[0].value, r#"2 5 "https://x.test/s""#);
|
||||
assert!(answer(256, b"\0\x02\0\x05").incomplete);
|
||||
}
|
||||
#[test]
|
||||
fn dname_only_accepts_a_complete_uncompressed_target() {
|
||||
assert_eq!(answer(39, b"\x04host\0").answers[0].value, "host");
|
||||
assert!(answer(39, b"\xc0\x0c").incomplete);
|
||||
assert!(answer(39, b"\x04host\0extra").incomplete);
|
||||
}
|
||||
Reference in new issue
Block a user