Linux Programmer | RHCE | RHCSA

Search This Blog

Friday 9 December 2016

Encrypt shell script using shc in linux


For shell script encryption you need to install shc package

1. install shc

https://drive.google.com/open?id=0BwPBGrPmbppAYm9JRmhwTEp4ck0

2. extract shc-3.8.7.tgz and compile

tar xvfz shc-3.8.7.tgz

cd shc-3.8.7/

 make

How To Encrypt Shell Script ??

Create a sample bash shell script that you like to encrypt using shc for testing purpose.
For testing purpose, let us create the following random.sh shell script which generates random numbers. You have to specify how many random numbers you like to generate.

$ nano random.sh
#!/bin/bash

echo -n "How many random numbers do you want to generate? "
read max

for (( start = 1; start <= $max; start++ ))
do
  echo -e $RANDOM
done

$ ./random.sh
How many random numbers do you want to generate? 3
24682
1678
491
shc -r -T -f random.sh
This will create the following two files:
$ ls -l random.sh*
-rwxrw-r--. 1 purval purval 149 Mar 27 01:09 random.sh
-rwx-wx--x. 1 purval purval 11752 Mar 27 01:12 random.sh.x
-rw-rw-r--. 1 purval purval 10174 Mar 27 01:12 random.sh.x.c


  • random.sh is the original unencrypted shell script
  • random.sh.x is the encrypted shell script in binary format
  • random.sh.x.c is the C source code of the random.sh file. This C source code is compiled to create the above encrypted random.sh.x file. The whole logic behind the shc is to convert the random.sh shell script to random.sh.x.c C program (and of course compile that to generate the random.sh.x executable)
$ file random.sh
random.sh: Bourne-Again shell script text executable

$ file random.sh.x
random.sh.x: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

$ file random.sh.x.c
random.sh.x.c: ASCII C program text

Now, let us execute the encrypted shell script to make sure it works as expected.
$ ./random.sh.x
How many random numbers do you want to generate? 3
7489
10494
29627

No comments:

Post a Comment

SSH not working with password after upgrade ubuntu 22.04

Issue: In recent upgrade of ubuntu 22.04 we are not able to login server with SSH password. but when we try to login with key then it allow...