Remote IT Support!! Please contact us

For Any Remote Support,Implementation/Upgrade Projects,Queries,Collaborations please mail us at

Encrypt Shell Script on Linux using SHC

No comments

Encrypt Shell Script on Linux using SHC

shc - Generic shell script compiler

       shc [ -e date ] [ -m addr ] [ -i iopt ] [ -x cmnd ] [ -l lopt ] [ -o outfile ] [ -ABCDhUHvSr ] -f script

       shc creates a stripped binary executable version of the script specified with -f on the command line.

       The  binary version will get a .x extension appended by default if outfile is not defined with [-o outfile] option and will usually be a bit larger in size than the original ascii code.  Generated C source code is saved in a file with the extension .x.c or in a file specified with an appropriate option.

       If you supply an expiration date with the -e option, the compiled binary will refuse to run after the date specified.  The message Please contact your provider will be displayed instead.  This message can be changed with the -m option.

       You can compile any kind of shell script, but you need to supply valid -i, -x, and -l options.

       The compiled binary will still be dependent on the shell specified in the first line of the shell code (i.e.  #!/bin/sh), thus shc does not create completely independent binaries.

       shc  itself is  not  a compiler such as cc, it rather encodes and encrypts a shell script and generates C source code with the added expiration capability.  It then uses the system compiler to compile a stripped binary that behaves exactly like the original script.  Upon execution, the compiled binary will decrypt and  execute  the  code  with  the  shell  -c
       option.  Unfortunately, it will not give you any speed improvement as a real C program would.

       shc's main purpose is to protect your shell scripts from modification or inspection.  You can use it if you wish to distribute your scripts but don't want them to be easily readable
       by other people.

Steps to encrypt the files

1) Install SHC on Linux.

I am using Linux 7 here

 yum install shc 
download from

2) Create the shell script which needs to be encrypted. I am using a sample script I have created for this example.

[himanshu@oel7 ~]$ cat
echo $menu1
echo $menu2

[himanshu@oel7 ~]$ ./

3) Encrypt shell script using SHC.

[himanshu@oel7 ~]$ shc -f

Once encrypted 3 files would be created.

-rwxr-xr-x. 1 himanshu himanshu    62 Jul 21 20:40
-rw-rw-r--  1 himanshu himanshu 17771 Oct 18 23:17
-rwxrwxr-x  1 himanshu himanshu 11216 Oct 18 23:17 is the original unencrypted shell script is the encrypted shell script in binary format is the C source code of the file. This C source code is compiled to create the above-encrypted file. The whole logic behind the shc is to convert the shell script to C program (and of course compile that to generate the executable)

[himanshu@oel7 ~]$ file Bourne-Again shell script, ASCII text executable
[himanshu@oel7 ~]$ file C source, ASCII text
[himanshu@oel7 ~]$ file ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=469b3a7758b1b165130711853a80075b8c940c43, stripped

4) Run encrypted shell script.

[himanshu@oel7 ~]$ ./

Now for security, you can move the and to a safe location and use the only binary file.

Other cool features available with SHC are 

a) Specifying Expiration Date for Shell Script

Using shc you can also specify an expiration date. i.e After this expiration date when somebody tries to execute the shell script, they’ll get an error message.

Create a new encrypted shell script using “shc -e” option to specify an expiration date. The expiration date is specified in the dd/mm/yyyy format.

$ shc -e 17/10/2020 -f
In this example, if someone tries to execute the, after 31-Dec-2014, they’ll get a default expiration message as shown below.

[himanshu@oel7 bkp]$ shc -e 17/10/2020 -f
[himanshu@oel7 bkp]$ ls -ltr
total 36
-rwxr-xr-x. 1 himanshu himanshu    62 Jul 21 20:40
-rw-rw-r--  1 himanshu himanshu 17972 Oct 18 23:29
-rwxrwxr-x  1 himanshu himanshu 11256 Oct 18 23:29
[himanshu@oel7 bkp]$ ./
./ has expired!
Please contact your provider

In case you want to Set a custom message to display, use below
$ shc -e 17/10/2020 -m "Contact for latest version of this script" -f

[himanshu@oel7 bkp]$ shc -e 17/10/2020 -m "Contact for latest version of this script" -f
[himanshu@oel7 bkp]$ ./
./ has expired!
Contact for latest version of this script

b) Create Redistributable Encrypted Shell Scripts

Apart from -e, and -m (for expiration), you can also use the following options:

-r will relax security to create a redistributable binary that executes on other systems that runs the same operating system as the one on which it was compiled.

-v is for verbose

$[himanshu@oel7 bkp]$ shc -v -r  -f
shc shll=bash
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc -o
shc: strip
shc: chmod ug=rwx,o=rx

If you like please follow and comment

No comments :

Post a Comment