Skip to main content

Python game hacking interface

Project description

pygamehack

pygamehack is a simple, dependency free Python 3.7+ library for reading/writing memory and binary analysis of running processes.

import pygamehack as gh

hack = gh.Hack()
hack.attach("MyProgram.exe")
print(hack.read_u32(0xdeadbeef))

pygamehack provides a simple API to read/write the memory of a process externally to the process. This can be done manually, or using named variables and a type hierarchy that that you define. You can entirely replicate the structure of an arbitrary compiled program in Python using a familiar dataclass-based type annotation syntax.

The goal of pygamehack is to prevent you from ever having to repeat tedious/manual labor tasks that are common when reverse engineering binary programs.

pygamehack also provides various tools for binary analysis, and defines a workflow for fully automating the tedious work of updating hard-coded values in a reverse-engineering project.

pygamehack also understands the file formats of some common reverse engineering tools (CheatEngine, ReClassNet) and allows you to quickly get started hacking in Python without having to redo any tedious manual labor.

Installation

pygamehack can be installed with pip.

pip install pygamehack

Features

  • Read/write process memory externally to the process.
  • Replicate a program's structure externally to the program
  • Named variables in Python that modify the memory of a target process
  • Familiar, dataclass based syntax in Python to define the structure of any compiled program

Core

pygamehack defines a few primary types that can then be used to build up the structure of any program. These types are Hack, Address, Buffer. There is also the concept of a Variable, which is an abstract representation of a typed memory region. pygamehack provides variable implementations for all of the basic numeric types, as well as strings, buffers and arrays. For more details see the Variable section.

Basic Types

A basic type is a numeric type. All naming conventions for types are based on the names of the basic types. pygamehack defines the following basic types:

Name C-type Size (bytes)
i8 int8_t 1
i16 int16_t 2
i32 int32_t 4
i64 int64_t 8
u8 uint8_t 1
u16 uint16_t 2
u32 uint32_t 4
u64 uint64_t 8
bool bool 1
float float 4
double double 8
usize size_t 4 or 8
ptr void* 4 or 8

There are 2 more types - str and buf - that have names that are used in the previously mentioned naming conventions, but are not numeric types. str represents a string (i.e. an array of characters) and buf represents an array of u8.

Hack

This is the primary interface to an external process' memory. Before reading/writing memory you must first attach to the target process

import pygamehack as gh

hack = gh.Hack()
hack.attach("MyProgram.exe")
# or you can use the Process ID
hack.attach(12345)

Reading/writing memory

For every basic type, there are corresponding 'Hack.read_NAME' and 'Hack.write_NAME' methods that can be used to read/write memory.

import pygamehack as gh
hack = gh.Hack()
# Read 
value = hack.read_u32(0xdeadbeef)
# Write
hack.write_u32(0xdeadbeef, 999)

Scanning memory

Address

This type is the representation of a memory address (I.e. A pointer) in the target process' memory. Addresses that depend on program parameters or runtime values must be loaded. There are 3 types of addresses:

  • Manual
  • Static
  • Dynamic

Manual

A manual address is what it sounds like, a hard-coded numeric value that represents a memory address in the target process. To create a manual address you need to provide the value of the address.

import pygamehack as gh
hack = gh.Hack()

address = gh.Address(hack, 0xdeadbeef)

NOTE: Because of practices like ASLR (address space layout randomization), it is recommended that you only use manual addresses when debugging or prototyping, as the address will likely stop working the next time that you run the target program.

Static

A static address represents an offset into the memory region of a process, or any of the dynamic libraries loaded into the target process' memory space. To create a static address, you need a module name and an offset.

import pygamehack as gh
hack = gh.Hack()

address = gh.Address(hack, "MyProgram.exe", 0xbeef)

Static addresses are the starting point of any program, and can be used to access data that has been compiled directly into the program binary. You can use static addresses as a base for all of the other interesting addresses.

Dynamic

A dynamic address represents an arbitrary memory address that depends on the value of another address in the target process' memory. The address is calculated by following an offset path in memory starting with the value of the parent address. To create a dynamic address, you need a parent address and a list of offsets.

import pygamehack as gh
hack = gh.Hack()

address = gh.Address(hack, "MyProgram.exe", 0xbeef)
d_address = gh.Address(address, [0x4])

Address loading

Each address type is created with different arguments and will respond differently to being loaded:

Type Requires Operation
Manual Nothing None
Static Base Address Query base address using library name
Dynamic Valid Parent Address (Load parent address), follow offset path

To load an address:

import pygamehack as gh
hack = gh.Hack()

address = gh.Address(hack, "MyProgram.exe", 0xbeef)
# Load
address.load()
# Get loaded value
print(address.value)
# Check if valid
print(address.valid)

Automatically loading addresses

When you have a lot of address objects and want to update them all in bulk, or you simply want to avoid calling 'Address.load()' every time you want to use an address, you can enable address auto loading.

When you call 'Hack.update()', every address that has been marked for auto loading will reload its value. You can conditionally load subsets of addresses by setting the 'Address.<>TODO<>' property.

Buffer

A buffer is a handle to dynamically allocated memory. Since memory cannot be accessed directly in Python, buffers provide a convenient interface for reading typed values from arbitrary memory. There are two types of buffers, an owning buffer and a view buffer.

Owning buffer

An owning buffer must explicitly read from the target process memory to update its contents. It will never observe changes in the contents of another buffer's memory.

To create an owning buffer, you only need to provide the size of the buffer.

import pygamehack as gh
hack = gh.Hack()

buffer = gh.Buffer(hack, 32)

