阅读其他进程 OS X 中的内存?
我一直在尝试了解如何读取 Mac OS X 上其他进程的内存,但运气不佳。 我在网上看到过很多使用 ptrace
和 PEEKDATA
等的例子,但是它在 BSD [man ptrace
]。
int pid = fork();
if (pid > 0) {
// mess around with child-process's memory
}
如何在 Mac OS X 上读取和写入另一个进程的内存?
I've been trying to understand how to read the memory of other processes on Mac OS X, but I'm not having much luck. I've seen many examples online using ptrace
with PEEKDATA
and such, however it doesn't have that option on BSD [man ptrace
].
int pid = fork();
if (pid > 0) {
// mess around with child-process's memory
}
How is it possible to read from and write to the memory of another process on Mac OS X?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
一般来说,我建议您使用常规 open() 来打开临时文件。 一旦它在两个进程中打开,您就可以从文件系统中 unlink() 它,并且您的设置就像使用 shm_open 一样。 该过程与 Scott Marcy 为 shm_open 指定的过程极其相似。
这种方法的缺点是,如果执行 unlink() 的进程崩溃,您最终会得到一个未使用的文件,并且没有进程负责清理它。 这个缺点与 shm_open 一样,因为如果没有任何 shm_unlinks 给定名称,该名称将保留在共享内存空间中,可供将来的进程 shm_opened。
In general, I would recommend that you use regular open() to open a temporary file. Once it's open in both processes, you can unlink() it from the filesystem and you'll be set up much like you would be if you'd used shm_open. The procedure is extremely similar to the one specified by Scott Marcy for shm_open.
The disadvantage to this approach is that if the process that will be doing the unlink() crashes, you end up with an unused file and no process has the responsibility of cleaning it up. This disadvantage is shared with shm_open, because if nothing shm_unlinks a given name, the name remains in the shared memory space, available to be shm_opened by future processes.
背后操纵进程的内存是一件坏事,并且充满危险。 这就是为什么 Mac OS X(像任何 Unix 系统一样)具有受保护的内存,并使进程彼此隔离。
当然可以做到:显式协作的进程之间有共享内存的设施。 还有一些方法可以操纵其他进程的地址空间,只要这样做的进程具有明确的这样做的权利(由安全框架授予)。 但这是供编写调试工具的人使用的。 对于 Mac OS X 上的绝大多数开发来说,这不应该是正常的——甚至是罕见的——发生。
Manipulating a process's memory behind its back is a Bad Thing and is fraught with peril. That's why Mac OS X (like any Unix system) has protected memory, and keeps processes isolated from one another.
Of course it can be done: There are facilities for shared memory between processes that explicitly cooperate. There are also ways to manipulate other processes' address spaces as long as the process doing so has explicit right to do so (as granted by the security framework). But that's there for people who are writing debugging tools to use. It's not something that should be a normal — or even rare — occurrence for the vast majority of development on Mac OS X.
使用
task_for_pid()
或者其他方法获取目标进程的任务端口。 此后,您可以使用vm_read()
、vm_write()
等直接操作进程的地址空间。Use
task_for_pid()
or other methods to obtain the target process’s task port. Thereafter, you can directly manipulate the process’s address space usingvm_read()
,vm_write()
, and others.我知道这个帖子已经有 100 年历史了,但是对于从搜索引擎来到这里的人来说:
xnumem 确实正是您正在寻找的内容,操作和读取进程间内存。
I know this thread is 100 years old, but for people coming here from a search engine:
xnumem does exactly what you are looking for, manipulate and read inter-process memory.
Matasano Chargen 不久前发表了一篇关于将一些调试代码移植到 OS X 的好文章,其中包括学习如何在另一个进程中读写内存(除其他外)。
它必须工作,否则 GDB 不会:
Matasano Chargen had a good post a while back on porting some debugging code to OS X, which included learning how to read and write memory in another process (among other things).
It has to work, otherwise GDB wouldn't:
如果您希望能够在进程之间共享内存块,则应该查看 shm_open(2) 和 mmap(2)。 在一个进程中分配一大块内存并将路径(用于 shm_open)传递给另一个进程非常容易,然后两者就可以一起疯狂。 正如 Chris Hanson 提到的,这比在另一个进程的地址空间中闲逛要安全得多。 当然,如果您无法控制这两个过程,那么这对您没有多大好处。
(请注意,shm_open 的最大路径长度似乎是 26 个字节,尽管这似乎没有在任何地方记录。)
It you're looking to be able to share chunks of memory between processes, you should check out shm_open(2) and mmap(2). It's pretty easy to allocate a chunk of memory in one process and pass the path (for shm_open) to another and both can then go crazy together. This is a lot safer than poking around in another process's address space as Chris Hanson mentions. Of course, if you don't have control over both processes, this won't do you much good.
(Be aware that the max path length for shm_open appears to be 26 bytes, although this doesn't seem to be documented anywhere.)
我确实找到了您需要的一个简短实现(只有一个源文件(main.c))。
它是专为XNU设计的。
它位于 Google 搜索前十名的结果中,关键字为 « dump process memory os x »
源代码为
但是从虚拟地址空间点的严格角度来看,您应该对这个问题更感兴趣:OS X:生成核心转储而不关闭进程?(另请参阅 这个)
当你查看gcore源代码时,它是相当复杂的这样做是因为您需要处理线程及其状态...
在大多数 Linux 发行版上,gcore 程序现在是 GDB 包的一部分。 我认为OSX版本安装了xcode/开发工具。
更新:wxHexEditor是一个可以编辑设备的编辑器。 它还可以像编辑常规文件一样编辑进程内存。 它适用于所有 UNIX 机器。
I have definitely found a short implementation of what you need (only one source file (main.c)).
It is specially designed for XNU.
It is in the top ten result of Google search with the following keywords « dump process memory os x »
The source code is here
but from a strict point of virtual address space point de vue, you should be more interested with this question: OS X: Generate core dump without bringing down the process? (look also this)
When you look at gcore source code, it is quite complex to do this since you need to deal with treads and their state...
On most Linux distributions, the gcore program is now part of the GDB package. I think the OSX version is installed with xcode/the development tools.
UPDATE: wxHexEditor is an editor which can edit devices. IT CAN also edit process memory the same way it does for regular files. It work on all UNIX machines.
您想使用共享内存方法进行进程间通信。 有关其他常用方法的摘要,请参阅此处
我没花很长时间就在这个 本书,其中包含当今所有 UNIX 通用的所有 API(比我想象的要多得多)。 将来你应该买它。 本书是一套(数百个)印刷版手册页,很少安装在现代机器上。
每个手册页都详细介绍了一个 C 函数。
没过多久我就找到了 shmat() shmctl(); 其中的 shmdt() 和 shmget() 。 我没有广泛搜索,也许还有更多。
它看起来有点过时,但是:是的,现代 UNIX 操作系统的基本用户空间 API 可以追溯到 80 年代。
更新:书中描述的大多数功能都是 POSIX C 头文件的一部分,您不需要安装任何东西。 很少有例外,例如原始库“curses”。
You want to do Inter-Process-Communication with the shared memory method. For a summary of other commons method, see here
It didn't take me long to find what you need in this book which contains all the APIs which are common to all UNIXes today (which many more than I thought). You should buy it in the future. This book is a set of (several hundred) printed man pages which are rarely installed on modern machines.
Each man page details a C function.
It didn't take me long to find shmat() shmctl(); shmdt() and shmget() in it. I didn't search extensively, maybe there's more.
It looked a bit outdated, but: YES, the base user-space API of modern UNIX OS back to the old 80's.
Update: most functions described in the book are part of the POSIX C headers, you don't need to install anything. There are few exceptions, like with "curses", the original library.