Craft Anything: understand how any software works, and craft your own.
Rust
Report malformed ClientHello offers and retain unknown TLS versions (R3.12) (#1089)
Afloat6cbc66984c51
diff --git a/crates/formats/src/pcap.rs b/crates/formats/src/pcap.rsindex d82dc8fe..695256f8 100644--- a/crates/formats/src/pcap.rs+++ b/crates/formats/src/pcap.rs@@ -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) {diff --git a/crates/formats/tests/regression_tls_bounds.rs b/crates/formats/tests/regression_tls_bounds.rsindex 674ff794..b616df15 100644--- a/crates/formats/tests/regression_tls_bounds.rs+++ b/crates/formats/tests/regression_tls_bounds.rs@@ -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);+}diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex 207d8583..30d62db5 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -1218,6 +1218,8 @@ Owner: open · Status: in progressROMFS dot roots, cramfs, ext and JFFS2 sizes and holes, PyInstaller modules outside theirPYZ, JSON config edits (comments kept, every duplicate key removed), and UDP, TCP, DNS overTCP, pcapng and TLS lengths (#688–#715, by Afloat16)+- [x] Check ClientHello vector lengths and extension values before reporting the handshake as complete.+ — claimed: Afloat16, branch bugfix/r3.12-tls-client-hello-values- [x] Address the combined parser review: preserve partial Java reference coverage,enforce DEX signed 32-bit LEB128 limits, recover legacy AAPT UTF-8 lengths withindiff --git a/docs/STATUS.md b/docs/STATUS.mdindex f221504b..7c436a8a 100644--- a/docs/STATUS.md+++ b/docs/STATUS.md@@ -19,6 +19,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi| Stop decoding extension headers inside noninitial IPv6 fragments | Noninitial IPv6 fragments no longer supply fabricated extension or transport headers. | Synthetic byte fixtures in `pcap_ipv6_fragments` integration regressions. Cover payloads resembling a second fragment header or destination-options header, plus valid first and atomic fragments. || Enforce the expanded DNS name wire-length limit | DNS names obey the 255-byte expanded wire limit. Invalid name-valued RDATA marks parsed answers incomplete; legal empty ANY/NONE UPDATE prerequisites remain supported. | Synthetic byte fixtures in `pcap_dns_name_limit` integration regressions. Cover plain and compressed names at the wire limit, literal-prefix compression overflow in questions and NS/CNAME/PTR/MX records, record boundaries, root names, and legal empty UPDATE prerequisites followed by another record. Coverage is limited to the parsed questions and first 64 answer or UPDATE prerequisite records. || Report truncated DNS TXT strings as incomplete | TXT validation covers at most 64 parsed answers, or prerequisites for DNS UPDATE; authority and additional records are not inspected. Empty ANY/NONE UPDATE prerequisites with zero TTL and empty TXT records for wire-identified DNS-SD instance owners in ordinary multicast/unicast DNS are retained. | Synthetic byte fixtures in `pcap_dns_txt_lengths` integration regressions. Cover a cut first string, a cut later string, a missing string body, and valid empty or multiple strings. Review regressions preserve empty UPDATE prerequisites and following records, while checking opcode, class, TTL, and truncated-string boundaries. DNS-SD cases cover known answers, cache-flush records, compressed owners, literal dots and case, plus ordinary/invalid owners and nonempty truncation. |+| TLS ClientHello offers | Packet inspection validates session IDs, cipher-suite and compression vectors, SNI, ALPN and supported versions. It keeps the first copy of a repeated extension, distinguishes reserved GREASE from unknown versions, and checks ALPN beyond the display limit. | Synthetic captures exercise invalid offers and complete legacy and modern handshakes. || Read HTTP Host fields only from the header section | HTTP Host values come only from complete request header lines. | Synthetic byte fixtures in `pcap_http_host_boundary` integration regressions. Cover forged Host lines in bodies, invalid UTF-8 before Host, mixed-case field names, and incomplete header lines. || Keep capture flow timestamps in chronological order | Flow time ranges use the earliest and latest available timestamps. | Synthetic byte fixtures in `pcap_flow_time_range` integration regressions. Cover out-of-order timestamps and a flow that starts in an untimestamped simple packet block. || Reject incomplete simple packet block payloads | Simple packet blocks validate captured lengths and exclude alignment padding. Rejected lengths count toward the packet scan limit and produce one summary warning when the walk ends. | Synthetic byte fixtures in `pcapng_simple_payload` integration regressions. Cover short payloads, excess payload bytes, unlimited snaplen, legitimate snaplen truncation, alignment padding, bounded warnings, and later valid frames. Unit regressions exercise rejected-only and mixed packet budgets, exact EOF, and repeated iterator exhaustion. |