Access data added to ELF binary

I have a static ELF binary that reads data from a zip file. To make distribution easier, I want to add a zip file to a binary file, for example:

$ cat mydata.zip >> mybinary 

I know that this will not harm mybinary, but I do not know how to access the contents of mydata.zip by doing this. Is it possible? If so, how?

In the past, I used the add data trick and then added the length of the data, so all I need to do is open the binary, read the last int of the stream, rewind that length, and then run unzip, but this will not work for various reasons (for example, I cannot guarantee that the file will still be on disk when a zip file is required).

Super-extra-doubleplus-points are all around if the solution works through OS X and MinGW.

+5
source share
2 answers

Assuming that at the beginning of the application you have access to the file, then opening the handle for it should prevent the operating system from being erased by the file on disk until the last link to the file is closed. This will allow you to search for the file in your heart. using this file without worry.

Create a global variable:

 int app_fd; 

The process for most of them is the same, in the main procedure it simply gives:

 app_fd = open(argv[0], O_RDONLY); 

at the beginning of execution. When it comes to the fact that you need to access the zip file, just use the file descriptor, not the file name.

At run time, if you do not have any form of handle to the source content of the application, you probably will not be able to access the contents of the zip file. This is due to the display of only the bootloader in the sections of the expected file. Content at the end of the binary will be considered garbage and not displayed.

To map the zip file in memory, you will need to take a different approach. You will need to insert .zip into the ELF (linux) / COFF (Windows) / Mach-O (Mac OS X) section of the binary file, which has properties set in such a way that it is guaranteed to be displayed in the application (this requires a lot of preliminary work in the application and a lot more work after processing). This is not trivial and probably involves quite a bit of coding to do it right for each platform.

As an aside, it is not difficult to remove the application from the Windows system while this application is running (I think you can move it if it is on NTFS, though).

+2
source

If you combine the ELF file and the zip file, the resulting file and (AFAIU) a valid ELF file and a valid zip file.

Demo:

 $ gcc hello.c -o hello $ ./hello Hello $ (cat hello ; test.zip) > hello2 $ chmod u+x hello2 $ ./hello2 Hello $ unzip ./hello2 Archive: ./hello2 warning [./hello2]: 6704 extra bytes at beginning or within zipfile (attempting to process anyway) Length Date Time Name --------- ---------- ----- ---- 119458 1999-11-24 13:08 hello.txt 

Many libraries (zlib, zzip) (erronously?) Do not recognize such a file as a valid zip file, but libminizip can do this:

 #include <stdio.h> #include <errno.h> #include <minizip/unzip.h> int main(int argc, char** argv) { unzFile uf = unzOpen(argv[0]); unzGoToFirstFile(uf); char filename_inzip[256] = {0}; unz_file_info64 file_info = {0}; const char *string_method = NULL; unzGetCurrentFileInfo64(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); printf("%s\n", filename_inzip); return 0; } 

gives:

 $ ./unzipme2 foo.txt 
0
source

Source: https://habr.com/ru/post/912936/


All Articles