convert shell script into a binary executable

Hello every one, i want to convert my shell script into a binary executable a .exe file , is it possible to do that if so are there any tools . Would the script take off when the arguments are parsed.

Thanks
Venu

There is a shell compiler maintained and available here:

Francisco Rosales, home page

Well, this is something I have often pondered. A way to write code that is far more difficult to decipher - unless you have the source.

We have a number of intelligent users (if that's not an oxymoron) who like to read scripts and work out what they do and how. Of course, there are various sensitive things that we want to keep secret from them, yet give them the scripted ability to do. Nothing too complex, but it often involves privileged access to a database, for which we store the passwords in a file. Of course, if you can read the script, you know where to find the file! :o

I grabbed at this link delighted that someone could solve all my woes, but I fail to get it to build. I have a few versions of AIX and two HPUX servers. From the README, it seems to suggest I just run a make, but that fails and I don't know even what it is trying to do. I've pretty much written everything as SQL or KSH (unless you count REXX on VM), so I've no experience in this. I did gcc compile the shc.c and get an executeable which I could then use to process test.ksh to create test.ksh.x, but this wouldn't run. Just the message "Killed" came back.

Am I being a complete fool? :confused:

Any pointers?

"Hat's Off" to Jim for finding this if it works. :b:

ksh93 comes with a compiler called shcomp

$ cat test
#!/bin/ksh
for (( i=1; i<=5; i++)); do
  echo Hello World
done
$ ( echo '#!/bin/ksh' ; shcomp test ) > test.bin && chmod +x test.bin
$ ./test.bin
Hello World
Hello World
Hello World
Hello World
Hello World

Which will not make it totally unreadable, but to a certain extent.

$ od -c < test.bin
0000000   #   !   /   b   i   n   /   k   s   h  \n  \v 023  \b  \0 003
0000020  \0 003  \f 002 005       i   =   1 001  \0 005  \f 002 004   i
0000040   +   + 001  \0  \f 002 006       i   <   =   5 001  \0 203  \0
0000060   @  \0 003 005   e   c   h   o 006   H   e   l   l   o 006   W
0000100   o   r   l   d  \0 003
0000106

shcomp doesn't really compile the script, but saves the parser tree that's generated internally. The script itself is still traceable using -x as if it were plain text.

shc, as suggested, will encrypt the script using RC4, but you'll lose the cross-platform advantage. Also, somehow it seems to dislike newer versions of Linux, or at least VirtualBox, as the compiled scripts always died.

So, are there any suggestion how to get shc running? As a non-C person, I don't understand the compile process to get the tool installed, so I don't know what I'm looking for to fix it. :o

Robin
Liverpool/Blackburn
UK

Are you on AIX 64 bit, as it looks like someone had a similar problem here and found a fix.

You could also try the -T option to shc, this also got it working of AIX 5.3 64bit for me, also fixed `PTRACE_ATTACH' undeclared error when generating on cygwin:

$ ./shc -T -f test.ksh
 
$ ./test.ksh.x test params 1 2 3
+ alias whence=type -a
+ alias ll=ls -l
+ alias la=ls -A
+ alias l=ls -CF
+ echo $@ is test params 1 2 3
$@ is test params 1 2 3
+ echo command line: ./test.ksh.x test params 1 2 3
command line: ./test.ksh.x test params 1 2 3
+ echo hello world
hello world
+ echo [581760] PAUSED... Hit return!
[581760] PAUSED... Hit return!
+ read DUMMY
done
+ exit 0

'
BTW I think you probably need to set CC=gcc before trying the make (If not I might be able to help if you supply the error make is giving):

$ CC=gcc ; export CC
$ make

Well, I'm running a variety of AIX platforms. We have 4.3.3 (to be decommissioned soon, honest) one at 5.1, 32-bit and a 6.1, 64 bit. I get the same on all apart from AIX 6.1, so I put it down to user error :wink:

Unfortunately, we don't even have the gcc compiler. We do have the official IBM XLC compiler, version 5 on AIX 5.1 and version 10 on AIX 6.1 yacc appears to be on all of them too, although I've never used it. Come to think of it, I've only used the available C compiler to sompile sudo, lsof and a few other tools. Sorry about that.

So, the errors:

AIX 4.3.3/5.1
RBATTE1 @ /usr/home/RBATTE1>zcat shc-3.8.7.tar.Z |tar -xvf -
x shc-3.8.7/CHANGES, 3637 bytes, 8 media blocks.
x shc-3.8.7/Copying, 17982 bytes, 36 media blocks.
x shc-3.8.7/Makefile, 1940 bytes, 4 media blocks.
x shc-3.8.7/match, 336 bytes, 1 media blocks.
x shc-3.8.7/pru.sh, 155 bytes, 1 media blocks.
x shc-3.8.7/shc-3.8.7.c, 24962 bytes, 49 media blocks.
x shc-3.8.7/shc.1, 3136 bytes, 7 media blocks.
x shc-3.8.7/shc.README, 1544 bytes, 4 media blocks.
x shc-3.8.7/shc.c is a symbolic link to shc-3.8.7.c.
x shc-3.8.7/shc.html, 4558 bytes, 9 media blocks.
x shc-3.8.7/test.bash, 138 bytes, 1 media blocks.
x shc-3.8.7/test.csh, 52 bytes, 1 media blocks.
x shc-3.8.7/test.ksh, 142 bytes, 1 media blocks.
RBATTE1 @ /usr/home/RBATTE1>cd shc-3.8.7
RBATTE1 @ /usr/home/RBATTE1/shc-3.8.7>make
        gcc -Wall -O6  shc.c -o shc
gcc: not found

make: 1254-004 The error code from the last command is 1.


Stop.
RBATTE1 @ /usr/home/RBATTE1/shc-3.8.7>export CC=cc
RBATTE1 @ /usr/home/RBATTE1/shc-3.8.7>make        
        cc -Wall -O6  shc.c -o shc
cc: 1501-210 command option Wall contains an incorrect subargument
make: 1254-004 The error code from the last command is 40.


Stop.
RBATTE1 @ /usr/home/RBATTE1/shc-3.8.7>
AIX 6.1
$ zcat shc-3.8.7.tar.Z |tar -xvf -
x shc-3.8.7/CHANGES, 3637 bytes, 8 media blocks.
x shc-3.8.7/Copying, 17982 bytes, 36 media blocks.
x shc-3.8.7/Makefile, 1940 bytes, 4 media blocks.
x shc-3.8.7/match, 336 bytes, 1 media blocks.
x shc-3.8.7/pru.sh, 155 bytes, 1 media blocks.
x shc-3.8.7/shc-3.8.7.c, 24962 bytes, 49 media blocks.
x shc-3.8.7/shc.1, 3136 bytes, 7 media blocks.
x shc-3.8.7/shc.README, 1544 bytes, 4 media blocks.
x shc-3.8.7/shc.c is a symbolic link to shc-3.8.7.c.
x shc-3.8.7/shc.html, 4558 bytes, 9 media blocks.
x shc-3.8.7/test.bash, 138 bytes, 1 media blocks.
x shc-3.8.7/test.csh, 52 bytes, 1 media blocks.
x shc-3.8.7/test.ksh, 142 bytes, 1 media blocks.
$ cd shc-3.8.7
$ export CC=gcc
$ make
gcc -Wall -O6  shc.c -o shc
***     �Do you want to probe shc with a test script?
***     Please try...   make test
$ echo "#!/bin/ksh\necho \"Hello world.\"">robin.ksh
$ shc -x robin -f robin.ksh
$ robin.ksh.x
Killed
$ 

I feel completely useless on this, but extra depressed because the diskwasher popped last night (overflow/flood detection triggerred), we're snowed in and the wife's parents are with us for a week:eek:

Any pointers? The place we really want this is on the AIX 5.1 servers, if that concentrates things.

.... on either the shc or dishwasher :slight_smile:

Thanks,
Robin
Liverpool/Blackburn
UK

---------- Post updated at 04:09 PM ---------- Previous update was at 03:49 PM ----------

:slight_smile: Aha! I have it working on AIX 6.1 by running :slight_smile:

shc -T -f robin.ksh

:o I am a fool for not reading the other post to the end. :o

=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

:frowning: Annoyingly, it is stil the AIX 5.1 that we really want. :frowning:

Robin

Oh snow, we don't see that much in Australia, especially around christmas time, in-laws - yes heaps, but snow, not so much, lol.

I think if you update the makefile and add the line is blue lines below it should compile with the CC=cc; export CC setting:

# For GNU C compiler
CFLAGS = -Wall -O6 # -pedantic
 
# For AIX 5.1
CFLAGS = -O3
 
SHELL = /bin/sh

Yes, yes, yes, yes, y....... no!

RBATTE1 @ /usr/home/RBATTE1/shc-3.8.7>make
        cc -O3 shc.c -o shc
    1500-036: (I) Optimization level 3 has the potential to alter the semantics of a program.  Please refer to documentation on -O3 and the STRICT option for more information.
ld: 0711-317 ERROR: Undefined symbol: .atoll
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.
make: 1254-004 The error code from the last command is 8.
RBATTE1 @ /usr/home/RBATTE1/shc-3.8.7>

I did try to go a bit wild when I saw the "Undefined symbol". I searched out every direcotry called lib and added it to my LIBPATH (ignoring 3rd party software)

No difference. :confused:

Robin

P.S. Let's not talk about the Cricket.

Umm, that's a bit of a concern as atoll() is typically part of the base system (in libc.a). If your system has strtoll() we can use this instead by defining a macro below the definition of SIZE (around line 102) as show in blue:

#define SIZE 4096
#define atoll(x) strtoll(x, (char **)NULL, 10)

[/COLOR]

Firstly, good news that your home is not affected by the floods.

That helps a little in that I get a shc executeable, however using it fails. I created a very simple script:-

#!/bin/ksh
echo "Hello"

and ran the following:-

$ shc -x my_hello -f robin
shc -x my_hello -f robin
ld: 0711-317 ERROR: Undefined symbol: .atoll
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.
shc: Error 0
$ ls -lrt | tail -5
-rw-r-----   1 RBATTE1  SYSSUP        25009 17 Jan 10:50 shc-3.8.7.c
-rwxr-xr-x   1 RBATTE1  SYSSUP        48454 17 Jan 10:50 shc
-rw-r--r--   1 RBATTE1  SYSSUP           24 17 Jan 10:53 robin
-rw-r--r--   1 RBATTE1  SYSSUP         8016 17 Jan 10:53 robin.x.o
-rw-r--r--   1 RBATTE1  SYSSUP         9514 17 Jan 10:53 robin.x.c

I also tried to edit shc.c / shc-3.8.7.c to change the two atoll references in the code to be strtoll (not even sure if this is valid! :o) and got the following:

shc -x my_hello -f robin
"robin.x.c", line 290.32: 1506-098 (E) Missing argument(s).
$ ls -lrt|tail -5
-rw-r--r--   1 RBATTE1  SYSSUP           24 17 Jan 10:53 robin
-rw-r-----   1 RBATTE1  SYSSUP        25013 17 Jan 10:57 shc-3.8.7.c
-rwxr-xr-x   1 RBATTE1  SYSSUP        48454 17 Jan 10:57 shc
-rw-r--r--   1 RBATTE1  SYSSUP         9231 17 Jan 10:57 robin.x.c
-rwx--x--x   1 RBATTE1  SYSSUP         7757 17 Jan 10:57 robin.x

Like a fool, I tried to see what would happen if I ran the file marked as executable:-

$ robin.x
robin.x: my_hello:  not found.
$

I did find this on an IBM site Using the GNU C/C++ compiler on AIX:-

Does this mean I've got a dead duck? :wall: We're on a mixture of AIX 5.1:o and 4.3.3 :eek:

Thanks again,
Robin
Liverpool/Blackburn
UK


Nice innings by Mr. Watson over the weekend.  Much better these games than the all five one-sided test matches.  England were lucky in my opinion not to have lost the first, and that would have changed the whole series.

Good news that sprtoll() is available on your system, one more simple change to shc.c should get you going (don't forget to make it again after change):

replace:

"       if (date[0] && (atoll(date)<time(NULL)))",

with:

"       if (date[0] && (sprtoll(date,(char **)NULL,10)<time(NULL)))",

Eureka!

I guessed that you meant strtoll where you had sprtoll.

It works wonderfully! Ther only thing I have to do is to ensure that LIBPATH includes /usr/lib at runtime. It is a pain because some idiot (and I don't mean me for a change) has hardcoded something else in /etc/profile. That will be fun to get changed.

Anyway, many, many, many, many thanks! :b::):D;):b:

Let's see what contest the cricket brings now.....:stuck_out_tongue:

Come on the Poms! :cool:

Actually, let's just have a good game like the last one, preferably with the other result, but hey let's have fun.

Robin
Liverpool/Blackburn
UK