zond-engine 0.18.0

The core networking library powering Zond. Raw packet injection, protocol parsing, service and OS fingerprinting, and asynchronous network scanning.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
//
// This file is part of Zond Engine, licensed under the GNU Affero General
// Public License, version 3 or later. See the LICENSE file for details, or
// <https://www.gnu.org/licenses/agpl-3.0.html>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! # The masking [`Redaction::Standard`](super::Redaction::Standard) applies
//!
//! One function per thing a report carries that names a person or a device: a
//! hostname and a hardware address. Beside them, crate-private, the mechanics
//! [`HostRedaction`](super::HostRedaction) applies to free text a host's own
//! replies filled: finding the names that host gave, and telling a reply that
//! is text from one that is not.
//!
//! [`Redaction`](super::Redaction) states the policy and its limits, including
//! why addresses are not masked. Read that first; these are the mechanics.
//!
//! The goal is not anonymity. A masked hostname stays distinguishable from the
//! next one so a reader can still follow a host through a report, and that is
//! the whole of what is promised.

use std::borrow::Cow;

use crate::model::mac::MacAddr;

/// Masks a hostname, keeping its first and last two characters so two masked
/// names still read as two names.
///
/// The middle becomes a fixed run of `X` rather than one per character, so the
/// length goes with it: `router` and a fifty-character name both mask to nine
/// characters.
///
/// A name with fewer than six characters is masked whole, and is the one case
/// that does not come out nine wide. Keeping two at each end of a five-character
/// name would leave four of its five, so a short name loses its ends as well.
///
/// # Examples
/// ```
/// use zond_engine::export::redact;
///
/// assert_eq!(redact::hostname("gateway.local"), "gaXXXXXal");
/// assert_eq!(redact::hostname("workstation"), "woXXXXXon");
/// assert_eq!(redact::hostname("router"), "roXXXXXer");
/// assert_eq!(redact::hostname("modem"), "XXXXX");
/// assert_eq!(redact::hostname("pc"), "XXXXX");
/// ```
pub fn hostname(name: &str) -> String {
    /// Below this, the two kept at each end are most of the name.
    const SHORTEST_WORTH_KEEPING_ENDS_OF: usize = 6;

    let char_count = name.chars().count();

    if char_count < SHORTEST_WORTH_KEEPING_ENDS_OF {
        return "XXXXX".to_string();
    }

    let first_two: String = name.chars().take(2).collect();
    let last_two: String = name
        .chars()
        .rev()
        .take(2)
        .collect::<String>()
        .chars()
        .rev()
        .collect();

    format!("{first_two}XXXXX{last_two}")
}

/// Masks a hardware address, keeping the OUI.
///
/// The first three octets are the vendor and the last three are the individual
/// card, so this is the cut that leaves a report saying what a device is made
/// by without saying which one it is.
///
/// # Examples
/// ```
/// use zond_engine::model::mac::MacAddr;
/// use zond_engine::export::redact;
///
/// let mac = MacAddr::new(0x2c, 0xcf, 0x67, 0x00, 0x00, 0x01);
/// assert_eq!(redact::mac_addr(&mac), "2c:cf:67:XX:XX:XX");
/// ```
pub fn mac_addr(mac: &MacAddr) -> String {
    let octets = mac.octets();
    format!(
        "{:02x}:{:02x}:{:02x}:XX:XX:XX",
        octets[0], octets[1], octets[2]
    )
}

/// What stands in for an excerpt that is not text, under redaction.
///
/// A binary reply carries names in forms a search of the text cannot be sure
/// to find: a NetBIOS name in half-ASCII, a DNS name as length-prefixed labels,
/// a realm inside DER, a name never recorded as one at all. Its bytes are also
/// what makes such an excerpt worth reading, so a masked copy of them is
/// neither safe nor useful, and the finding keeps its title and its claim
/// without them.
pub(crate) const WITHHELD_EXCERPT: &str = "(binary reply withheld under redaction)";

