From c0b0324a127d364e609e92719364bf715c99bea7 Mon Sep 17 00:00:00 2001 From: zx Date: Fri, 25 Oct 2024 23:04:40 -0400 Subject: [PATCH] chore: move injection functions to MainExecutable --- cyan/tbhtypes/executable.py | 37 ------------------------------ cyan/tbhtypes/main_executable.py | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/cyan/tbhtypes/executable.py b/cyan/tbhtypes/executable.py index 5d0975d..b83c026 100644 --- a/cyan/tbhtypes/executable.py +++ b/cyan/tbhtypes/executable.py @@ -1,12 +1,6 @@ import os import sys import subprocess -from typing import Optional - -try: - import lief -except Exception: - pass from cyan import tbhutils @@ -44,12 +38,6 @@ class Executable: self.path = path self.bn = os.path.basename(path) - self.inj: Optional = None # type: ignore - - if os.path.isfile(self.idylib): - self.inj_func = self.idyl_inject - else: - self.inj_func = self.lief_inject def is_encrypted(self) -> bool: proc = subprocess.run( @@ -77,31 +65,6 @@ class Executable: stderr=subprocess.DEVNULL ) - def lief_inject(self, cmd: str) -> None: - if self.inj is None: # type: ignore - try: - lief.logging.disable() # type: ignore - except Exception: - sys.exit("[!] did you forget to install lief?") - - self.inj = lief.parse(self.path) # type: ignore - - try: - self.inj.add(lief.MachO.DylibCommand.weak_lib(cmd)) # type: ignore - except AttributeError: - sys.exit("[!] couldn't add LC (lief), did you use a valid app?") - - def idyl_inject(self, cmd: str) -> None: - proc = subprocess.run( - [ - self.idylib, "--weak", "--inplace", "--strip-codesig", "--all-yes", - cmd, self.path - ], capture_output=True, text=True - ) - - if proc.returncode != 0: - sys.exit(f"[!] couldn't add LC (insert_dylib), error:\n{proc.stderr}") - def fix_dependencies(self, tweaks: dict[str, str], need: set[str]) -> None: self.remove_signature() diff --git a/cyan/tbhtypes/main_executable.py b/cyan/tbhtypes/main_executable.py index 9f560ff..24d4d8a 100644 --- a/cyan/tbhtypes/main_executable.py +++ b/cyan/tbhtypes/main_executable.py @@ -1,6 +1,13 @@ import os +import sys import shutil import subprocess +from typing import Optional + +try: + import lief +except Exception: + pass from cyan import tbhutils from .executable import Executable @@ -10,6 +17,13 @@ class MainExecutable(Executable): super().__init__(path) self.bundle_path = bundle_path + self.inj: Optional = None # type: ignore + + if os.path.isfile(self.idylib): + self.inj_func = self.idyl_inject + else: + self.inj_func = self.lief_inject + def inject(self, tweaks: dict[str, str], tmpdir: str) -> None: ENT_PATH = f"{self.bundle_path}/cyan.entitlements" PLUGINS_DIR = f"{self.bundle_path}/PlugIns" @@ -115,3 +129,28 @@ class MainExecutable(Executable): [self.ldid, f"-S{entitlements}", "-M", self.path] ).returncode == 0 + def lief_inject(self, cmd: str) -> None: + if self.inj is None: # type: ignore + try: + lief.logging.disable() # type: ignore + except Exception: + sys.exit("[!] did you forget to install lief?") + + self.inj = lief.parse(self.path) # type: ignore + + try: + self.inj.add(lief.MachO.DylibCommand.weak_lib(cmd)) # type: ignore + except AttributeError: + sys.exit("[!] couldn't add LC (lief), did you use a valid app?") + + def idyl_inject(self, cmd: str) -> None: + proc = subprocess.run( + [ + self.idylib, "--weak", "--inplace", "--all-yes", + cmd, self.path + ], capture_output=True, text=True + ) + + if proc.returncode != 0: + sys.exit(f"[!] couldn't add LC (insert_dylib), error:\n{proc.stderr}") +