Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

List DNS answers the same way on both packet engines (#514)

NetterSilverd0c5feefa761
diff --git a/crates/engine/src/tools/packet_tools.rs b/crates/engine/src/tools/packet_tools.rs
index 036e5d9e..efb15d02 100644
--- a/crates/engine/src/tools/packet_tools.rs
+++ b/crates/engine/src/tools/packet_tools.rs
@@ -540,7 +540,6 @@ fn packets_inventory_show(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput
if cut {
lims.push(Limitation::new("tshark_output_capped", "tshark printed more than 64 MiB; packets after that point are not counted."));
}
- lims.push(Limitation::new("dns_answer_attribution", "DNS answer addresses are attributed to the first question of the same response."));
("tshark", inv)
}
None => {
@@ -557,7 +556,7 @@ fn packets_inventory_show(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput
protocol_stacks: s.stacks,
endpoints: s.endpoints,
dns_queries: s.dns_queries,
- dns_answers: s.dns_answers,
+ dns_answers: s.dns_addresses,
tls_sni: s.tls_sni,
http_requests: http,
};
@@ -565,6 +564,7 @@ fn packets_inventory_show(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput
}
};
ctx.cancel.check()?;
+ lims.push(Limitation::new("dns_answer_attribution", "DNS answer addresses are attributed to the first question of the same response."));
lims.push(Limitation::new("no_credentials", "user:password parts of HTTP URIs are removed; query strings are kept as recorded."));
let counts = json!({
"protocol_stacks": inv.protocol_stacks.len(), "endpoints": inv.endpoints.len(), "dns_queries": inv.dns_queries.len(),
@@ -675,4 +675,42 @@ mod tests {
assert_eq!(inv.endpoints[0].value, "10.0.0.1");
assert_eq!(inv.endpoints[0].count, 3);
}
+
+ /// Both engines list a CNAME-chained answer the same way: the address, keyed by the question (#510).
+ #[test]
+ fn dns_answers_agree_between_engines_for_cname_chains() {
+ fn name(n: &str) -> Vec<u8> {
+ let mut v = Vec::new();
+ for p in n.split('.') {
+ v.push(p.len() as u8);
+ v.extend(p.as_bytes());
+ }
+ v.push(0);
+ v
+ }
+ fn rr(owner: &str, rtype: u16, data: &[u8]) -> Vec<u8> {
+ let mut v = name(owner);
+ v.extend(rtype.to_be_bytes());
+ v.extend([0, 1, 0, 0, 0, 60]);
+ v.extend((data.len() as u16).to_be_bytes());
+ v.extend(data);
+ v
+ }
+ let mut dns = vec![0x12, 0x34, 0x81, 0x80, 0, 1, 0, 3, 0, 0, 0, 0];
+ dns.extend(name("audit.example"));
+ dns.extend([0, 1, 0, 1]);
+ dns.extend(rr("audit.example", 5, &name("alias.example")));
+ dns.extend(rr("alias.example", 5, &name("final.example")));
+ dns.extend(rr("final.example", 1, &[203, 0, 113, 9]));
+ let frame = fx::eth(0x0800, &fx::ipv4_pkt(17, [10, 0, 0, 2], [10, 0, 0, 1], &fx::udp(53, 40000, &dns)));
+ let e = Engine::new(crate::testing::session());
+ let p = temp_file("cname.pcap", &fx::pcap(&[frame]));
+ let v = call(&e, "packets_inventory_show", json!({"path": p, "engine": "built_in"}));
+ let built_in: Vec<String> = v["result"]["dns_answers"].as_array().unwrap().iter().map(|c| c["value"].as_str().unwrap().to_string()).collect();
+
+ let line = "eth:ethertype:ip:udp:dns\t10.0.0.2\t10.0.0.1\t\t\t1\taudit.example\t1\t203.0.113.9\t\t\t\t\t\n";
+ let tshark: Vec<String> = aggregate_tshark(line).dns_answers.into_iter().map(|c| c.value).collect();
+ assert_eq!(tshark, ["audit.example A 203.0.113.9"]);
+ assert_eq!(built_in, tshark);
+ }
}
diff --git a/crates/formats/src/pcap.rs b/crates/formats/src/pcap.rs
index 5922f6f9..bb5d6ad7 100644
--- a/crates/formats/src/pcap.rs
+++ b/crates/formats/src/pcap.rs
@@ -1034,6 +1034,9 @@ pub struct Summary {
pub dns_queries: Vec<Count>,
/// "name TYPE value" from DNS responses.
pub dns_answers: Vec<Count>,
+ /// "question A|AAAA address" from DNS responses: each address answered, keyed by the response's first
+ /// question (so CNAME chains end at the name that was asked), as Wireshark's fields report them.
+ pub dns_addresses: Vec<Count>,
/// "METHOD host target" from HTTP/1.x requests.
pub http_requests: Vec<Count>,
/// Server names from TLS ClientHellos.
@@ -1082,6 +1085,7 @@ pub fn summarize(data: &[u8], cancelled: &dyn Fn() -> bool) -> Result<Summary, F
let mut flow_rows: Vec<Flow> = Vec::new();
let (mut protocols, mut stacks, mut endpoints) = (Tally::default(), Tally::default(), Tally::default());
let (mut queries, mut answers, mut http, mut sni, mut alpn) = (Tally::default(), Tally::default(), Tally::default(), Tally::default(), Tally::default());
+ let mut addresses = Tally::default();
for f in cap.by_ref() {
if f.number % 4096 == 0 && cancelled() {
return Err(FormatError::Malformed("cancelled".into()));
@@ -1151,6 +1155,13 @@ pub fn summarize(data: &[u8], cancelled: &dyn Fn() -> bool) -> Result<Summary, F
for a in &dns.answers {
answers.add(format!("{} {} {}", a.name, a.rtype, a.value));
}
+ if dns.response
+ && let Some(q) = dns.questions.first()
+ {
+ for a in dns.answers.iter().filter(|a| matches!(a.rtype.as_str(), "A" | "AAAA") && a.value.parse::<IpAddr>().is_ok()) {
+ addresses.add(format!("{} {} {}", q.name, a.rtype, a.value));
+ }
+ }
}
if let Some(h) = &d.http
&& h.kind == "request"
@@ -1175,6 +1186,7 @@ pub fn summarize(data: &[u8], cancelled: &dyn Fn() -> bool) -> Result<Summary, F
s.endpoints = endpoints.done("endpoints", &mut capped);
s.dns_queries = queries.done("dns_queries", &mut capped);
s.dns_answers = answers.done("dns_answers", &mut capped);
+ s.dns_addresses = addresses.done("dns_addresses", &mut capped);
s.http_requests = http.done("http_requests", &mut capped);
s.tls_sni = sni.done("tls_sni", &mut capped);
s.tls_alpn = alpn.done("tls_alpn", &mut capped);