I found this tool and when I downloaded it is a bash executable that contains binary code.
First of all I didn't know this is possible. Does anyone know who this can be done? Also I am not sure how safe generally this is since I can't really be sure what the bash file contains
1 Answer
This is a self-contained Java program which relies on a few “tricks”:
- Java programs can be packaged as JAR files, which are ZIP files containing a manifest (
META-INF/MANIFEST.MF
) which specifies the Java class to start; java -jar
, given a JAR file, will look at the manifest and start the corresponding class, loading any other required resources from the JAR file and the Java runtime (one can also specify another class to run, but that’s not relevant here);- ZIP files can start with arbitrary data; this is how self-extracting executables work (executable ZIP files which will extract themselves when run).
So svg2png
is a JAR file, but with a prepended shell script:
#!/bin/sh exec java -Xmx1G -jar "$0" "$@"
When run, this will run a shell, which will then replace itself with java -Xmx1G -jar
and the name of the “script” (svg2png
with whatever path was used), and any arguments. -Xmx1G
sets the heap size, and -jar
tells the JVM to “run” the given JAR file, svg2png
in this case. The rest of svg2png
is the JAR file; in particular, it contains the following manifest:
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Built-By: sterlp X-Compile-Target-JDK: 1.8 X-Compile-Source-JDK: 1.8 Created-By: Apache Maven 3.3.3 Build-Jdk: 1.8.0_51 Main-Class: org.sterl.svg2png.Main
This instructs the JVM to run the org.sterl.svg2png.Main
class, which starts the program.
You’ll find more details about the ZIP format on Wikipedia, in particular the structure description which shows how the format allows arbitrary data at the start of the file (and in other positions). ZIP files are identified by their “central directory”, which resides at the end of the file.
svg2png
. Download it and view it with an editor. The first line isexec java -Xmx1G -jar "$0" "$@"
and the rest is binary dataunzip -l svg2png
to see its content ;-)exec
but using the other file (which would be a.jar
file). No difference.