I experimented with reading and writing process memory in Ruby in the hope of porting some old C ++ programs to a more dynamic language. However, it’s not easy for me to move. I have done some reading, but I cannot find much on my specific problem. I probably have some pretty fundamental errors, as I'm not too sure how Ruby-ffi pointer management works.
Anyway, I currently have ffigem installed , which is used to capture functions. This is what I have:
module Memory
PROC_READ = 0x10
PROC_WRITE = 0x20
PROC_RW = PROC_READ | PROC_WRITE
extend FFI::Library
ffi_lib 'kernel32'
attach_function :open, :OpenProcess, [:uint, :bool, :uint], :pointer
attach_function :close, :CloseHandle, [:pointer], :bool
attach_function :read, :ReadProcessMemory, [:pointer, :pointer, :pointer, :int, :int], :bool
attach_function :write, :WriteProcessMemory, [:pointer, :pointer, :pointer, :int, :int], :bool
attach_function :error, :GetLastError, [], :uint
end
It seems when I call Memory.open, I get the correct pen. I'm not quite sure, but here is the output of a variable storing the result in the case, I'm wrong.
#<FFI::Pointer address=0x00000000000150>
Here is the complete code that I have:
handle = Memory::open(Memory::PROC_RW, false, 1048)
puts "GetLastError: #{Memory::error()}"
loc = 0x057C75F8
out = 0
read = 0
val = FFI::MemoryPointer.new :uint, out
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)
It produces the following:
GetLastError: 0
GetLastError: 0
ReadProcessMemory: false
0
0
, - . addr FFI::Pointer :uint loc, ReadProcessMemory true, out read .
, . , - .