Is it possible to extract data out of memory using Ruby?

Is it possible to extract data out of memory using Ruby?

Something like this: Memory.get_byte(pos)

Or something like this Perl module:

Disassemble::X86::MemRegion - Represent a region of memory - metacpan.org

This Perl module is not an extension in C, I am not sure how the
author pulled that off. I looked at the source code, but I am not very
good at Perl.

  • Erik

2008/8/14 Erik T. [email protected]:

Is it possible to extract data out of memory using Ruby?

Erik, to read and write longs (4 bytes) I use the DL standard library
with the following code:

require “dl”

ptr = DL::PtrData.new 0
ptr.struct! “L”, “val”
size = ptr.size

read

ptr[ address, size ].unpack( “L” )[ 0 ]

write

ptr[ address, size ] = [ val ].pack( “L” )

For reading and writing bytes you have to change the “L” to something
else. See the docs for the DL standard library.

Regards,
Pit

So if you would like to look at the ‘in memory’ representation of a Ruby
String how would that work?

require “dl”

ptr = DL::PtrData.new 0
ptr.struct! “L”, “val”
size = ptr.size

str = ‘Hello world!’
address = str.object_id * 2
value = ptr[ address, size ]

puts [address, value].inspect

TIA,

Erik.

Pit C. wrote:

2008/8/14 Erik T. [email protected]:

Is it possible to extract data out of memory using Ruby?

Erik, to read and write longs (4 bytes) I use the DL standard library
with the following code:

require “dl”

ptr = DL::PtrData.new 0
ptr.struct! “L”, “val”
size = ptr.size

read

ptr[ address, size ].unpack( “L” )[ 0 ]

write

ptr[ address, size ] = [ val ].pack( “L” )

For reading and writing bytes you have to change the “L” to something
else. See the docs for the DL standard library.

Regards,
Pit

2008/8/14 Erik T. [email protected]:

So if you would like to look at the ‘in memory’ representation of a Ruby
String how would that work?

Erik, you have to look at the C source code of Ruby to find the
structure of String instances. In MRI 1.8, you can get at the address
of a Ruby object via

object.object_id * 2

Regards,
Pit