【问题标题】:Read the contents from memory in python在python中从内存中读取内容
【发布时间】:2021-05-17 07:57:53
【问题描述】:

我正在尝试从内存中读取内容,但未显示输出。请让我知道我在哪里做错了。

import ctypes
memory_address = 0xE000A208 # Get address of sn variable
m = hex(memory_address)
value2 = ctypes.cast(0xE000A208, ctypes.py_object).value #Get value from  address of sn variable
print("value2 =",value2)

【问题讨论】:

  • 您是否遇到任何类型的错误?
  • 不,我没有在输出屏幕上看到任何东西
  • 你想做什么?

标签: python-3.x ctypes


【解决方案1】:

以下是特定于 CPython 的,您只能在 Python 进程本身中读取已提交的虚拟地址,因此取决于您的地址所代表的内容,这可能适合您,也可能不适合您:

>>> import ctypes
>>> test = "Here is a string"
>>> addr = id(test) # In CPython, this is the address of the PyObject in memory.
>>> import sys
>>> size = sys.getsizeof(test)  # this is the size in bytes of the PyObject
>>> s = (ctypes.c_char * size).from_address(addr)  # build byte string
>>> # display all bytes.  These include internal variables as well as string content
>>> s.raw  
b"\x01\x00\x00\x00\x00\x00\x00\x00\x90\xaeZ:\xf8\x7f\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\xc2X'\xb2\x80\x12\x1c\xb1\xe4\x06^:\xf8\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Here is a string\x00"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-18
    • 2018-01-03
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多