Skip to content

Commit

Permalink
fix: improved efficiency of docker mount in software plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jstucke committed Sep 27, 2024
1 parent 914fa1f commit ff56862
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get_version_for_component(self, result, file_object: FileObject):
key_strings = [s for _, _, s in result['strings'] if '%s' in s]
if key_strings:
versions.update(
extract_data_from_ghidra(file_object.binary, key_strings, config.backend.docker_mount_base_dir)
extract_data_from_ghidra(file_object.file_path, key_strings, config.backend.docker_mount_base_dir)
)
if '' in versions and len(versions) > 1: # if there are actual version results, remove the "empty" result
versions.remove('')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@

from helperFunctions.docker import run_docker_container

CONTAINER_TARGET_PATH = '/work'
CONTAINER_TARGET_PATH = Path('/work')
INPUT_PATH = CONTAINER_TARGET_PATH / 'ghidra_input'
DOCKER_IMAGE = 'fact/format_string_resolver'
DOCKER_OUTPUT_FILE = 'ghidra_output.json'
TIMEOUT = 300
KEY_FILE = 'key_file'


def extract_data_from_ghidra(input_file_data: bytes, key_strings: list[str], path: str) -> list[str]:
def extract_data_from_ghidra(file_path: str, key_strings: list[str], path: str) -> list[str]:
with TemporaryDirectory(prefix='FSR_', dir=path) as tmp_dir:
tmp_dir_path = Path(tmp_dir)
ghidra_input_file = tmp_dir_path / 'ghidra_input'
(tmp_dir_path / KEY_FILE).write_text(json.dumps(key_strings))
ghidra_input_file.write_bytes(input_file_data)
with suppress(DockerException, TimeoutError):
run_docker_container(
DOCKER_IMAGE,
logging_label='FSR',
timeout=TIMEOUT,
command=f'/work/ghidra_input {CONTAINER_TARGET_PATH}',
command=f'{INPUT_PATH} {CONTAINER_TARGET_PATH}',
mounts=[
Mount(CONTAINER_TARGET_PATH, tmp_dir, type='bind'),
Mount(str(CONTAINER_TARGET_PATH), tmp_dir, type='bind'),
Mount(str(INPUT_PATH), file_path, type='bind', read_only=True),
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def test_extract_data_from_ghidra(backend_config):
key_string = 'get_version v%s'
test_file = Path(__file__).parent / 'data' / 'format_string_arm-linux-gnueabihf'
result = extract_data_from_ghidra(test_file.read_bytes(), [key_string], str(backend_config.docker_mount_base_dir))
result = extract_data_from_ghidra(str(test_file), [key_string], str(backend_config.docker_mount_base_dir))
assert len(result) == 1
assert result == ['1.2.3']

Expand Down

0 comments on commit ff56862

Please sign in to comment.