-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyInterface.java
208 lines (180 loc) · 7.74 KB
/
MyInterface.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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
public class MyInterface implements ActionListener {
private static List<String> uniquePermissions = new ArrayList<>();
JButton openBtn, executeBtn;
JLabel label1, label2, label3, label4, label5, label6;
JComboBox<String> cb;
String apkToBeUsedPath;
String categorySelected;
public MyInterface() {
JFrame f = new JFrame("Major Project Interface");
JPanel p = new JPanel();
f.add(p);
p.setLayout(null);
// label1 = new JLabel("Choose the APK of the App to be classified");
openBtn = new JButton("open");
openBtn.setBounds(250, 70, 70, 25);
openBtn.addActionListener(this);
label1 = new JLabel("No file selected...");
label1.setBounds(225, 95, 500, 25);
label2 = new JLabel("Select the Category");
label2.setBounds(230, 115, 200, 25);
String categories[]={"ArtAndDesign","AugmentedReality","AutoAndVehicles","Beauty","Books", "Comics", "Communication", "HouseAndHome", "LibrariesAndDemo", "Lifestyle", "MapsAndNavigation", "Personalization", "VideoPlayersAndEditors", "Weather"};
cb = new JComboBox<>(categories);
cb.setBounds(205, 140, 160, 25);
cb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
label3.setBounds(210, 165, 200, 25);
label3.setText("Category - " + cb.getSelectedItem());
categorySelected = (String) cb.getSelectedItem();
}
});
label3 = new JLabel("Category - ");
label3.setBounds(230, 165, 200, 25);
executeBtn = new JButton("Execute");
executeBtn.setBounds(240, 210, 100, 25);
executeBtn.addActionListener(this);
label4 = new JLabel("Execution Results");
label4.setBounds(230, 230, 200, 25);
label5 = new JLabel("");
label5.setBounds(220, 260, 200, 25);
label6 = new JLabel("");
label6.setBounds(225, 290, 200, 25);
p.add(openBtn); p.add(label1); p.add(label2); p.add(label3); p.add(label4); p.add(label5); p.add(label6); p.add(cb); p.add(executeBtn);
f.setSize(600, 500);
f.setLocation(400, 100);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static String findAppName(File appFolder){
String appFolderName = appFolder.toString();
String stringsFileName = appFolderName+"/res/values/strings.xml";
try {
BufferedReader in = new BufferedReader(new FileReader(stringsFileName));
String line;
line = in.readLine(); // for <?xml version="1.0" encoding="utf-8"?>
while ((line = in.readLine()) != null) {
// System.out.println(line.trim());
int cotesStart = line.indexOf('"');
int cotesEnd = line.indexOf('"', cotesStart+1);
if(cotesStart != -1 && cotesEnd != -1 && line.substring(cotesStart+1, cotesEnd).equals("app_name")){
int strStart = line.indexOf('>');
int strEnd = line.indexOf('<', strStart+1);
String appName = line.substring(strStart+1, strEnd);
return appName;
}
}
in.close();
} catch (IOException e) {
System.out.println("error in reading file" + stringsFileName);
}
return "";
}
public static ManifestObject findManifestFile(File apkFolderName, String appName, String category){
String manifestFileLocation = apkFolderName.toString() + "/AndroidManifest.xml";
ReadXML readxml = new ReadXML();
return readxml.getManifestDataObject(manifestFileLocation, appName, category);
}
private static void makeTotalPermissionsList() throws Exception {
uniquePermissions = Permissions.makeList();
}
public static void main(String[] args) throws Exception {
new MyInterface();
makeTotalPermissionsList();
}
public void actionPerformed (ActionEvent ae)
{
if(ae.getSource() == openBtn) {
JFileChooser j = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int r = j.showOpenDialog(null);
if (r == JFileChooser.APPROVE_OPTION) {
label1.setBounds(105, 95, 500, 25);
label1.setText("File Choosen ::: " + j.getSelectedFile().getAbsolutePath());
apkToBeUsedPath = j.getSelectedFile().getAbsolutePath();
} else
label1.setText("the user cancelled the operation");
} else if (ae.getSource() == executeBtn) {
// apkToBeUsedPath, categorySelected
File SourceFilePath = new File(apkToBeUsedPath);
File DestinationFilePath = new File("C:/Users/Admin/eclipse-workspace/MajorFinalInterface/src/TestApp/TestAppApk.apk");
try {
Files.copy(SourceFilePath.toPath(), DestinationFilePath.toPath());
} catch (IOException e) {
e.printStackTrace();
}
String batFilePath = System.getProperty("user.dir") + "/TestApp/reapk.bat";
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "reapk.bat");
File dir = new File("C:/Users/Admin/eclipse-workspace/MajorFinalInterface/src/TestApp");
pb.directory(dir);
try {
Process p = pb.start();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(30*1000); // 60 seconds 1 minute
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("apk folder made");
File TestAppFolderName = new File("C:/Users/Admin/eclipse-workspace/MajorFinalInterface/src/TestApp/TestAppApk");
String appName = MyInterface.findAppName(TestAppFolderName);
System.out.println(" App Name ::: " + appName);
ManifestObject obj = findManifestFile(TestAppFolderName, appName, categorySelected);
System.out.println(obj.getAppName());
System.out.println(obj.getCategory());
ArrayList<String> permList = obj.getPermissionsList();
for(String s : permList) {
System.out.println(s);
}
ArrayList<Float> permSparse = new ArrayList<>();
String permString = null;
for(String s : uniquePermissions) {
if(permList.contains(s)) {
permSparse.add((float) 1);
} else {
permSparse.add((float) 0);
}
}
System.out.println("permSparse array ::: " + permSparse);
String listString = "";
for (Float s : permSparse) {
listString += s + " ";
}
String s = null;
try {
Process p = Runtime.getRuntime().exec("python classifiers_output.py " + listString + " " + categorySelected);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("Here is the standard output of the command:\n");
int i=0;
while ((s = stdInput.readLine()) != null) {
i++;
System.out.println(s);
label5.setText("Predicted ::: " + s);
if(s.equals("Benign"))
label6.setText("The App is fit to use.");
else if(s.equals("Malware"))
label6.setText("The App is not fit to use.");
}
System.out.println(i);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}