Skip to main content

A tool/library allowing to inject python code into a running python process.

Project description

Hypno

PyPI version PyPI Supported Python Versions GitHub license Tests (GitHub Actions)

A tool/library allowing to inject python code into a running python process.

Installation

pip install hypno

Both source distributions and manylinux1 wheels are upoloaded to pypi for every release.

Usage

CLI

hypno <pid> <python_code>

API

from hypno import inject_py

inject_py(pid, python_code)

Example

This example runs a python program that prints its pid, and then attaches to the newly created process and injects it with another print statement using hypno.

python -c "import os, time; print('Hello from', os.getpid()); time.sleep(0.5)" &\
hypno $! "import os; print('Hello again from', os.getpid())"

How it works

We use the pyinjector library as a primitive allowing us to inject arbitrary code into processes, because it is simple to use and can be installed easily and on most linux machines.

pyinjector needs an .so to inject, so we compile a minimal library as a C-extension. That library calls PyRun_SimpleString upon initialization.

In order to allow injecting a process multiple times, the injected library must have a unique path for each injection. We do that by copying the library for every call.

As we don't want to recompile the library for every injection to replace the executed string, and we already control the path from which our library is injected, we encode the injected python code into the the injected libraries' paths. When the injected library is loaded, it finds its own name using dladdr and parses the python code from it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hypno-0.1.2.zip (8.9 kB view hashes)

Uploaded Source

Built Distributions

hypno-0.1.2-cp38-cp38-manylinux2014_x86_64.whl (17.7 kB view hashes)

Uploaded CPython 3.8

hypno-0.1.2-cp38-cp38-manylinux1_x86_64.whl (17.7 kB view hashes)

Uploaded CPython 3.8

hypno-0.1.2-cp37-cp37m-manylinux2014_x86_64.whl (18.8 kB view hashes)

Uploaded CPython 3.7m

hypno-0.1.2-cp37-cp37m-manylinux1_x86_64.whl (18.8 kB view hashes)

Uploaded CPython 3.7m

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page