[英]Embed a binary in C program
I am trying to write a program in C that would be able to call certain binaries (ex. lsof, netstat) with options. 我试图用C编写一个程序,该程序可以使用选项调用某些二进制文件(例如lsof,netstat)。 The purpose of this program is to collect forensic data from a computer, while at the same time this program should not use the binaries of the computer under analysis as they might be compromised. 该程序的目的是从计算机收集取证数据,与此同时,该程序不应使用被分析计算机的二进制文件,因为它们可能会受到威胁。 As a result it is required the certified/uncompromised binaries (ex. lsof, netstat -antpu etc) already to be embedded in a C program or to be called by the C program stored in a usb drive for example. 结果,要求已认证/未破坏的二进制文件(例如lsof,netstat -antpu等)已经嵌入到C程序中,或由例如存储在USB驱动器中的C程序调用。
$ ld -s -r -b binary -o testls.o bin-x86-2.4/ls
$ nm testls.o
000000000007a0dc D _binary_bin_x86_2_4_ls_end
000000000007a0dc A _binary_bin_x86_2_4_ls_size
0000000000000000 D _binary_bin_x86_2_4_ls_start
The next step would be to call the "function" from the main program with some options that I might need for example "ls -al". 下一步将是使用可能需要的一些选项从主程序中调用“函数”,例如“ ls -al”。 Thus I made a C program to call the entry point of the object file. 因此,我制作了一个C程序来调用目标文件的入口点。
Then I compiled the program with the following gcc options 然后我用以下gcc选项编译了程序
gcc -Wall -static testld.c testls.o -o testld
This is the main program: 这是主程序:
#include <stdio.h>
extern int _binary_bin_x86_2_4_ls_start();
int main(void)
{
_binary_bin_x86_2_4_ls_start();
return 0;
}
When I run the program I am getting a segmentation fault. 当我运行程序时,出现了段错误。 I checked the entry points using the objdump in the testld program and the linking seems to be successful. 我在testld程序中使用objdump检查了入口点,并且链接似乎成功。 Why then I am getting a segmentation fault? 为什么会出现细分错误? I still need also to call "ls" with options. 我还需要用选项调用“ ls”。 How I could do this, ie call the "function" with the arguments "-al". 我该怎么做,即用参数“ -al”调用“函数”。
Thank you. 谢谢。
The ELF header of a binary isn't a function. 二进制文件的ELF标头不是函数。 You can't call it. 你不能叫它。 If you could (like in some ancient binary formats) it would be a really bad idea because it would never return. 如果可以(例如采用某些古老的二进制格式),这将是一个非常糟糕的主意,因为它永远也不会返回。
If you want to run another program midstream do this: 如果要在中间运行另一个程序,请执行以下操作:
int junk;
pid_t pid;
if (!(pid = fork())) {
execl("ls", "/bin/ls", ...); /* this results in running ls in current directory which is probably what you want but maybe you need to adjust */
_exit(3);
}
if (pid > 0) waitpid(pid, &junk, 0);
Error handling omitted for brevity. 为简洁起见,省略了错误处理。
In your case, you should ship your own copies of your binaries alongside your program. 对于您的情况,您应该将自己的二进制副本与程序一起发送。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.