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 missing fields in to_dict() methods #197

Merged
merged 5 commits into from
Mar 17, 2024
Merged

Fix missing fields in to_dict() methods #197

merged 5 commits into from
Mar 17, 2024

Conversation

nzedler
Copy link
Contributor

@nzedler nzedler commented Mar 12, 2024

SigmaCorrelationCondition to_dict() method

The "field" field of a condition is missing in the to_dict() method of SigmaCorrelationCondition.

I found the bug when converting a value_count SigmaCorrelation rule to_dict() and then importing the created dictionary via from_dict(), resulting in a "sigma.exceptions.SigmaCorrelationRuleError: Value count correlation rule without field reference".

Example:

condition:
    field: User
    gte: 99

SigmaCorrelationCondition.to_dict() results in the version with the bug in:

{"gte": 99}

Now it is correctly:

{"field": "User", "gte": 99}

SigmaRuleBase to_dict() method

The rule "name" field is missing in the to_dict() method of SigmaRuleBase.

941     # Convert to string where possible
942     for field in ("id", "status", "level", "author", "description"):
943         if (s := self.__getattribute__(field)) is not None:
944            d[field] = str(s)

The simple fix:

941     # Convert to string where possible
942     for field in ("id", "status", "level", "author", "description", "name"):
943         if (s := self.__getattribute__(field)) is not None:
944            d[field] = str(s)

Calling to_dict() of the SigmaCorrelationCondition resulted in only the operator being their. Thus calling to_dict() and then from_dict() resulted in a loss of information. Correlation type: value_count has the 'field' reference mandatory.
"name" field was lost when converting rule back to dictionary
@nzedler nzedler changed the title Fix missing fieldref in SigmaCorrelationCondition to_dict() Fix missing fields in to_dict() methods Mar 12, 2024
@thomaspatzke
Copy link
Member

Thanks for the fix! Totally forgot about to_dict() when I extended the class.

Can you also fix the test test_sigmarule_to_dict? The name field is missing here in the test result.

nzedler added 2 commits March 13, 2024 08:35
Tests, if the "field" field of a correlation condition is correctly put into a dict via the to_dict() method
@nzedler
Copy link
Contributor Author

nzedler commented Mar 13, 2024

Is done, I fixed the test test_sigmarule_to_dict() and additionaly added the following test_correlation_condition_with_field_to_dict() in tests/test_correlations.py:

def test_correlation_condition_with_field_to_dict():
    assert SigmaCorrelationCondition(
        op=SigmaCorrelationConditionOperator.GTE,
        count=10,
        fieldref="test"
    ).to_dict() == {
        "field": "test",
        "gte": 10
    }

You can close the two issues I created when the pull request is merged.

@thomaspatzke
Copy link
Member

The tests failed due to lack of formatting with black. Just committed, now it works.

@thomaspatzke thomaspatzke merged commit 1e2f076 into SigmaHQ:main Mar 17, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants