如何使用Python修改外部进程
初学者在这里。我试图弄清楚如何使用 Python 修改 Linux 系统上正在运行的进程。
示例:我有一个 python 程序,它接受 PID 作为参数。我的目标是使用这个 PID 并获取有关使用该 PID 运行的进程的信息。 (1)查找它在内存中的位置 (2)指令指针在哪里 (3) 修改程序,使下一条执行的指令是其他指令 (4) 将指针返回到下一条合法指令 (5) 让原始进程按其应有的方式执行
我正在尝试开发一个 POC 来展示如何将一小段代码注入到正在运行的进程中,以便仅将“hello”打印到标准输出,而不干扰进程的其余部分。
我查找了trace和其他一些模块,但它们似乎都与遵循当前执行的python进程有关。还查看了 pyhook,但它主要是为了捕获来自键盘等的信号。另外,我查了一下 pygdb 。
任何人都可以向我指出一些可能有用的模块或一些代码示例。我尝试用谷歌搜索“python检查进程PID”等,但没有得到任何有用的东西。
非常感谢任何帮助。
谢谢!
Beginner here. I am trying to figure out how to modify a running process on a linux system using Python.
Example: I have a python program that takes in as an argument a PID. My goal is to use this PID and get info about the running process with that PID.
(1) Find where it is located in memory
(2) Where is the instruction pointer
(3) Modify the program such that the next executed instruction is something else
(4) Return the pointer back to the next legitimate instruction
(5) Let the original process execute as it should have
I am trying to develop a POC to show how a small piece of code can be injected into a running process to just print 'hello' to stdout and not disturb the rest of the process.
I looked up trace and some other modules but they all seem to do with following the currently executing python process. Also looked at pyhook, but its mainly to trap signals from keyboards etc.. additionally, I looked up pygdb a bit.
Can anyone please point me to some modules that might be useful, or some code samples. I tried googling for "python inspect process PID" etc.. did not get anything very useful.
Any help is very appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也是一个较新的 python 用户。只用命令行就能完成所有这些事情吗?如果是这样,那么您可以使用 os.system('[command]') 或 子进程模块。例如,您可以使用 pmap 命令获取内存映射。至于2-5我没有经验。祝你好运。
Also a newer python user. Can you do all these things with just the command line? If so then you could use os.system('[command]') or the subprocess module. For example you could use the pmap command to get the memory mappings. As for 2-5 I have no experience there. Good Luck.