threatflux-string-analysis 0.2.2

Deterministic, configurable string analysis primitives for security tooling
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! String categorization functionality.

use crate::types::{
    AnalysisError, AnalysisResult, MAX_DESCRIPTION_BYTES, compact_string, validate_identifier,
};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::net::IpAddr;
use std::sync::LazyLock;

const MAX_CATEGORY_RULES: usize = 4_096;

static EMAIL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")
        .expect("the built-in email regex must compile")
});

/// Thread-safe predicate used by a [`CategoryRule`].
pub type CategoryMatcher = Box<dyn Fn(&str) -> bool + Send + Sync>;

/// A named category that can be assigned to a string.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct StringCategory {
    /// Stable category name.
    pub name: String,
    /// Optional parent category name.
    pub parent: Option<String>,
    /// Human-readable category description.
    pub description: String,
}

impl StringCategory {
    pub(crate) fn compact(mut self) -> Self {
        self.name = compact_string(self.name);
        self.parent = self.parent.map(compact_string);
        self.description = compact_string(self.description);
        self
    }
}

/// Rule for categorizing strings.
pub struct CategoryRule {
    /// Unique rule name.
    pub name: String,
    /// Predicate that determines whether a string matches.
    pub matcher: CategoryMatcher,
    /// Category assigned on a match.
    pub category: StringCategory,
    /// Priority; larger values are evaluated first.
    pub priority: i32,
}

impl CategoryRule {
    fn validate(&self) -> AnalysisResult<()> {
        validate_identifier("category rule", &self.name)?;
        validate_category(&self.category)
    }
}

/// Interface for deterministic, thread-safe string categorizers.
pub trait Categorizer: Send + Sync {
    /// Categorize a string in deterministic rule order.
    fn categorize(&self, value: &str) -> Vec<StringCategory>;

    /// Validate and add a uniquely named rule.
    fn add_rule(&mut self, rule: CategoryRule) -> AnalysisResult<()>;

    /// Remove an existing rule by name.
    fn remove_rule(&mut self, name: &str) -> AnalysisResult<()>;

    /// Return distinct categories in deterministic rule order.
    fn get_categories(&self) -> Vec<StringCategory>;
}

/// Default heuristic categorizer.
pub struct DefaultCategorizer {
    rules: Vec<CategoryRule>,
}

impl DefaultCategorizer {
    /// Create a categorizer with the built-in informational rules.
    pub fn new() -> Self {
        let mut categorizer = Self::empty();
        categorizer.add_default_rules();
        categorizer
    }

    /// Create a categorizer without built-in rules.
    pub fn empty() -> Self {
        Self { rules: Vec::new() }
    }

    fn add_default_rules(&mut self) {
        self.rules = vec![
            rule(
                "url",
                "url",
                "network",
                "URL or web address",
                100,
                |value| {
                    [
                        "http://",
                        "https://",
                        "ftp://",
                        "ssh://",
                        "telnet://",
                        "rdp://",
                    ]
                    .iter()
                    .any(|scheme| starts_with_ascii_case(value, scheme))
                },
            ),
            rule(
                "registry",
                "registry",
                "windows",
                "Windows registry key",
                95,
                |value| {
                    starts_with_ascii_case(value, "HKEY_")
                        || contains_ascii_case(value, "\\SOFTWARE\\")
                },
            ),
            rule(
                "ip_address",
                "ip_address",
                "network",
                "Syntactically valid IPv4 or IPv6 address",
                95,
                |value| value.parse::<IpAddr>().is_ok(),
            ),
            rule(
                "path",
                "path",
                "filesystem",
                "Absolute or drive-qualified file-system path",
                90,
                |value| {
                    value.starts_with('/')
                        || value.starts_with('\\')
                        || (value.len() >= 3
                            && value.as_bytes()[1] == b':'
                            && matches!(value.as_bytes()[2], b'\\' | b'/'))
                },
            ),
            rule(
                "api_call",
                "api_call",
                "system",
                "Known system API function name",
                90,
                is_known_api_call,
            ),
            rule(
                "library",
                "library",
                "binary",
                "Shared library or DLL name",
                85,
                |value| {
                    ends_with_ascii_case(value, ".dll")
                        || ends_with_ascii_case(value, ".so")
                        || ends_with_ascii_case(value, ".dylib")
                        || contains_ascii_case(value, ".so.")
                },
            ),
            rule(
                "email",
                "email",
                "contact",
                "Email-address-shaped string",
                85,
                |value| EMAIL_REGEX.is_match(value),
            ),
            rule(
                "command",
                "command",
                "execution",
                "Command or shell interpreter reference",
                80,
                is_command_reference,
            ),
        ];
        sort_rules(&mut self.rules);
    }
}

impl Categorizer for DefaultCategorizer {
    fn categorize(&self, value: &str) -> Vec<StringCategory> {
        let mut seen = BTreeSet::new();
        let mut categories = Vec::new();
        for rule in &self.rules {
            if (rule.matcher)(value) && seen.insert(rule.category.name.clone()) {
                categories.push(rule.category.clone());
            }
        }

        if categories.is_empty() {
            categories.push(StringCategory {
                name: "generic".to_string(),
                parent: None,
                description: "Generic string".to_string(),
            });
        }
        categories
    }

    fn add_rule(&mut self, mut rule: CategoryRule) -> AnalysisResult<()> {
        rule.validate()?;
        if self.rules.len() >= MAX_CATEGORY_RULES {
            return Err(AnalysisError::CapacityExceeded {
                resource: "category rules",
                limit: MAX_CATEGORY_RULES,
            });
        }
        if self.rules.iter().any(|existing| existing.name == rule.name) {
            return Err(AnalysisError::DuplicateName {
                kind: "category rule",
                name: rule.name.to_string(),
            });
        }
        rule.name = compact_string(rule.name);
        rule.category = rule.category.compact();
        self.rules.push(rule);
        sort_rules(&mut self.rules);
        Ok(())
    }

