Spaces:
Running
Running
import gradio as gr | |
import pickle | |
import os | |
from androguard.misc import AnalyzeAPK | |
class SimplifiedAPKAnalyzer: | |
def __init__(self, model_path): | |
self.model = self.load_model(model_path) | |
self.columns = [ | |
'android.permission.INTERNET', | |
'android.permission.READ_EXTERNAL_STORAGE', | |
'android.permission.WRITE_EXTERNAL_STORAGE', | |
'android.permission.ACCESS_NETWORK_STATE', | |
'android.permission.WAKE_LOCK', | |
'android.permission.VIBRATE', | |
'android.permission.ACCESS_WIFI_STATE', | |
'android.permission.RECEIVE_BOOT_COMPLETED', | |
'android.permission.GET_ACCOUNTS', | |
'android.permission.CAMERA', | |
'other_permission', | |
'num_of_permissions' | |
] | |
def load_model(self, model_path): | |
with open(model_path, 'rb') as f: | |
model_data = pickle.load(f) | |
return model_data['model'] | |
def unpack_apk(self, apk_path): | |
a, _, _ = AnalyzeAPK(apk_path) | |
return { | |
"package_name": a.get_app_name(), | |
"package": a.get_package(), | |
"permissions": a.get_permissions(), | |
} | |
def apk_to_features(self, apk_data): | |
features = {col: 0 for col in self.columns} | |
for permission in apk_data["permissions"]: | |
if permission in features: | |
features[permission] = 1 | |
else: | |
features["other_permission"] += 1 | |
features["num_of_permissions"] = len(apk_data["permissions"]) | |
return list(features.values()) | |
def analyze_apk(self, file): | |
apk_data = self.unpack_apk(file.name) | |
features = self.apk_to_features(apk_data) | |
prediction = self.model.predict([features])[0] | |
result = "Malware" if prediction == 1 else "Not Malware" | |
return f"Analysis result for {apk_data['package_name']}: {result}" | |
def run_analysis(file): | |
analyzer = SimplifiedAPKAnalyzer("apk_malware.model") | |
return analyzer.analyze_apk(file) | |
iface = gr.Interface( | |
fn=run_analysis, | |
inputs=gr.File(label="Upload APK file"), | |
outputs="text", | |
title="APK Malware Analyzer", | |
description="Upload an Android APK file to analyze it for potential malware." | |
) | |
iface.launch() |