Thursday, September 16, 2010

Converting Bash script to Binary code

Bash Shell basically comprises of commands written in C language. Hence we basically need a Compiler that can convert the C code into executable Binary file.  I guess there are many C compilers available;  the one which I use is SHC (SHell script Compiler).  When we compile using this tool, it creates an execute file with .x extension, which is basically a executable version of your Bash script which can’t be readable or modifiable.

Illustration:

Extract the shc tar file.
Go into the shc-3.8.7 folder
Install the package by executing 'make'. It will create the 'shc' executable.

Create a sample bash script "sample.sh".

[root@host01 opt]$ cat sample.sh
#!/bin/bash

echo "Hello how r u"
echo
[root@host01 opt]$
[root@host01 opt]# ./sample.sh                              #  Take a note on output
Hello how r u

[root@host01 opt]#
[root@host01 shc-3.8.7]$ ./shc -f /opt/sample.sh
[root@host01 opt]# ls -l sample.sh*
-rwxr-xr-x 1 root root    39 Sep 13 12:11 sample.sh
-rwx--x--x 1 root root 11720 Sep 13 12:12 sample.sh.x     #  .x file created
-rw-r--r-- 1 root root  9406 Sep 13 12:12 sample.sh.x.c
[root@host01 opt]#
[root@host01 opt]# ./sample.sh.x                                #  Same result as before
Hello how r u

[root@host01 opt]# tail sample.sh.x                            #  File is in Binary format
@
@
 @
  @.
    @>
      @N
        @OÞ}¬÷ú=hòL³})âïM 3ði¹_=ÄÊÚî¦kpsJ=¸­¡`h]L1ôÌ7©+Éj3
                                                          ÌÜ.*Å=ßBl`H>û²ÞÎþýºD`44vÉáÛÑJ-h2»ñûMBpñ<D#ë0óÃH(rAÊß~jÀ

4 comments:

  1. Wonderful !! thanks a lot !!!!

    ReplyDelete
  2. That's What I Want

    ReplyDelete
  3. Is the reverse possible .coverting binary to readable script ?

    ReplyDelete
  4. why can't i export the script to a remote host and run it on the remote host? i get the following error:
    $ ./sample.sh.x
    ./sample.sh.x: error while loading shared libraries: requires glibc 2.5 or later dynamic linker

    ReplyDelete