#binary-file #source-file #binary-data #data-source #byte-array #binary #source

app bin2src

Converts and embed a binary file as an array of bytes into a source file of other languages

2 releases

0.0.58 Jun 27, 2021
0.0.57 Jul 18, 2020
0.0.56 Jul 17, 2020

#903 in Command line utilities

MIT license

63KB
1.5K SLoC

bin2src

bin2src is a simple command line that converts a binary file to an array of bytes, assigned to a variable in a source file of another language; therefore you can embed it into your program.

Currently supported output languages:

  • C
  • C (shellcode)
  • Pascal
  • Python
  • Rust

Overview

bin2src reads a binary file (e.g. jpg, wav, mp3, etc.) and generates a source file with the binary data embeded into it as a byte array and assigned to a variable.

It is useful if you don't want to distribute a binary file together with your application and let users have direct access to it.

With bin2src you can embed the data inside the final executable and use it through the variable stored in memory.

Keep in mind that it is always possible for an "advanced" user to extract the data, either from the executable or from a memory dump.

Usage

bin2src < -l LANG | --out-lang LANG > [ OPTIONS ] < FILE >

LANG and FILE are required.

Options:

        -l, --out-language LANG         specify the language, where LANG={c|cshell|pascal|python|rust}

        -d, --out-dir PATH              specify where to output source(s) file(s);
                                        if not specified, generate in current directory

        -f, --out-file OUTFILE          specify the output file(s) name (* without extension *);
                                        if not specified, output file(s) will have the same name
                                        of input file (without extra dots).

        -h, --hex                       output bytes in hexadecimal (for C shellcode this flag has
                                        diferent behaviors. See the Github site for more information)

Currently supported languages:

  - C
  - C for shellcode
  - Pascal
  - Python
  - Rust	

Some Examples

Suppose you have an image myimage.jpg that you want to embed into your executable:

Example 1:

bin2src --out-language pascal --out-dir "X:\My Projects\project01" --out-file image01 myimage.jpg

Windows paths with spaces need quotation marks

This command will create the file ...\image01.pas with the data defined using decimal format: [210, 0, ...].

Example 2:

bin2src -l c -d "X:\My Projects\project02" -f image01 -h myimage.jpg

This command will create the files, but with binary data in hexadecimal format: [0x10, 0xfa, ...]:

  • ...\image01.h
  • ...\image01.c

Example 3:

bin2src --out-language python myimage.jpg

This command will create the file myimage.py at the current directory.

Check other examples at examples directory for some practical uses of bin2src.

Atention

  • Beware of the file size that you will embed in your code!!!

    Ensure that it is accepted by your O.S., compiler, language standards, memory at runtime, etc.

  • If the input file name has more dots, in addition to the dot that splits the name and the extension, without the --out-file or -f command line option the output file name will be the first name before the first dot. Example:

    abc.def.ghi.x => abc.y

  • The option --hex or -h for C shellcode output works differently. Without this flag, it will define the data type as unsigned char bytes, otherwise with the hexadecimal flag, it will embed the bytes as a string type (char *).

  • To embed the generated C shellcode using string format, make sure that the binary data doesn't contains null bytes ("\x00") or don't use string functions from stdlib, like strlen. This may break your code and could raise exceptions (e.g. access violations, etc.).

  • All of the tests were made with Windows 10 Pro (2004), and to execute the alpha release maybe you have to install the latest MSVC runtime. Even though, it should works well on other platforms.

  • There are a lot of things to organize and improvements to do. Suggestions are always welcome.

License

Developed by Alexandre Gomiero de Oliveira under the MIT License.

Any code generated by bin2src are under MIT License.

Please, contact me if you need another license type.

No runtime deps