Как сделать бинарник из bash скрипта
Появилась необходимость, скомпилировать bash скрипт в бинарный исполняемый файл. Смысл и цели этих манипуляций каждый может найти себе сам, вдруг понадобится. Нашелся такой проект http://www.datsi.fi.upm.es/~frosal/ , некоего Francisco Javier Rosales García. Утилита называется shc, транслятор языка bash в C, с последующей компиляцией в двоичный формат. Все манипуляции производим на виртуальной машине с debian 8 jessie на борту. Скачиваем архив, разархивируем, компилируем:
root@debian8:~ # wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9b.tgz root@debian8:~ # tar -xzf ./shc-3.8.9b.tgz root@debian8:~ # cd shc-3.8.9b root@debian8:~/shc-3.8.9b # make cc -Wall shc.c -o shc *** ▒Do you want to probe shc with a test script? *** Please try. make test
Использование shc
root@debian8:~/shc-3.8.9b # ./shc -help shc Version 3.8.9b, Generic Script Compiler shc Copyright (c) 1994-2015 Francisco Rosalesshc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script -e %s Expiration date in dd/mm/yyyy format [none] -m %s Message to display upon expiration ["Please contact your provider"] -f %s File name of the script to compile -i %s Inline option for the shell interpreter i.e: -e -x %s eXec command, as a printf format i.e: exec('%s',@ARGV); -l %s Last shell option i.e: -- -r Relax security. Make a redistributable binary -v Verbose compilation -D Switch ON debug exec calls [OFF] -T Allow binary to be traceable [no] -C Display license and exit -A Display abstract and exit -h Display help and exit Environment variables used: Name Default Usage CC cc C compiler command CFLAGS C compiler flags Please consult the shc(1) man page.
root@debian8:~/shc-3.8.9b # ./script.sh testing testing
root@debian8:~/shc-3.8.9b # ./shc -r -T -f ./script.sh
На выходе получаем 2 новых файла: script.sh.x — двоичный исполняемый файл script.sh.x.c — С код полученный из bash скрипта, при желании его можно подправить руками и скомпилировать штатными средствами системы. Проверяем бинарник:
root@debian8:~/shc-3.8.9b # ./script.sh.x "binary file" binary file
Использовать полученный бинарник можно практически на любой версии linux. Скрипту можно установить срок годности (опция -e), по истечение которого файл будет выдавать сообщение (опция -m).
How to Edit and Convert Binary Files on Linux
If a file stores data in contiguous bytes format, a program trying to read this file will need to be instructed on how to read it since such files do not directly define a compatible method for reading their associated content.
This type of file is called a binary file. Opening such a file on a normal text editor program will only display unreadable characters. It is because binary data store data as bytes and not as textual characters.
The headers of a binary file are accompanied by an instruction set that reveals how its stored data should be read. Since binary files can store any data type, we can broadly classify all file types as either binary or text.
Create Binary File in Linux
We are going to create a sample binary file that we will try to edit. We will first create a text file with some data in it and then convert the text file to a binary file using the hexdump command.
$ echo "LinuxShellTips changed my Linux perspective!" > simple.txt $ hexdump simple.txt > simple.bin
The cat command should confirm to us that the binary conversion was a success.
Editing Binary Files in Linux
We are going to use the xxd command associated with the vim editor package. We first need to open the file on Vim editor using the -b flag since we are dealing with a binary file.
Use keyboard key [i] to enter insert mode and edit the binary file where needed. For instance, we can remove the first-line hex entries 694c to see what happens.
Convert Binary File to Text in Linux
To convert the binary file to text mode to view the implemented changes, we will switch to command mode using the keyboard key [Esc] and then key in the vim command:
Once we hit [Enter] on the keyboard, we should see the edits we made.
To save changes and quit vim use:
We have successfully demonstrated the possibility of editing a binary file in Linux using vim editor. Know of other cool ways of editing binary files? Feel free to leave a comment or feedback.
How can I create a binary file using Bash?
Even if you probably simplified your example to make it shorter: This code doesn’t check for errors AND DON’T USE write(2) because it is perfectly ok not only to fail, but also to do only partial writes. Use fwrite(3) or similar instead
4 Answers 4
There’s only one byte you cannot pass as an argument in a Bash command line: 0
For any other value, you can just redirect it. It’s safe.
echo -n $'\x01' > binary.dat echo -n $'\x02' >> binary.dat .
For the value 0, there’s another way to output it to a file
dd if=/dev/zero of=binary.dat bs=1c count=1
dd if=/dev/zero oflag=append conv=notrunc of=binary.dat bs=1c count=1
Just dd if=/dev/zero bs=1 count=1 wothout of and oflag outputs the NUL byte to stdout. So you can do a > or >> .
xxd: creates a hex dump of a given file or standard input. It can also convert a hex dump back to its original binary form.
For those wanting to know how to use xxd to write without having to go away and look it up (like I had to): echo «0000400: 4142 4344» | xxd -r — data.bin where 0000400 is the byte offset into the file and the hex bytes 41 thru 44 are what’s written (the embedded space is ignored). This example writes the string ‘ABCD’ at 1024 bytes into the file ‘data.bin’.
If you don’t mind to not use an existing command and want to describe you data in a text file, you can use binmake. That is a C++ program that you can compile and use like following:
First get and compile binmake (the binary will be in bin/ ):
git clone https://github.com/dadadel/binmake cd binmake make
Create your text file file.txt :
big-endian 00010203 04050607 # Separated bytes not concerned by endianness 08 09 0a 0b 0c 0d 0e 0f
Generate your binary file file.bin :
./binmake file.txt file.bin hexdump file.bin 0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e 0000008
Note: you can also use it with standard input and standard output.