From b9a9eb128b3cd74518b8888e180528babadc9e14 Mon Sep 17 00:00:00 2001 From: Xusheng Date: Fri, 29 Nov 2024 15:01:20 +0800 Subject: [PATCH] Add a unit test for Binary Ninja database --- tests/data | 2 +- tests/fixtures.py | 39 ++++++++++++++++++++++++++++++++++++ tests/test_binja_features.py | 2 +- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/tests/data b/tests/data index 3f42b20ab..b40457c61 160000 --- a/tests/data +++ b/tests/data @@ -1 +1 @@ -Subproject commit 3f42b20ab5cbdca3ad24edc80b5379c792bb19b2 +Subproject commit b40457c616604ee65f44e1ae8597cfeec7da0f70 diff --git a/tests/fixtures.py b/tests/fixtures.py index 19285eca4..6ad6a1fe9 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -332,6 +332,8 @@ def get_data_path_by_name(name) -> Path: return CD / "data" / "Practical Malware Analysis Lab 12-04.exe_" elif name == "pma16-01": return CD / "data" / "Practical Malware Analysis Lab 16-01.exe_" + elif name == "pma16-01_binja_db": + return CD / "data" / "Practical Malware Analysis Lab 16-01.dll_.bndb" elif name == "pma21-01": return CD / "data" / "Practical Malware Analysis Lab 21-01.exe_" elif name == "al-khaser x86": @@ -1387,6 +1389,43 @@ def parametrize(params, values, **kwargs): ("mimikatz", "file", capa.features.file.Import("cabinet.FCIAddFile"), True), ] +FEATURE_BINJA_DATABASE_TESTS = sorted( + [ + # insn/regex + ("pma16-01_binja_db", "function=0x4021B0", capa.features.common.Regex("HTTP/1.0"), True), + ( + "pma16-01_binja_db", + "function=0x402F40", + capa.features.common.Regex("www.practicalmalwareanalysis.com"), + True, + ), + ( + "pma16-01_binja_db", + "function=0x402F40", + capa.features.common.Substring("practicalmalwareanalysis.com"), + True, + ), + ("pma16-01_binja_db", "file", capa.features.file.FunctionName("__aulldiv"), True), + # os & format & arch + ("pma16-01_binja_db", "file", OS(OS_WINDOWS), True), + ("pma16-01_binja_db", "file", OS(OS_LINUX), False), + ("pma16-01_binja_db", "function=0x404356", OS(OS_WINDOWS), True), + ("pma16-01_binja_db", "function=0x404356,bb=0x4043B9", OS(OS_WINDOWS), True), + ("pma16-01_binja_db", "file", Arch(ARCH_I386), True), + ("pma16-01_binja_db", "file", Arch(ARCH_AMD64), False), + ("pma16-01_binja_db", "function=0x404356", Arch(ARCH_I386), True), + ("pma16-01_binja_db", "function=0x404356,bb=0x4043B9", Arch(ARCH_I386), True), + ("pma16-01_binja_db", "file", Format(FORMAT_PE), True), + ("pma16-01_binja_db", "file", Format(FORMAT_ELF), False), + # format is also a global feature + ("pma16-01_binja_db", "function=0x404356", Format(FORMAT_PE), True), + ], + # order tests by (file, item) + # so that our LRU cache is most effective. + key=lambda t: (t[0], t[1]), +) + + FEATURE_COUNT_TESTS = [ ("mimikatz", "function=0x40E5C2", capa.features.basicblock.BasicBlock(), 7), ("mimikatz", "function=0x4702FD", capa.features.common.Characteristic("calls from"), 0), diff --git a/tests/test_binja_features.py b/tests/test_binja_features.py index fe7d60d30..f57c3b50f 100644 --- a/tests/test_binja_features.py +++ b/tests/test_binja_features.py @@ -36,7 +36,7 @@ @pytest.mark.skipif(binja_present is False, reason="Skip binja tests if the binaryninja Python API is not installed") @fixtures.parametrize( "sample,scope,feature,expected", - fixtures.FEATURE_PRESENCE_TESTS + fixtures.FEATURE_SYMTAB_FUNC_TESTS, + fixtures.FEATURE_PRESENCE_TESTS + fixtures.FEATURE_SYMTAB_FUNC_TESTS + fixtures.FEATURE_BINJA_DATABASE_TESTS, indirect=["sample", "scope"], ) def test_binja_features(sample, scope, feature, expected):