解压缩并加载到LD_PRELOAD

问题描述

假设我有一个包含1个共享库的tar.gz存档。 我的意图是“即时”解压缩该文件,然后将其提取.so放到LD_PRELOAD上并运行我的代码

因此,我编写了一个脚本:

n-1

#!/bin/bash myTarLib=$1 tar -zxf $myTarLib --to-command "export LD_PRELOAD=" ./run_the_func 的执行未使用tar中的.so。 我的印象是“ --to-command”选项创建了另一个外壳程序;正确吗?

您对我的建议有什么建议吗?重要的是,我不想将.so放在磁盘上。

谢谢!

解决方法

我找到了解决问题的方法... 使用memfd_create

memfd_create创建一个文件描述符。然后,可以将其用于存储其中的任何数据。 联机帮助页为here

要使用它,您需要创建一个用于处理untar的C-Wrapper(在我的情况下)。代码是:

#include <linux/memfd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

int main()
{
    int fd = memfd_create("my_test",MFD_CLOEXEC);
    if (fd == -1)
    {
        fprintf(stderr,"Creation failed\n");
    }


    char command_1[128];
    char *path="hiddenLibrary/libmy_func_real.tgz";
    //feel free to modify it to the path of your encrypted library
    sprintf(command_1,"tar -zxf %s --to-stdout > /proc/%ld/fd/%d",path,(long) getpid(),fd);

    printf("Running decrypt command\n");
    system(command_1);

    printf("The untar-ed library is located at:/proc/%ld/fd/%d\nOnce you finished type a number and hit enter\n",fd);
    float temp;
    scanf("%f",&temp);

return 0;
}

现在的想法是,上面的C代码将运行untar并将结果存储到fd。使用完后,您只需点击一个数字,C代码就会退出。 在出口期间,所有fds均被释放,因此未压缩的库已“消失”。