Shashikant shah

Monday 12 October 2015

How to Encrypt Your Bash Shell Script on Linux


1. Download shc and install it

Download shc and install it as shown below.

# wget http://www.encryptsystem.com/download/sch/shc-3.8.7.tgz
# tar xvfz shc-3.8.7.tgz
# cd shc-3.8.7
# make

Verify that shc is installed properly.

$ ./shc -v
shc parse(-f): No source file specified

shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-rvDTCAh] -f script

2. Create a Sample Shell Script

$ vi script.sh
#!/bin/bash

echo “Hello friends my first encrypt bash script.”

$ ./script.sh
Hello friends my first encrypt bash script.

3. Encrypt the Shell Script Using shc

Encrypt the script.sh shell scripting using shc as shown below.

$ ./shc -f script.sh
This will create the following two files:

$ ls -l script.sh*
-rwxrw-r–. 1 shashi shashi 149 Mar 27 01:09 script.sh
-rwx-wx–x. 1 shashi shashi 11752 Mar 27 01:12 script.sh.x
-rw-rw-r–. 1 shashi shashi 10174 Mar 27 01:12 script.sh.x.c

4. Execute the Encrypted Shell Script

$ ./linux.sh.x
Hello friends my first encrypt bash script

5. Specifying Expiration Date for Your 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.

Let us say that you don’t want anybody to execute the script.sh.x after 31-Dec-2014 (I used last year date for testing purpose).

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

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

$ ./script.sh.x
./script.sh.x: has expired!
Please contact your provider
If you like to specify your own custom expiration message, use -m option (along with -e option as shown below).

$ ./shc -e 31/12/2014 -m “Contact admin@encryptsystem.com for new version of this script” -f script.sh

$ ./script.sh.x
./script.sh.x: has expired!
Contact admin@encryptsystem.com for new version of this script

6. 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.
-T will allow the created binary files to be traceable using programs like strace, ltrace, etc.
-v is for verbose
Typically you might want to use both -r and -T option to craete a redistributable and tracable shell encrypted shell script as shown below.

$ ./shc -v -r -T -f script.sh
shc shll=bash
shc [-i]=-c
shc [-x]=exec ‘%s’ “$@”
shc [-l]=
shc opts=
shc: cc script.sh.x.c -o script.sh.x
shc: strip script.sh.x
shc: chmod go-r script.x

$ ./script.sh.x
Hello friends my first encrypt bash script.

Finally, it is worth repeating again: You should not be encrypting your shell script in the first place. But, if you decided to encrypt your shell script using shc, please remember that a smart person can still generate the original shell script from the encrypted binary that was created by shc.






Friday 9 October 2015

script for insert variable to other tty

#!/bin/bash
COMMAND=$1
OBJ_SEQUENCE_NUM=$2
RUN_COMMAND=$(echo "$COMMAND" | tr '[a-z]' '[A-Z]')
file="/tmp/update_obj.txt"

if [[ ! -f $file ]];then
    echo "$file not found."
else
    remove=$(rm -rf /tmp/update_obj.txt)
fi
    case $RUN_COMMAND in
        "COMPLETE")
        echo "comlete comand exc $2."
        ;;
        "FAIL")
        echo "fail comand exc $2."
        ;;
        *)
        echo "invaild command."
        exit 0
        ;;
esac

arr=$(echo $OBJ_SEQUENCE_NUM | tr "," "\n")

for i in $arr
do
create_file=$(echo "$RUN_COMMAND $i" >> /tmp/update_obj.txt)
all_command=$(cat /tmp/update_obj.txt)
done

sleep 1

. ./OBJCmdLineadmin.ksh << EOF > update.log
    $all_command
    exit
EOF
=========================================

script.sh complete 7673254,87328

Tuesday 6 October 2015

Script for copy data

#!/bin/bash

Souce_path=$1
Dest_path=$2

if [[ "$Souce_path" == '' ]] || [[ "$Dest_path" == '' ]];then
    echo "Source or Destination path can not be empty.";
    exit 0
fi

run=$(cp -rf $Source_path $Dest_path 2> /tmp/err)
input=$?
   
    if [ "$input" = = 0 ];then
    echo "Successfuly copied Directory and File."
else
    echo "Do not copy Directory and File."
exit 2
fi
=====================
# ./script  /shashi/test.txt   /test/