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?
-
1You may need to deal with memory protection. If this is code or read only data then you won't be able to write to that address.David Heffernan– David Heffernan2012-04-28 13:54:05 +00:00Commented Apr 28, 2012 at 13:54
-
Are you talking about a virtual memory address or a physical memory address?Roland Smith– Roland Smith2012-04-28 14:05:42 +00:00Commented Apr 28, 2012 at 14:05
3 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 ;)
2 Comments
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!
Generally, only the root user has access to this device, though.