-
Notifications
You must be signed in to change notification settings - Fork 661
fixtures: add dynamic sample discovery and reverse MD5 lookup #2831
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
kami922
wants to merge
5
commits into
mandiant:master
Choose a base branch
from
kami922:fix-1743-dynamic-sample-discovery
base: master
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
5 commits
Select commit
Hold shift + click to select a range
b37b3de
fixtures: add dynamic sample discovery and reverse MD5 lookup
kami922 b45c649
fixtures: add return type hint to get_sample_short_name_by_md5
kami922 b2ebed7
tests: tighten assertions in test_fixtures to use exact equality
kami922 452609f
fixtures: prefer friendly names over hash-named duplicates in sample …
kami922 5975bde
Merge branch 'master' into fix-1743-dynamic-sample-discovery
kami922 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
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,138 @@ | ||
| # Copyright 2020 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Tests for fixtures module, covering sample discovery and reverse MD5 lookups.""" | ||
|
|
||
| import pytest | ||
| import fixtures | ||
|
|
||
| # Known sample names for testing (simple named samples) | ||
| SIMPLE_SAMPLES = [ | ||
| "mimikatz", | ||
| "kernel32", | ||
| "kernel32-64", | ||
| "pma12-04", | ||
| "pma16-01", | ||
| "pma01-01", | ||
| "pma21-01", | ||
| "al-khaser x86", | ||
| "al-khaser x64", | ||
| ] | ||
|
|
||
| # Hash-based samples (where file name is a hash prefix) | ||
| HASH_BASED_SAMPLES = [ | ||
| "39c05", | ||
| "499c2", | ||
| "9324d", | ||
| "a1982", | ||
| "a933a", | ||
| "bfb9b", | ||
| "c9188", | ||
| "64d9f", | ||
| "82bf6", | ||
| "77329", | ||
| "3b13b", | ||
| "7351f", | ||
| "79abd", | ||
| "946a9", | ||
| "b9f5b", | ||
| "294b8d", | ||
| "2bf18d", | ||
| "ea2876", | ||
| ] | ||
|
|
||
|
|
||
| class TestGetSampleMd5ByName: | ||
| """Tests for get_sample_md5_by_name function.""" | ||
|
|
||
| @pytest.mark.parametrize("name", SIMPLE_SAMPLES) | ||
| def test_simple_sample_lookup(self, name): | ||
| """Test that simple sample names have valid MD5 hashes.""" | ||
| md5 = fixtures.get_sample_md5_by_name(name) | ||
| assert isinstance(md5, str) | ||
| assert len(md5) == 32 | ||
| assert all(c in "0123456789abcdef" for c in md5) | ||
|
|
||
| @pytest.mark.parametrize("name", HASH_BASED_SAMPLES) | ||
| def test_hash_sample_lookup(self, name): | ||
| """Test that hash-based samples have valid MD5 hashes.""" | ||
| md5 = fixtures.get_sample_md5_by_name(name) | ||
| assert isinstance(md5, str) | ||
| assert len(md5) == 32 | ||
| assert all(c in "0123456789abcdef" for c in md5) | ||
|
|
||
| def test_unknown_sample_raises_error(self): | ||
| """Test that unknown samples raise ValueError.""" | ||
| with pytest.raises(ValueError, match="unexpected sample fixture"): | ||
| fixtures.get_sample_md5_by_name("nonexistent_sample") | ||
|
|
||
| def test_empty_string_raises_error(self): | ||
| """Test that empty string raises ValueError.""" | ||
| with pytest.raises(ValueError, match="unexpected sample fixture"): | ||
| fixtures.get_sample_md5_by_name("") | ||
|
|
||
|
|
||
| class TestGetSampleShortNameByMd5: | ||
| """Tests for get_sample_short_name_by_md5 function (reverse lookup).""" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "md5, name", | ||
| [ | ||
| ("5f66b82558ca92e54e77f216ef4c066c", "mimikatz"), | ||
| ("e80758cf485db142fca1ee03a34ead05", "kernel32"), | ||
| ("a8565440629ac87f6fef7d588fe3ff0f", "kernel32-64"), | ||
| ("db648cd247281954344f1d810c6fd590", "al-khaser_x86"), | ||
| ("3cb21ae76ff3da4b7e02d77ff76e82be", "al-khaser_x64"), | ||
| ], | ||
| ) | ||
| def test_reverse_lookup(self, md5, name): | ||
| """Test reverse MD5 lookup returns correct sample name.""" | ||
| result = fixtures.get_sample_short_name_by_md5(md5) | ||
| assert result == name | ||
|
|
||
| def test_unknown_md5_raises_error(self): | ||
| """Test that unknown MD5 hash raises ValueError.""" | ||
| with pytest.raises(ValueError, match="unexpected sample MD5"): | ||
| fixtures.get_sample_short_name_by_md5("00000000000000000000000000000000") | ||
|
|
||
| def test_empty_md5_raises_error(self): | ||
| """Test that empty MD5 raises ValueError.""" | ||
| with pytest.raises(ValueError, match="unexpected sample MD5"): | ||
| fixtures.get_sample_short_name_by_md5("") | ||
|
|
||
| def test_malformed_md5_raises_error(self): | ||
| """Test that malformed MD5 raises ValueError.""" | ||
| with pytest.raises(ValueError, match="unexpected sample MD5"): | ||
| fixtures.get_sample_short_name_by_md5("not_a_valid_md5") | ||
|
|
||
|
|
||
| class TestMd5NameLookupRoundtrip: | ||
| """Tests for round-trip MD5/name lookups.""" | ||
|
|
||
| @pytest.mark.parametrize("name", ["mimikatz", "kernel32", "kernel32-64"]) | ||
| def test_roundtrip_simple_samples(self, name): | ||
| """Test name->MD5->name roundtrip for samples whose filename stem matches the lookup name.""" | ||
| md5 = fixtures.get_sample_md5_by_name(name) | ||
| result_name = fixtures.get_sample_short_name_by_md5(md5) | ||
| assert result_name == name | ||
|
|
||
| @pytest.mark.parametrize("name", HASH_BASED_SAMPLES) | ||
| def test_roundtrip_hash_samples(self, name): | ||
| """Test name->MD5->name roundtrip for hash-based samples.""" | ||
| md5 = fixtures.get_sample_md5_by_name(name) | ||
| result_name = fixtures.get_sample_short_name_by_md5(md5) | ||
| # Verify the roundtrip succeeds and returns a valid result | ||
| # Note: Hash-based filenames may use MD5 while lookup uses SHA256 prefix | ||
| assert isinstance(result_name, str) | ||
| assert len(result_name) > 0 |
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.