How to Convert Your Shell Script to Binary

How to create a binary file of your Shell Scripting.


This article will help you create a binary file of your Shell Scripting, so that no one can see the source code of your script and we can use it as a command. To create a binary file from a script, we use the SHC compiler .

Introduction

shc, a Shell Script compiler, produces the source code in C. The generated source code is then compiled and linked to produce a separate binary executable.

The compiled binary will continue to be dependent on the shell specified in the first line of the shell code (ie, shebang) (ie #!/bin/sh), so shc does not create completely independent binaries.

shc itself is not a compiler like gcc, it encodes and encrypts a shell script and generates the C source code with the added expiration feature. It then uses the system compiler to compile a separate binary that behaves exactly like the original script. After execution, the compiled binary will decrypt and execute the code with the shell -c option.

Installation

In Gentoo and similar, use the portage that shc is already available on the tree:

emerge dev-util/shc

For other distributions, for example derived from APT, first install the necessary dependencies:

sudo apt-get install libc6-dev # Debian, Ubuntu, Mint, ...
sudo yum install glibc-devel # RHEL, CentOS, Fedora, ...

Then download the package, decompress and compile the code:

wget https://github.com/neurobin/shc/archive/release.zip
cd shc-*
make
sudo make install

Use

Create a test file to see how it works: vim script.sh:

#!/bin/bash

echo -e "The first 3 characters of each location file / directory are:\n"
for i in *; do
	echo "$i" | cut -c 1-3 | tr '\n' '\ '
done
echo

Then use shc to transform it into binary:

shc -f script.sh -o binary

And just run the program: ./binary

If you try to see the contents of the binary file, you will see everything encoded, example: cat binary

...
@x@�@�@�@�@�@�@�@�@�@�H�H��/H��t��H���5�/�%�/@�%�
�@����%�/h
          �0����%�/h
�����%r/h�����%j/h������%b/h
...

Para mais informações e opções via linha de comando, utilize a ajuda e o manual:

shc --help
man shc

Official page shc: https://neurobin.org/projects/softwares/unix/shc/

Thanks for reading!


bash shellscript clang


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles