HOWTO: Encrypting a shell script on a Linux or Unix based system
Purpose: This blog entry will explain how to encrypt a shell script on your Linux or a Unix based system. Although there might be other ways to encrypt your shell script I found this one the most easiest to use. We will encrypt the script using the shc utility. I have been using this utility since last 3 years and it works great in situations where you want users to execute the shell script but at the same time you don’t want them to see the source code of the shell script. So let’s get started…
Step 1: Download or obtain the source
Luckily Debian Etch has “.deb” package for the shc utility so we will use it. On a Debian Etch system do:
# apt-get update;
# apt-get install shc
Now go to step 3 if you are on a Debian Etch system. If you are on a Lenny system then follow step 2.
If you are on Debian Lenny system (chances are very high) then you will need to download the “.deb” file from Debian Etch repos. You either download it from here or you can give the following command:
# apt-get update; apt-get install wget
# wget http://http.us.debian.org/debian/pool/main/s/shc/shc_3.8.6-2_i386.deb
This will download the file named shc_3.8.6-2_i386.deb into the directory from where you gave the above command.
If you are on a system other than Debian, you can download the tarball from here or by giving the command:
fedora# wget http://www.datsi.fi.upm.es/%7Efrosal/sources/shc-3.8.6.tgz
Step 2: Install the “.deb” file (only for users on Debian Lenny or Debian Sid)
Once you have downloaded the “.deb” file on your Linux system, install it using dpkg command:
# dpkg -i <file-you-downloaded-from-step1>
Example:
# dpkg -i shc_3.8.6-2_i386.deb
You only need to do the above on a Lenny system. If you are on a Debian Etch system it gets installed automatically.
Step 3: Encrypt your shell script
Now get hold of your shell script that you would like to encrypt. In this example, we will use a bash shell script called cleanlog.sh whose contents are as follow:
#!/bin/bash
echo "Starting to clear Log files..."
cd /var/log;
find ./ -type f -print >> list.txt
cat list.txt | while read a_line
do
cat /dev/null > $a_line;
done
cd;
echo "Log files cleared!"
Now give the following command to encrypt your shell script:
# shc -f cleanlog.sh
You will noticed that the above command creates two files:
# ls -l
cleanlog.sh.x.c
cleanlog.sh.x
cleanlog.sh.x – is the encrypted binary file that we will use
cleanlog.sh.x.c – is the C source code file.
Basically the shc command coverts your shell script into a C program first and then it compiles the C program into a binary using an encryption algorithm:
Shell Script-> C source code Program-> Binary executable
You can delete the cleanlog.sh.x.c file and your original shell script, cleanlog.sh, safely.
Step 4: Execute your encrypted shell script
Now you are ready to execute your shell script:
# ./cleanlog.sh
Starting to clear Log files...
Log files cleared!
#
Additional Notes
1. There are some neat features that you can enable by passing some options to the shc command. For example do this:
# shc -v -r -f cleanlog.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec '%s' "$@"
shc [-l]=
shc opts=
shc: cc cleanlog.sh.x.c -o cleanlog.sh.x
shc: strip cleanlog.sh.x
shc: chmod go-r cleanlog.sh.x
#
The above options:
“-f” – Tells that file name follows and is to be given every time.
“-v” – Tells shc command to be verboase
“-r” – Tells shc command to relax the security measure i.e. make a redistributable binary which executes on different systems running the same operating system (from man page).
So for example, if you try to execute the binary generated from Step 4 (i.e. no giving the “-r” option) on a different Linux system (by copying the executable from the system on which it was compiled to another Linux system) you won’t be able to execute it and you will get an error message like this:
system2# ./cleanlog.sh.x
./cleanlog.sh.x: ªÃ¨«ZÉÆ¥«ÞÀãì+
ï'ÛìHhas expired!
Please contact your provider
system2:~#
2. Furthermore, there are other options also like following which can try:
-e date Expiration date in dd/mm/yyyy format [none]-m message message to display upon expiration ["Please contact your provider"]
# shc -v -r -e 01/17/2009 -m "Your program has expired" -f cleanlog.sh
3. If you get the following error messages upon give the above command:
# shc -f cleanlog.sh
cleanlog.sh.x.c:108:22: error: sys/stat.h: No such file or directory
cleanlog.sh.x.c:109:23: error: sys/types.h: No such file or directory
cleanlog.sh.x.c:111:19: error: errno.h: No such file or directory
cleanlog.sh.x.c:112:19: error: stdio.h: No such file or directory
cleanlog.sh.x.c:113:20: error: stdlib.h: No such file or directory
cleanlog.sh.x.c:114:20: error: string.h: No such file or directory
cleanlog.sh.x.c:115:18: error: time.h: No such file or directory
cleanlog.sh.x.c:116:20: error: unistd.h: No such file or directory
cleanlog.sh.x.c: In function 'key_with_file':
cleanlog.sh.x.c:178: error: array type has incomplete element type
cleanlog.sh.x.c:179: error: array type has incomplete element type
cleanlog.sh.x.c:185: warning: incompatible implicit declaration of built-in function 'memset'
.....................
.....................
then give the following command:
# apt-get install gcc libc6-dev
Last but not the least. There is no guarantee that this utility will provide you a very strong security protection. Experienced users or hackers who have sufficient knowledge about “gdb” or other debugger tools can decrypt your shell script. Although it does provide a good started point to encrypt (hide) shell scripts from “regular” users if you are a system administrator.
That’s it folks. Enjoy encrypting your shell scripts.


