- Java 87.2%
- Shell 12.8%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| sillies | ||
| .gitignore | ||
| BadParser.java | ||
| base-archive | ||
| base-archive.min | ||
| java-archive.sh | ||
| original.png | ||
| README.md | ||
| Restore.java | ||
jvm-class-archive
Proof-of-concept that JVM .class files can be self-extracting archives, where the
extraction part of things is done within the JVM.
The program, called Restore.java, reads itself while it is running, parses itself partially, and then prints to standard output the contents of the last Attribute.
Restore.java then gets compiled and disassembled by BadParser.java, which also
annotates the raw .class file in hexadecimal representation with some meaning. We cleverly modify it to obtain
base-archive, which is essentially a hexadecimal representation of Restore.class, the compiled Restore.java.
Finally, the shell script java-archive.sh can substitute any file of size at most 2^32 bytes into base-archive,
which then can be converted back to raw bytes on disk, after which we have successfully prepared
a .class file that any JVM that supports class file version 46 (Java 2) can run.
Of course, because Java has had support for GZIP since its earliest versions, this POC can very easily be extended to not just support self-extracting, but also self-decompressing archives.
Archiving an empty file leads to a file size of only 957 bytes, that is how much we need to represent the entire compiled code
Requirements
- Java 2 or higher
- Bash
xxd
Example
chmod +x java-archive.sh
./java-archive.sh original.png
java original > restored.png
diff -s original.png restored.png
# Files original.png and restored.png are identical
Improvements
As said before, we can make the Java side of things use GZIPInputStream, and then also gzip inputs with gzip in java-archive.sh,
in order to have even any benefit from this system at all; without this, we have essentially added a useless
header to the original files, which provides barely any steganography. With gzipping, we would have self-extracting compressed folders,
which can run on virtually any machine due to its amazing binary compatibility for Java (only Java 1 is unsupported),
and Java runs on pretty much any platform anyway. In an environment where it is difficult to install additional tools,
(say, lack of packages in the distribution, or no distribution at all), we can usually still get our hands on a distribution
of Java.
Additionally, we could add cryptography, which would make for self-decoding archives.
Of course, these improvements and applicability concerns listed above are entirely ridiculous.
Limitations
- Files can be of size at most
2^32bytes - File names can be of length at most
2^16 - 6characters - Java 1 incompatible
- Renaming archives breaks them, the
javacommand wants the name of the class and of the file to match