- How to Edit and Convert Binary Files on Linux
- Create Binary File in Linux
- Editing Binary Files in Linux
- Convert Binary File to Text in Linux
- Как сделать бинарник из bash скрипта
- Использование shc
- How to convert a shell script into a binary executable?
- 3 Answers 3
- How can I create a binary file using Bash?
- 4 Answers 4
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.
Как сделать бинарник из 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 convert a shell script into a binary executable?
I want to convert my shell scripts into binary executable so that nobody else could edit or read it. Is there a way to convert it into a binary executable?
@l0b0 That link mentions two reasons. The third reason: you want it to run on machines that don’t have the interpreter installed (eg. running a bash script on a machine that only have sh). The fourth reason: it needs setuid root for some reason (eg. a script that lets the user mount volumes without sudo).
3 Answers 3
shc is what you’re looking for. get it here: shc
Extract, cd into dir, make and then ./shc -f SCRIPT . Done.
Everything you need to do this, you find here:
SHC Howto
+1 wow, that is interesting. Just beware that if the process is exactly as it is described, this is a weak method of concealing a password — it could still be extracted by someone who knows how executables are structured. However, it would be very effective for obscuring a script from casual observers. I’d be interested to learn if scripts pre-compiled this way are faster to execute.
Note that it doesn’t work with bash (or sh based on bash ), as one can do: env SHELLOPTS=verbose ./script.x to see the content of the script. There’s probably easy ways to bypass with other shells as well.
From the shc man page, «Unfortunatelly, it will not give you any speed improvement as a real C program would.» datsi.fi.upm.es/~frosal/sources/shc.html
Although SHC might not give you a speed improvement you hope for, it offers the possibility of hacking SHC itself to obtain a better translation of your script. Writing a clean shell script and then improving SHC could be more productive than writing a C program from scratch, or convoluting a shell script to make it faster.
If the goal here is to hide your shell script so it cant be read or modified, try pasting it to the following website:
When you submit your script to this site, a zip file will be produced for you. Perform the following steps to generate a standalone executable from this zip file.
- After submitting your script, right click on the download link
- Go to your Unix host and wget or (curl -O) the download link
- Unzip the zip file
- cd EnScryption.com
- ./(your-script.sh)
Step 5 will automatically configure the script and will generate a standalone copy for you. You can then scp this standalone copy to different hosts or distribute it however you want. Note, once encrypted, you cannot change the name of your script. This is for security reasons.
Another option available to you, if you have the time, is to come up with your own secure obfuscation algorithm. If your script is to be portable, you must use tools that are readily available on most Unix systems. ie. openssl or base64. This requires a lot of time. But if the motivation is there, it is indeed possible to make unveiling your scripts impossible or at very least, extremely time consuming!
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.