-
Notifications
You must be signed in to change notification settings - Fork 115
/
Driver.java
156 lines (134 loc) · 5.58 KB
/
Driver.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package org.cf.oracle;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.lang.Class;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cf.oracle.options.InvocationTarget;
import org.cf.oracle.options.TargetParser;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonElement;
public class Driver {
private static final String DRIVER_DIR = "/data/local";
private static final String OUTPUT_HEADER = "===ORACLE DRIVER OUTPUT===\n";
private static final String EXCEPTION_LOG = "od-exception.txt";
private static final String OUTPUT_FILE = "od-output.json";
private static Gson GSON = buildGson();
private static Gson buildGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
JsonSerializer<Class> serializer = new JsonSerializer<Class>() {
@Override
public JsonElement serialize(Class src, Type typeOfSrc, JsonSerializationContext context) {
JsonPrimitive value = new JsonPrimitive(ClassNameUtils.toInternal(src));
return value;
}
};
gsonBuilder.registerTypeAdapter(Class.class, serializer);
gsonBuilder.disableHtmlEscaping();
return gsonBuilder.create();
}
private static void die(String msg, Exception exception) {
PrintWriter writer;
try {
writer = new PrintWriter(EXCEPTION_LOG, "UTF-8");
} catch (Exception e) {
return;
}
writer.println(msg);
writer.println(exception);
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
writer.println(sw.toString());
writer.close();
// app_process, dalvikvm, and dvz don't propagate exit codes, so this doesn't matter
System.exit(-1);
}
private static String invokeMethod(Method method, Object[] arguments) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
method.setAccessible(true);
Object returnObject = method.invoke(null, arguments);
Class<?> returnClass = method.getReturnType();
if (returnClass.getName().equals("Ljava.lang.Void;")) {
// I hear an ancient voice, whispering from the Void, and it chills my lightless heart...
return null;
}
String output = "";
try {
output = GSON.toJson(returnClass.cast(returnObject));
} catch (Exception ex) {
output = GSON.toJson(returnObject);
}
return output;
}
private static void showUsage() {
System.out.println("Usage: export CLASSPATH=/data/local/od.zip; app_process /system/bin org.cf.driver.Driver <class> <method> [<parameter type>:<parameter value json>]");
System.out.println(" export CLASSPATH=/data/local/od.zip; app_process /system/bin org.cf.driver.Driver @<json file>");
}
public static void main(String[] args) {
boolean multipleTargets = args.length < 2 && args[0].startsWith("@");
if (args.length < 1 && !multipleTargets) {
showUsage();
System.exit(-1);
}
try {
StackSpoofer.init();
} catch (NumberFormatException | IOException e) {
die("Error parsing stack spoof info", e);
}
List<InvocationTarget> targets = null;
try {
targets = TargetParser.parse(args, GSON);
} catch (IOException e) {
die("Exception parsing targets", e);
}
String output = null;
if (!multipleTargets) {
InvocationTarget target = targets.get(0);
try {
output = invokeMethod(target.getMethod(), target.getArguments());
} catch (Exception e) {
die("Error executing " + target, e);
}
if (output != null) {
System.out.println(OUTPUT_HEADER + output);
}
} else {
Map<String, String[]> idToOutput = new HashMap<String, String[]>();
for (InvocationTarget target : targets) {
String status;
if (target.getParseException() != null) {
output = "Error parsing " + target;
status = "failure";
} else {
try {
output = invokeMethod(target.getMethod(), target.getArguments());
status = "success";
//} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NullPointerException e) {
} catch (Exception e) {
output = "Error executing " + target;
status = "failure";
}
}
idToOutput.put(target.getId(), new String[] { status, output });
}
String json = GSON.toJson(idToOutput);
try {
FileUtils.writeFile(OUTPUT_FILE, json);
} catch (FileNotFoundException | UnsupportedEncodingException e) {
die("Unable to write output to " + OUTPUT_FILE, e);
}
System.out.println(OUTPUT_HEADER + "success");
}
}
}