Pymem

1.14.0 · active · verified Fri Apr 17

Pymem is a Python library that simplifies direct memory access and manipulation in Windows processes. It provides functionalities to open processes, read and write various data types to memory, and search for patterns. Currently at version 1.14.0, its release cadence is infrequent, focusing on stability and compatibility with Windows system updates rather than rapid feature additions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open a target process (notepad.exe), retrieve its Process ID (PID) and base memory address, and attempt to read a few bytes from it. It's crucial that the target process is running and the Python script has sufficient permissions (e.g., run as administrator) to interact with it.

import os
import time
from pymem import Pymem, process
from pymem.exception import PymemError

# NOTE: Pymem is Windows-specific and often requires administrator privileges.
# For this example, we'll try to find 'notepad.exe'.
# Make sure Notepad is running before executing this code.

process_name = "notepad.exe"
p = None

try:
    # Find the process ID of notepad.exe
    pid = process.get_pid_by_name(process_name)
    print(f"Found {process_name} with PID: {pid}")

    # Open the process
    p = Pymem(process_name)
    print(f"Successfully opened process {process_name}.")

    # Example: Reading the base address (typically the module's base address)
    # This is often used to calculate offsets to specific memory locations.
    base_address = p.base_address
    print(f"Base address of {process_name}: {hex(base_address)}")

    # For a more meaningful example, one would typically search for a known pattern
    # or address relative to the base address and then read/write data.
    # As a simple demonstration, let's just confirm we can access some memory.
    # Reading a few bytes from the base address itself (usually code):
    try:
        first_bytes = p.read_bytes(base_address, 8)
        print(f"First 8 bytes at base address: {first_bytes.hex()}")
    except PymemError as e:
        print(f"Could not read from base address (might be protected): {e}")

finally:
    if p:
        p.close_process()
        print(f"Closed process handle for {process_name}.")
    else:
        print(f"Could not open process {process_name}. Ensure it's running and you have permissions.")

view raw JSON →