DroidDetective / app.py
User1342's picture
Update app.py
4856931
import os
import sys
import gradio as gr
from androguard.misc import AnalyzeAPK
from DroidDetective.DroidDetective import APK_Analyser
def check_apk(apk_to_check):
if apk_to_check == None:
return "Provide an APK to analyse."
output_builder = ""
analyser = APK_Analyser()
model_path = f"{os.path.dirname(os.path.abspath(__file__))}/DroidDetective/apk_malware.model"
file_to_check = apk_to_check.name
if not file_to_check.endswith(".apk"):
return "Please provide an .apk file."
# Check should train
if not os.path.isfile(model_path):
if os.path.isdir("malware") and os.path.isdir("normal"):
apk_info = analyser.train_model(malware_apks_folder_path="malware", normal_apks_folder_path="normal")
else:
return "When training a model, ensure that a 'malware' and 'normal' folder exist at the root of this project and that training APKs exist in both folders."
# Check if model exists
if os.path.exists(model_path):
print("test0")
result, apk_data = analyser.identify(file_to_check, model_path)
print("test1")
if result == 1:
output_builder = output_builder + "Analysed file, identified as malware!"
else:
output_builder = output_builder + "Analysed file, identified as not malware.".format(file_to_check)
print("test2")
output_builder = output_builder + "\n"+"-"*20 +"\n"+"Permissions: \n"
print("test3")
permissions = apk_data["permissions"]
print("permissions: {}".format(permissions))
print("test4")
for permission in permissions:
print(permission)
output_builder = output_builder + permission + "\n"
print("test5")
print(output_builder)
return output_builder
print("Test6")
else:
return "No model found, please train model"
print(output_builder)
return output_builder
demo = gr.Interface(
fn=check_apk,
inputs= "file",
outputs="text",
)
demo.launch()