[英]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.