DEV Community

Cover image for Embedding Files in an Executable - Linux/WSL2 - Part 1
Gurigraphics
Gurigraphics

Posted on • Edited on

2

Embedding Files in an Executable - Linux/WSL2 - Part 1

1) Create data.txt

echo "Hello World" > data.txt
Enter fullscreen mode Exit fullscreen mode

2) Convert data.txt to data.o

objcopy -I binary -O elf64-x86-64 -B i386 data.txt data.o
Enter fullscreen mode Exit fullscreen mode

3) Get Symbols

hexdump -C data.o
Enter fullscreen mode Exit fullscreen mode
000000b0  00 5f 62 69 6e 61 72 79  5f 64 61 74 61 5f 74 78  |._binary_data_tx|
000000c0  74 5f 73 74 61 72 74 00  5f 62 69 6e 61 72 79 5f  |t_start._binary_|
000000d0  64 61 74 61 5f 74 78 74  5f 65 6e 64 00 5f 62 69  |data_txt_end._bi|
000000e0  6e 61 72 79 5f 64 61 74  61 5f 74 78 74 5f 73 69  |nary_data_txt_si|
000000f0  7a 65 00 00 2e 73 79 6d  74 61 62 00 2e 73 74 72  |ze...symtab..str|
00000100  74 61 62 00 2e 73 68 73  74 72 74 61 62 00 2e 64  |tab..shstrtab..d|
00000110  61 74 61 00 00 00 00 00  00 00 00 00 00 00 00 00  |ata.............|
00000120  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
Enter fullscreen mode Exit fullscreen mode
_binary_data_txt_start
_binary_data_txt_end
_binary_data_txt_size
Enter fullscreen mode Exit fullscreen mode

4) Create main.c

#include <stdio.h>

extern char _binary_data_txt_start;
extern char _binary_data_txt_end;

int main(){
   char* p = &_binary_data_txt_start;
   while ( p != &_binary_data_txt_end ) putchar(*p++);
   return 0;
}
Enter fullscreen mode Exit fullscreen mode

5) Compile

gcc main.c data.o -o myprogram
Enter fullscreen mode Exit fullscreen mode

6) Run

./myprogram
Enter fullscreen mode Exit fullscreen mode

Compile windows exe

1) Install

sudo apt install gcc-mingw-w64-i686-win32
sudo apt install g++-mingw-w64-i686-win32
Enter fullscreen mode Exit fullscreen mode

2) Test

i686-w64-mingw32-gcc --version
i686-w64-mingw32-g++ --version
Enter fullscreen mode Exit fullscreen mode

3) Convert data.txt to data.o

i686-w64-mingw32-objcopy -I binary -O pe-i386 -B i386 data.txt data.o
Enter fullscreen mode Exit fullscreen mode

4) Get Symbols

hexdump -C data.o
Enter fullscreen mode Exit fullscreen mode
00000070  30 00 00 00 0c 00 00 00  ff ff 00 00 02 00 46 00  |0.............F.|
00000080  00 00 5f 62 69 6e 61 72  79 5f 64 61 74 61 5f 74  |.._binary_data_t|
00000090  78 74 5f 73 74 61 72 74  00 5f 62 69 6e 61 72 79  |xt_start._binary|
000000a0  5f 64 61 74 61 5f 74 78  74 5f 65 6e 64 00 5f 62  |_data_txt_end._b|
000000b0  69 6e 61 72 79 5f 64 61  74 61 5f 74 78 74 5f 73  |inary_data_txt_s|
000000c0  69 7a 65 00                               
Enter fullscreen mode Exit fullscreen mode

5) Remove initial underscores

binary_data_txt_start
binary_data_txt_end
binary_data_txt_size
Enter fullscreen mode Exit fullscreen mode

6) Create main.c or main.cpp

#include <stdio.h>

extern char binary_data_txt_start;
extern char binary_data_txt_end;

int main(){
   char* p = &binary_data_txt_start;
   while ( p != &binary_data_txt_end ) putchar(*p++);
   return 0;
}
Enter fullscreen mode Exit fullscreen mode
#include <iostream>

extern char binary_data_txt_start;
extern char binary_data_txt_end;

int main(){
    char* p = &binary_data_txt_start;
    while (p != &binary_data_txt_end) std::cout << *p++;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

7) Compile

i686-w64-mingw32-gcc main.c data.o -o main.exe
or
i686-w64-mingw32-g++ main.cpp data.o -o main.exe
Enter fullscreen mode Exit fullscreen mode

Part 2

https://dev.to/gurigraphics/embedding-files-in-an-executable-windows-part-2-3gja

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs