Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

java - Embed a Executable Binary in a shell script

First, I already googled but only found examples where a compressed file (say a .tar.gz) is embedded into a shell script.

Basically if I have a C program (hello.c) that prints a string, say Hello World!.

I compile it to get an executable binary

gcc hello.c -o hello

Now I have a shell script testEmbed.sh

What I am asking is if it is possible to embed the binary (hello) inside the shell script so that when I run

./testEmbed.sh

it executes the binary to print Hello World!.

Clarification: One alternative is that I compress the executable into an archive and then extract it when the script runs. What I am asking is if it is possible to run the program without that.

Up until now, I was trying the method here. But it does not work for me. I guess the author was using some other distribution on another architecture. So, basically this did not work for me. :P

Also, if the workflow for a C program differs from a Java jar, I would like to know that too!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, this can be done. It's actually quite similar in concept to your linked article. The trick is to use uuencode to encode the binary into text format then tack it on to the end of your script.

Your script is then written in such a way that it runs uudecode on itself to create a binary file, change the permissions then execute it.

uuencode and uudecode were originally created for shifting binary content around on the precursor to the internet, which didn't handles binary information that well. The conversion into text means that it can be shipped as a shell script as well. If, for some reason your distribution complains when you try to run uuencode, it probably means you have to install it. For example, on Debian Squeeze:

sudo aptitude install sharutils

will get the relevant executables for you. Here's the process I went through. First create and compile your C program hello.c:

pax> cat hello.c

#include <stdio.h>
int main (void) {
    printf ("Hello
");
    return 0;
}

pax> gcc -o hello hello.c

Then create a shell script testEmbed.sh, which will decode itself:

pax> cat testEmbed.sh

#!/bin/bash
rm -f hello
uudecode $0
./hello
rm -f hello
exit

The first rm statement demonstrates that the hello executable is being created anew by this script, not left hanging around from your compilation. Since you need the payload in the file as well, attach the encoded executable to the end of it:

pax> uuencode hello hello >>testEmbed.sh

Afterwards, when you execute the script testEmbed.sh, it extracts the executable and runs it.

The reason this works is because uudecode looks for certain marker lines in its input (begin and end) which are put there by uuencode, so it only tries to decode the encoded program, not the entire script:

pax> cat testEmbed.sh

#!/bin/bash
rm -f hello
uudecode $0
./hello
rm -f hello
exit

begin 755 hello
M?T5,1@$!`0````````````(``P`!````$(,$"#0```#`!@```````#0`(``'
M`"@`'@`;``8````T````-(`$"#2`!`C@````X`````4````$`````P```!0!
: : :
M:&%N9&QE`%]?1%1/4E]%3D1?7P!?7VQI8F-?8W-U7VEN:70`7U]B<W-?<W1A
M<G0`7V5N9`!P=71S0$!'3$E"0UR+C``7V5D871A`%]?:38X-BYG971?<&-?
4=&AU;FLN8G@`;6%I;@!?:6YI=```
`
end

There are other things you should probably worry about, such as the possibility that your program may require shared libraries that don't exist on the target system, but the process above is basically what you need.


The process for a JAR file is very similar, except that the way you run it is different. It's still a single file but you need to replace the line:

./hello

with something capable of running JAR files, such as:

java -jar hello.jar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...