Last active
March 25, 2025 10:26
-
-
Save ay0ks/d8852a29e7758489b7d5ae98b99d15cc to your computer and use it in GitHub Desktop.
PoC Python virtual memory manipulation utility classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import * | |
class memaddr: | |
def __init__(self, address): | |
self.address = address | |
def write(self, data): | |
a = { | |
int: c_int, | |
float: c_float, | |
str: c_char_p, | |
bytes: c_wchar_p, | |
# ... | |
}.get(type(data)).from_address( | |
self.address + type(data).__basicsize__ | |
).value = data | |
if a is not None: | |
return 0 | |
return 1 | |
def get(self, item): | |
return (item).from_address(self.address) | |
def __repr__(self): | |
return "[memoryaddr: %s]" % hex(self.address) | |
@type.__call__ | |
class mem: | |
def __init__(self): | |
self.cells = [] | |
def __getitem__(self, *cells): | |
for cell in cells: | |
if not isinstance(cell, memaddr): | |
raise Exception("expected memaddr object, got %s" % type(c)) | |
else: | |
pass | |
self.cells = cells[0] if type(cells[0]) is tuple else cells | |
return self | |
def get(self, item): | |
return self.cells[item] | |
def __repr__(self): | |
return "[memorypack%s%s]" % ( | |
";" if len(self.cells) < 1 else ": ", | |
" ".join([cell.__repr__() for cell in self.cells]), | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> a = 99 | |
>>> ptr = mem[memaddr(id(a))] | |
>>> ptr.get(0).write(98) | |
0 | |
>>> a | |
98 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment