【问题标题】:Code injection - Solaris & Linux代码注入 - Solaris 和 Linux
【发布时间】:2010-09-27 13:10:14
【问题描述】:

我有一个第三方创建的可执行模块。我想将我的代码(一种在单独线程中运行的看门狗)“注入”到这个进程中。

到目前为止,有两种可能的方法 - 一种是将我的代码作为可执行文件运行并在其上动态加载一个过程(似乎非常困难和棘手)或使我的代码成为共享对象,通过 LD_PRELOAD 加载它并从一些静态变量构造函数初始化。

有没有更方便的方法来做到这一点? 我的操作系统是 Linux x86 和 Solaris-SPARC。

更新:如果可能,我不想修补进程,而是动态加载我的代码。

【问题讨论】:

    标签: linux gcc solaris elf


    【解决方案1】:

    听起来您正在寻找InjectSo。有一个Powerpoint presentation 解释了它是如何工作的。我还没来得及尝试。

    【讨论】:

    • Dmitry,你能在什么特定的系统上让它工作?我还没有能够使用各种新旧系统。
    【解决方案2】:

    Hotpatch 应该为您执行此操作。比injectso更能干。

    【讨论】:

      【解决方案3】:

      Rob Kennedy 向您介绍了 InjectSo - 这可能就是您所需要的。

      请注意,将线程引入非线程进程会充满同步问题。如果应用程序已经线程化,问题就不那么严重了,但即便如此,应用程序可能会反对它不知道的线程。

      【讨论】:

        【解决方案4】:

        我没有使用过上面提到的 InjectSo,但这是一个值得注意的信息。 如果您正在寻找替代方案,这是一种注入代码的简单方法:

        #include <stdio.h>
        #include <sys/types.h>
        #include <pwd.h>
        int main()
        {
            struct passwd* pswd = getpwuid(1000);
            if(pswd) 
                printf("%s\n", pswd->pw_name);
            return 0;
        }
        

        gcc test.c -o test

        #define _GNU_SOURCE
        #include <dlfcn.h>
        #include <sys/types.h>
        #include <pwd.h>
        #include <stdlib.h>
        #include <stdio.h>
        
        static char* hocus = "hocus pocus";
        
        struct passwd *getpwuid(uid_t uid)
        {
            static struct passwd *(*orig_getpwuid)(uid_t uid);
            if(!orig_getpwuid) {
                orig_getpwuid = (struct passwd* (*)(uid_t))dlsym(RTLD_NEXT, "getpwuid");
            }
        
            struct passwd* original_passwd = (*orig_getpwuid)(uid);
            if(original_passwd) {
                original_passwd->pw_name = hocus;
            }
            // your code here
            return original_passwd;
        }
        

        gcc inject.c -shared -o libinject.so

        使用LD_LIBRARY_PATH=. LD_PRELOAD=libinject.so ./test运行

        应该说hocus pocus。您可以覆盖任意 libc 函数,例如 printfsnprintf - 只需找到该模块使用的内容即可。

        在“您的代码”中,您可以启动任意线程、看门狗等。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-12
          • 1970-01-01
          • 2011-01-20
          • 1970-01-01
          • 1970-01-01
          • 2011-07-12
          • 1970-01-01
          • 2018-09-14
          相关资源
          最近更新 更多