Shell script with jar file at the end

I am downloading an archive . The archive will have a file with .sh . expansion. When I opened this file with VI, I found below code at the beginning of the file:

 #!/bin/sh MYSELF=`which "$0" 2>/dev/null` [ $? -gt 0 -a -f "$0" ] && MYSELF="./$0" java=java if test -n "$JAVA_HOME"; then java="$JAVA_HOME/bin/java" fi exec "$java" $java_args -jar $MYSELF " $@ " exit 1 

I can start the jar by running java -jar file or `./file '.

Can someone explain to me what is going on? How can you create such a file?

+6
source share
2 answers

Try the following commands yourself. Start creating a regular jar file with any content, or use someone who has one. I will call him "myjar.jar"

Then create the file "hello.sh" with the content:

 #!/bin/bash exec echo hello 

add this file when starting a new jar file:

 cat hello.sh myjar.jar > mytrick.jar chmod 700 mytrick.jar 

And finally, the interesting part, type:

 ./mytrick.jar jar -tf mytrick.jar unzip mytrick.jar 

In other words, usually jar / unzip skips any content to its own header. In addition, the shell script "ends" on the line that calls "exec" (because the interpreter interprets the command at the point in the line exec).

However, this trick is based on jar / unzip behavior, possibly outside the standards. Note, for example, that this operator does not work (has no effects):

 jar -xf mytrick.jar 
+3
source

If the file starts start-superbeam.sh after extracting the tar file, try chmod +x start-superbeam.sh && ./start-superbeam.sh or /bin/sh ./start-superbeam.sh .

If the program has arguments, put them at the end. It will run java on that superbeam.sh, which is like a jar file at the end.

If you need special java parameters, such as memory size, you must set them in the java_args environment java_args .

As for this, this is a shell script with a jar file at the end of it after exit . Quote from ReallyExecutable Jars :

A hack is known in some circles, but not widely, to make banks really executable, in the sense of chmod + x. Hacking uses the fact that jar files are zip files and zip files allow arbitrary toughness to be added to the zip file itself (this is how self-extracting zip files work).

To create, see the accepted answer or link.

+3
source

Source: https://habr.com/ru/post/989484/


All Articles