-
Notifications
You must be signed in to change notification settings - Fork 16
fix: clarify CLI rule feed selection #12
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
Merged
+290
−26
Merged
Changes from all commits
Commits
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
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,181 @@ | ||
| import io | ||
| import json | ||
| import sys | ||
| import zipfile | ||
|
|
||
| import pytest | ||
|
|
||
| import valhallaAPI.valhalla as valhalla_module | ||
| import valhallaAPI.valhalla_cli as valhalla_cli | ||
| from valhallaAPI.filters import ApiError | ||
| from valhallaAPI.valhalla import ValhallaAPI | ||
|
|
||
|
|
||
| class MockResponse(object): | ||
| def __init__(self, payload): | ||
| self.text = json.dumps(payload) | ||
|
|
||
|
|
||
| def test_cli_check_mentions_sigma_feed_flag(monkeypatch, capsys): | ||
| monkeypatch.setattr(sys, "argv", ["valhalla-cli", "--check"]) | ||
| monkeypatch.setattr(valhalla_cli.os.path, "exists", lambda path: False) | ||
| monkeypatch.setattr( | ||
| valhalla_cli.ValhallaAPI, | ||
| "get_subscription", | ||
| lambda self: {"active": True}, | ||
| ) | ||
|
|
||
| with pytest.raises(SystemExit) as exc: | ||
| valhalla_cli.main() | ||
|
|
||
| captured = capsys.readouterr() | ||
|
|
||
| assert exc.value.code == 0 | ||
| assert "Account is active" in captured.err | ||
| assert "YARA is the default rule feed" in captured.err | ||
| assert "--feed sigma or --sigma/-s" in captured.err | ||
|
|
||
|
|
||
| def test_cli_feed_sigma_retrieves_sigma_rules(monkeypatch, capsys, tmp_path): | ||
| output_file = tmp_path / "sigma-rules.zip" | ||
|
|
||
| monkeypatch.setattr(sys, "argv", [ | ||
| "valhalla-cli", | ||
| "--feed", | ||
| "sigma", | ||
| "-o", | ||
| str(output_file), | ||
| ]) | ||
| monkeypatch.setattr(valhalla_cli.os.path, "exists", lambda path: False) | ||
|
|
||
| def fake_get_sigma_rules_zip(self, search="", private_only=False): | ||
| self.last_retrieved_rules_count = 2 | ||
| return b"zip-bytes" | ||
|
|
||
| def unexpected_yara_fetch(self, **kwargs): | ||
| raise AssertionError("YARA retrieval should not be used for --feed sigma") | ||
|
|
||
| monkeypatch.setattr( | ||
| valhalla_cli.ValhallaAPI, | ||
| "get_sigma_rules_zip", | ||
| fake_get_sigma_rules_zip, | ||
| ) | ||
| monkeypatch.setattr( | ||
| valhalla_cli.ValhallaAPI, | ||
| "get_rules_text", | ||
| unexpected_yara_fetch, | ||
| ) | ||
|
|
||
| valhalla_cli.main() | ||
|
|
||
| captured = capsys.readouterr() | ||
|
|
||
| assert output_file.read_bytes() == b"zip-bytes" | ||
| assert "Selected rule feed: SIGMA" in captured.err | ||
| assert "Retrieving Sigma rules with params" in captured.err | ||
|
|
||
|
|
||
| def test_cli_yara_feed_access_error_suggests_sigma(monkeypatch, capsys): | ||
| monkeypatch.setattr(sys, "argv", ["valhalla-cli"]) | ||
| monkeypatch.setattr(valhalla_cli.os.path, "exists", lambda path: False) | ||
|
|
||
| def fake_get_rules_text(self, **kwargs): | ||
| raise ApiError("user has no rule feed access") | ||
|
|
||
| monkeypatch.setattr(valhalla_cli.ValhallaAPI, "get_rules_text", fake_get_rules_text) | ||
|
|
||
| with pytest.raises(SystemExit) as exc: | ||
| valhalla_cli.main() | ||
|
|
||
| captured = capsys.readouterr() | ||
|
|
||
| assert exc.value.code == 1 | ||
| assert "user has no rule feed access" in captured.err | ||
| assert "This request targets the YARA feed" in captured.err | ||
| assert "--feed sigma or --sigma/-s" in captured.err | ||
|
|
||
|
|
||
| def test_cli_sigma_warns_about_ignored_yara_flags(monkeypatch, capsys, tmp_path): | ||
| output_file = tmp_path / "sigma-rules.zip" | ||
|
|
||
| monkeypatch.setattr(sys, "argv", [ | ||
| "valhalla-cli", | ||
| "--sigma", | ||
| "-fp", | ||
| "CarbonBlack", | ||
| "-fs", | ||
| "75", | ||
| "-o", | ||
| str(output_file), | ||
| ]) | ||
| monkeypatch.setattr(valhalla_cli.os.path, "exists", lambda path: False) | ||
| monkeypatch.setattr( | ||
| valhalla_cli.ValhallaAPI, | ||
| "get_sigma_rules_zip", | ||
| lambda self, search="", private_only=False: b"zip-bytes", | ||
| ) | ||
|
|
||
| valhalla_cli.main() | ||
|
|
||
| captured = capsys.readouterr() | ||
|
|
||
| assert output_file.read_bytes() == b"zip-bytes" | ||
| assert "Ignoring YARA-only flags for Sigma retrieval: -fp, -fs" in captured.err | ||
|
|
||
|
|
||
| def test_sigma_zip_updates_retrieved_rule_count(monkeypatch): | ||
| def fake_post(url, data=None, proxies=None, headers=None): | ||
| assert url.endswith("/getsigma") | ||
| return MockResponse( | ||
| { | ||
| "rules": [ | ||
| { | ||
| "signature_type": "sigma", | ||
| "type": "Process Creation", | ||
| "filename": "first.yml", | ||
| "content": "title: first", | ||
| }, | ||
| { | ||
| "signature_type": "sigma", | ||
| "type": "Network Connection", | ||
| "filename": "second.yml", | ||
| "content": "title: second", | ||
| }, | ||
| ] | ||
| } | ||
| ) | ||
|
|
||
| monkeypatch.setattr(valhalla_module.requests, "post", fake_post) | ||
|
|
||
| v = ValhallaAPI(api_key=ValhallaAPI.DEMO_KEY) | ||
| archive = v.get_sigma_rules_zip() | ||
|
|
||
| assert v.last_retrieved_rules_count == 2 | ||
|
|
||
| with zipfile.ZipFile(io.BytesIO(archive), "r") as zip_file: | ||
| names = sorted(zip_file.namelist()) | ||
|
|
||
| assert names == [ | ||
| "sigma/NetworkConnection/second.yml", | ||
| "sigma/ProcessCreation/first.yml", | ||
| ] | ||
|
|
||
|
|
||
| def test_sigma_zip_raises_api_error(monkeypatch): | ||
| def fake_post(url, data=None, proxies=None, headers=None): | ||
| assert url.endswith("/getsigma") | ||
| return MockResponse( | ||
| { | ||
| "status": "error", | ||
| "message": "user has no sigma rule feed access", | ||
| } | ||
| ) | ||
|
|
||
| monkeypatch.setattr(valhalla_module.requests, "post", fake_post) | ||
|
|
||
| v = ValhallaAPI(api_key="invalid") | ||
|
|
||
| with pytest.raises(ApiError) as exc: | ||
| v.get_sigma_rules_zip() | ||
|
|
||
| assert exc.value.message == "user has no sigma rule feed access" |
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
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.
Uh oh!
There was an error while loading. Please reload this page.