/// Whether `text` holds a character no text reply carries: a control other
/// than a tab or a line break, as a reply read byte for byte has wherever the
/// protocol is binary.
pub(crate) fn is_binary(text: &str) -> bool {
    text.chars()
        .any(|c| c.is_control() && !matches!(c, '\t' | '\n' | '\r'))
}

/// Labels a registry puts under a country's top-level domain for anyone to
/// register beneath, as in `example.com.au` or `example.gov.uk`: part of the
/// suffix rather than of a name, so never masked on their own. See
/// [`Needle::for_names`].
const GENERIC_LABELS: &[&str] = &[
    "com", "net", "org", "edu", "gov", "mil", "int", "ltd", "plc",
];

/// One name to find in free text, with the mask it is replaced by.
#[derive(Debug, Clone)]
pub(crate) struct Needle {
    chars: Vec<char>,
    mask: String,
}

impl Needle {
    /// The needles for the names a host is known by: each name whole, the
    /// first label of a dotted one, which is the machine's own name and the
    /// form a banner or a NetBIOS reply gives it in, and every label between
    /// that and the last. A label that is also a name in its own right is
    /// masked as that name is, so a NetBIOS domain reads the same in the text
    /// as in its field. Longest first, so a whole name is masked before any
    /// label of it could be.
    ///
    /// The labels between, because text spells a name in other ways than
    /// dotted: `DC=corp,DC=example` in a directory's reply, `%2E` between
    /// labels in a URL, a realm upper-cased on its own. Each label of a domain
    /// is part of what names the organisation, and one left standing reads it
    /// out. Three exceptions, each a label that names no one: the last, which
    /// is the top-level domain and is shared by millions; one of fewer than
    /// three characters, which as a word of its own is too often an ordinary
    /// one (`co`, `ad`, `us`) for masking it to leave the text readable; and
    /// the handful of generic second-level labels a country's registry puts
    /// under its own, [`GENERIC_LABELS`], which would otherwise mask `com` in
    /// every URL of a report on `example.com.au`.
    pub(crate) fn for_names<'a>(names: impl IntoIterator<Item = &'a str>) -> Vec<Needle> {
        let names: Vec<&str> = names.into_iter().collect();
        let mut needles: Vec<Needle> = Vec::new();
        let mut push = |name: &str| {
            let name = name.trim();
            if name.is_empty()
                || needles
                    .iter()
                    .any(|needle| same_letters(&needle.chars, name))
            {
                return;
            }
            needles.push(Needle {
                chars: name.chars().collect(),
                mask: hostname(name),
            });
        };
        for name in &names {
            push(name);
        }
        for name in &names {
            if let Some((label, _)) = name.split_once('.') {
                push(label);
            }
        }
        for name in &names {
            let labels: Vec<&str> = name.trim().split('.').collect();
            for label in labels.iter().take(labels.len().saturating_sub(1)).skip(1) {
                if label.chars().count() >= 3
                    && !GENERIC_LABELS
                        .iter()
                        .any(|generic| generic.eq_ignore_ascii_case(label))
                {
                    push(label);
                }
            }
        }
        needles.sort_by_key(|needle| std::cmp::Reverse(needle.chars.len()));
        needles
    }

    /// How many characters of `text` from `at` this name takes up, if it is
    /// there: as itself, a word of its own and in any case, or as UTF-16LE read
    /// a byte to a character, each letter followed by a NUL, which is how SMB,
    /// NTLM and Kerberos carry a name and how a reply read byte for byte holds
    /// it.
    fn at(&self, text: &[char], at: usize) -> Option<usize> {
        let len = self.chars.len();
        let rest = &text[at..];

        let plain = rest.len() >= len
            && rest
                .iter()
                .zip(&self.chars)
                .all(|(a, b)| same_letter(*a, *b))
            && (at == 0 || !text[at - 1].is_alphanumeric() || after_escape(text, at))
            && rest.get(len).is_none_or(|c| !c.is_alphanumeric());
        if plain {
            return Some(len);
        }

        let wide = rest.len() >= 2 * len
            && self
                .chars
                .iter()
                .enumerate()
                .all(|(i, c)| same_letter(rest[2 * i], *c) && rest[2 * i + 1] == '\0');
        wide.then_some(2 * len)
    }
}

/// Whether the three characters before `at` are a URL's percent escape, such
/// as the `%2E` a URL can spell a dot as. It ends in a letter or a digit, and
/// yet it is a separator, so a name after it starts a word of its own.
fn after_escape(text: &[char], at: usize) -> bool {
    at >= 3 && text[at - 3] == '%' && text[at - 2..at].iter().all(char::is_ascii_hexdigit)
}

/// Masks every name `needles` holds wherever it appears in `text`, borrowing
/// when none does.
pub(crate) fn names_in<'a>(text: &'a str, needles: &[Needle]) -> Cow<'a, str> {
    if needles.is_empty() {
        return Cow::Borrowed(text);
    }

    let chars: Vec<char> = text.chars().collect();
    let mut masked = String::with_capacity(text.len());
    let mut changed = false;
    let mut at = 0;
    while at < chars.len() {
        match needles
            .iter()
            .find_map(|needle| needle.at(&chars, at).map(|len| (len, needle)))
        {
            Some((len, needle)) => {
                masked.push_str(&needle.mask);
                at += len;
                changed = true;
            }
            None => {
                masked.push(chars[at]);
                at += 1;
            }
        }
    }

    if changed {
        Cow::Owned(masked)
    } else {
        Cow::Borrowed(text)
    }
}

/// Two letters that are one in any case.
fn same_letter(a: char, b: char) -> bool {
    a == b || a.to_lowercase().eq(b.to_lowercase())
}

/// Two names that are one in any case.
fn same_letters(a: &[char], b: &str) -> bool {
    a.len() == b.chars().count() && a.iter().zip(b.chars()).all(|(x, y)| same_letter(*x, y))
}

