Python在读/写进程内存应用程序中打印ctype字符串缓冲区

2024-04-19 10:55:47 发布

您现在位置:Python中文网/ 问答频道 /正文

让我说这是我第一次尝试正确的记忆读写应用程序。写函数甚至还没有实现,但它会及时出现的。只是别因为密码而炒我鱿鱼。在

第一件事是完整的程序,这样每个人都可以看到(不要评论我的“糟糕的编码选择”,这件事起作用了,我得到的进程句柄和ID很好)

import ctypes
from ctypes import wintypes
from time import *
import win32ui, win32process ,win32api

PROCESS_ALL_ACCESS = 0x1F0FFF

ReadProcessMemory = ctypes.WinDLL('kernel32',use_last_error=True).ReadProcessMemory
ReadProcessMemory.argtypes = [wintypes.HANDLE,wintypes.LPCVOID,wintypes.LPVOID,ctypes.c_size_t,ctypes.POINTER(ctypes.c_size_t)]
ReadProcessMemory.restype = wintypes.BOOL

WriteProcessMemory = ctypes.WinDLL('kernel32',use_last_error=True).WriteProcessMemory
WriteProcessMemory.argtypes = [wintypes.HANDLE,wintypes.LPVOID,wintypes.LPCVOID,ctypes.c_size_t,ctypes.POINTER(ctypes.c_size_t)]
WriteProcessMemory.restype = wintypes.BOOL

def main():
    WindowName = input("Enter the window name: ")
    if (len(WindowName) > 0):
        Process, pID = attach(WindowName)
        Choice = input("Read or Write?").lower()
        if (Choice == "write"):
            #DoWriteMethod
            pass
        if (Choice == "read"):
            Read(Process)

    else:
        print("Invalid Window Name!\n\n")
        sleep(2)
        return 0

def attach(WindowName):
    try: 
        hWnd = win32ui.FindWindow(None,WindowName).GetSafeHwnd()
    except:
        print("Window not found!\n\n")
        sleep(2)
        return 0,0

    pID = win32process.GetWindowThreadProcessId(hWnd)[1]
    Process = win32api.OpenProcess(PROCESS_ALL_ACCESS,0,pID).handle

    print("\nProcess = ", Process)
    print("ProcessID = ", pID)

    return Process,pID

def Write():
    return 0

def Read(Process):
    Address = input("Enter the address in hexadecial (0x form) \n")
    if (Address[0] == "0" and Address[1] == "x"):
        BufferAddress = ctypes.create_string_buffer(64)
        ptr = ctypes.pointer(BufferAddress)
        ReadProcessMemory(Process,Address,BufferAddress,64,None)
        print("Pointer contains: ", BufferAddress, "\n\n")
        main()
    else:
        print("Invalid address!\n\n")
        sleep(2)
        Read(Process)
    return 0

if (main() == 0):
    main()
if (main() == 1):
    exit(0)

在第54行我创建了一个'创建字符串缓冲区(64)'

然后,我在ReadProcessMemory中使用这个值,该值应该存储在那里,但是每当我在第57行打印它时,它只返回以下值:

^{pr2}$

我知道这个值应该是90(在作弊引擎中找到一个随机地址),所以它必须包含值90?我怎么能找到这个值。在

我使用的是python3.5.0和相应的win32 modules包

'pip install pypiwin32'


Tags: importreadsizereturnifmaindefctypes
1条回答
网友
1楼 · 发布于 2024-04-19 10:55:47

您正在打印对象。打印内容:

>>> import ctypes
>>> BufferAddress = ctypes.create_string_buffer(64)
>>> print(BufferAddress)
<ctypes.c_char_Array_64 object at 0x0000000003124248>
>>> dir(BufferAddress)
['__class__', '__ctypes_from_outparam__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__f
ormat__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__',
'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setstate_
_', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_b_base_', '_b_needsfree_', '_length_', '_objects', '_t
ype_', 'raw', 'value']
>>> BufferAddress.raw
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00'
>>> BufferAddress.raw[0]
0

相关问题 更多 >