syara-x 0.3.1

Super YARA — extends YARA-compatible rules with semantic, classifier, and LLM-based matching
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
use std::collections::HashMap;
use regex::{Regex, RegexBuilder};
use crate::models::{Modifier, MatchDetail, StringRule};
use crate::error::SyaraError;

/// Handles string and regex pattern matching with YARA modifier semantics.
/// Maintains a compiled-regex cache keyed by pattern + modifier set.
pub struct StringMatcher {
    cache: HashMap<String, Regex>,
}

impl StringMatcher {
    pub fn new() -> Self {
        Self {
            cache: HashMap::new(),
        }
    }

    fn cache_key(rule: &StringRule) -> String {
        let mut mods: Vec<&str> = rule
            .modifiers
            .iter()
            .map(|m| match m {
                Modifier::NoCase => "nocase",
                Modifier::Wide => "wide",
                Modifier::Ascii => "ascii",
                Modifier::Dotall => "dotall",
                Modifier::FullWord => "fullword",
            })
            .collect();
        mods.sort_unstable();
        format!("{}:{}", rule.pattern, mods.join(","))
    }

    fn compile(&mut self, rule: &StringRule) -> Result<(), SyaraError> {
        let key = Self::cache_key(rule);
        if self.cache.contains_key(&key) {
            return Ok(());
        }

        let nocase = rule.modifiers.contains(&Modifier::NoCase);
        let dotall = rule.modifiers.contains(&Modifier::Dotall);
        let fullword = rule.modifiers.contains(&Modifier::FullWord);

        let mut pattern = if rule.is_regex {
            rule.pattern.clone()
        } else {
            regex::escape(&rule.pattern)
        };

        if fullword {
            pattern = format!(r"\b(?:{})\b", pattern);
        }

        let regex = RegexBuilder::new(&pattern)
            .case_insensitive(nocase)
            .dot_matches_new_line(dotall)
            .build()
            .map_err(|e| SyaraError::InvalidPattern {
                pattern: rule.pattern.clone(),
                reason: e.to_string(),
            })?;

        self.cache.insert(key, regex);
        Ok(())
    }

    pub fn match_rule(
        &mut self,
        rule: &StringRule,
        text: &str,
    ) -> Result<Vec<MatchDetail>, SyaraError> {
        let search_wide = rule.modifiers.contains(&Modifier::Wide);
        let search_ascii = rule.modifiers.contains(&Modifier::Ascii) || !search_wide;

        let mut details = Vec::new();

        if search_ascii {
            self.compile(rule)?;
            let key = Self::cache_key(rule);
            let regex = &self.cache[&key];
            for m in regex.find_iter(text) {
                details.push(
                    MatchDetail::new(rule.identifier.clone(), m.as_str())
                        .with_position(m.start(), m.end()),
                );
            }
        }

        if search_wide {
            details.extend(self.match_wide(rule, text)?);
        }

        Ok(details)
    }

    fn match_wide(
        &mut self,
        rule: &StringRule,
        text: &str,
    ) -> Result<Vec<MatchDetail>, SyaraError> {
        let nocase = rule.modifiers.contains(&Modifier::NoCase);
        let fullword = rule.modifiers.contains(&Modifier::FullWord);
        let mut details = Vec::new();

        if rule.is_regex {
            // Build position map from stripped text back to original
            let mut stripped = String::new();
            let mut pos_map: Vec<usize> = Vec::new(); // stripped byte idx -> original byte idx
            for (orig_idx, c) in text.char_indices() {
                if c != '\x00' {
                    for _ in 0..c.len_utf8() {
                        pos_map.push(orig_idx);
                    }
                    stripped.push(c);
                }
            }
            // Sentinel for end positions
            pos_map.push(text.len());

            let wide_key = format!("{}:wide_regex", Self::cache_key(rule));
            if !self.cache.contains_key(&wide_key) {
                let mut pattern = rule.pattern.clone();
                if fullword {
                    pattern = format!(r"\b(?:{})\b", pattern);
                }
                let regex = RegexBuilder::new(&pattern)
                    .case_insensitive(nocase)
                    .build()
                    .map_err(|e| SyaraError::InvalidPattern {
                        pattern: rule.pattern.clone(),
                        reason: e.to_string(),
                    })?;
                self.cache.insert(wide_key.clone(), regex);
            }
            let regex = &self.cache[&wide_key];
            for m in regex.find_iter(&stripped) {
                let orig_start = pos_map.get(m.start()).copied().unwrap_or(0);
                let orig_end = pos_map.get(m.end()).copied().unwrap_or(text.len());
                details.push(
                    MatchDetail::new(rule.identifier.clone(), m.as_str())
                        .with_position(orig_start, orig_end),
                );
            }
        } else {
            let wide_key = format!("{}:wide_literal", Self::cache_key(rule));
            if !self.cache.contains_key(&wide_key) {
                let wide: String = rule
                    .pattern
                    .chars()
                    .flat_map(|c| [c, '\x00'])
                    .collect();
                let mut pattern = regex::escape(&wide);
                if fullword {
                    pattern = format!(r"\b(?:{})\b", pattern);
                }
                let regex = RegexBuilder::new(&pattern)
                    .case_insensitive(nocase)
                    .build()
                    .map_err(|e| SyaraError::InvalidPattern {
                        pattern: rule.pattern.clone(),
                        reason: e.to_string(),
                    })?;
                self.cache.insert(wide_key.clone(), regex);
            }
            let regex = &self.cache[&wide_key];
            for m in regex.find_iter(text) {
                details.push(
                    MatchDetail::new(rule.identifier.clone(), m.as_str())
                        .with_position(m.start(), m.end()),
                );
            }
        }

        Ok(details)
    }

