Skip to main content

pyrun-injected

Run python files and scripts in python which has been injected into another process.

Why?

The usual way of running python scripts with an injected python dll wasn't able to be run on python 3.12 and above.

It seems that this was because the PyRun_SimpleString command needs to be run in the same thread as the Py_InitializeFromConfig and Py_FinalizeEx calls. Using the windows API it doesn't seem possible to execute multiple commands in the same thread.

pyrun-injected fixes this by calling all the necessary functions in one function so that we can call this function and have everything run in the same thread.

Finally, because we are writing this from scratch, we can add some extra functionality into the API for added flexibility.

To this end pyrun-injected uses PyRun_String and PyRun_File under the hood instead of PyRun_SimpleString and PyRun_SimpleFile so that we may get back any exception which is raised by calling the code, thus giving us better visibility on what went wrong if something did.

c API

pyrun-injected provides 3 functions which are useful for calling:

int pyrun_injected.dll.run_file(const char* filename):

Run the specified file name in the injected process. This will return 0 on success, and 1 on failure.

int pyrun_injected.dll.run_string(const char* string):

Run the specified string in the injected process. This will return 0 on success, and 1 on failure.

int pyrun_injected.dll.run_data(RunData* data):

Run a sequence of strings and/or files sequentially within a single session. This will return 0 on success, and 1 on failure. On failure, RunData->error will have the stacktrace written into it. See below for details.

RunData and associated structs are defined as follows:

typedef struct {
    char* error_msg;         // The actual error message we get from the traceback.
    size_t msg_length;       // The length of the error message in bytes.
    uint32_t bad_string_idx; // The index of the string that had the issue.
} ErrorData;

typedef struct {
    const char* string_value;
    uint8_t is_file;
} StringData;

typedef struct {
    uint32_t count;      // Total size of strings field.
    StringData* strings; // The actual string data to be run.
    ErrorData* error;    // Error data (if an error occurs).
} RunData;

We pass in a struct like this rather than having multiple arguments because the Windows API only allows us to pass in one argument.

python API

The python API provides a single class which simplifies injecting and calling strings and files in python.

pyrun_injected.dllinject.pyRunner(pm: pymem.Pymem):

This function takes an instance of a Pymem class as it's only constructor argument. This is because we use pymem a lot internally. The running python version and pyrun-injected will both be injected into this process.

Once initialised, this class provides just one method for running code:

def run_data(
    self,
    strings: list[StringType],
    run_in_directory: Optional[str] = None,
    inject_sys_path: bool = False,
):

where StringType is a NamedTuple defined as such:

class StringType(NamedTuple):
    value: str
    is_file: bool

This function allows you to pass in a mix of filepaths and strings to be run in the specified order. It is strongly recommended that filepaths are provided as absolute paths, unless all are in a specifi directory, in which case that path should be passed in to this function as the run_in_directory argument.

Note: It's important that all python code which is to be run which requires any other piece of code be run together. Once the code finalises after running the strings or files any data will not be persisted.

Example usage

import subprocess
import time
from pyrun_injected.dllinject import pyRunner, StringType
import pymem
import os.path as op

cwd = op.dirname(__file__)

notepad = subprocess.Popen(['notepad.exe'])
time.sleep(1)
print(f"Running on pid {notepad.pid}. Press ctrl + C to stop.")
pm = pymem.Pymem("notepad.exe")
injected = pyRunner(pm)
string = """import platform
with open("output.txt", "w") as f:
    f.write(f"hello from {platform.python_version()}")
"""
injected.run_data([StringType(string, False)], run_in_directory=cwd)
notepad.kill()

The above will start notepad, and then inject python and run the string in it. You should see the output.txt file be produced in the same directory as the file with the version of python used.

Download files

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

Source Distribution