    fn remove_rule(&mut self, name: &str) -> AnalysisResult<()> {
        validate_identifier("category rule", name)?;
        let Some(index) = self.rules.iter().position(|rule| rule.name == name) else {
            return Err(AnalysisError::NotFound {
                kind: "category rule",
                name: name.to_string(),
            });
        };
        self.rules.remove(index);
        Ok(())
    }

    fn get_categories(&self) -> Vec<StringCategory> {
        let mut seen = BTreeSet::new();
        self.rules
            .iter()
            .filter(|rule| seen.insert(rule.category.name.clone()))
            .map(|rule| rule.category.clone())
            .collect()
    }
}

impl Default for DefaultCategorizer {
    fn default() -> Self {
        Self::new()
    }
}

pub(crate) fn validate_category(category: &StringCategory) -> AnalysisResult<()> {
    validate_identifier("category", &category.name)?;
    if let Some(parent) = &category.parent {
        validate_identifier("parent category", parent)?;
    }
    if category.description.len() > MAX_DESCRIPTION_BYTES {
        return Err(AnalysisError::InputTooLarge {
            field: "category.description",
            actual: category.description.len(),
            limit: MAX_DESCRIPTION_BYTES,
        });
    }
    if category.description.trim().is_empty() {
        return Err(AnalysisError::InvalidIdentifier {
            kind: "category description",
            name: category.description.clone(),
            reason: "must not be empty or whitespace-only",
        });
    }
    if category.description.chars().any(char::is_control) {
        return Err(AnalysisError::InvalidIdentifier {
            kind: "category description",
            name: category.description.to_string(),
            reason: "must not contain control characters",
        });
    }
    Ok(())
}

fn sort_rules(rules: &mut [CategoryRule]) {
    rules.sort_by(|left, right| {
        right
            .priority
            .cmp(&left.priority)
            .then_with(|| left.name.cmp(&right.name))
    });
}

fn rule(
    rule_name: &str,
    category_name: &str,
    parent: &str,
    description: &str,
    priority: i32,
    matcher: impl Fn(&str) -> bool + Send + Sync + 'static,
) -> CategoryRule {
    CategoryRule {
        name: rule_name.to_string(),
        matcher: Box::new(matcher),
        category: StringCategory {
            name: category_name.to_string(),
            parent: Some(parent.to_string()),
            description: description.to_string(),
        },
        priority,
    }
}

fn is_command_reference(value: &str) -> bool {
    value
        .split(|character: char| !character.is_ascii_alphanumeric() && character != '.')
        .any(|token| {
            [
                "cmd",
                "cmd.exe",
                "powershell",
                "powershell.exe",
                "pwsh",
                "pwsh.exe",
                "bash",
                "dash",
                "zsh",
                "ksh",
                "sh",
            ]
            .iter()
            .any(|command| token.eq_ignore_ascii_case(command))
        })
}

fn starts_with_ascii_case(value: &str, prefix: &str) -> bool {
    value
        .as_bytes()
        .get(..prefix.len())
        .is_some_and(|candidate| candidate.eq_ignore_ascii_case(prefix.as_bytes()))
}

fn ends_with_ascii_case(value: &str, suffix: &str) -> bool {
    value
        .as_bytes()
        .get(value.len().saturating_sub(suffix.len())..)
        .is_some_and(|candidate| candidate.eq_ignore_ascii_case(suffix.as_bytes()))
}

fn contains_ascii_case(value: &str, needle: &str) -> bool {
    value
        .as_bytes()
        .windows(needle.len())
        .any(|candidate| candidate.eq_ignore_ascii_case(needle.as_bytes()))
}

fn is_known_api_call(value: &str) -> bool {
    matches!(
        value,
        "CreateProcess"
            | "CreateProcessA"
            | "CreateProcessW"
            | "VirtualAlloc"
            | "VirtualAllocEx"
            | "WriteProcessMemory"
            | "GetProcAddress"
            | "LoadLibrary"
            | "LoadLibraryA"
            | "LoadLibraryW"
            | "OpenProcess"
            | "CreateRemoteThread"
            | "malloc"
            | "calloc"
            | "realloc"
            | "free"
            | "fork"
            | "exec"
            | "open"
            | "read"
            | "write"
    )
}

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

    #[test]
    fn added_rules_compact_owned_metadata() {
        let mut name = String::with_capacity(1_000_000);
        name.push_str("compact_rule");
        let mut category_name = String::with_capacity(1_000_000);
        category_name.push_str("compact_category");
        let mut parent = String::with_capacity(1_000_000);
        parent.push_str("parent");
        let mut description = String::with_capacity(1_000_000);
        description.push_str("Category compaction regression");
        let mut categorizer = DefaultCategorizer::empty();
        categorizer
            .add_rule(CategoryRule {
                name,
                matcher: Box::new(|_| true),
                category: StringCategory {
                    name: category_name,
                    parent: Some(parent),
                    description,
                },
                priority: 0,
            })
            .unwrap();

        let rule = &categorizer.rules[0];
        assert_eq!(rule.name.capacity(), rule.name.len());
        assert_eq!(rule.category.name.capacity(), rule.category.name.len());
        assert_eq!(
            rule.category.parent.as_ref().unwrap().capacity(),
            rule.category.parent.as_ref().unwrap().len()
        );
        assert_eq!(
            rule.category.description.capacity(),
            rule.category.description.len()
        );
    }
}