Some time ago, i wrote some bash scripts for my school. I thought it would be very clever to ‘protect’ them, so i compiled them with shc into a binary file. Some weeks later, i lost the uncompiled scripts and now i have only my binarys left.
Is there a way to retrieve the scripts back from the shc generated binarys? I looked into the source code of shc to find a way to decompile the binarys with no luck.
Using shc to compile your scripts does not protect them. You don’t get more security this way. The shc compiled binary decrypts and loads the script into memory when started. You could then, right after you started the binary, just segfault it and retrieve your script from the coredump.
Here’s a little example script named test.sh:
Compile it with shc:
Start it as background process and segfault it right away:
sleep 0.2 will give the binary enough time to start up and decrypt the original script. The variable $! contains the pid of the last background process started, so we can easily kill it with the segmentation fault signal SIGSEGV (same as kill -11 $!).
Now we can search the dump for the original script:
We pipe the data in the dumpfile to strings, which will then show us all the printable characters in the file and we can now see the original script between the garbage:
If the script is pretty big, maybe you have to adjust the core file size with ulimit.
Pretty easy, right?