开发者

How Do I Expand A File Resource Embedded in C Program

开发者 https://www.devze.com 2023-03-18 08:43 出处:网络
On GCC Linux, is开发者_如何学Python there a way I can add a file resource to be embedded statically into the C program, and then how would I expand that out to /tmp from main()?You can use objcopy to

On GCC Linux, is开发者_如何学Python there a way I can add a file resource to be embedded statically into the C program, and then how would I expand that out to /tmp from main()?


You can use objcopy to embed the resource, and then normal file operations to create the file in /tmp.

If you compile this program:

#include <stdio.h>

extern char _binary_resource_bin_start, _binary_resource_bin_end;

int main() {
  FILE*out = fopen("/tmp/rsrc.bin","wb");
  fwrite(&_binary_resource_bin_start,
    &_binary_resource_bin_end - &_binary_resource_bin_start, 1, out);
  fclose(out);
}

with this makefile:

program: program.o resource.o
    $(CC) -o program program.o resource.o

resource.o: resource.bin
    objcopy -I binary -O elf32-i386 -B i386 resource.bin resource.o

resource.bin:
    echo resource-file-contents > resource.bin

I believe you will get what you want.


Just store it as a string (char *). If it has embedded nulls, or you don't want to bother with escapes for quotes, backslashes, and such, uuencode-, hex- or base64-encode it, and either put the decoding routine into your C program or call it externally. Just use regular file operations to write the data to the /tmp file.


Perhaps you are trying to hide your PHP code. If that is the case, bare in mind that it is trivial to retrieve the PHP code from the generated executable, without even running the latter. If hiding/obfuscation is the goal, then you may want to consider using a PHP encoder.

0

精彩评论

暂无评论...
验证码 换一张
取 消