8

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?

2
  • 1
    You 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. Commented Apr 28, 2012 at 13:54
  • Are you talking about a virtual memory address or a physical memory address? Commented Apr 28, 2012 at 14:05

3 Answers 3

11
>>> 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 ;)

Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't this give you the virtual memory address of the python process rather than the physical address? The latter would probably be inacessible depending on the operating system and process privileges.
when you want to access a non-data-field, or a field outside of reachable memory, you'll get a segfault, of course.
2

Have you tried the built in ctypes module? It provides a memset function which should give you what you need.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.