I download an archive file. In the archive there will be a file that has a .sh
. extension. When I opened that file with VI I found the below code in 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 run the jar by doing java -jar file
or `./file'.
Can someone explain me what is going on? How can you create such file?
In computing, end-of-file (EOF) is a condition in a computer operating system where no more data can be read from a data source. The data source is usually called a file or stream.
After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, 0 on success or an integer in the range 1 - 255 on error.
Try by yourself the following commands. Start creating a normal jar file with any content, or use someone you have. I will name it "myjar.jar"
Next, create a file "hello.sh" with content:
#!/bin/bash
exec echo hello
now, add this file at start of 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 until their own header. Moreover, a shell script "ends" in a line who call "exec" (because shell interpreter is replace at this point by the command in the exec line).
However, this trick is based in a behaviour of jar/unzip probably out of standards. Note, by example, that this statement fails (has no effects):
jar -xf mytrick.jar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With