| Name | pyrun-injected JSON |
| Version |
0.2.0
JSON |
| download |
| home_page | None |
| Summary | python injector and script runner |
| upload_time | 2025-08-15 11:27:08 |
| maintainer | monkeyman192 |
| docs_url | None |
| author | monkeyman192 |
| requires_python | >=3.9 |
| license | None |
| keywords |
|
| VCS |
|
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
# 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:
```c
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:
```py
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:
```py
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
```py
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.
Raw data
{
"_id": null,
"home_page": null,
"name": "pyrun-injected",
"maintainer": "monkeyman192",
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": null,
"author": "monkeyman192",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/e4/cc/5d2ba23144e61daa187ad33368e16ca69bf6192ed187de1d9321c1f4a9b3/pyrun_injected-0.2.0.tar.gz",
"platform": null,
"description": "# pyrun-injected\r\n\r\nRun python files and scripts in python which has been injected into another process.\r\n\r\n## Why?\r\n\r\nThe usual way of running python scripts with an injected python dll wasn't able to be run on python 3.12 and above.\r\n\r\nIt 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.\r\n\r\n`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.\r\n\r\nFinally, because we are writing this from scratch, we can add some extra functionality into the API for added flexibility.\r\n\r\nTo 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.\r\n\r\n## c API\r\n\r\n`pyrun-injected` provides 3 functions which are useful for calling:\r\n\r\n### `int pyrun_injected.dll.run_file(const char* filename)`:\r\n\r\nRun the specified file name in the injected process.\r\nThis will return 0 on success, and 1 on failure.\r\n\r\n### `int pyrun_injected.dll.run_string(const char* string)`:\r\n\r\nRun the specified string in the injected process.\r\nThis will return 0 on success, and 1 on failure.\r\n\r\n### `int pyrun_injected.dll.run_data(RunData* data)`:\r\n\r\nRun a sequence of strings and/or files sequentially within a single session.\r\nThis will return 0 on success, and 1 on failure.\r\nOn failure, `RunData->error` will have the stacktrace written into it. See below for details.\r\n\r\n`RunData` and associated structs are defined as follows:\r\n\r\n```c\r\ntypedef struct {\r\n char* error_msg; // The actual error message we get from the traceback.\r\n size_t msg_length; // The length of the error message in bytes.\r\n uint32_t bad_string_idx; // The index of the string that had the issue.\r\n} ErrorData;\r\n\r\ntypedef struct {\r\n const char* string_value;\r\n uint8_t is_file;\r\n} StringData;\r\n\r\ntypedef struct {\r\n uint32_t count; // Total size of strings field.\r\n StringData* strings; // The actual string data to be run.\r\n ErrorData* error; // Error data (if an error occurs).\r\n} RunData;\r\n```\r\n\r\nWe pass in a struct like this rather than having multiple arguments because the Windows API only allows us to pass in one argument.\r\n\r\n## python API\r\n\r\nThe python API provides a single class which simplifies injecting and calling strings and files in python.\r\n\r\n### `pyrun_injected.dllinject.pyRunner(pm: pymem.Pymem)`:\r\n\r\nThis function takes an instance of a `Pymem` class as it's only constructor argument. This is because we use pymem a lot internally.\r\nThe running python version and `pyrun-injected` will both be injected into this process.\r\n\r\nOnce initialised, this class provides just one method for running code:\r\n\r\n```py\r\ndef run_data(\r\n self,\r\n strings: list[StringType],\r\n run_in_directory: Optional[str] = None,\r\n inject_sys_path: bool = False,\r\n):\r\n```\r\n\r\nwhere `StringType` is a `NamedTuple` defined as such:\r\n\r\n```py\r\nclass StringType(NamedTuple):\r\n value: str\r\n is_file: bool\r\n```\r\n\r\nThis function allows you to pass in a mix of filepaths and strings to be run in the specified order.\r\nIt 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.\r\n\r\nNote: 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.\r\n\r\n## Example usage\r\n\r\n```py\r\nimport subprocess\r\nimport time\r\nfrom pyrun_injected.dllinject import pyRunner, StringType\r\nimport pymem\r\nimport os.path as op\r\n\r\ncwd = op.dirname(__file__)\r\n\r\nnotepad = subprocess.Popen(['notepad.exe'])\r\ntime.sleep(1)\r\nprint(f\"Running on pid {notepad.pid}. Press ctrl + C to stop.\")\r\npm = pymem.Pymem(\"notepad.exe\")\r\ninjected = pyRunner(pm)\r\nstring = \"\"\"import platform\r\nwith open(\"output.txt\", \"w\") as f:\r\n f.write(f\"hello from {platform.python_version()}\")\r\n\"\"\"\r\ninjected.run_data([StringType(string, False)], run_in_directory=cwd)\r\nnotepad.kill()\r\n```\r\n\r\nThe 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.\r\n",
"bugtrack_url": null,
"license": null,
"summary": "python injector and script runner",
"version": "0.2.0",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "e6fb8df173ec16b5bb46c1802ae630461a91852be55f6497bbb0fd706670f37d",
"md5": "7c15f4177a34008dc56ac51f9442e992",
"sha256": "c2f61181dde95b02eff1d9df2962c6264a5e8f2d66f1f70b1bb40124f2f96219"
},
"downloads": -1,
"filename": "pyrun_injected-0.2.0-cp310-cp310-win_amd64.whl",
"has_sig": false,
"md5_digest": "7c15f4177a34008dc56ac51f9442e992",
"packagetype": "bdist_wheel",
"python_version": "cp310",
"requires_python": ">=3.9",
"size": 16254,
"upload_time": "2025-08-15T11:27:02",
"upload_time_iso_8601": "2025-08-15T11:27:02.918780Z",
"url": "https://files.pythonhosted.org/packages/e6/fb/8df173ec16b5bb46c1802ae630461a91852be55f6497bbb0fd706670f37d/pyrun_injected-0.2.0-cp310-cp310-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "e9bc179b4aae44dd090aac3a43ac060cffdfc6ae06df83051197fa24243a7b5e",
"md5": "6f518f3e4e2f3a48e30344c13b37084a",
"sha256": "62b1903322e652ecee662008da6644690af6d0469bcbf212638ee32f5e3741ea"
},
"downloads": -1,
"filename": "pyrun_injected-0.2.0-cp311-cp311-win_amd64.whl",
"has_sig": false,
"md5_digest": "6f518f3e4e2f3a48e30344c13b37084a",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.9",
"size": 16256,
"upload_time": "2025-08-15T11:27:03",
"upload_time_iso_8601": "2025-08-15T11:27:03.955728Z",
"url": "https://files.pythonhosted.org/packages/e9/bc/179b4aae44dd090aac3a43ac060cffdfc6ae06df83051197fa24243a7b5e/pyrun_injected-0.2.0-cp311-cp311-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "67b0b03ee1d97123f7bad5791b32ec05df6e32e4dd2e2f45fe5584bf5ceef7cc",
"md5": "e1387e6f80ac735dbcbe659d45dadf6f",
"sha256": "fe0a483426beeee7ab1506c67ddf50571bcf115dc436d2828a055e8f6b3d996a"
},
"downloads": -1,
"filename": "pyrun_injected-0.2.0-cp312-cp312-win_amd64.whl",
"has_sig": false,
"md5_digest": "e1387e6f80ac735dbcbe659d45dadf6f",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": ">=3.9",
"size": 16303,
"upload_time": "2025-08-15T11:27:05",
"upload_time_iso_8601": "2025-08-15T11:27:05.114245Z",
"url": "https://files.pythonhosted.org/packages/67/b0/b03ee1d97123f7bad5791b32ec05df6e32e4dd2e2f45fe5584bf5ceef7cc/pyrun_injected-0.2.0-cp312-cp312-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "646d090d650393863b432a3b2518a9abd8e47b63e3bcf33a94d3a8713449784c",
"md5": "463fb95957acabe31549712e70f9812c",
"sha256": "dfe9bef35d819319fac73aa08cf9833c99f1f634325486ef457a60ec1d0bef4c"
},
"downloads": -1,
"filename": "pyrun_injected-0.2.0-cp313-cp313-win_amd64.whl",
"has_sig": false,
"md5_digest": "463fb95957acabe31549712e70f9812c",
"packagetype": "bdist_wheel",
"python_version": "cp313",
"requires_python": ">=3.9",
"size": 16299,
"upload_time": "2025-08-15T11:27:06",
"upload_time_iso_8601": "2025-08-15T11:27:06.154984Z",
"url": "https://files.pythonhosted.org/packages/64/6d/090d650393863b432a3b2518a9abd8e47b63e3bcf33a94d3a8713449784c/pyrun_injected-0.2.0-cp313-cp313-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "4d4ec64200e92f7d37f7a492dde796ace68b0c7ead5247ff43d8c16961b0d7ff",
"md5": "cb0f789ca1032ca4a7d55866aa46b2bb",
"sha256": "e9c76de1413c9b6619a8b9debfb3e5df7c6a215502808e58eb281db67a817d2b"
},
"downloads": -1,
"filename": "pyrun_injected-0.2.0-cp39-cp39-win_amd64.whl",
"has_sig": false,
"md5_digest": "cb0f789ca1032ca4a7d55866aa46b2bb",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": ">=3.9",
"size": 16253,
"upload_time": "2025-08-15T11:27:07",
"upload_time_iso_8601": "2025-08-15T11:27:07.387752Z",
"url": "https://files.pythonhosted.org/packages/4d/4e/c64200e92f7d37f7a492dde796ace68b0c7ead5247ff43d8c16961b0d7ff/pyrun_injected-0.2.0-cp39-cp39-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "e4cc5d2ba23144e61daa187ad33368e16ca69bf6192ed187de1d9321c1f4a9b3",
"md5": "ea9f10724c8df3185c9640970d9d11a1",
"sha256": "46388968f789544318c7b23ab66c7187b8758e485c908f70dff996d0cc2e0db0"
},
"downloads": -1,
"filename": "pyrun_injected-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "ea9f10724c8df3185c9640970d9d11a1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 9907,
"upload_time": "2025-08-15T11:27:08",
"upload_time_iso_8601": "2025-08-15T11:27:08.638802Z",
"url": "https://files.pythonhosted.org/packages/e4/cc/5d2ba23144e61daa187ad33368e16ca69bf6192ed187de1d9321c1f4a9b3/pyrun_injected-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-15 11:27:08",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "pyrun-injected"
}