vrkforever commited on
Commit
10e0ae2
1 Parent(s): f471f76

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pickle
3
+ import os
4
+ from androguard.misc import AnalyzeAPK
5
+
6
+ class SimplifiedAPKAnalyzer:
7
+ def __init__(self, model_path):
8
+ self.model = self.load_model(model_path)
9
+ self.columns = [
10
+ 'android.permission.INTERNET',
11
+ 'android.permission.READ_EXTERNAL_STORAGE',
12
+ 'android.permission.WRITE_EXTERNAL_STORAGE',
13
+ 'android.permission.ACCESS_NETWORK_STATE',
14
+ 'android.permission.WAKE_LOCK',
15
+ 'android.permission.VIBRATE',
16
+ 'android.permission.ACCESS_WIFI_STATE',
17
+ 'android.permission.RECEIVE_BOOT_COMPLETED',
18
+ 'android.permission.GET_ACCOUNTS',
19
+ 'android.permission.CAMERA',
20
+ 'other_permission',
21
+ 'num_of_permissions'
22
+ ]
23
+
24
+ def load_model(self, model_path):
25
+ with open(model_path, 'rb') as f:
26
+ model_data = pickle.load(f)
27
+ return model_data['model']
28
+
29
+ def unpack_apk(self, apk_path):
30
+ a, _, _ = AnalyzeAPK(apk_path)
31
+ return {
32
+ "package_name": a.get_app_name(),
33
+ "package": a.get_package(),
34
+ "permissions": a.get_permissions(),
35
+ }
36
+
37
+ def apk_to_features(self, apk_data):
38
+ features = {col: 0 for col in self.columns}
39
+ for permission in apk_data["permissions"]:
40
+ if permission in features:
41
+ features[permission] = 1
42
+ else:
43
+ features["other_permission"] += 1
44
+ features["num_of_permissions"] = len(apk_data["permissions"])
45
+ return list(features.values())
46
+
47
+ def analyze_apk(self, file):
48
+ apk_data = self.unpack_apk(file.name)
49
+ features = self.apk_to_features(apk_data)
50
+ prediction = self.model.predict([features])[0]
51
+ result = "Malware" if prediction == 1 else "Not Malware"
52
+ return f"Analysis result for {apk_data['package_name']}: {result}"
53
+
54
+ def run_analysis(file):
55
+ analyzer = SimplifiedAPKAnalyzer("apk_malware.model")
56
+ return analyzer.analyze_apk(file)
57
+
58
+ iface = gr.Interface(
59
+ fn=run_analysis,
60
+ inputs=gr.File(label="Upload APK file"),
61
+ outputs="text",
62
+ title="APK Malware Analyzer",
63
+ description="Upload an Android APK file to analyze it for potential malware."
64
+ )
65
+
66
+ iface.launch()