簡體   English   中英

Python-將字節地址(從ReadProcessMemory)轉換為字符串?

[英]Python - Converting byte address (from ReadProcessMemory) to string?

更新1:

我已經嘗試使用buffer.value.decode()下面給出的解決方案。 這導致剛好返回(# ,這是朝正確方向邁出的一步,但我想將其返回到9000當我使用作弊引擎將地址值更改為2000 ,該方法也遇到了UnicodeDecodeError: 'utf-8' can't decode byte 0xd0 in position 0: invalid continuation byte錯誤UnicodeDecodeError: 'utf-8' can't decode byte 0xd0 in position 0: invalid continuation byte 。因此,我猜我編碼有誤,對此我有何建議?

原始問題:

我正在嘗試使用ctypes的ReadProcessMemory來從進程中的地址獲取值。 我似乎可以毫無問題地獲取字節數據,但是我很難將其轉換為適當的格式。 我正在使用Cheat Engine通過第三方方法檢查數據,並使用它來更改地址中的數據,我確定我的腳本輸出的地址正確。 我期望返回的值是9000。我正在使用的完整代碼如下,但是我正在將內存讀入c_char_p類型的緩沖區(來自ctypes)。 我嘗試了幾種將值轉換為字符串並打印的方法。 只使用str(buffer) ,正如我期望的那樣,我得到了字節表示形式c_char_p(b'(#') 。當我嘗試使用str(int.from_bytes(buffer, sys.byteorder))轉換數據時str(int.from_bytes(buffer, sys.byteorder))我得到了43586512我在其他地方看到了使用buffer.decode()從字節轉換的能力,但是它似乎不適用於ctypes版本,因為c_char_p沒有解碼方法(這可能表明我應該采用其他方法來獲取進程地址值而不是ctypes?)。有人可以指出我需要做些什么才能以我要的形式正確獲取該地址的值嗎?

碼:

from ctypes import *
from ctypes.wintypes import *

OpenProcess = windll.kernel32.OpenProcess
ReadProcessMemory = windll.kernel32.ReadProcessMemory
CloseHandle = windll.kernel32.CloseHandle

PROCESS_ALL_ACCESS = 0x1F0FFF #I'll try to do minimum permissions later.

pid = 3112 #Don't worry about this, I know it's right.
address = 0x2411918 #This too.

buffer = c_char_p(b"The data goes here") #Maybe I should be using something else?
bufferSize = len(buffer.value)
bytesRead = c_ulong(0)

processHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
if ReadProcessMemory(processHandle, address, buffer, bufferSize, byref(bytesRead)):
    print("Success:" + str(int.from_bytes(buffer, "little"))) #Here's the problem print line.
else:
    print("Failed.")

CloseHandle(processHandle)

事實證明,我的問題與ctypes表示形式有關。 如果我使用記憶移動將c_char_p移至c_int,那么最終會打印出正確的結果。 可以在下面的完整代碼中看到這一點。

from ctypes import *
from ctypes.wintypes import *
import struct

OpenProcess = windll.kernel32.OpenProcess
ReadProcessMemory = windll.kernel32.ReadProcessMemory
CloseHandle = windll.kernel32.CloseHandle

PROCESS_ALL_ACCESS = 0x1F0FFF

pid = 3112
address = 0x2411918

buffer = c_char_p(b"The data goes here")
val = c_int()
bufferSize = len(buffer.value)
bytesRead = c_ulong(0)

processHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
if ReadProcessMemory(processHandle, address, buffer, bufferSize, byref(bytesRead)):
    memmove(ctypes.byref(val), buffer, ctypes.sizeof(val))
    print("Success:" + str(val.value))
else:
    print("Failed.")

CloseHandle(processHandle)

c_char_p(b"test").value.decode()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM