-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSootFX.java
More file actions
244 lines (209 loc) · 8.97 KB
/
Copy pathSootFX.java
File metadata and controls
244 lines (209 loc) · 8.97 KB
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
package api;
import core.fx.FxUtil;
import core.rm.*;
import manager.ClassFX;
import manager.ManifestFX;
import manager.MethodFX;
import manager.WholeProgramFX;
import resource.SootConnector;
import core.fx.base.*;
import java.io.*;
import java.util.*;
public class SootFX {
private String mainClass;
private List<String> classPaths;
private boolean appOnly;
private String androidJars;
public static boolean isAPK = false;
public SootFX addClassPath(String classPath) {
if (this.classPaths == null) {
this.classPaths = new ArrayList<>();
}
this.classPaths.add(classPath);
return this;
}
public SootFX mainClass(String fullyQualifiedClassName) {
this.mainClass = fullyQualifiedClassName;
return this;
}
public SootFX androidJars(String androidJars) {
this.androidJars = androidJars;
isAPK = true;
return this;
}
public SootFX appOnly() {
appOnly = true;
return this;
}
private void validate() {
if (classPaths == null || classPaths.isEmpty()) {
throw new RuntimeException("Class Path not set. call addClassPath(/class/path/to/target/program)");
}
}
public Set<MethodFeatureSet> extractMethodFeaturesInclude(Set<MethodFEU> featureExtractors) {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
//SootClass sc = Scene.v().forceResolve(mainClass, SootClass.BODIES);
//Chain<SootClass> classes = Scene.v().getApplicationClasses();
return new MethodFX().getFeatures(featureExtractors);
}
public Set<MethodFeatureSet> extractAllMethodFeatures() {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new MethodFX().getAllFeatures();
}
public Set<MethodFeatureSet> extractMethodFeaturesExclude(Set<String> exclusion) {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new MethodFX().getAllFeaturesExclude(exclusion);
}
public Set<MethodFeatureSet> extractMethodFeaturesInclude(List<String> featureExtractors) {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new MethodFX().getFeatures(featureExtractors);
}
public List<FeatureDescription> listAllMethodFeatures(){
return FxUtil.listAllMethodFeatures();
}
public Set<ClassFeatureSet> extractClassFeaturesInclude(Set<ClassFEU> featureExtractors) {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new ClassFX().getFeatures(featureExtractors);
}
public Set<ClassFeatureSet> extractAllClassFeatures() {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new ClassFX().getAllFeatures();
}
public Set<ClassFeatureSet> extractClassFeaturesExclude(Set<String> exclusion) {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new ClassFX().getAllFeaturesExclude(exclusion);
}
public Set<ClassFeatureSet> extractClassFeaturesInclude(List<String> featureExtractors) {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new ClassFX().getFeatures(featureExtractors);
}
public WholeProgramFeatureSet extractWholeProgramFeaturesInclude(Set<WholeProgramFEU> featureExtractors) {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new WholeProgramFX().getFeatures(featureExtractors);
}
public WholeProgramFeatureSet extractAllWholeProgramFeatures() {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new WholeProgramFX().getAllFeatures();
}
public WholeProgramFeatureSet extractWholeProgramFeaturesExclude(Set<String> exclusion) {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new WholeProgramFX().getAllFeaturesExclude(exclusion);
}
public WholeProgramFeatureSet extractWholeProgramFeaturesInclude(List<String> featureExtractors, List<FeatureResource> featureResources) {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new WholeProgramFX().getFeatures(featureExtractors, featureResources);
}
public ManifestFeatureSet extractManifestFeaturesInclude(Set<ManifestFEU> featureExtractors) {
return new ManifestFX(getApkPath()).getFeatures(featureExtractors);
}
public ManifestFeatureSet extractAllManifestFeatures() {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new ManifestFX(getApkPath()).getAllFeatures();
}
public ManifestFeatureSet extractManifestFeaturesExclude(Set<String> exclusion) {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new ManifestFX(getApkPath()).getAllFeaturesExclude(exclusion);
}
public ManifestFeatureSet extractManifestFeaturesInclude(List<String> featureExtractors) {
validate();
SootConnector.setupSoot(mainClass, classPaths, appOnly, androidJars);
return new ManifestFX(getApkPath()).getFeatures(featureExtractors);
}
public void printMethodToCSV(Set<MethodFeatureSet> set, String path) {
boolean isFirst = true;
try (OutputStream out = new FileOutputStream(path);
Writer writer = new OutputStreamWriter(out, "UTF-8")) {
for (MethodFeatureSet featureSet : set) {
String instanceName = featureSet.getMethod().getName();
List<String> values = new ArrayList<>();
if (isFirst) {
writer.append("name,");
for (Feature feature : featureSet.getFeatures()) {
if (isFirst) {
writer.append(feature.getName() + ",");
isFirst = false;
}
}
writer.append(System.lineSeparator());
}
writer.append(instanceName + ",");
for (Feature feature : featureSet.getFeatures()) {
if (isFirst) {
writer.append(feature.getName() + ",");
isFirst = false;
}
}
writer.append(System.lineSeparator());
}
} catch (IOException e) {
System.err.println(e);
}
}
public void printMultiSetToCSV(Set<? extends AbstractFeatureSet> set, String path) throws IOException {
boolean isFirst = true;
File file = new File(path);
if(file.getParentFile()!=null){
file.getParentFile().mkdirs();
}
file.createNewFile();
try (OutputStream out = new FileOutputStream(file);
Writer writer = new OutputStreamWriter(out, "UTF-8")) {
for (AbstractFeatureSet featureSet : set) {
String instanceName = featureSet.getSignature();
if (isFirst) {
writer.append("name;");
for (Feature feature : featureSet.getFeatures()) {
writer.append(feature.getName() + ";");
}
writer.append(System.lineSeparator());
isFirst = false;
}
writer.append(instanceName + ";");
for (Feature feature : featureSet.getFeatures()) {
writer.append(feature.getValue().toString() + ";");
}
writer.append(System.lineSeparator());
}
}
}
public void printSingleSetToCSV(AbstractFeatureSet set, String path) throws IOException {
boolean isFirst = true;
File file = new File(path);
file.getParentFile().mkdirs();
file.createNewFile();
try (OutputStream out = new FileOutputStream(file);
Writer writer = new OutputStreamWriter(out, "UTF-8")) {
String instanceName = set.getSignature();
writer.append("name;");
for (Feature feature : set.getFeatures()) {
writer.append(feature.getName() + ";");
}
writer.append(System.lineSeparator());
writer.append(instanceName + ";");
for (Feature feature : set.getFeatures()) {
writer.append(feature.getValue().toString() + ";");
}
}
}
private String getApkPath() {
Optional<String> apkPath = classPaths.stream().filter(c -> c.endsWith(".apk")).findFirst();
if (!apkPath.isPresent()) {
throw new RuntimeException("classPaths do not contain any apk files");
}
return apkPath.get();
}
}