Skip to main content

A Python library for memory manipulation, code injection and function hooking

Project description

PyJectify logo

PyJectify

A Python library for memory manipulation, code injection and function hooking.

Quick start

PyJectify is available on Pypi.

Alternatively, you can download releases from GitHub or clone the project.

Documentation is available at https://petitoto.github.io/pyjectify/

Features

Windows

Core

  • Allocate / Free / Read / Write memory
  • Create threads
  • List loaded modules
  • PE parser
  • Use kernel32 or ntdll functions

Modules

  • MemScan: scan memory using regex patterns
  • Inject: load library, from disk (remote LoadLibrary) or from memory (fully map the DLL into the remote process)
  • Hook: set up inline hooks in the target process
  • PythonLib: embed python into a remote process

Utils

  • Syscall: Parse syscall codes from ntdll.dll (from the loaded library or from the disk), and produce a ntdll-like object which can be used by the Inject module to use direct syscalls
  • ApiSetSchema: parse Windows ApiSet

Example

import pyjectify

# Open notepad.exe process (only the first found if multiple instances of notepad are running)
notepad = pyjectify.byName('Notepad.exe')[0]

# Search for the secret in notepad memory:
pattern = rb's;e;c;r;e;t;( ;i;s;)?:; (;.){10}'.replace(b';', b'\x00') # ; -> \x00 just to keep the pattern readable (notepad use wide strings)
addrs = notepad.memscan.scan(pattern)
for addr in addrs:
    secret = notepad.process.read(addr, 50)
    print('[+] Found secret:', str(secret.replace(b'\x00', b'')))
    notepad.process.write(addr, b'*\x00'*25) # let's hide the secret

# Inject Python DLL, from bytes loaded in memory
notepad.pythonlib.python_mod = notepad.inject.load_library("python3xx.dll")
notepad.pythonlib.python_mod.parse_exports()

# Run some Python code from notepad
notepad.pythonlib.initialize()
notepad.pythonlib.exec('import os; os.system("calc.exe")')

# Let's hook GetClipboardData!
# Step 1: define our new function
pycode = """
import ctypes
def GetClipboardData(uFormat:ctypes.c_uint) -> ctypes.c_void_p:
  ctypes.windll.user32.MessageBoxW(0, "I hooked you :D", "MyNewGetClipboardData", 0)
  return o_GetClipboardData(uFormat)
"""
notepad.pythonlib.exec(pycode)

# Step 2: get original function address and setup a trampoline (of 15 bytes size)
user32 = notepad.process.get_module('user32.dll')
user32.parse_exports()
oaddr = user32.exports['GetClipboardData'] + user32.base_addr
trampoline_addr = notepad.hook.trampoline(oaddr, 15)

# Step 3: prepare Python function hooking, ie create o_GetClipboardData and get ou Python GetClipboardData address
hook_addr = notepad.pythonlib.prepare_hook('GetClipboardData', trampoline_addr)

# Step 4: inline hook
notepad.hook.inline(oaddr, hook_addr)

# A final fix (for now)
# To prevent Python API to clean our Python hook, we need to exit a PyRun_SimpleString abruptly, or keeping it open using a sleep
# This issue is investigated and should be fixed in the next release
notepad.pythonlib.exec('ctypes.windll.kernel32.ExitThread(0)')

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

pyjectify-0.1.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

pyjectify-0.1-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

Details for the file pyjectify-0.1.tar.gz.

File metadata

  • Download URL: pyjectify-0.1.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pyjectify-0.1.tar.gz
Algorithm Hash digest
SHA256 5fc56915edb2443c6010024c1583b974066786d3b392dd4f7327b5a2edb93208
MD5 58cb25f21222fe5c6c3bf1c2e757b5dd
BLAKE2b-256 52b3cb05a59a297b0bf12d62615fff303069070f86cd4d17e93c3154dbda880f

See more details on using hashes here.

File details

Details for the file pyjectify-0.1-py3-none-any.whl.

File metadata

  • Download URL: pyjectify-0.1-py3-none-any.whl
  • Upload date:
  • Size: 22.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for pyjectify-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5d3f3a6b8032ea566100468b53801f73303a5302ce68aa35fdbfc63d8b7e2b0
MD5 e393111a3a98a04f18ec41285c88b819
BLAKE2b-256 0f911d19d5faf8fb7f9cabaf124d9ac8eca788290a9f9c6f8b0faf691777ef5f

See more details on using hashes here.

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