vrkforever commited on
Commit
124a21a
1 Parent(s): 75e78a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -60
app.py CHANGED
@@ -1,66 +1,61 @@
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()
 
1
  import gradio as gr
2
+ import tempfile
3
  import os
4
+ from androguard.core.bytecodes.apk import APK
5
+
6
+ def extract_apk_info(apk_file):
7
+ if apk_file is None:
8
+ return "No file uploaded. Please upload an APK file."
9
+
10
+ # Create a temporary file to store the uploaded APK
11
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".apk") as temp_apk:
12
+ temp_apk.write(apk_file.read())
13
+ temp_apk_path = temp_apk.name
14
+
15
+ try:
16
+ apk = APK(temp_apk_path)
17
+
18
+ output = []
19
+ output.append(f"Package: {apk.get_package()}")
20
+ output.append(f"Version: {apk.get_androidversion_name()}")
21
+ output.append(f"Main Activity: {apk.get_main_activity()}")
22
+
23
+ output.append("\nPermissions:")
24
+ for permission in apk.get_permissions():
25
+ output.append(f"- {permission}")
26
+
27
+ output.append("\nActivities:")
28
+ for activity in apk.get_activities():
29
+ output.append(f"- {activity}")
30
+
31
+ output.append("\nServices:")
32
+ for service in apk.get_services():
33
+ output.append(f"- {service}")
34
+
35
+ output.append("\nReceivers:")
36
+ for receiver in apk.get_receivers():
37
+ output.append(f"- {receiver}")
38
+
39
+ output.append("\nProviders:")
40
+ for provider in apk.get_providers():
41
+ output.append(f"- {provider}")
42
+
43
+ return "\n".join(output)
44
+
45
+ except Exception as e:
46
+ return f"Error analyzing APK: {str(e)}"
47
+ finally:
48
+ # Clean up the temporary file
49
+ os.unlink(temp_apk_path)
50
+
51
+ # Create Gradio interface
 
 
 
 
 
 
52
  iface = gr.Interface(
53
+ fn=extract_apk_info,
54
+ inputs=gr.File(label="Upload APK File", file_types=['.apk']),
55
+ outputs=gr.Textbox(label="Extracted APK Information", lines=25),
56
+ title="APK Extractor",
57
+ description="Upload an APK file to extract and view its components."
58
  )
59
 
60
+ # Launch the interface
61
  iface.launch()