From 9d062176c95e6bd3a9023d87c20c91d57ba95711 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Tue, 10 Oct 2023 17:55:01 -0700 Subject: [PATCH] Remove manadatory busybox injection --- binharness/common/busybox.py | 1 - binharness/common/qemu.py | 13 ++++++++----- binharness/types/environment.py | 27 +-------------------------- binharness/types/injection.py | 13 +++---------- binharness/util.py | 7 +++++++ 5 files changed, 19 insertions(+), 42 deletions(-) diff --git a/binharness/common/busybox.py b/binharness/common/busybox.py index 30f84d6..03d3ced 100644 --- a/binharness/common/busybox.py +++ b/binharness/common/busybox.py @@ -40,7 +40,6 @@ def install(self: BusyboxInjection, environment: Environment) -> None: self._environment = environment else: raise - environment.busybox_injection = self def mktemp( self: BusyboxInjection, directory: bool = False # noqa: FBT001, FBT002 diff --git a/binharness/common/qemu.py b/binharness/common/qemu.py index 2e672c8..f180326 100644 --- a/binharness/common/qemu.py +++ b/binharness/common/qemu.py @@ -3,11 +3,12 @@ from __future__ import annotations from pathlib import Path -from typing import TYPE_CHECKING, Generator +from typing import TYPE_CHECKING, Generator, cast +from binharness import IO from binharness.types.executor import InjectableExecutor from binharness.types.injection import ExecutableInjection -from binharness.util import read_lines +from binharness.util import generate_random_suffix, read_lines if TYPE_CHECKING: from binharness import Process, Target @@ -35,7 +36,9 @@ def run_with_log( cwd: Path | None = None, ) -> tuple[Process, Generator[bytes, None, None]]: """Run the injection in the environment and return a log generator.""" - logfile = self.environment.busybox_injection.mktemp() + logfile = ( + self.environment.get_tempdir() / f"qemu-{generate_random_suffix()}.log" + ) proc = self.run( "-D", str(logfile), @@ -45,8 +48,8 @@ def run_with_log( ) def log_generator() -> Generator[bytes, None, None]: - cat_proc = self.environment.busybox_injection.cat(logfile) - yield from read_lines(cat_proc.stdout) + file = self.environment.open_file(logfile, "rb") + yield from read_lines(cast(IO[bytes], file)) # Cleanup log file self.environment.run_command("rm", logfile) diff --git a/binharness/types/environment.py b/binharness/types/environment.py index 4c9f12e..6bb305b 100644 --- a/binharness/types/environment.py +++ b/binharness/types/environment.py @@ -7,11 +7,7 @@ if TYPE_CHECKING: from pathlib import Path - from binharness import IO, BusyboxInjection, Process - - -class BusyboxInjectionNotInstalledError(Exception): - """BusyBox is not installed in the environment.""" + from binharness import IO, Process class Environment(ABC): @@ -22,27 +18,6 @@ class Environment(ABC): well as running commands. """ - _busybox_injection: BusyboxInjection | None - - def __init__(self: Environment) -> None: - """Create an Environment.""" - self._busybox_injection = None - - @property - def busybox_injection(self: Environment) -> BusyboxInjection: - """Return the BusyboxInjection for the environment.""" - if self._busybox_injection is None: - raise BusyboxInjectionNotInstalledError - return self._busybox_injection - - @busybox_injection.setter - def busybox_injection( - self: Environment, - busybox_injection: BusyboxInjection, - ) -> None: - """Set the BusyboxInjection for the environment.""" - self._busybox_injection = busybox_injection - @abstractmethod def run_command( self: Environment, diff --git a/binharness/types/injection.py b/binharness/types/injection.py index 85f00c4..b1bb905 100644 --- a/binharness/types/injection.py +++ b/binharness/types/injection.py @@ -3,8 +3,6 @@ from typing import TYPE_CHECKING -from binharness.types.environment import BusyboxInjectionNotInstalledError - if TYPE_CHECKING: from pathlib import Path @@ -47,14 +45,9 @@ def install(self: Injection, environment: Environment) -> None: if self._environment is not None: raise InjectionAlreadyInstalledError if self.env_path is None: - try: - bb_injection = environment.busybox_injection - except BusyboxInjectionNotInstalledError: - from binharness.common.busybox import BusyboxInjection - - bb_injection = BusyboxInjection() - bb_injection.install(environment) - self.env_path = bb_injection.mktemp(directory=True) + self.env_path = environment.get_tempdir() / ( + self.__class__.__name__ + ".bh-inj" + ) environment.inject_files([(self.host_path, self.env_path)]) self._environment = environment diff --git a/binharness/util.py b/binharness/util.py index 55247b2..ee06a53 100644 --- a/binharness/util.py +++ b/binharness/util.py @@ -1,7 +1,9 @@ """binharness.util - Utility functions for binharness.""" from __future__ import annotations +import random import shlex +import string from pathlib import Path from typing import TYPE_CHECKING, Generator, Sequence @@ -43,3 +45,8 @@ def read_lines(file: IO[bytes]) -> Generator[bytes, None, None]: while b"\n" in buffer: line, buffer = buffer.split(b"\n", 1) yield line + b"\n" + + +def generate_random_suffix(n: int = 6) -> str: + """Generate a random suffix.""" + return "".join(random.choices(string.ascii_letters, k=n)) # noqa: S311