-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
106 lines (74 loc) · 3.27 KB
/
utils.py
File metadata and controls
106 lines (74 loc) · 3.27 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import shutil
import subprocess
import pprint
import platform
import numpy as np
MAGIC_PREFIX = ""
if platform.system() == "Windows":
MAGIC_PREFIX = "\\\\?\\"
BAKSMALI_PATH = os.path.abspath(os.path.join('tools', 'baksmali-2.1.3.jar'))
BAKSMALI_OUTPUT = MAGIC_PREFIX + os.path.abspath(os.path.join('tools_output', 'baksmali_output'))
APKTOOL_PATH = os.path.abspath(os.path.join('tools', 'apktool.jar'))
APKTOOL_OUTPUT = MAGIC_PREFIX + os.path.abspath(os.path.join('tools_output', 'apktool_output'))
APKTOOL_OUTPUT_MANIFEST = os.path.join(APKTOOL_OUTPUT, "AndroidManifest.xml")
AAPT_PATH = os.path.abspath(os.path.join('tools', 'aapt.exe'))
AAPT_OUTPUT = MAGIC_PREFIX + os.path.abspath(os.path.join('tools_output', 'aapt_output'))
AAPT_OUTPUT_MANIFEST = os.path.join(AAPT_OUTPUT, "AndroidManifest.xml")
MALWARE_DATASET_DIR_PATH = os.path.abspath(os.path.join('datasets', 'malwares'))
BENIGN_DATASET_DIR_PATH = os.path.abspath(os.path.join('datasets', 'benign'))
SQLITE_DB_PATH = os.path.abspath(os.path.join('DB_scripts', 'droid_sec.sqlite'))
RANDOM_STATE = 42
def clean_directory(dir_path):
_, dirnames, filenames = next(os.walk(dir_path), (None, None, None))
if dirnames:
for dirname in dirnames:
shutil.rmtree(os.path.abspath(os.path.join(dir_path, dirname)))
if filenames:
for filename in filenames:
os.remove(os.path.abspath(os.path.join(dir_path, filename)))
def clean_baksmali_output_dir():
clean_directory(BAKSMALI_OUTPUT)
def clean_apktool_output():
clean_directory(APKTOOL_OUTPUT)
def clean_aapt_output():
clean_directory(AAPT_OUTPUT)
def execute_baksmali_jar(apk_path: str):
subprocess.run(['java', '-jar', BAKSMALI_PATH, apk_path, '-o', BAKSMALI_OUTPUT], shell=True)
def execute_apktool(apk_path: str):
subprocess.run(['java', '-jar', APKTOOL_PATH, "-o", APKTOOL_OUTPUT, "-s", "-f", "d", apk_path],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
def execute_aapt(apk_path: str):
f = open(AAPT_OUTPUT_MANIFEST, "w")
subprocess.run([AAPT_PATH, 'dump', "xmltree", apk_path, "AndroidManifest.xml"], stdout=f, shell=True)
f.close()
def set_aapt_path(aapt_path: str):
global AAPT_PATH
AAPT_PATH = aapt_path
def list_files(path: str) -> set:
files = set()
for (dirpath, _, filenames) in os.walk(path):
for filename in filenames:
files.add(os.path.abspath(os.path.join(dirpath, filename)))
return files
def get_smali_files() -> str:
for (dirpath, _, filenames) in os.walk(BAKSMALI_OUTPUT):
if filenames:
for file_name in filenames:
yield os.path.join(dirpath, file_name)
def get_smali_lines(file_path: str) -> str:
with open(file_path, encoding='utf8') as file:
for line in file:
yield line
def find_closest_values(array, values):
closes_values = set()
for value in values:
idx = np.abs(array - value).argmin()
closes_values.add(array[idx])
return closes_values
def apk_print(apk):
pprint.pprint({'apk_name': apk.get_name(),
'apk_path': apk.get_path(),
'apk_malignity': apk.get_malignity(),
'apk_nb_features': len(apk.get_features),
'apk_features': apk.get_features})