File size: 2,275 Bytes
10e0ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()