Skip to content

Commit

Permalink
Deploy the model
Browse files Browse the repository at this point in the history
  • Loading branch information
tderick committed Oct 26, 2024
1 parent 38daa86 commit b864e06
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 3 deletions.
8 changes: 8 additions & 0 deletions app/feature_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from androguard.core.apk import APK

features = {
'transact': 0,
'onServiceConnected': 0,
'bindService': 0,
'attachInterface': 0,
Expand Down Expand Up @@ -262,6 +263,13 @@ def extract_manifest_intents(self,apk_path):
# Load the APK
apk = APK(apk_path)

self.app_name = apk.get_app_name()
self.package_name = apk.get_package()
self.version_name = apk.get_androidversion_name()
self.version_code = apk.get_androidversion_code()
self.app_features = apk.get_features()


# Get the AndroidManifest.xml
manifest_xml = apk.get_android_manifest_xml()

Expand Down
46 changes: 43 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import tempfile
import json
import pickle
import pandas as pd
from fastapi import FastAPI, File, UploadFile, HTTPException, Response
from pathlib import Path

from .feature_extractor import APKFeatureExtractor

Expand All @@ -11,7 +14,19 @@
version="0.0.1",
)

base_dir = Path(__file__).resolve().parent
file_path = f"{base_dir}/model/"

# Load the model
loaded_model_lightgbm = pickle.load(open(f"{file_path}lightgbm_model.sav", 'rb'))
selected_features_loaded = pd.read_csv(f'{file_path}selected_features_lgbm.csv')
selected_features_loaded_list = selected_features_loaded['Feature'].tolist()

def predict_status_of_app(input_data):
input_df = pd.DataFrame([input_data])
input_df_filtered = input_df[selected_features_loaded_list]
prediction = loaded_model_lightgbm.predict(input_df_filtered)
return prediction[0]

@app.post('/api/v1/android-malware-detection')
async def android_malware_detection(file: UploadFile = File(...)):
Expand All @@ -28,9 +43,34 @@ async def android_malware_detection(file: UploadFile = File(...)):
# Get the path of the temporary file
temp_file_path = temp_file.name

rs = APKFeatureExtractor(temp_file_path).extract_features()
# Extract features from the APK file
apk_features_extractor = APKFeatureExtractor(temp_file_path)

features = apk_features_extractor.extract_features()
app_name = apk_features_extractor.app_name
package_name = apk_features_extractor.package_name
version_name = apk_features_extractor.version_name
version_code = apk_features_extractor.version_code
app_features = apk_features_extractor.app_features

# Predict the status of the app
prediction = predict_status_of_app(features)
if prediction == 0:
status = "Benign"
else:
status = "Malware"


rs = {
"app_name": app_name,
"package_name": package_name,
"version_name": version_name,
"version_code": version_code,
"app_features": ", ".join(f.split(".")[-1] for f in app_features),
"status": status
}

json_response = json.dumps(rs)
return Response(content=json_response,media_type="application/json")
rs_json = json.dumps(rs)
return Response(content=rs_json,media_type="application/json")


Binary file added app/model/lightgbm_model.sav
Binary file not shown.
101 changes: 101 additions & 0 deletions app/model/selected_features_lgbm.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Feature
transact
bindService
attachInterface
onServiceConnected
android.os.Binder
SEND_SMS
Ljava.lang.Class.getCanonicalName
Ljava.lang.Class.getMethods
Ljava.lang.Class.cast
Ljava.net.URLDecoder
android.content.pm.Signature
READ_PHONE_STATE
android.telephony.SmsManager
ClassLoader
Landroid.content.Context.unregisterReceiver
getBinder
Landroid.content.Context.registerReceiver
Ljava.lang.Class.getDeclaredField
Ljava.lang.Class.getField
RECEIVE_SMS
GET_ACCOUNTS
READ_SMS
getCallingUid
Ljavax.crypto.spec.SecretKeySpec
TelephonyManager.getLine1Number
USE_CREDENTIALS
android.content.pm.PackageInfo
android.intent.action.BOOT_COMPLETED
HttpGet.init
MANAGE_ACCOUNTS
KeySpec
android.intent.action.SEND
Ljava.lang.Class.getMethod
SecretKey
System.loadLibrary
Ljavax.crypto.Cipher
android.telephony.gsm.SmsManager
WRITE_HISTORY_BOOKMARKS
WRITE_SMS
TelephonyManager.getSubscriberId
Ljava.lang.Class.forName
android.intent.action.PACKAGE_REPLACED
CAMERA
Runtime.getRuntime
READ_HISTORY_BOOKMARKS
INSTALL_PACKAGES
WAKE_LOCK
TelephonyManager.getDeviceId
android.os.IBinder
Ljava.lang.Object.getClass
Binder
mount
MODIFY_AUDIO_SETTINGS
URLClassLoader
INTERNET
RECORD_AUDIO
NFC
HttpUriRequest
abortBroadcast
Ljava.lang.Class.getPackage
createSubprocess
chmod
ACCESS_LOCATION_EXTRA_COMMANDS
sendMultipartTextMessage
android.intent.action.TIME_SET
RESTART_PACKAGES
android.intent.action.TIMEZONE_CHANGED
GET_TASKS
RECEIVE_BOOT_COMPLETED
BLUETOOTH
SET_WALLPAPER
CHANGE_NETWORK_STATE
WRITE_EXTERNAL_STORAGE
remount
CALL_PRIVILEGED
BROADCAST_STICKY
TelephonyManager.getSimCountryIso
WRITE_SECURE_SETTINGS
getCallingPid
BATTERY_STATS
HttpPost.init
ACCESS_WIFI_STATE
Runtime.exec
TelephonyManager.getNetworkOperator
TelephonyManager.getSimOperator
TelephonyManager.isNetworkRoaming
MASTER_CLEAR
VIBRATE
PackageInstaller
findClass
TelephonyManager.getSimSerialNumber
/system/app
READ_CONTACTS
Ljava.lang.Class.getResource
Ljava.lang.Class.getClasses
READ_CALENDAR
READ_LOGS
ACCESS_COARSE_LOCATION
ACCESS_NETWORK_STATE
divideMessage
Binary file added sample/sample.apk
Binary file not shown.

0 comments on commit b864e06

Please sign in to comment.