pyrun_injected-0.2.1.tar.gz (72.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyrun_injected-0.2.1-cp314-cp314-win_amd64.whl (19.3 kB view details)

Uploaded CPython 3.14Windows x86-64

pyrun_injected-0.2.1-cp314-cp314-win32.whl (18.5 kB view details)

Uploaded CPython 3.14Windows x86

pyrun_injected-0.2.1-cp313-cp313-win_amd64.whl (19.0 kB view details)

Uploaded CPython 3.13Windows x86-64

pyrun_injected-0.2.1-cp313-cp313-win32.whl (18.2 kB view details)

Uploaded CPython 3.13Windows x86

pyrun_injected-0.2.1-cp312-cp312-win_amd64.whl (19.0 kB view details)

Uploaded CPython 3.12Windows x86-64

pyrun_injected-0.2.1-cp312-cp312-win32.whl (18.2 kB view details)

Uploaded CPython 3.12Windows x86

pyrun_injected-0.2.1-cp311-cp311-win_amd64.whl (19.0 kB view details)

Uploaded CPython 3.11Windows x86-64

pyrun_injected-0.2.1-cp311-cp311-win32.whl (18.3 kB view details)

Uploaded CPython 3.11Windows x86

pyrun_injected-0.2.1-cp310-cp310-win_amd64.whl (19.0 kB view details)

Uploaded CPython 3.10Windows x86-64

pyrun_injected-0.2.1-cp310-cp310-win32.whl (18.3 kB view details)

Uploaded CPython 3.10Windows x86

pyrun_injected-0.2.1-cp39-cp39-win_amd64.whl (19.0 kB view details)

Uploaded CPython 3.9Windows x86-64

pyrun_injected-0.2.1-cp39-cp39-win32.whl (18.3 kB view details)

Uploaded CPython 3.9Windows x86

File details

Details for the file pyrun_injected-0.2.1.tar.gz.

File metadata

  • Download URL: pyrun_injected-0.2.1.tar.gz
  • Upload date:
  • Size: 72.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pyrun_injected-0.2.1.tar.gz
Algorithm Hash digest
SHA256 ccde815fd32c736db1031fb7d32ff9bbd07354c6e1d382320d6046e8efadf01f
MD5 6ce2ecc50f643df7e859733ad95c715e
BLAKE2b-256 fb0075d8674a29dd19975f8d363697e5bc4cfeb617871b0f2ef1f64e511a536a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1.tar.gz:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for pyrun_injected-0.2.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 88103d3ef15419e1d3fe3129a81ac7c43d0f990c54aef9f695e8a2bef95ef5e2
MD5 a169c3c6380f1be248f69d80cafc9f1b
BLAKE2b-256 8e610ce6d953acb819984612478043078f2a17beeb05fbd6212b5418aeaca3bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp314-cp314-win_amd64.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp314-cp314-win32.whl.

File metadata

  • Download URL: pyrun_injected-0.2.1-cp314-cp314-win32.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: CPython 3.14, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pyrun_injected-0.2.1-cp314-cp314-win32.whl
Algorithm Hash digest
SHA256 019d542031251d64685d7cc58f99ab538883fe071d17fb02adc9ad19dae15017
MD5 38afe7b635c1fa38e169f7621067bc19
BLAKE2b-256 99d9e788b484e2c6875bd9e02da32f467353ef5d6512f221ecd23a928e7c33f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp314-cp314-win32.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for pyrun_injected-0.2.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 16d3af62eb4404fa4cd739c7e23b88d20e9b902eb496d1a045cf6ce77b068d79
MD5 9972e595fe144efcbe4ae906fbd4f1bb
BLAKE2b-256 e965704e4ec0d86562400d94b249adf5cf37045dfe8bce710593c0ed9fbf1476

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp313-cp313-win_amd64.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp313-cp313-win32.whl.

File metadata

  • Download URL: pyrun_injected-0.2.1-cp313-cp313-win32.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: CPython 3.13, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pyrun_injected-0.2.1-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 84cc72c1b7d8833cc505de15acb146dc626d3889bc6ac57420cbe09e28e9136d
MD5 b7841e936bcfc0c6feb18cd404cc96d5
BLAKE2b-256 77279b2a014472ea69e091d1560db0cf33e74578bc3f2fd3321345c6a7e78d8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp313-cp313-win32.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for pyrun_injected-0.2.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 19bf2677a37d2c9ac625ed9078507541a1c3513257f7943d2c0634e11be8b116
MD5 2509502711f3907844435fdc68348b01
BLAKE2b-256 11df9e2cc1624ae666dd467eba4aab98e7c11370a21b397b81f7793475696acb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp312-cp312-win_amd64.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp312-cp312-win32.whl.

File metadata

  • Download URL: pyrun_injected-0.2.1-cp312-cp312-win32.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: CPython 3.12, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pyrun_injected-0.2.1-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 1120d8e703fc2b18712af81968597b1723a86a21f9cd2fd2aceb49666e21ce8f
MD5 3669a33b69dd5c4cd28b0f0be4b35c33
BLAKE2b-256 4dd49fa3a92170b59e54dfd6a8bcd441e7265ccd7588f43d668f3e1a870f8957

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp312-cp312-win32.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for pyrun_injected-0.2.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 060f3bd8651e243a2519ad91c16c02026ed6e5147947599547aa948f1e26d6b8
MD5 0e23900dcd4edac24a85695ecdc2ae48
BLAKE2b-256 543bc570c6d6b55cad7acc0af192e5fea979bad9fc6b7c926049ca7f450ef4b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp311-cp311-win_amd64.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp311-cp311-win32.whl.

File metadata

  • Download URL: pyrun_injected-0.2.1-cp311-cp311-win32.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: CPython 3.11, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pyrun_injected-0.2.1-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 06067784fb5a763d3f95ea5fe21934f73ed0c4c32c46413f7a8614ff45872185
MD5 c0d74d19d459f1a31f14080495c1b177
BLAKE2b-256 1771a21731d15c3e19396f6e327fbebb314c8f2335f2a5720036435abe922684

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp311-cp311-win32.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for pyrun_injected-0.2.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6cb213b5ffbf1e7841726a52edb8bfe2b22a570fc0da368e88e784fe9ebb2df5
MD5 8000e190cb4d3a027d567d404e6c8c48
BLAKE2b-256 0a6b0d8b205dcbd3fa3ab000c2dea61b077a28ada2ecb4ee34cc95e7f813d52b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp310-cp310-win_amd64.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp310-cp310-win32.whl.

File metadata

  • Download URL: pyrun_injected-0.2.1-cp310-cp310-win32.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: CPython 3.10, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pyrun_injected-0.2.1-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 ce5f461ecbf9a7eeef7a850bf0afdc8e27db6431a094d4de27312ed377b5ff4a
MD5 5eabb9098fd6c49feae04ea66a9ac6b4
BLAKE2b-256 123f247cd5ce36ff8e8df0810f9b4edc2b839205c18550c37a9dafe5ee95a345

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp310-cp310-win32.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for pyrun_injected-0.2.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 f6bfce64268dc2210603e31de1a4bab29bbe26ce187bbddd14c4e55ca03d3d39
MD5 9bef0669475ed88d6a222547a4de7312
BLAKE2b-256 f66ede26de2d776f707cab5538df125b869c9f69e3049616ed80b3f41d0d67cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp39-cp39-win_amd64.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrun_injected-0.2.1-cp39-cp39-win32.whl.

File metadata

  • Download URL: pyrun_injected-0.2.1-cp39-cp39-win32.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: CPython 3.9, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pyrun_injected-0.2.1-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 3d77c0acafe536d8fe4f737129835d2f26eff79551d46ca1c351cb98c6d4cd6a
MD5 de2f6950f0fab12ccb8b2d1ca2fcfa16
BLAKE2b-256 2877d2a54581e6095ac84aea2c6f25d2c569d4e61d28c2a925f84c566d1c2a99

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrun_injected-0.2.1-cp39-cp39-win32.whl:

Publisher: pipeline.yml on monkeyman192/pyrun_injected

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.2.1 This release

13 files

0.2.0

6 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page