View buffer

A view buffer is a view into the memory of another buffer, and will observe changes to its memory when its parent reads from the target process. View buffers also allow for a number of useful internal optimizations.

To create a view buffer, you must provide a parent buffer, an offset into the parent buffer, and the size of the buffer.

import pygamehack as gh
hack = gh.Hack()

buffer = gh.Buffer(hack, 32)
v_buffer = gh.Buffer(buffer, 8, 16)

Reading/writing buffers

Buffers are like memory middle-men in pygamehack. Like a Hack, a Buffer has 'read_' and 'write_' methods for all of the basic types. Unlike a Hack, calling 'read_' and 'write_' on a Buffer will not read/write the target process memory. These methods operate on the buffer's local storage, and if you want to read/write the target process memory you must do so explicitly using the 'Buffer.read_from()' and 'Buffer.write_to()' methods.

import pygamehack as gh
hack = gh.Hack()

# ... attach ...

addr = 0xdeadbeef
buffer = gh.Buffer(hack, 32)

# 0 since buffer has not read from process
assert buffer.read_u32(0) == 0
# Read entire content of buffer from process
buffer.read_from(addr)
# Buffer has data ready to read
value = buffer.read_u32(0)

# Write a new value to local storage
buffer.write_u32(0, 5)
# Process unaffected
assert hack.read_u32(addr) == value
# Write entire content of buffer to process
buffer.write_to(addr)
# Process has been updated
assert hack.read_u32(addr) == 5

Variables

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

pygamehack-1.0.10.tar.gz (53.4 kB view details)

Uploaded Source

Built Distributions

pygamehack-1.0.10-pp37-pypy37_pp73-win_amd64.whl (437.0 kB view details)

Uploaded PyPy Windows x86-64

pygamehack-1.0.10-cp310-cp310-win_amd64.whl (437.8 kB view details)

Uploaded CPython 3.10 Windows x86-64

pygamehack-1.0.10-cp39-cp39-win_amd64.whl (424.7 kB view details)

Uploaded CPython 3.9 Windows x86-64

pygamehack-1.0.10-cp38-cp38-win_amd64.whl (437.9 kB view details)

Uploaded CPython 3.8 Windows x86-64

pygamehack-1.0.10-cp37-cp37m-win_amd64.whl (435.4 kB view details)

Uploaded CPython 3.7m Windows x86-64

File details

Details for the file pygamehack-1.0.10.tar.gz.

File metadata

  • Download URL: pygamehack-1.0.10.tar.gz
  • Upload date:
  • Size: 53.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for pygamehack-1.0.10.tar.gz
Algorithm Hash digest
SHA256 ceca955726158a5f24ef973d105c956a6c0dd4a1dee86aa890114cbcc80fa123
MD5 395ce014131c9f7f32e95dbfafa178e2
BLAKE2b-256 9c06c662e3b58b9294fde85407ceb12e86d14cb2c5f1ea88cd67b38561bb3a87

See more details on using hashes here.

File details

Details for the file pygamehack-1.0.10-pp37-pypy37_pp73-win_amd64.whl.

File metadata

  • Download URL: pygamehack-1.0.10-pp37-pypy37_pp73-win_amd64.whl
  • Upload date:
  • Size: 437.0 kB
  • Tags: PyPy, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for pygamehack-1.0.10-pp37-pypy37_pp73-win_amd64.whl
Algorithm Hash digest
SHA256 721148d1799e348aabf7a155a797265bf629180463408558bd4d16e76ec64693
MD5 376ac2c2de92859f6c690f33b03ca230
BLAKE2b-256 8c73124882fe967e4617e316427275b0cc0909d0cecbdee7a9d57f5032621933

See more details on using hashes here.

File details

Details for the file pygamehack-1.0.10-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pygamehack-1.0.10-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 437.8 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for pygamehack-1.0.10-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 01d449602657aec5b4d535bc80d4e8b5e57a2e4c97eaba49e0de7cb3855a6f86
MD5 65a8a837ad9daaaa1f9fdb6c047e8d11
BLAKE2b-256 f1c428e1f4fa2c38dd2b711ad59eaa6b98a0e7e7e5ebca860f8e481538c9526a

See more details on using hashes here.

File details

Details for the file pygamehack-1.0.10-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: pygamehack-1.0.10-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 424.7 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for pygamehack-1.0.10-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 61a0428153538e4f3324b63fa48dc54169c352966f1a327bbb897becf673f7a7
MD5 ffa87655c191eb47fac07981225af073
BLAKE2b-256 700cea90e37e7edcf76b56203888272eb208feb68aee300974594255516fe348

See more details on using hashes here.

File details

Details for the file pygamehack-1.0.10-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: pygamehack-1.0.10-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 437.9 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for pygamehack-1.0.10-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 8db70101bf89eeb64e56057d67998fe9f5978d02bdace901d4e3c2a19ca4413b
MD5 43b3de45756ba6f597a3eeea280c33dd
BLAKE2b-256 572b8e1e112aec043f159619385a8edced00d1cea52ad819214fbf774998d348

See more details on using hashes here.

File details

Details for the file pygamehack-1.0.10-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: pygamehack-1.0.10-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 435.4 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for pygamehack-1.0.10-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 47017c0f281688121987aebd737fed8ef4e99cb940f243415270f43bd11b42ba
MD5 3301898b8f76149fc63ab31b3da5425c
BLAKE2b-256 2c52c44ec3dc2e94aaaf5d8b4244f5da7e995a8c83555d0fa4c668b57951228c

See more details on using hashes here.

Supported by

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