-
Notifications
You must be signed in to change notification settings - Fork 2
Auto Parser #286
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
ipmach
wants to merge
5
commits into
development
Choose a base branch
from
feat/autoparser
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
Auto Parser #286
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,54 @@ | ||
| # flake8: noqa | ||
|
|
||
| # --8<-- [start:example_1] | ||
|
|
||
| from detectmatelibrary.parsers.autoparser import AutoParser | ||
|
|
||
| from detectmatelibrary.helper.from_to import From | ||
|
|
||
| config_dict = { | ||
| "parsers": { | ||
| "AutoParser": { | ||
| "method_type": "auto_parser", | ||
| "data_use_training": 10, | ||
| } | ||
| } | ||
| } | ||
| parser = AutoParser(config=config_dict) | ||
| path = "tests/test_data/audit.log" | ||
|
|
||
| for j, parsed_log in enumerate(From.log(parser, path)): | ||
| if j == 15: | ||
| break | ||
|
|
||
| print(parsed_log["template"]) # Returns the parsed log as audit | ||
|
|
||
| # --8<-- [end:example_1] | ||
|
|
||
| # --8<-- [start:example_2] | ||
|
|
||
| from detectmatelibrary.parsers.autoparser import AutoParser | ||
|
|
||
| from detectmatelibrary.helper.from_to import From | ||
|
|
||
| config_dict = { | ||
| "parsers": { | ||
| "AutoParser": { | ||
| "method_type": "auto_parser", | ||
| "data_use_training": 10, | ||
| "params": { | ||
| "fix_type": "Audit" # Fix search to only Audit | ||
| } | ||
| } | ||
| } | ||
| } | ||
| parser = AutoParser(config=config_dict) | ||
| path = "tests/test_data/audit.log" | ||
|
|
||
| for j, parsed_log in enumerate(From.log(parser, path)): | ||
| if j == 15: | ||
| break | ||
|
|
||
| print(parsed_log["template"]) # Returns the parsed log as audit | ||
|
|
||
| # --8<-- [end:example_2] |
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
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,34 @@ | ||
| # Auto Parser | ||
|
|
||
| Parse the logs using the templates saved in the dataset. (HDFS, BGL, Audit, SysLog, Apache, OpenVPN, Thunderbird). | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does it mean "saved in the dataset"? |
||
|
|
||
| It wraps functionality from the DetectMatePerformance project: https://github.com/ait-detectmate/DetectMatePerformance. When parsing large numbers of log lines in non-stream (batch) mode, it is recommended to use the performance-oriented implementation. | ||
|
|
||
| | | Schema | Description | | ||
| |------------|----------------------------|--------------------| | ||
| | **Input** | [LogSchema](../schemas.md) | Unstructured log | | ||
| | **Output** | [ParserSchema](../schemas.md) | Structured log | | ||
|
|
||
| ## Configuration | ||
|
|
||
| Auto parser parameters: | ||
|
|
||
| - `method_type` (string): identifier for the parser type (e.g., `"aut_parser"`). | ||
| - `fix_type` (str): fix type of logs to process. | ||
|
|
||
|
|
||
| ## Usage example | ||
|
|
||
| Without fixing log type: | ||
|
|
||
| ```python | ||
| --8<-- "docs/examples/parsers/auto_parser.py:example_1" | ||
| ``` | ||
|
|
||
| With fixing log type: | ||
|
|
||
| ```python | ||
| --8<-- "docs/examples/parsers/auto_parser.py:example_2" | ||
| ``` | ||
|
|
||
| Go back to [Index](../index.md) | ||
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
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
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,59 @@ | ||
| from detectmatelibrary.common.parser import CoreParser, CoreParserConfig | ||
| from detectmatelibrary import schemas | ||
|
|
||
| from detectmateperformance.match_tree import TreeMatcher | ||
| from detectmateperformance.autoparser import AutoParse | ||
|
|
||
|
|
||
| from typing import Any | ||
| import warnings | ||
| import re | ||
|
|
||
|
|
||
| class AutoParserConfig(CoreParserConfig): | ||
| method_type: str = "auto_parser" | ||
| fix_type: str = "" | ||
|
|
||
|
|
||
| class AutoParser(CoreParser): | ||
| def __init__( | ||
| self, | ||
| name: str = "AutoParser", | ||
| config: AutoParserConfig | dict[str, Any] = AutoParserConfig() | ||
| ) -> None: | ||
|
|
||
| if isinstance(config, dict): | ||
| config = AutoParserConfig.from_dict(config, name) | ||
| super().__init__(name=name, config=config) | ||
|
|
||
| self.config: AutoParserConfig | ||
| self.auto_gen = AutoParse(num_use=self.config.data_use_training) | ||
| self.tree_match: TreeMatcher | None = None | ||
|
|
||
| self.config_buffer: list[str] = [] | ||
|
|
||
| def train(self, input_: schemas.LogSchema) -> None: # type: ignore | ||
| self.auto_gen.add(input_["log"]) | ||
|
|
||
| def post_train(self) -> None: | ||
| try: | ||
| self.tree_match, _regex = self.auto_gen.generate(self.config.fix_type) | ||
| self.config._regex = re.compile(_regex) | ||
| except RuntimeError as e: | ||
| warnings.warn(str(e)) | ||
|
|
||
| def parse( | ||
| self, | ||
| input_: schemas.LogSchema, | ||
| output_: schemas.ParserSchema | ||
| ) -> None: | ||
|
|
||
| if self.tree_match is None: | ||
| output_["EventID"] = -1 | ||
| output_["template"] = "templates not yet generated" | ||
| else: | ||
| parsed = self.tree_match.match_log(input_["log"], get_var=True)[0] | ||
|
|
||
| output_["EventID"] = parsed["EventID"] | ||
| output_["variables"].extend(parsed["ParamList"]) | ||
| output_["template"] = parsed["Template"] |
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,67 @@ | ||
| """Most of the functionality is test it in DetectMatePerformance.""" | ||
| from detectmatelibrary.parsers.autoparser import AutoParser | ||
|
|
||
| from detectmatelibrary.helper.from_to import From | ||
| import pytest | ||
|
|
||
| temp = "pid <*> uid <*> auid <*> ses <*> msg op <*> acct <*> exe <*> hostname <*> addr <*> terminal <*> res <*>" # noqa: E501 | ||
|
|
||
|
|
||
| class TestAutoParserParser: | ||
| def test_train_process(self): | ||
| config_dict = { | ||
| "parsers": { | ||
| "AutoParser": { | ||
| "method_type": "auto_parser", | ||
| "data_use_training": 10, | ||
| } | ||
| } | ||
| } | ||
| parser = AutoParser(config=config_dict) | ||
| path = "tests/test_data/audit.log" | ||
|
|
||
| for j, parsed_log in enumerate(From.log(parser, path)): | ||
| if j == 15: | ||
| break | ||
|
|
||
| assert parsed_log["template"] == temp | ||
|
|
||
| def test_fix_templates(self): | ||
| config_dict = { | ||
| "parsers": { | ||
| "AutoParser": { | ||
| "method_type": "auto_parser", | ||
| "data_use_training": 10, | ||
| "params": { | ||
| "fix_type": "BGL" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| parser = AutoParser(config=config_dict) | ||
| path = "tests/test_data/audit.log" | ||
|
|
||
| for j, parsed_log in enumerate(From.log(parser, path)): | ||
| if j == 15: | ||
| break | ||
|
|
||
| assert parsed_log["template"] == "template not found" | ||
|
|
||
| def test_unknonw_fix_dataset(self): | ||
| config_dict = { | ||
| "parsers": { | ||
| "AutoParser": { | ||
| "method_type": "auto_parser", | ||
| "data_use_training": 1, | ||
| "params": { | ||
| "fix_type": "Unknown" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| parser = AutoParser(config=config_dict) | ||
| path = "tests/test_data/audit.log" | ||
|
|
||
| next(From.log(parser, path)) | ||
| with pytest.warns(): | ||
| next(From.log(parser, path)) |
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.
It's not fully clear what the auto parser does. Pls add more information.