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

P

Pit Capitain

2008/8/14 Erik Terpstra said:
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
 
E

Erik Terpstra

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 said:
2008/8/14 Erik Terpstra said:
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
 
P

Pit Capitain

2008/8/14 Erik Terpstra said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,888
Messages
2,569,964
Members
46,293
Latest member
BonnieHamb

Latest Threads

Top