代码注入 - Solaris & Linux
我有一个由第三方创建的可执行模块。 我想将我的代码(一种在单独线程中运行的看门狗)“注入”到这个进程中。
到目前为止,有两种可能的方法 - 一种是将我的代码作为可执行文件运行并在其之上动态加载进程(似乎非常困难和棘手),或者使我的代码成为共享对象,通过 LD_PRELOAD 加载它并从一些静态变量构造函数。
有更方便的方法吗? 我的操作系统是 Linux x86 和 Solaris-SPARC。
更新:如果可能的话,我不想修补进程,而是动态加载我的代码。
I have an executable module created by third party. I would like to "inject" my code (kind of watchdog running in separate thread) into this process.
So far there are two possible ways - one is to run my code as executable and dynamically load a proess on top of it (seems to be very hard and tricky) or to make my code a shared object, load it via LD_PRELOAD and initialize from some static variable constructor.
Are there more convenient ways to do this ?
My OS are Linux x86 and Solaris-SPARC.
Update: If possible, I'd like not to patch the process, but load my code dynamicaly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您正在寻找 InjectSo。 有一个 Powerpoint 演示文稿 解释了它是如何工作的。 我还没有抽出时间尝试一下。
Sounds like you're looking for InjectSo. There's a Powerpoint presentation that explains how it works. I haven't gotten around to trying it out yet.
Hotpatch 应该可以为你做到这一点。 它比injectso 更强大。
Hotpatch should do this for you. It is more capable than injectso.
Rob Kennedy 向您介绍了 InjectSo - 这可能正是您所需要的。
请注意,将线程引入非线程进程会充满同步问题。 如果应用程序已经是线程化的,那么问题就不那么严重,但即使如此,应用程序也可能会拒绝它不知道的线程。
Rob Kennedy told you about InjectSo - that's probably what you need.
Beware that the introduction of a thread into a non-threaded process would be fraught with synchronization issues. The problems are less serious if the application is already threaded, but even so, the application may object to a thread that it doesn't know about.
我没有使用过提到的 InjectSo,但它是一个值得注意的信息。
如果您正在寻找替代方案,这里有一种注入代码的简单方法:
gcc test.c -o test
gccject.c -shared -o libinject.so
run with LD_LIBRARY_PATH=。 LD_PRELOAD=libinject.so ./test
应该是
hocus pocus
。 您可以覆盖任意libc
函数,例如printf
、snprintf
- 只需查找该模块使用的内容即可。在“您的代码”中,您可以启动任意线程、看门狗等。
I have not used the mentioned InjectSo but it is a noteworthy information.
If you are looking for alternatives here is a simple way to inject your code:
gcc test.c -o test
gcc inject.c -shared -o libinject.so
run with
LD_LIBRARY_PATH=. LD_PRELOAD=libinject.so ./test
Should say
hocus pocus
. You can override arbitrarylibc
functions, likeprintf
,snprintf
- just find what is that module using.In the "your code here" you can start arbitrary threads, watchdogs etc.