mirror of
https://github.com/mandiant/capa.git
synced 2026-01-11 04:33:54 -08:00
Changes os.path to pathlib.Path usage
changed args.rules , args.signatures types in handle_common_args.
This commit is contained in:
@@ -14,6 +14,7 @@ import itertools
|
||||
import contextlib
|
||||
import collections
|
||||
from typing import Set, Dict
|
||||
from pathlib import Path
|
||||
from functools import lru_cache
|
||||
|
||||
import pytest
|
||||
@@ -44,9 +45,9 @@ from capa.features.address import Address
|
||||
from capa.features.extractors.base_extractor import BBHandle, InsnHandle, FunctionHandle
|
||||
from capa.features.extractors.dnfile.extractor import DnfileFeatureExtractor
|
||||
|
||||
CD = os.path.dirname(__file__)
|
||||
DOTNET_DIR = os.path.join(CD, "data", "dotnet")
|
||||
DNFILE_TESTFILES = os.path.join(DOTNET_DIR, "dnfile-testfiles")
|
||||
CD = Path(__file__).resolve().parent
|
||||
DOTNET_DIR = CD / "data" / "dotnet"
|
||||
DNFILE_TESTFILES = DOTNET_DIR / "dnfile-testfiles"
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
@@ -94,11 +95,11 @@ def get_viv_extractor(path):
|
||||
import capa.features.extractors.viv.extractor
|
||||
|
||||
sigpaths = [
|
||||
os.path.join(CD, "data", "sigs", "test_aulldiv.pat"),
|
||||
os.path.join(CD, "data", "sigs", "test_aullrem.pat.gz"),
|
||||
os.path.join(CD, "..", "sigs", "1_flare_msvc_rtf_32_64.sig"),
|
||||
os.path.join(CD, "..", "sigs", "2_flare_msvc_atlmfc_32_64.sig"),
|
||||
os.path.join(CD, "..", "sigs", "3_flare_common_libs.sig"),
|
||||
CD / "data" / "sigs" / "test_aulldiv.pat",
|
||||
CD / "data" / "sigs" / "test_aullrem.pat.gz",
|
||||
CD.parent / "sigs" / "1_flare_msvc_rtf_32_64.sig",
|
||||
CD.parent / "sigs" / "2_flare_msvc_atlmfc_32_64.sig",
|
||||
CD.parent / "sigs" / "3_flare_common_libs.sig",
|
||||
]
|
||||
|
||||
if "raw32" in path:
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
# 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.
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
import fixtures
|
||||
from fixtures import *
|
||||
@@ -52,8 +53,8 @@ def test_binja_feature_counts(sample, scope, feature, expected):
|
||||
|
||||
@pytest.mark.skipif(binja_present is False, reason="Skip binja tests if the binaryninja Python API is not installed")
|
||||
def test_standalone_binja_backend():
|
||||
CD = os.path.dirname(__file__)
|
||||
test_path = os.path.join(CD, "..", "tests", "data", "Practical Malware Analysis Lab 01-01.exe_")
|
||||
CD = Path(__file__).resolve().parent
|
||||
test_path = CD / ".." / "tests" / "data" / "Practical Malware Analysis Lab 01-01.exe_"
|
||||
assert capa.main.main([test_path, "-b", capa.main.BACKEND_BINJA]) == 0
|
||||
|
||||
|
||||
|
||||
@@ -46,14 +46,14 @@ import io
|
||||
import sys
|
||||
import inspect
|
||||
import logging
|
||||
import os.path
|
||||
import binascii
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
try:
|
||||
sys.path.append(os.path.dirname(__file__))
|
||||
sys.path.append(str(Path(__file__).parent))
|
||||
import fixtures
|
||||
from fixtures import *
|
||||
finally:
|
||||
|
||||
@@ -144,7 +144,7 @@ def assert_meta(meta: rd.Metadata, dst: capa_pb2.Metadata):
|
||||
assert meta.analysis.arch == dst.analysis.arch
|
||||
assert meta.analysis.os == dst.analysis.os
|
||||
assert meta.analysis.extractor == dst.analysis.extractor
|
||||
assert list(meta.analysis.rules) == dst.analysis.rules
|
||||
assert list(str(r) for r in meta.analysis.rules) == dst.analysis.rules
|
||||
assert capa.render.proto.addr_to_pb2(meta.analysis.base_address) == dst.analysis.base_address
|
||||
|
||||
assert len(meta.analysis.layout.functions) == len(dst.analysis.layout.functions)
|
||||
|
||||
@@ -76,12 +76,12 @@ def test_ruleset_cache_save_load():
|
||||
|
||||
path = capa.rules.cache.get_cache_path(cache_dir, id)
|
||||
try:
|
||||
os.remove(path)
|
||||
path.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
capa.rules.cache.cache_ruleset(cache_dir, rs)
|
||||
assert os.path.exists(path)
|
||||
assert path.exists()
|
||||
|
||||
assert capa.rules.cache.load_cached_ruleset(cache_dir, content) is not None
|
||||
|
||||
@@ -93,23 +93,23 @@ def test_ruleset_cache_invalid():
|
||||
cache_dir = capa.rules.cache.get_default_cache_directory()
|
||||
path = capa.rules.cache.get_cache_path(cache_dir, id)
|
||||
try:
|
||||
os.remove(path)
|
||||
path.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
capa.rules.cache.cache_ruleset(cache_dir, rs)
|
||||
assert os.path.exists(path)
|
||||
assert path.exists()
|
||||
|
||||
with open(path, "rb") as f:
|
||||
buf = f.read()
|
||||
buf = path.read_bytes()
|
||||
|
||||
# corrupt the magic header
|
||||
# Corrupt the magic header
|
||||
buf = b"x" + buf[1:]
|
||||
|
||||
with open(path, "wb") as f:
|
||||
f.write(buf)
|
||||
# Write the modified contents back to the file
|
||||
path.write_bytes(buf)
|
||||
|
||||
assert os.path.exists(path)
|
||||
# Check if the file still exists
|
||||
assert path.exists()
|
||||
assert capa.rules.cache.load_cached_ruleset(cache_dir, content) is None
|
||||
# the invalid cache should be deleted
|
||||
assert not os.path.exists(path)
|
||||
assert not path.exists()
|
||||
|
||||
@@ -10,27 +10,28 @@ import os
|
||||
import sys
|
||||
import textwrap
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from fixtures import *
|
||||
|
||||
CD = os.path.dirname(__file__)
|
||||
CD = Path(__file__).resolve().parent
|
||||
|
||||
|
||||
def get_script_path(s):
|
||||
return os.path.join(CD, "..", "scripts", s)
|
||||
return CD / ".." / "scripts" / s
|
||||
|
||||
|
||||
def get_file_path():
|
||||
return os.path.join(CD, "data", "9324d1a8ae37a36ae560c37448c9705a.exe_")
|
||||
return CD / "data" / "9324d1a8ae37a36ae560c37448c9705a.exe_"
|
||||
|
||||
|
||||
def get_rules_path():
|
||||
return os.path.join(CD, "..", "rules")
|
||||
return CD / ".." / "rules"
|
||||
|
||||
|
||||
def get_rule_path():
|
||||
return os.path.join(get_rules_path(), "lib", "allocate-memory.yml")
|
||||
return get_rules_path() / "lib" / "allocate-memory.yml"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -52,13 +53,17 @@ def test_scripts(script, args):
|
||||
assert p.returncode == 0
|
||||
|
||||
|
||||
def test_bulk_process(tmpdir):
|
||||
def test_bulk_process(tmp_path):
|
||||
# create test directory to recursively analyze
|
||||
t = tmpdir.mkdir("test")
|
||||
with open(os.path.join(CD, "data", "ping_täst.exe_"), "rb") as f:
|
||||
t.join("test.exe_").write_binary(f.read())
|
||||
t = tmp_path / "test"
|
||||
t.mkdir()
|
||||
|
||||
p = run_program(get_script_path("bulk-process.py"), [t.dirname])
|
||||
source_file = Path(__file__).resolve().parent / "data" / "ping_täst.exe_"
|
||||
dest_file = t / "test.exe_"
|
||||
|
||||
dest_file.write_bytes(source_file.read_bytes())
|
||||
|
||||
p = run_program(get_script_path("bulk-process.py"), [t.parent])
|
||||
assert p.returncode == 0
|
||||
|
||||
|
||||
@@ -68,19 +73,18 @@ def run_program(script_path, args):
|
||||
return subprocess.run(args, stdout=subprocess.PIPE)
|
||||
|
||||
|
||||
def test_proto_conversion(tmpdir):
|
||||
t = tmpdir.mkdir("proto-test")
|
||||
def test_proto_conversion(tmp_path):
|
||||
t = tmp_path / "proto-test"
|
||||
t.mkdir()
|
||||
json_file = Path(__file__).resolve().parent / "data" / "rd" / "Practical Malware Analysis Lab 01-01.dll_.json"
|
||||
|
||||
json = os.path.join(CD, "data", "rd", "Practical Malware Analysis Lab 01-01.dll_.json")
|
||||
|
||||
p = run_program(get_script_path("proto-from-results.py"), [json])
|
||||
p = run_program(get_script_path("proto-from-results.py"), [json_file])
|
||||
assert p.returncode == 0
|
||||
|
||||
pb = os.path.join(t, "pma.pb")
|
||||
with open(pb, "wb") as f:
|
||||
f.write(p.stdout)
|
||||
pb_file = t / "pma.pb"
|
||||
pb_file.write_bytes(p.stdout)
|
||||
|
||||
p = run_program(get_script_path("proto-to-results.py"), [pb])
|
||||
p = run_program(get_script_path("proto-to-results.py"), [pb_file])
|
||||
assert p.returncode == 0
|
||||
|
||||
assert p.stdout.startswith(b'{\n "meta": ') or p.stdout.startswith(b'{\r\n "meta": ')
|
||||
|
||||
Reference in New Issue
Block a user