在 OS X 上运行的 Python 中读取/写入另一个进程内存 [英] Reading/Writing another process memory in Python running on OS X

查看:59
本文介绍了在 OS X 上运行的 Python 中读取/写入另一个进程内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Mac OS X Lion 上运行时,是否可以使用 Python 读/写另一个进程内存(不是 Python 进程,并且不使用共享内存或任何东西)?

Is is possible to read/write another process memory (not a Python process, and doesn't use share memory or anything) using Python when running on Mac OS X Lion?

例如,我想启动 Safari 并监控 Safari 使用的几个内存地址.

For example, I want to launch Safari and monitor several memory addresses used by Safari.

在 Windows 中我找到了很多解决方案,但是在 Mac OS X 上可以吗?

In Windows I've found plenty of solutions, but can it be done on Mac OS X?

推荐答案

是的,可以.

当然不是直接——但是你可以从 Python 调用任何你想要的 C API,或者通过在 C(或 Pyrex 等)中构建一个 Python 扩展模块,或者通过在 Python 中使用 ctypes.

Not directly, of course—but you can call any C API you want from Python, either by building a Python extension module in C (or Pyrex, etc.), or by using ctypes from within Python.

您要调用的特定 C API 是 task_for_pid 和 mach_vm 方法.这些方法的联机帮助页在现代 OS X 中不存在,但标头有据可查(因此,就这点而言,它是源,随时可用),您可以在线找到其他基于 Mach 的系统的联机帮助页,还有很多第三方文档.

The particular C APIs you want to call are task_for_pid and the mach_vm methods. The manpages for these methods don't exist in modern OS X, but the headers are well documented (and so, for that matter, is the source, which is readily available), and you can find manpages for other Mach-based systems online, and there's plenty of third-party documentation.

请参阅 https://github.com/abarnert/pymach 以了解概念的快速证明.您应该能够使用python setup.py build_ext --inplace"或sudo python setup.py install"来构建它,然后查看 test.py 以了解如何使用它的简单示例.

See https://github.com/abarnert/pymach for a quick proof of concept. You should be able to build it with "python setup.py build_ext --inplace" or "sudo python setup.py install", and then see test.py for a simple example of how to use it.

请记住,在现代 OS X 中,除非您是 root,否则您只能访问子进程.解决此问题的最简单方法是让您的脚本实际启动 Safari.或者,如果您不能这样做,只需 sudo 您的脚本.或者,您可以随意使用 ptrace 附加到正在运行的进程,但这留给读者作为练习.

Keep in mind that in modern OS X, unless you're root, you only have access to child processes. The easiest way around this is to have your script actually launch Safari. Or, if you can't do that, just sudo your script. Alternatively, you can get fancy and use ptrace to attach to a running process, but that's left as an exercise for the reader.

这篇关于在 OS X 上运行的 Python 中读取/写入另一个进程内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