    pub fn clear_cache(&mut self) {
        self.cache.clear();
    }
}

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

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

    fn literal(id: &str, pattern: &str, mods: Vec<Modifier>) -> StringRule {
        StringRule {
            identifier: id.to_string(),
            pattern: pattern.to_string(),
            modifiers: mods,
            is_regex: false,
        }
    }

    fn regex_rule(id: &str, pattern: &str, mods: Vec<Modifier>) -> StringRule {
        StringRule {
            identifier: id.to_string(),
            pattern: pattern.to_string(),
            modifiers: mods,
            is_regex: true,
        }
    }

    // ── BUG-002: fullword modifier must add word boundaries ─────────────

    #[test]
    fn test_fullword_literal_no_substring_match() {
        let mut matcher = StringMatcher::new();
        let rule = literal("$s1", "cat", vec![Modifier::FullWord]);

        // "cat" as a standalone word
        let hits = matcher.match_rule(&rule, "the cat sat").unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].matched_text, "cat");

        // "cat" as substring of "concatenate" — must NOT match
        let hits = matcher.match_rule(&rule, "concatenate").unwrap();
        assert!(hits.is_empty(), "fullword must not match substrings");
    }

    #[test]
    fn test_fullword_regex() {
        let mut matcher = StringMatcher::new();
        let rule = regex_rule("$r1", "err(or)?", vec![Modifier::FullWord]);

        let hits = matcher.match_rule(&rule, "an error occurred").unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].matched_text, "error");

        // Should not match inside "erroneous"
        let hits = matcher.match_rule(&rule, "erroneous").unwrap();
        assert!(hits.is_empty(), "fullword regex must not match substrings");
    }

    #[test]
    fn test_no_fullword_matches_substring() {
        let mut matcher = StringMatcher::new();
        let rule = literal("$s1", "cat", vec![]);

        let hits = matcher.match_rule(&rule, "concatenate").unwrap();
        assert_eq!(hits.len(), 1, "without fullword, substrings should match");
    }

    // ── BUG-026: wide regex positions map to original text ──────────────

    #[test]
    fn test_wide_regex_positions_map_to_original() {
        let mut matcher = StringMatcher::new();
        let rule = regex_rule("$r1", "he.lo", vec![Modifier::Wide]);

        // Simulate wide-encoded text: "hello" with null bytes interleaved
        let wide_text = "h\x00e\x00l\x00l\x00o\x00";
        let hits = matcher.match_rule(&rule, wide_text).unwrap();
        assert!(!hits.is_empty(), "wide regex should match null-stripped text");

        // Positions should reference the original wide text, not the stripped version
        let h = &hits[0];
        assert!(h.start_pos.is_some());
        assert!(h.end_pos.is_some());
        let start = h.start_pos.unwrap();
        let end = h.end_pos.unwrap();
        // The match spans the original wide-encoded bytes
        assert!(end > start);
        assert!(end <= wide_text.len());
    }

    #[test]
    fn test_wide_literal_match() {
        let mut matcher = StringMatcher::new();
        let rule = literal("$s1", "AB", vec![Modifier::Wide]);

        let wide_text = "A\x00B\x00";
        let hits = matcher.match_rule(&rule, wide_text).unwrap();
        assert_eq!(hits.len(), 1);
    }

    // ── Basic modifier tests ────────────────────────────────────────────

    #[test]
    fn test_literal_match() {
        let mut matcher = StringMatcher::new();
        let rule = literal("$s1", "hello", vec![]);
        let hits = matcher.match_rule(&rule, "say hello world").unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].matched_text, "hello");
        assert_eq!(hits[0].start_pos, Some(4));
        assert_eq!(hits[0].end_pos, Some(9));
    }

    #[test]
    fn test_regex_match() {
        let mut matcher = StringMatcher::new();
        let rule = regex_rule("$r1", r"\d{3}-\d{4}", vec![]);
        let hits = matcher.match_rule(&rule, "call 555-1234 now").unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].matched_text, "555-1234");
    }

    #[test]
    fn test_nocase_modifier() {
        let mut matcher = StringMatcher::new();
        let rule = literal("$s1", "hello", vec![Modifier::NoCase]);
        let hits = matcher.match_rule(&rule, "say HELLO there").unwrap();
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn test_dotall_modifier() {
        let mut matcher = StringMatcher::new();
        let rule = regex_rule("$r1", "start.*end", vec![Modifier::Dotall]);
        let hits = matcher.match_rule(&rule, "start\nmiddle\nend").unwrap();
        assert_eq!(hits.len(), 1, "dotall should make . match newlines");
    }

    #[test]
    fn test_dotall_without_flag_no_newline() {
        let mut matcher = StringMatcher::new();
        let rule = regex_rule("$r1", "start.*end", vec![]);
        let hits = matcher.match_rule(&rule, "start\nend").unwrap();
        assert!(hits.is_empty(), "without dotall, . should not match newlines");
    }

    #[test]
    fn test_multiple_matches() {
        let mut matcher = StringMatcher::new();
        let rule = literal("$s1", "ab", vec![]);
        let hits = matcher.match_rule(&rule, "ab cd ab ef ab").unwrap();
        assert_eq!(hits.len(), 3);
    }

    #[test]
    fn test_no_match_returns_empty() {
        let mut matcher = StringMatcher::new();
        let rule = literal("$s1", "xyz", vec![]);
        let hits = matcher.match_rule(&rule, "nothing here").unwrap();
        assert!(hits.is_empty());
    }

    #[test]
    fn test_special_chars_escaped_in_literal() {
        let mut matcher = StringMatcher::new();
        // Literal "a.b" should not match "axb" — the dot must be escaped
        let rule = literal("$s1", "a.b", vec![]);
        let hits = matcher.match_rule(&rule, "axb").unwrap();
        assert!(hits.is_empty());
        let hits = matcher.match_rule(&rule, "a.b").unwrap();
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn test_cache_reuse() {
        let mut matcher = StringMatcher::new();
        let rule = literal("$s1", "test", vec![]);
        // First call compiles and caches
        let _ = matcher.match_rule(&rule, "test").unwrap();
        assert!(!matcher.cache.is_empty());
        // Second call uses cache — same result
        let hits = matcher.match_rule(&rule, "test again test").unwrap();
        assert_eq!(hits.len(), 2);
    }

    #[test]
    fn test_clear_cache() {
        let mut matcher = StringMatcher::new();
        let rule = literal("$s1", "x", vec![]);
        let _ = matcher.match_rule(&rule, "x").unwrap();
        assert!(!matcher.cache.is_empty());
        matcher.clear_cache();
        assert!(matcher.cache.is_empty());
    }

    #[test]
    fn test_wide_and_ascii_combined() {
        // When both wide and ascii are specified, both searches run
        let mut matcher = StringMatcher::new();
        let rule = literal("$s1", "AB", vec![Modifier::Wide, Modifier::Ascii]);
        // Text contains both plain "AB" and wide "A\0B\0"
        let text = "AB and A\x00B\x00";
        let hits = matcher.match_rule(&rule, text).unwrap();
        // Should get at least the ASCII match
        assert!(hits.len() >= 1);
    }

    /// Inline `(?m)` parity: in multiline mode `^` anchors after newlines,
    /// not just at the start of input. Confirms the regex crate's flag
    /// syntax is honoured end-to-end through StringMatcher.
    #[test]
    fn test_multiline_inline_flag() {
        let mut matcher = StringMatcher::new();
        let rule = regex_rule("$s", "(?m)^foo", vec![]);

        let hits = matcher.match_rule(&rule, "bar\nfoo").unwrap();
        assert_eq!(hits.len(), 1, "(?m)^foo must match after a newline");

        let hits = matcher.match_rule(&rule, "barfoo").unwrap();
        assert!(hits.is_empty(), "^ must not match inside a line");
    }
}