-
Notifications
You must be signed in to change notification settings - Fork 2
Event Sequence Detector #245
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
Open
Leokaufi
wants to merge
11
commits into
development
Choose a base branch
from
feat/event-sequence-detector
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
024878b
Merge pull request #170 from ait-detectmate/feature/persistency-impro…
viktorbeck98 a8469fa
Revert "Implement custom add_value methods with add_value_fn argument…
viktorbeck98 bcefea3
Merge pull request #231 from ait-detectmate/revert-170-feature/persis…
viktorbeck98 48bfbf8
start new_sequence_detector
Leokaufi 1700130
add new_sequence_detector
Leokaufi 5725255
add test, delete ipykernel dependency
Leokaufi 331a430
resolve flake8 and mypy findings in new_sequence_detector
Leokaufi cece0a2
Merge branch 'development' into feat/event-sequence-detector
viktorbeck98 3bad05c
update dependencies
viktorbeck98 028f17a
Merge remote-tracking branch 'origin/development' into feat/event-seq…
Leokaufi 1502272
Merge branch 'feat/event-sequence-detector' of https://github.com/ait…
Leokaufi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| from collections import deque | ||
| from typing import Sequence | ||
|
|
||
| from detectmatelibrary.common._config._compile import generate_detector_config | ||
| from detectmatelibrary.common.detector import CoreDetectorConfig, CoreDetector | ||
| from detectmatelibrary.utils import persistency | ||
| from detectmatelibrary.utils.data_buffer import BufferMode | ||
| from detectmatelibrary.schemas import ParserSchema, DetectorSchema | ||
|
|
||
| _SEQUENCE_SEPARATOR = "\x1f" | ||
|
|
||
|
|
||
| def _encode_sequence(sequence: Sequence[int]) -> str: | ||
| return _SEQUENCE_SEPARATOR.join(str(event_id) for event_id in sequence) | ||
|
|
||
|
|
||
| class NewSequenceDetectorConfig(CoreDetectorConfig): | ||
| method_type: str = "new_sequence_detector" | ||
| max_sequence_length: int = 3 | ||
|
|
||
|
|
||
| class NewSequenceDetector(CoreDetector): | ||
| def __init__( | ||
| self, | ||
| name: str = "NewSequenceDetector", | ||
| config: NewSequenceDetectorConfig = NewSequenceDetectorConfig() | ||
| ) -> None: | ||
| if isinstance(config, dict): | ||
| config = NewSequenceDetectorConfig.from_dict(config, name) | ||
|
|
||
| super().__init__(name=name, buffer_mode=BufferMode.NO_BUF, config=config) | ||
| self.config: NewSequenceDetectorConfig | ||
| self._window: deque[int] = deque(maxlen=self.config.max_sequence_length) | ||
| self.persistency = persistency.EventPersistency( | ||
| event_data_class=persistency.EventStabilityTracker, | ||
| ) | ||
| self.auto_conf_persistency = persistency.EventPersistency( | ||
| event_data_class=persistency.EventStabilityTracker | ||
| ) | ||
| self._register_persistency(self.persistency) | ||
|
|
||
| def train(self, input_: ParserSchema) -> None: # type: ignore | ||
| """Train the detector by learning EventID sequences from the input | ||
| data.""" | ||
| self._window.append(input_["EventID"]) | ||
| if len(self._window) < self.config.max_sequence_length: | ||
| return | ||
| self.persistency.ingest_event( | ||
| event_id=_encode_sequence(self._window), | ||
| event_template=input_["template"] | ||
| ) | ||
|
|
||
| def detect(self, input_: ParserSchema, output_: DetectorSchema) -> bool: # type: ignore | ||
| self._window.append(input_["EventID"]) | ||
| if len(self._window) < self.config.max_sequence_length: | ||
| return False | ||
|
|
||
| if _encode_sequence(self._window) not in self.persistency.get_events_seen(): | ||
| output_["score"] = 1.0 | ||
| output_["description"] = f"{self.name} detects unknown EventID sequences as anomalies." | ||
| output_["alertsObtain"].update({ | ||
| f"Sequence {tuple(self._window)}": f"Unknown sequence: {tuple(self._window)}" | ||
| }) | ||
| return True | ||
| return False | ||
|
|
||
| def configure(self, input_: ParserSchema) -> None: # type: ignore | ||
| self.auto_conf_persistency.ingest_event( | ||
| event_id=input_["EventID"], | ||
| event_template=input_["template"] | ||
| ) | ||
|
|
||
| def set_configuration(self) -> None: | ||
| old_persist = self.config.persist | ||
| config_dict = generate_detector_config( | ||
| variable_selection={}, | ||
| detector_name=self.name, | ||
| method_type=self.config.method_type, | ||
| max_sequence_length=self.config.max_sequence_length, | ||
| ) | ||
| self.config = NewSequenceDetectorConfig.from_dict(config_dict, self.name) | ||
| self.config.persist = old_persist | ||
| self._window = deque(self._window, maxlen=self.config.max_sequence_length) | ||
|
|
||
| def reset_window(self) -> None: | ||
| self._window.clear() | ||
|
|
||
| def get_known_sequences(self) -> set[tuple[str, ...]]: | ||
| return { | ||
| tuple(str(encoded).split(_SEQUENCE_SEPARATOR)) | ||
| for encoded in self.persistency.get_events_seen() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| """Tests for NewSequenceDetector class. | ||
|
|
||
| This module tests the NewSequenceDetector implementation including: | ||
| - Initialization and configuration | ||
| - Training functionality to learn known EventID sequences | ||
| - Detection logic for unknown sequences | ||
| - Window handling (reset_window) and end-to-end regression on audit.log | ||
| """ | ||
|
|
||
| from detectmatelibrary.detectors.new_sequence_detector import NewSequenceDetector, \ | ||
| NewSequenceDetectorConfig, BufferMode | ||
| from detectmatelibrary.parsers.template_matcher import MatcherParser | ||
| from detectmatelibrary.helper.from_to import From | ||
| import detectmatelibrary.schemas as schemas | ||
| from detectmatelibrary.utils.aux import time_test_mode | ||
| from tests.test_data import AUDIT_LOG, AUDIT_TEMPLATES, TRAIN_UNTIL | ||
|
|
||
| # Set time test mode for consistent timestamps | ||
| time_test_mode() | ||
|
|
||
|
|
||
| config = { | ||
| "detectors": { | ||
| "CustomInit": { | ||
| "method_type": "new_sequence_detector", | ||
| "auto_config": False, | ||
| "params": { | ||
| "max_sequence_length": 2 | ||
| } | ||
| }, | ||
| "MultipleDetector": { | ||
| "method_type": "new_sequence_detector", | ||
| "auto_config": False, | ||
| "params": { | ||
| "max_sequence_length": 2 | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| def _make_schema(event_id, template="test template", log_id="1"): | ||
| return schemas.ParserSchema({ | ||
| "parserType": "test", | ||
| "EventID": event_id, | ||
| "template": template, | ||
| "variables": ["adsasd", "asdasd"], | ||
| "logID": log_id, | ||
| "parsedLogID": log_id, | ||
| "parserID": "test_parser", | ||
| "log": "test log message", | ||
| "logFormatVariables": {"level": "INFO"} | ||
| }) | ||
|
|
||
|
|
||
| class TestNewSequenceDetectorInitialization: | ||
| """Test NewSequenceDetector initialization and configuration.""" | ||
|
|
||
| def test_default_initialization(self): | ||
| detector = NewSequenceDetector() | ||
|
|
||
| assert detector.name == "NewSequenceDetector" | ||
| assert hasattr(detector, "config") | ||
| assert detector.data_buffer.mode == BufferMode.NO_BUF | ||
| assert detector.input_schema == schemas.ParserSchema | ||
| assert detector.output_schema == schemas.DetectorSchema | ||
| assert hasattr(detector, "persistency") | ||
| assert detector.config.max_sequence_length == 3 | ||
|
|
||
| def test_custom_config_initialization(self): | ||
| detector = NewSequenceDetector(name="CustomInit", config=config) | ||
|
|
||
| assert detector.name == "CustomInit" | ||
| assert detector.config.max_sequence_length == 2 | ||
| assert hasattr(detector, "persistency") | ||
| assert isinstance(detector.persistency.events_data, dict) | ||
|
|
||
|
|
||
| class TestNewSequenceDetectorTraining: | ||
| """Test NewSequenceDetector training functionality.""" | ||
|
|
||
| def test_train_learns_sequence(self): | ||
| detector = NewSequenceDetector(config=config, name="MultipleDetector") | ||
|
|
||
| for event_id in [1, 2, 3]: | ||
| detector.train(_make_schema(event_id)) | ||
|
|
||
| # max_sequence_length=2 -> sequences (1, 2) and (2, 3) are learned | ||
| assert detector.get_known_sequences() == {("1", "2"), ("2", "3")} | ||
|
|
||
| def test_train_below_window_length_learns_nothing(self): | ||
| detector = NewSequenceDetector(config=config, name="MultipleDetector") | ||
|
|
||
| # Only one event seen so far, window not yet full (max_sequence_length=2) | ||
| detector.train(_make_schema(1)) | ||
|
|
||
| assert detector.get_known_sequences() == set() | ||
|
|
||
|
|
||
| class TestNewSequenceDetectorDetection: | ||
| """Test NewSequenceDetector detection functionality.""" | ||
|
|
||
| def test_detect_known_sequence_no_alert(self): | ||
| detector = NewSequenceDetector(config=config, name="MultipleDetector") | ||
|
|
||
| # Repeating the cycle 1 -> 2 -> 3 teaches sequences (1,2), (2,3), (3,1) | ||
| for event_id in [1, 2, 3, 1, 2, 3]: | ||
| detector.train(_make_schema(event_id)) | ||
| # Window now holds the tail (2, 3); appending 1 reproduces the known (3, 1) | ||
| output = schemas.DetectorSchema() | ||
| result = detector.detect(_make_schema(1, log_id="7"), output) | ||
|
|
||
| assert not result | ||
| assert output.score == 0.0 | ||
|
|
||
| def test_detect_unknown_sequence_alert(self): | ||
| detector = NewSequenceDetector(config=config, name="MultipleDetector") | ||
|
|
||
| for event_id in [1, 2, 3, 1, 2, 3]: | ||
| detector.train(_make_schema(event_id)) | ||
|
|
||
| # (3, 9) was never seen during training | ||
| output = schemas.DetectorSchema() | ||
| result = detector.detect(_make_schema(9, log_id="7"), output) | ||
|
|
||
| assert result | ||
| assert output.score == 1.0 | ||
| assert any("Sequence" in key for key in output["alertsObtain"]) | ||
|
|
||
| def test_detect_below_window_length_no_alert(self): | ||
| detector = NewSequenceDetector(config=config, name="MultipleDetector") | ||
|
|
||
| # First event with an empty detector: window not yet full, can't be an anomaly | ||
| output = schemas.DetectorSchema() | ||
| result = detector.detect(_make_schema(1), output) | ||
|
|
||
| assert not result | ||
| assert output.score == 0.0 | ||
|
|
||
|
|
||
| class TestNewSequenceDetectorWindow: | ||
| """Test sliding-window behaviour.""" | ||
|
|
||
| def test_reset_window_clears_state(self): | ||
| detector = NewSequenceDetector(config=config, name="MultipleDetector") | ||
|
|
||
| detector.train(_make_schema(1)) | ||
| assert len(detector._window) == 1 | ||
|
|
||
| detector.reset_window() | ||
| assert len(detector._window) == 0 | ||
|
|
||
|
|
||
| _PARSER_CONFIG = { | ||
| "parsers": { | ||
| "MatcherParser": { | ||
| "method_type": "matcher_parser", | ||
| "auto_config": False, | ||
| "log_format": "type=<Type> msg=audit(<Time>:*): <Content>", | ||
| "time_format": None, | ||
| "params": { | ||
| "remove_spaces": True, | ||
| "remove_punctuation": True, | ||
| "lowercase": True, | ||
| "path_templates": AUDIT_TEMPLATES, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| class TestNewSequenceDetectorEndToEnd: | ||
| """Regression test: full train/detect pipeline on audit.log.""" | ||
|
|
||
| def test_audit_log_anomalies(self): | ||
| pars = MatcherParser(config=_PARSER_CONFIG) | ||
| detector = NewSequenceDetector( | ||
| config=NewSequenceDetectorConfig(auto_config=False), | ||
| name="NewSequenceDetector", | ||
| ) | ||
|
|
||
| logs = list(From.log(pars, in_path=AUDIT_LOG, do_process=True)) | ||
|
|
||
| for log in logs[:TRAIN_UNTIL]: | ||
| detector.train(log) | ||
|
|
||
| detected_ids: set[str] = set() | ||
| for log in logs[TRAIN_UNTIL:]: | ||
| output = schemas.DetectorSchema() | ||
| if detector.detect(log, output_=output): | ||
| detected_ids.add(log["logID"]) | ||
|
|
||
| assert detected_ids == {"1863", "1864", "1865"} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially the data_buffer was intended to be used for such a case but I am not sure if it fits. It even has a window mode. Pls check