Skip to content

Commit 45a6b09

Browse files
authored
fix invalid escape when string ends with \ (#21)
1 parent bfb4ff0 commit 45a6b09

File tree

2 files changed

+53
-35
lines changed

2 files changed

+53
-35
lines changed

src/field.rs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::error::ParserError::{IPParsing, InvalidYAML};
1111
use crate::event::{Event, EventValue};
1212
use crate::field::transformation::{encode_base64, encode_base64_offset, windash_variations};
1313
use crate::field::ValueTransformer::{Base64, Base64offset, Windash};
14-
use crate::wildcard::tokenize;
14+
use crate::wildcard::{tokenize, WildcardToken};
1515
use cidr::IpCidr;
1616
use regex::Regex;
1717
use serde_yml::Value;
@@ -117,40 +117,12 @@ impl Field {
117117
let mut order_modifier_provided = false;
118118
for v in self.values.iter_mut() {
119119
match self.modifier.match_modifier {
120-
Some(MatchModifier::Contains) => {
121-
if self.modifier.fieldref {
122-
continue;
123-
}
124-
if let FieldValue::Base(BaseValue::String(s)) = v {
125-
s.insert(0, '*');
126-
s.push('*');
127-
} else {
128-
return Err(ParserError::InvalidValueForStringModifier(
129-
self.name.clone(),
130-
));
131-
}
132-
}
133-
Some(MatchModifier::StartsWith) => {
134-
if self.modifier.fieldref {
135-
continue;
136-
}
137-
if let FieldValue::Base(BaseValue::String(s)) = v {
138-
s.push('*');
139-
} else {
140-
return Err(ParserError::InvalidValueForStringModifier(
141-
self.name.clone(),
142-
));
143-
}
144-
}
145-
Some(MatchModifier::EndsWith) => {
146-
if self.modifier.fieldref {
147-
continue;
148-
}
149-
if let FieldValue::Base(BaseValue::String(s)) = v {
150-
s.insert(0, '*');
151-
} else {
120+
Some(
121+
MatchModifier::StartsWith | MatchModifier::EndsWith | MatchModifier::Contains,
122+
) => {
123+
if !matches!(v, FieldValue::Base(BaseValue::String(_))) {
152124
return Err(ParserError::InvalidValueForStringModifier(
153-
self.name.clone(),
125+
self.name.to_string(),
154126
));
155127
}
156128
}
@@ -172,7 +144,22 @@ impl Field {
172144
if !self.modifier.fieldref && !order_modifier_provided {
173145
for v in self.values.iter_mut() {
174146
if let FieldValue::Base(BaseValue::String(s)) = v {
175-
*v = FieldValue::WildcardPattern(tokenize(s, !self.modifier.cased));
147+
let mut tokens = tokenize(s, !self.modifier.cased);
148+
match self.modifier.match_modifier {
149+
Some(MatchModifier::StartsWith) => {
150+
tokens.push(WildcardToken::Star);
151+
}
152+
Some(MatchModifier::EndsWith) => {
153+
tokens.insert(0, WildcardToken::Star);
154+
}
155+
Some(MatchModifier::Contains) => {
156+
tokens.insert(0, WildcardToken::Star);
157+
tokens.push(WildcardToken::Star);
158+
}
159+
_ => {}
160+
}
161+
162+
*v = FieldValue::WildcardPattern(tokens);
176163
}
177164
}
178165
}

tests/json_events.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,37 @@ fn test_match_event_from_json() {
4040
assert!(check_rule(&rule, &event));
4141
}
4242

43+
#[cfg(feature = "serde_json")]
44+
#[test]
45+
fn test_readme_sample() {
46+
let rule_yaml = r#"
47+
title: A test rule
48+
logsource:
49+
category: test
50+
detection:
51+
selection_1:
52+
Event.ID: 42
53+
TargetFilename|contains: ':\temp\'
54+
TargetFilename|endswith:
55+
- '.au3'
56+
- '\autoit3.exe'
57+
selection_2:
58+
Image|contains: ':\temp\'
59+
Image|endswith:
60+
- '.au3'
61+
- '\autoit3.exe'
62+
condition: 1 of selection_*
63+
"#;
64+
65+
let rule = rule_from_yaml(rule_yaml).unwrap();
66+
let event = event_from_json(
67+
r#"{"TargetFilename": "C:\\temp\\file.au3", "Image": "C:\\temp\\autoit4.exe", "Event": {"ID": 42}}"#,
68+
)
69+
.unwrap();
70+
71+
assert!(rule.is_match(&event));
72+
}
73+
4374
#[cfg(feature = "serde_json")]
4475
#[test]
4576
fn test_match_multiple_events_from_json() {

0 commit comments

Comments
 (0)