Skip to content

Instantly share code, notes, and snippets.

@flounderK
Created March 2, 2024 15:56
Show Gist options
  • Save flounderK/a2d09e2afb20fde6b55abc85e67b58d5 to your computer and use it in GitHub Desktop.
Save flounderK/a2d09e2afb20fde6b55abc85e67b58d5 to your computer and use it in GitHub Desktop.
python ctypes primitives
import ctypes
def arb_read(addr, size=4):
return bytes((ctypes.c_byte*size).from_address(addr))
def arb_write(addr, byts):
(ctypes.c_byte*len(byts)).from_address(addr)[:] = byts
def rough_addr_of(a):
"""address of the object, not the value"""
return id(a)
def win_arb_exec(sc):
OldProtect = ctypes.wintypes.PDWORD(ctypes.c_ulong(0))
ctypes.windll.kernel32.VirtualProtect.argtypes = [ctypes.wintypes.LPVOID, ctypes.c_size_t, ctypes.wintypes.DWORD, ctypes.wintypes.LPVOID]
sc_page = id(sc) & ~0xfff
x = ctypes.windll.kernel32.VirtualProtect(sc_page, 0x1000, 0x40, OldProtect)
offset_to_sc = bytes((ctypes.c_byte*(128+len(sc))).from_address(id(sc))).find(sc)
func = ctypes.CFUNCTYPE(None)(id(sc)+offset_to_sc)
func()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment