Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: |re matching is not done correctly, so use is_match instead of find_iter #1212

Merged
merged 5 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-Japanese.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
**バグ修正:**

- バージョン`2.10.0`の`update-rules`コマンドでは、新しいルールがダウンロードされても、`You currently have the latest rules`というメッセージを出力していた。 (#1209) (@fukusuket)
- 正規表現が正しく処理されない場合があった。 (#1212) (@fukusuket)

**その他:**

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
**Bug Fixes:**

- `update-rules` command would output `You currently have the latest rules` even if new rules were downloaded in version `2.10.0`. (#1209) (@fukusuket)
- Regular expressions would sometimes be incorrectly handled. (#1212) (@fukusuket)

**Other:**

Expand Down
26 changes: 21 additions & 5 deletions src/detections/rule/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,8 @@ impl DefaultMatcher {
}

/// このmatcherの正規表現とマッチするかどうか判定します。
/// 判定対象の文字列とこのmatcherが保持する正規表現が完全にマッチした場合のTRUEを返します。
/// 例えば、判定対象文字列が"abc"で、正規表現が"ab"の場合、正規表現は判定対象文字列の一部分にしか一致していないので、この関数はfalseを返します。
fn is_regex_fullmatch(&self, value: &str) -> bool {
return self.re.as_ref().unwrap().find_iter(value).any(|match_obj| {
return match_obj.as_str() == value;
});
return self.re.as_ref().unwrap().is_match(value);
}

/// Hayabusaのルールファイルのフィールド名とそれに続いて指定されるパイプを、正規表現形式の文字列に変換します。
Expand Down Expand Up @@ -1374,6 +1370,26 @@ mod tests {
check_select(rule_str, record_json_str, true);
}

#[test]
fn test_detect_regex_partial_match() {
// 正規表現の部分一致
let rule_str = r#"
enabled: true
detection:
selection:
Computer|re: DESKTOP
details: 'command=%CommandLine%'
"#;

let record_json_str = r#"
{
"Event": {"System": {"EventID": 4103, "Channel": "Program", "Computer":"DESKTOP-ICHIICHI"}},
"Event_attributes": {"xmlns": "http://schemas.microsoft.com/win/2004/08/events/event"}
}"#;

check_select(rule_str, record_json_str, true);
}

#[test]
fn test_detect_regexes() {
// regexes.txtが正しく検知できることを確認
Expand Down