我一直在尝试在 Ruby 中读写进程内存,希望将一些旧的 C++ 程序迁移到更动态的语言中。但是,我搬过来并不容易。我已经阅读了一些内容,但在我的具体问题上找不到太多。我可能在下面有一些非常基本的错误,因为我不太确定指针管理在 Ruby-ffi 中是如何工作的。
无论如何,我目前已经ffi
安装了 gem,并一直在使用它来获取功能。这就是我所拥有的:
module Memory
PROC_READ = 0x10
PROC_WRITE = 0x20
PROC_RW = PROC_READ | PROC_WRITE
extend FFI::Library
ffi_lib 'kernel32'
# HANDLE WINAPI OpenProcess(DWORD, BOOL, DWORD)
attach_function :open, :OpenProcess, [:uint, :bool, :uint], :pointer
# BOOL WINAPI CloseHandle(HANDLE)
attach_function :close, :CloseHandle, [:pointer], :bool
# BOOL WINAPI ReadProcessMemory(HANDLE, LPCVOID, out LPVOID, SIZE_T, out SIZE_T)
attach_function :read, :ReadProcessMemory, [:pointer, :pointer, :pointer, :int, :int], :bool
# BOOL WINAPI WriteProcessMemory(HANDLE, LPCVOID, LPVOID, SIZE_T, out SIZE_T)
attach_function :write, :WriteProcessMemory, [:pointer, :pointer, :pointer, :int, :int], :bool
# DWORD WINAPI GetLastError(void)
attach_function :error, :GetLastError, [], :uint
end
似乎当我调用 Memory.open 时,我得到了正确的句柄。我不太确定,但这是存储结果的变量的输出,以防万一我错了。
#<FFI::Pointer address=0x00000000000150>
这是我目前拥有的完整代码:
# 1048 is a fixed pid currently
handle = Memory::open(Memory::PROC_RW, false, 1048)
puts "GetLastError: #{Memory::error()}"
# Address to read from
loc = 0x057C75F8
out = 0
read = 0
# Supposed to be the address of out to store the value read
val = FFI::MemoryPointer.new :uint, out
# Supposed to be a pointer to loc which holds the address to read from
addr = FFI::MemoryPointer.new :pointer, loc
res = Memory::read(handle, addr, val, 4, read)
puts "GetLastError: #{Memory::error()}"
puts "ReadProcessMemory: #{res}"
puts read
puts out
Memory::close(handle)
这将打印出以下内容:
GetLastError: 0
GetLastError: 0
ReadProcessMemory: false
0
0
我知道我必须对指针变量做一些根本错误的事情。如果我更改addr
为 anFFI::Pointer
类型:uint
和值,loc
则 ReadProcessMemory 返回true
,但out
andread
变量不会改变。
我希望这已经足够清楚了。我可以尝试澄清是否缺少某些东西。