// ╔════════════════════════════════════════════╗
// ║ ████████╗███████╗███████╗████████╗███████╗ ║
// ║ ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝ ║
// ║    ██║   █████╗  ███████╗   ██║   ███████╗ ║
// ║    ██║   ██╔══╝  ╚════██║   ██║   ╚════██║ ║
// ║    ██║   ███████╗███████║   ██║   ███████║ ║
// ║    ╚═╝   ╚══════╝╚══════╝   ╚═╝   ╚══════╝ ║
// ╚════════════════════════════════════════════╝

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn an_address_of_every_octet_keeps_the_three_that_name_a_vendor() {
        let mac = MacAddr::new(0xff, 0xff, 0xff, 0x00, 0x11, 0x22);
        assert_eq!(mac_addr(&mac), "ff:ff:ff:XX:XX:XX");
    }

    /// A masked name is nine characters whatever went in, so the mask hides the
    /// length as well as the letters. The short case is the documented
    /// exception and is five.
    #[test]
    fn masking_a_name_hides_how_long_it_was() {
        for name in ["router", "workstation", &"a".repeat(50)] {
            assert_eq!(hostname(name).chars().count(), 9, "{name}");
        }

        assert_eq!(hostname("modem"), "XXXXX");
    }

    /// A name is found as a word of its own and in any case, and a word it is
    /// only part of is left alone: `FS01` names the machine, `FS010` does not.
    #[test]
    fn a_name_is_masked_as_a_word_in_any_case() {
        let needles = Needle::for_names(["fs01.example.com", "EXAMPLE"]);

        assert_eq!(
            names_in("220 FS01.EXAMPLE.COM ready", &needles),
            "220 fsXXXXXom ready"
        );
        assert_eq!(
            names_in("CN=example-FS01-CA", &needles),
            "CN=EXXXXXXLE-XXXXX-CA"
        );
        assert!(matches!(
            names_in("FS010 and examples", &needles),
            Cow::Borrowed(_)
        ));
    }

    /// **Each label of a domain between its first and its last is masked on
    /// its own**, since a directory's reply spells a domain as
    /// `DC=corp,DC=example` and a URL as `%2E`-joined labels, and a label
    /// left standing names the organisation. The top-level label, a label of
    /// under three characters and a generic second-level one stay, being no
    /// one's name.
    #[test]
    fn each_label_between_the_first_and_the_last_is_masked() {
        let needles = Needle::for_names(["fs01.ad.contoso.com.au"]);

        assert_eq!(
            names_in("DC=ad,DC=contoso,DC=com,DC=au", &needles),
            "DC=ad,DC=coXXXXXso,DC=com,DC=au"
        );
        assert_eq!(
            names_in("GET /%2Econtoso%2Ecom%2Eau", &needles),
            "GET /%2EcoXXXXXso%2Ecom%2Eau"
        );
    }

    /// SMB, NTLM and Kerberos carry a name in UTF-16LE, which a reply read a
    /// byte to a character holds as each letter followed by a NUL.
    #[test]
    fn a_name_in_utf16_is_masked() {
        let needles = Needle::for_names(["EXAMPLE"]);

        assert_eq!(
            names_in("\u{ff}SMBe\0x\0a\0m\0p\0l\0e\0\0\0", &needles),
            "\u{ff}SMBEXXXXXXLE\0\0"
        );
    }

    /// A reply is text when it holds nothing but printable characters and
    /// line breaks, and binary as soon as it holds any other control.
    #[test]
    fn a_reply_holding_a_control_is_binary() {
        assert!(!is_binary("HTTP/1.1 200 OK\r\n\tServer: x\n"));
        assert!(!is_binary("caf\u{e9}"));
        assert!(is_binary("\0\0\0\x55\u{ff}SMB"));
        assert!(is_binary("\u{1b}[31m"));
    }
}

#[cfg(test)]
mod property_tests {
    use super::*;
    use proptest::prelude::*;

    proptest::proptest! {
        /// A masked name keeps its two ends and nothing between them, whatever
        /// was there and however much of it.
        #[test]
        fn a_masked_hostname_keeps_its_ends_and_loses_its_middle(
            name in "[a-zA-Z0-9.-]{6,64}"
        ) {
            let redacted = hostname(&name);
            prop_assert!(redacted.contains("XXXXX"));
            prop_assert!(redacted.starts_with(&name[..2]));
            prop_assert!(redacted.ends_with(&name[name.len() - 2..]));
            prop_assert_eq!(redacted.len(), 9, "every masked name is one width");
        }

        /// And a name short enough that its ends would be most of it keeps
        /// neither.
        #[test]
        fn a_short_hostname_is_masked_whole(name in "[a-zA-Z0-9.-]{0,5}") {
            prop_assert_eq!(hostname(&name), "XXXXX");
        }

        /// The vendor survives and the card does not, for every address there
        /// is.
        #[test]
        fn a_masked_address_keeps_its_oui_and_loses_the_rest(
            o1 in 0..=255u8, o2 in 0..=255u8, o3 in 0..=255u8,
            o4 in 0..=255u8, o5 in 0..=255u8, o6 in 0..=255u8
        ) {
            let redacted = mac_addr(&MacAddr::new(o1, o2, o3, o4, o5, o6));
            // Built outside the assertion: `prop_assert!` re-expands its
            // expression through `format_args!`, which cannot capture from
            // around it.
            let oui = format!("{o1:02x}:{o2:02x}:{o3:02x}");

            prop_assert!(redacted.starts_with(&oui));
            prop_assert!(redacted.ends_with("XX:XX:XX"));
        }
    }
}