-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcg.java
325 lines (300 loc) · 15.1 KB
/
cg.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package soot_call_graph.test;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.xmlpull.v1.XmlPullParserException;
import soot_call_graph.test.CGExporter;
import soot.MethodOrMethodContext;
import soot.PackManager;
import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.jimple.infoflow.InfoflowConfiguration.CallgraphAlgorithm;
import soot.jimple.infoflow.android.InfoflowAndroidConfiguration;
import soot.jimple.infoflow.android.SetupApplication;
import soot.jimple.toolkits.callgraph.CallGraph;
import soot.jimple.toolkits.callgraph.Edge;
import soot.jimple.toolkits.callgraph.Targets;
import soot.options.Options;
public class CGGenerator {
public final static String androidPlatformPath = "E:/flowdroid/android-platforms-master/android-platforms-master/";
public final static String appPath = "E:/flowdroid/SmartHome-DB-Sdk-Dev-78006-9.0.605-4059-develop-20231130.apk";
public final static String outputPath = "E:/flowdroid/sootOutput/";
public final static String sourceSinkFile = "E:/flowdroid/CallGraph_source.txt";
static Object ob = new Object();
private static Map<String,Boolean> visited = new HashMap<String,Boolean>();
private static CGExporter cge = new CGExporter();
public static void main(String[] args) throws IOException, XmlPullParserException{
// Provide the path to the APK file
String apkPath = "E:/flowdroid/SmartHome-DB-Sdk-Dev-77400-8.7.501-2066-develop-20230706.apk";
soot.G.reset();
Options.v().set_output_format(Options.output_format_jimple);
Options.v().set_allow_phantom_refs(true);
Options.v().set_process_dir(Collections.singletonList(apkPath));
Options.v().set_src_prec(Options.src_prec_apk);
Options.v().set_android_jars("E:/flowdroid/android-platforms-master/android-platforms-master/");
Options.v().set_whole_program(true);
Options.v().set_process_multiple_dex(true);
System.out.println("before Call Graph:");
// Load the APK file
Scene.v().loadNecessaryClasses();
// Build the call graph
PackManager.v().getPack("cg").apply();
CallGraph callGraph = Scene.v().getCallGraph();
// Print the call graph
System.out.println("Call Graph:");
// Print the call graph (you can customize the output format)
System.out.println(callGraph);
for (SootMethod method : Scene.v().getEntryPoints()) {
if (method.isConcrete()) {
Iterator<Edge> edges = cg.edgesOutOf(method);
while (edges.hasNext()) {
Edge edge = edges.next();
System.out.println(method + " -> " + edge.getTgt());
}
}
}
// final InfoflowAndroidConfiguration config = new InfoflowAndroidConfiguration();
// config.getAnalysisFileConfig().setTargetAPKFile(appPath);
// config.getAnalysisFileConfig().setAndroidPlatformDir(androidPlatformPath);
// config.setMergeDexFiles(true);
// // config.setCodeEliminationMode(InfoflowConfiguration.CodeEliminationMode.NoCodeElimination);
// config.setCallgraphAlgorithm(CallgraphAlgorithm.CHA); // CHA or SPARK
// SetupApplication app = new SetupApplication(config);
// System.out.print("before runInfoflow \n");
// Options.v().set_whole_program(true);
// Options.v().set_allow_phantom_refs(true);
// //app.runInfoflow(sourceSinkFile);
// System.out.print("before call graph\n");
// // Iterate over the methods and print their names
// app.constructCallgraph();
// CallGraph callGraph = Scene.v().getCallGraph();
// //SootMethod
// SootMethod entryPoint = app.getDummyMainMethod();
CallGraph cg = Scene.v().getCallGraph();
//visit(cg,entryPoint);
//cge.exportMIG("flowdroidCFG", outputPath);
//String className_pre = "com.tuya.smart.rnplugin.tyrctcameramanager.TYRCTCameraManager";
String className_pre = "com.xiaomi.router.miio.miioplugin$Stub";
SootClass sootClass = Scene.v().loadClassAndSupport(className_pre);
sootClass.setApplicationClass();
for (SootMethod method : sootClass.getMethods()) {
System.out.println(method.getSignature());
}
SootClass sootClass_new = Scene.v().forceResolve(className_pre, SootClass.BODIES);
if (sootClass_new.isPhantom()) {
System.out.println(className_pre + " does not exist in the APK.");
} else {
System.out.println(className_pre + " exists in the APK.");
}
SootClass sootClass1 = Scene.v().loadClassAndSupport(className_pre);
sootClass1.setApplicationClass();
// Get all methods in the class
List<SootMethod> methods = sootClass1.getMethods();
// Iterate over the methods and print their names
for (SootMethod method : methods) {
System.out.println("Method: " + method.getName());
}
className_pre = "com.xiaomi.router.miio.miioplugin";
sootClass_new = Scene.v().forceResolve(className_pre, SootClass.BODIES);
if (sootClass_new.isPhantom()) {
System.out.println(className_pre + " does not exist in the APK.");
} else {
System.out.println(className_pre + " exists in the APK.");
}
SootClass sootClass11 = Scene.v().loadClassAndSupport(className_pre);
sootClass11.setApplicationClass();
// Get all methods in the class
List<SootMethod> methods1 = sootClass11.getMethods();
// Iterate over the methods and print their names
for (SootMethod method : methods1) {
System.out.println("Method: " + method.getName());
}
//tuya
//String targetMethodName = "<com.tuya.smart.rnplugin.tyrctcameramanager.TYRCTCameraManager: void startPtzLeft()>";
// //Xiaomi
String targetMethodName = "<com.xiaomi.router.miio.miioplugin$Stub: boolean onTransact(int,android.os.Parcel,android.os.Parcel,int)>";
//String targetMethodName = "<com.bsgamesdk.android.BSGameSdk: void login(com.bsgamesdk.android.callbacklistener.CallbackListener)>";
// String targetMethodName = "<com.bsgamesdk.android.BSGameSdk: com.bsgamesdk.android.BSGameSdk initialize(boolean,android.app.Activity,java.lang.String,java.lang.String,java.lang.String,java.lang.String,android.os.Handler)>";
SootMethod targetMethod = Scene.v().grabMethod(targetMethodName);
if (targetMethod != null) {
Iterator<MethodOrMethodContext> ctargets = new Targets(callGraph.edgesInto(targetMethod));
if (ctargets != null) {
while (ctargets.hasNext()) {
SootMethod child = (SootMethod) ctargets.next();
System.out.println(targetMethodName +" may call" + child);
if (!visited.containsKey(child.getSignature())) visit(callGraph, child);
}
}
}
if (targetMethod != null) {
Iterator<MethodOrMethodContext> ctargets = new Targets(callGraph.edgesOutOf(targetMethod));
if (ctargets != null) {
while (ctargets.hasNext()) {
SootMethod child = (SootMethod) ctargets.next();
System.out.println(targetMethodName +" may call" + child);
if (!visited.containsKey(child.getSignature())) visit(callGraph, child);
}
}
// for (Iterator<Edge> it = callGraph.edgesOutOf(targetMethod); it.hasNext();) {
// Edge edge = it.next();
// SootMethod srcMethod = edge.getSrc().method();
// System.out.println(srcMethod.getSignature());
// }
}
String className = "com.tuya.smart.rnplugin.tyrctcameramanager.TYRCTCameraManager";
SootClass sootClass111 = Scene.v().loadClassAndSupport(className_pre);
sootClass111.setApplicationClass();
// Get all methods in the class
List<SootMethod> methods11 = sootClass111.getMethods();
// Iterate over the methods and print their names
for (SootMethod method : methods11) {
System.out.println("Method: " + method.getName());
}
//// System.out.println("new");
// className = "com.tuya.smart.camera.devicecontrol.MqttIPCCameraDeviceManager";
// sootClass = Scene.v().loadClassAndSupport(className);
//// sootClass.setApplicationClass();
//// // Get all methods in the class
//// methods = sootClass.getMethods();
//// for (SootMethod method : methods) {
//// System.out.println("Method: " + method.getName());
//// }
// String methodName = "startPtz";
// String methodSubSignature = "void startPtz(com.tuya.smart.camera.devicecontrol.mode.PTZDirection)";
//// String methodName = "startPtzLeft";
//// String methodSubSignature = "void startPtzLeft()";
// targetMethod = sootClass.getMethod(methodSubSignature);
//
// if (targetMethod != null) {
// System.out.println("调用 " + targetMethodName + " 的方法:");
// for (Iterator<Edge> it = callGraph.edgesInto(targetMethod); it.hasNext();) {
// Edge edge = it.next();
// SootMethod srcMethod = edge.getSrc().method();
// System.out.println(srcMethod.getSignature());
// }
// }
// if (sootClass.declaresMethodByName(methodName)) {
// SootMethod method = sootClass.getMethod(methodSubSignature);
// System.out.println("Found method: " + method.getSignature());
// Iterator<Edge> edgeIterator = callGraph.edgesInto(method);
// while (edgeIterator.hasNext()) {
// Edge edge = edgeIterator.next();
// SootMethod tgt = edge.getTgt().method();
// System.out.println("before tgt" + tgt.getSignature());
// System.out.println(" " + tgt.getSignature());
// }
// }else {
// System.out.println("Method not found in class.");
// }
// if (sootClass.declaresMethodByName(methodName)) {
// SootMethod method = sootClass.getMethod(methodSubSignature);
// System.out.println("Found method: " + method.getSignature());
// visit(callGraph,method);
// cge.exportMIG("flowdroidCFG", outputPath);
// } else {
// System.out.println("Method not found in class.");
// }
// SetupApplication app = new SetupApplication(androidPlatformPath, appPath);
// soot.G.reset();
// //传入AndroidCallbacks文件
// app.setCallbackFile(CGGenerator.class.getResource("/AndroidCallbacks.txt").getFile());
// app.constructCallgraph();
//
// //SootMethod
// SootMethod entryPoint = app.getDummyMainMethod();
// CallGraph cg = Scene.v().getCallGraph();
// visit(cg,entryPoint);
// cge.exportMIG("flowdroidCFG", outputPath);
// System.out.println("Method not found in class.");
}
// public static void main(String[] args) throws IOException, XmlPullParserException{
// // Configure Soot for APK analysis
// Options.v().set_src_prec(Options.src_prec_apk);
//
// Options.v().set_android_jars(androidPlatformPath);
// Options.v().set_process_dir(Collections.singletonList(appPath)); // Replace with APK path
// Options.v().set_whole_program(true);
// Options.v().set_allow_phantom_refs(true);
// Options.v().set_process_multiple_dex(true);
// Options.v().set_output_format(Options.output_format_none);
//
// // Enable call graph generation
// Options.v().setPhaseOption("cg.spark", "on");
//
// // Load classes and methods from APK
// Scene.v().loadNecessaryClasses();
// System.out.println("test");
// // Execute Soot
// PackManager.v().runPacks();
//
// // Retrieve and process the call graph
// CallGraph callGraph = Scene.v().getCallGraph();
// String targetFunctionSignature = "com.tuya.smart.camera.devicecontrol.MqttIPCCameraDeviceManager。startPtz(com.tuya.smart.camera.devicecontrol.mode.PTZDirection)";
// for (Edge edge : callGraph) {
// SootMethod srcMethod = edge.src();
// SootMethod tgtMethod = edge.tgt();
//
// if (tgtMethod.getSignature().equals(targetFunctionSignature)) {
// // Found a caller of the target function
// System.out.println("Caller Method: " + srcMethod.getSignature());
// // You can analyze or store information about the caller method here
// }
// }
// System.out.println("test");
//
//
//// SetupApplication app = new SetupApplication(androidPlatformPath, appPath);
//// soot.G.reset();
//// //传入AndroidCallbacks文件
//// app.setCallbackFile(CGGenerator.class.getResource("/AndroidCallbacks.txt").getFile());
//// app.constructCallgraph();
////
//// //SootMethod
SootMethod entryPoint = app.getDummyMainMethod();
CallGraph cg = Scene.v().getCallGraph();
visit(cg,entryPoint);
cge.exportMIG("flowdroidCFG", outputPath);
// }
private static boolean isJavaLibraryMethod(SootMethod method) {
String packageName = method.getDeclaringClass().getPackageName();
System.out.println(packageName.startsWith("java.") || packageName.startsWith("javax.") || packageName.startsWith("sun."));
return packageName.startsWith("java.") || packageName.startsWith("javax.") || packageName.startsWith("sun.");
}
private static void visit(CallGraph cg,SootMethod m){
String identifier = m.getSignature();
visited.put(identifier, true);
cge.createNode(identifier);
Iterator<MethodOrMethodContext> ptargets = new Targets(cg.edgesInto(m));
if(ptargets != null){
while(ptargets.hasNext())
{
SootMethod p = (SootMethod) ptargets.next();
if(p == null){
System.out.println("p is null");
}
if(!visited.containsKey(p.getSignature())){
visit(cg,p);
}
}
}
Iterator<MethodOrMethodContext> ctargets = new Targets(cg.edgesOutOf(m));
if(ctargets != null){
while(ctargets.hasNext())
{
SootMethod c = (SootMethod) ctargets.next();
if(c == null){
System.out.println("c is null");
}
cge.createNode(c.getSignature());
cge.linkNodeByID(identifier, c.getSignature());
if(!visited.containsKey(c.getSignature())){
visit(cg,c);
}
}
}
}
}