Email Subscription









January 27th, 2009 at 11:17 pm
Great post
I have a problem:
[root@CRBC-SRV-SYSLOG shc-3.8.6]# shc -f test.sh
sh: cc: command not found
shc: Success
It creates the test.sh.x.c file but not else.
Looks like a great app but how do I get it to work?
test.sh:
#!/bin/sh
echo “This is a test!
Reply to this comment
Kushal Reply:
January 27th, 2009 at 11:36 pm
Did you install gcc? do:
apt-get update
apt-get install gcc libc6-dev
If this does not work then let me know which Distro are you using….
Thank you for your comment.
Reply to this comment
SeeFor Reply:
January 28th, 2009 at 2:56 pm
That did it, thanks for your help.
This is a good way to protect your shell scripts.
Thanks,
Sif
Reply to this comment
January 28th, 2009 at 2:55 pm
That did it thanks, I’m using Fedora 10
Thanks for the help, this is really cool way of protecting your shell scripts.
Reply to this comment
March 12th, 2009 at 5:23 am
Thanks Friend!
You made my day :) It works!
Reply to this comment
July 26th, 2009 at 1:59 pm
thanks for this post
but after i encrypted one of my bash scripts and it works fine
i had a problem with my HDD and i have only the encrypted script with my friend
is there any way to decrypt this files ?
some of this scripts need update and it’s about 900 line i can’t type it again
please let me know if there any possible way to decrypt this files
Reply to this comment
Admin Reply:
July 26th, 2009 at 8:15 pm
Hmm…I guess you didn’t see that coming…Well I can tell you two things:
a) Try reading the source code and see if you can get some clue from there as to how to disassemble it. AFAIK, the script uses rc4 algorithm to encrypt it.
b) Try emailing the author and see if he replies back. Most likely they will not.
http://www.datsi.fi.upm.es/~frosal/
Reply to this comment
bins Reply:
February 13th, 2013 at 7:21 am
run your script and open another terminal and type ps -ef | grep script name
Reply to this comment
September 21st, 2009 at 2:18 pm
having a problem.
whenever i try encrpyt a script it gives this error. am trying to use the expect command in the script so am guessing shc doesnt recognize it
here the error.
shc Unknown shell (expect): specify [-i][-x][-l]
shc: Success
Thanx in advance
Reply to this comment
Admin Reply:
September 23rd, 2009 at 10:42 pm
It seems that expect is some sort of interactive program. I have never used it so I don’t have much idea. It seems that expect command is expecting (no pun intended) some kind of data input. If you can post your part of the script in which you are having trouble may be I can give you some more ideas?
Reply to this comment
Mohammed Reply:
February 9th, 2010 at 2:37 am
You can use tclkit and Starkit to encrypt expect scripts.
Refer http://www.equi4.com/ for more details.
~mohammed
Reply to this comment
April 16th, 2010 at 10:06 am
The ps command with -fwwu switches will still show the original script. Other than adding a bunch of blank comments to the beginning of the script, do you have any idea how to prevent this?
Reply to this comment
May 18th, 2010 at 7:30 am
Can I have shell encryption utility shc on my RHEL OS. Please give me link to have the RPM for the same and procedure to follow the installation of the same.
Reply to this comment
June 3rd, 2010 at 1:00 am
maddy,
check this out:
http://www.linuxsecurity.com/content/view/117920/49/1/1/
~mohammed
Reply to this comment
August 23rd, 2010 at 11:49 pm
[...] This also may be helpfull: Encrypting shell scripts Encrypting shell scripts 2 Kind regards, [...]
August 25th, 2010 at 3:12 am
Hi,
I’m using shc-3.8.6-1 under Centos. I dont have any error while compiling the shell scripts. But I got following error while running the test.sh.x file or C executable of corresponding shell script.
-bash: ./test.sh.x: Permission denied
What is the problem with shc any clue please.
Thanks,
Haridas N.
Reply to this comment
October 1st, 2010 at 9:07 am
Hi ,
while compiling through shc of test.sh it gives below error:
[ghoshd@/home/ghoshd] #./shc -f test.sh
ksh: ./shc: cannot execute
[ghoshd@/home/ghoshd] #w
[ghoshd@/home/ghoshd] #ll shc
-rwxrwxrwx 1 ghoshd orasupp 39767 Jul 10 2006 shc
[ghoshd@/home/ghoshd] #
anyidea ?
thanks,
Dipankar
Reply to this comment
Admin Reply:
October 1st, 2010 at 9:18 am
You are trying to execute the command “shc” as “./shc” which is incorrect. You only use those syntax for scripts. What you need is this:
# shc -f test.sh
# sh tesh.sh
or
#./test.sh
Hope it is clear now.
Reply to this comment
October 11th, 2010 at 11:09 am
I can successfully create *.sh.x files but when I try to run them I receive the following error:
somefilename.sh.x: command not found
Reply to this comment
Admin Reply:
October 19th, 2010 at 4:26 pm
Did you make the file executable?
Try:
# sh somefilename.sh.x
Reply to this comment
October 19th, 2010 at 7:54 pm
Thank you for your reply. I will try this and cross my fingers. I’ve been trying this on the Backtack 4 r1 “start persistent cd” and after getting the “command not found” error my disk slowly filled up(4% left and then 0%)and then just nuke the USB disk and start over. I was beginning to think shc was a virus :-)
Reply to this comment
October 27th, 2010 at 4:18 am
i have tried compiling my script using the command below
shc -r -v -f
when i ran the ./scriptname.sh.x, it works!
but when i copied the scriptname.sh.x to another linux machine, give it the permission (chmod 777), it throws an error…
-bash : scriptname.sh.x: cannot execute binary file
any ideas?
Reply to this comment
February 9th, 2011 at 2:02 pm
I am on an Ubuntu 10.10 64 bit machine. I have followed the above directions to make the binary file (update.sh.x) I unable to execute the script.
$ ./update.sh.x
./update.sh.x: Operation not permitted
Killed
When I made the file I ran into a couple lines that may have not seemed right. Here is what I got on the terminal. Can anyone make any sense of this?
$ shc -v -e 01/01/2020 -m “The update Script has expired” -f update.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec ‘%s’ “$@”
shc [-l]=
shc opts=
shc: cc update.sh.x.c -o update.sh.x
update.sh.x.c: In function ‘chkenv’:
update.sh.x.c:269: warning: cast from pointer to integer of different size
shc: strip update.sh.x
shc: chmod go-r update.sh.x
Reply to this comment
April 1st, 2011 at 12:39 am
I have loaded shc in RHEL 5.2.
Got a script encrypted with the utility as test1.sh.x by using
shc -v -r -f test1.sh
I tried running this in another machine with RHEL 4.4 , it is giving a floating point error.
Can you help.
Reply to this comment
June 20th, 2011 at 3:55 am
[...] Fonte: http://linux.koolsolutions.com [...]
September 16th, 2011 at 1:29 pm
Does it work with awk script implemented in shell script?
Reply to this comment
November 18th, 2011 at 6:47 pm
I had to download the tar file, extract it, make install, and the shc command works. I also have the correct libc6 files. When I run shc -f scriptname.sh it gives me:
shc: invalid first line in script: cd /root/Desktop
shc: Success
It does not create any files.
??
Reply to this comment
February 1st, 2012 at 5:45 am
How to convers .x file back to standard code .sh code
Reply to this comment
March 1st, 2012 at 6:50 pm
on Centos 6.2, I have installed SHC with rpm version is shc-3.8.6-1.el6.rf.i686
I have run
#shc -v -e 01/01/2013 -m “Please find the updated version” -f ll.sh
now ll.sh.x and ll.sh.x.c files are there but when I am going to execute ll.sh.x like this
sh ll.sh.x or ./ll.sh.x
it gives me error:
ll.sh.x: ll.sh.x: cannot execute binary file
$file ll.sh.x
ll.sh.x: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
will you please help me regarding this??
Thank you for such a nice post, it help me lot
I am successfully run these commands and scripts on CENTOS 5x
but problem is on CENTOS 6.2
Reply to this comment
May 25th, 2012 at 12:37 am
I’m getting a
./test.sh: has expired!
Please contact your provider
Any ideas?
Reply to this comment