kmaork/hypno

injector_inject returned -5: dlopen failed

Closed this issue · 2 comments

Python 3.11.2 (main, Feb 12 2023, 00:48:52) [GCC 12.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import madbg

In [2]: madbg.attach_to_process(3453466)
---------------------------------------------------------------------------
InjectorError                             Traceback (most recent call last)
Cell In [2], line 1
----> 1 madbg.attach_to_process(3453466)

File /usr/lib/python3/dist-packages/madbg/api.py:34, in attach_to_process(pid, port, connect_timeout)
     32 def attach_to_process(pid: int, port=DEFAULT_PORT, connect_timeout=DEFAULT_CONNECT_TIMEOUT):
     33     ip = '127.0.0.1'
---> 34     _inject_set_trace(pid, ip, port)
     35     connect_to_debugger(ip, port, timeout=connect_timeout)

File /usr/lib/python3/dist-packages/madbg/api.py:28, in _inject_set_trace(pid, ip, port)
     26 assert isinstance(port, int)
     27 sig_num = DEBUGGER_CONNECTED_SIGNAL.value
---> 28 inject_py(pid, f'__import__("signal").signal({sig_num},lambda _,f:__import__("madbg").set_trace(f,"{ip}",{port}))')
     29 os.kill(pid, sig_num)

File /usr/lib/python3/dist-packages/hypno/hypno.py:42, in inject_py(pid, python_code)
     40     temp.write(lib[code_addr + len(python_code) + 1:])
     41 try:
---> 42     inject(pid, str(temp.name))
     43 except InjectorError as e:
     44     # On Windows we are failing the load on purpose so the library will be immediately unloaded
     45     if not WINDOWS or e.ret_val != -5 or e.error_str != \
     46             "LoadLibrary in the target process failed: " \
     47             "A dynamic link library (DLL) initialization routine failed.":

File /usr/lib/python3/dist-packages/pyinjector/pyinjector.py:103, in inject(pid, library_path)
    101 injector = Injector.attach(pid)
    102 try:
--> 103     return injector.inject(library_path)
    104 finally:
    105     injector.detach()

File /usr/lib/python3/dist-packages/pyinjector/pyinjector.py:87, in Injector.inject(self, library_path)
     85 assert os.path.isfile(library_path), f'Library not found at "{library_path.decode()}"'
     86 handle = c_void_p()
---> 87 call_c_func(libinjector.injector_inject, self.injector_p, library_path, pointer(handle))
     88 return handle.value

File /usr/lib/python3/dist-packages/pyinjector/pyinjector.py:66, in call_c_func(func, exception_map, *args)
     64 exception_map = {} if exception_map is None else exception_map
     65 exception_cls = exception_map.get(ret, InjectorError)
---> 66 raise exception_cls(func.__name__, ret, libinjector.injector_error())

InjectorError: injector_inject returned -5: dlopen failed

Trying to load the lib (saved from /tmp) directly (am i doing this right 😅 ) from ipython3 does not work, too:

In [1]: import ctypes

In [2]: ctypes.CDLL?

In [3]: ctypes.CDLL('/tmp/hypno61cl3r9b.so')
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In [3], line 1
----> 1 ctypes.CDLL('/tmp/hypno61cl3r9b.so')

File /usr/lib/python3.11/ctypes/__init__.py:376, in CDLL.__init__(self, name, mode, handle, use_errno, use_last_error, winmode)
    373 self._FuncPtr = _FuncPtr
    375 if handle is None:
--> 376     self._handle = _dlopen(self._name, mode)
    377 else:
    378     self._handle = handle

OSError: /tmp/hypno61cl3r9b.so: failed to map segment from shared object

This is on debian unstable with packages created using stdeb:

ii  python3-hypno      1.0.0-1      amd64        A tool/library allowing to inject python code into a running
ii  python3-madbg      1.3.1-1      all          A fully-featured remote debugger for python
ii  python3-pyinjector 1.1.1-1      amd64        A tool/library allowing dynamic library injection into runni

I had to disable some tests to get it to build, maybe that's part of the problem...?

kmaork commented

Hmmm did you create the stdeb packages? Does it work if you install the packages with pip instead of stdeb?

kmaork commented

Probably fixed in hypno==1.0.2