Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the value stored in memory address?

Tags:

python

memory

Lets say that memory address 0A7F03E4 stores a value 124. How to change it to 300 using python? Is there a module supplied for this kind of task?

like image 723
Piotr Dabkowski Avatar asked Apr 28 '12 13:04

Piotr Dabkowski


People also ask

How do I change the memory address in Python?

On UNIX, you can try to open() the /dev/mem device to access the physical memory, and then use the seek() method of the file object set the file pointer to the right location. Then use the write() method to change the value. Don't forget to encode the number as a raw string!

Is a memory address where data can be stored and changed?

This eventually allocates memory for the variables declared by a programmer via the compiler. Memory addresses are a property of the hardware and cannot change, but the data stored in memory - stored in a variable - can change or vary over time. That isn't true. Memory or RAM is located external to the CPU.

What is stored in a memory address?

In computing, a memory address is a reference to a specific memory location used at various levels by software and hardware. Memory addresses are fixed-length sequences of digits conventionally displayed and manipulated as unsigned integers.

Can you change the address of the value during runtime?

Yes. You can dynamically change the address referenced by an object using the index feature.


1 Answers

>>> import ctypes
>>> memfield = (ctypes.c_char).from_address(0x0A7F03E4)

Now you can read memfield, assign to it, do whatever you want. As long as you have access to that memory location, of course.

you can also get a memarray with

>>> memarray = (ctypes.c_char*memoryfieldlen).from_address(0x0A7F03E4)

which gives you a list of memfields.

Update: I just read that you want to retrieve an integer. in this case, use ctypes.c_int, of course. In general: use the appropriate ctype ;)

like image 175
ch3ka Avatar answered Sep 29 '22 13:09

ch3ka