-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from IoT-Inspector/refact-processing
Refactor processing, get rid of strategies.py
- Loading branch information
Showing
12 changed files
with
248 additions
and
169 deletions.
There are no files selected for viewing
This file contains 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 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 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,73 @@ | ||
from pathlib import Path | ||
|
||
from unblob.finder import make_handler_map, make_yara_rules, search_yara_patterns | ||
from unblob.models import Handler | ||
|
||
|
||
class _BaseTestHandler(Handler): | ||
def calculate_chunk(self, *args, **kwargs): | ||
pass | ||
|
||
@staticmethod | ||
def make_extract_command(*args, **kwargs): | ||
return [] | ||
|
||
|
||
class TestHandler1(_BaseTestHandler): | ||
NAME = "handler1" | ||
YARA_RULE = r""" | ||
strings: | ||
$magic = { 21 3C } | ||
condition: | ||
$magic | ||
""" | ||
|
||
|
||
class TestHandler2(_BaseTestHandler): | ||
NAME = "handler2" | ||
YARA_RULE = r""" | ||
strings: | ||
$tar_magic = { 75 73 74 61 72 } | ||
condition: | ||
$tar_magic | ||
""" | ||
|
||
|
||
def test_make_yara_rules(): | ||
rules = make_yara_rules(tuple([TestHandler1, TestHandler2])) | ||
matches = rules.match(data=b"!< ustar") | ||
assert len(matches) == 2 | ||
assert matches[0].strings == [(0, "$magic", b"!<")] | ||
assert matches[1].strings == [(10, "$tar_magic", b"ustar")] | ||
|
||
|
||
def test_search_yara_patterns(tmp_path: Path): | ||
handler1 = TestHandler1() | ||
handler2 = TestHandler2 | ||
rules = make_yara_rules(tuple([TestHandler1, TestHandler2])) | ||
handler_map = {"handler1": handler1, "handler2": handler2} | ||
test_file = tmp_path / "test_file" | ||
test_file.write_bytes(b"!< ustar") | ||
results = search_yara_patterns(rules, handler_map, test_file) | ||
|
||
assert len(results) == 2 | ||
result1, result2 = results | ||
|
||
assert result1.handler is handler1 | ||
assert result1.match.strings == [(0, "$magic", b"!<")] | ||
|
||
assert result2.handler is handler2 | ||
assert result2.match.strings == [(10, "$tar_magic", b"ustar")] | ||
|
||
|
||
def test_make_handler_map(): | ||
handler_map = make_handler_map(tuple([TestHandler1, TestHandler2])) | ||
assert isinstance(handler_map["handler1"], TestHandler1) | ||
assert isinstance(handler_map["handler2"], TestHandler2) | ||
|
||
|
||
def test_make_handler_map_instances_are_cached(): | ||
handler_map1 = make_handler_map(tuple([TestHandler1, TestHandler2])) | ||
handler_map2 = make_handler_map(tuple([TestHandler1, TestHandler2])) | ||
assert handler_map1["handler1"] is handler_map2["handler1"] | ||
assert handler_map1["handler2"] is handler_map2["handler2"] |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
""" | ||
File extraction related functions. | ||
""" | ||
import io | ||
import shlex | ||
import subprocess | ||
|
This file contains 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 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 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.