-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_list_extraction.py
More file actions
158 lines (139 loc) · 5.22 KB
/
api_list_extraction.py
File metadata and controls
158 lines (139 loc) · 5.22 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import os
from typing import OrderedDict
from Lib.library import *
from collections import Counter
import json
def extract_apis_names(path, data = None):
"""
extract api of android app
@param[in] path : path of android app
@param[in] api_dataset : list of api is extracted in anroid app
@param[in] label : the label is set to the file
return
"""
if (data == None): data = set()
for smaliFile in list_smali_file(path):
try:
file_content = open(smaliFile, 'r').readlines()
api_found = False
for row in file_content:
if re.match(r'^.method', row):
api_found = True
continue
if (api_found == True):
row = row.strip()
if re.search(r'^invoke', row):
a = API(row)
if ((a.api.find("Landroid") != -1) or (a.api.find("Ljava") != -1)):
data.add(a.api)
else:
continue
if re.match(r'^.end method', row):
api_found = False
except:
pass
os.system('rmdir /s /q \"{}\"'.format(path))
return data
def get_list_malicious_apis(path, data_malware = None):
if data_malware == None:
data_malware = dict()
data_benign = dict()
path_benign = path + '/Benign/'
path_adware = path + '/Adware/'
path_botnet = path + '/Botnet/'
path_ransomware = path + '/Ransomware/'
path_scareware = path + '/Scareware/'
path_smsmalware = path + '/SMSmalware/'
list_benign = [path_benign + f for f in os.listdir(path_benign)]
list_adware = [path_adware + f for f in os.listdir(path_adware)]
list_botnet = [path_botnet + f for f in os.listdir(path_botnet)]
list_ransomware = [path_ransomware + f for f in os.listdir(path_ransomware)]
list_scareware = [path_scareware + f for f in os.listdir(path_scareware)]
list_smsmalware = [path_smsmalware + f for f in os.listdir(path_smsmalware)]
for i, file in enumerate(list_benign):
os.system('cls')
print(f'Benign :{i}')
path = decompile(file)
if path != 'err':
temp = extract_apis_names(path = path)
for i in temp:
if i in data_benign:
data_benign[i] += 1
else:
data_benign[i] = 1
for i, file in enumerate(list_adware):
os.system('cls')
print(f'Adware :{i}')
path = decompile(file)
if path != 'err':
temp = extract_apis_names(path = path)
for i in temp:
if i in data_malware:
data_malware[i] += 1
else:
data_malware[i] = 1
for i, file in enumerate(list_botnet):
os.system('cls')
print(f'Botnet :{i}')
path = decompile(file)
if path != 'err':
temp = extract_apis_names(path = path)
for i in temp:
if i in data_malware:
data_malware[i] += 1
else:
data_malware[i] = 1
for i, file in enumerate(list_ransomware):
os.system('cls')
print(f'Ransomeware :{i}')
path = decompile(file)
if path != 'err':
temp = extract_apis_names(path = path)
for i in temp:
if i in data_malware:
data_malware[i] += 1
else:
data_malware[i] = 1
for i, file in enumerate(list_scareware):
os.system('cls')
print(f'Scareware :{i}')
path = decompile(file)
if path != 'err':
temp = extract_apis_names(path = path)
for i in temp:
if i in data_malware:
data_malware[i] += 1
else:
data_malware[i] = 1
for i, file in enumerate(list_smsmalware):
os.system('cls')
print(f'Smsmalware :{i}')
path = decompile(file)
if path != 'err':
temp = extract_apis_names(path = path)
for i in temp:
if i in data_malware:
data_malware[i] += 1
else:
data_malware[i] = 1
#a = set(data_malware) - set(data_benign)
data1 = OrderedDict(sorted(data_benign.items(), key=lambda x: x[1], reverse=True))
data2 = OrderedDict(sorted(data_malware.items(), key=lambda x: x[1], reverse=True))
try:
textfile = open("api_dataset_benign.json", "w")
json.dump(obj = data1, fp = textfile, indent=4)
textfile = open("api_dataset_malware.json", "w")
json.dump(obj = data2, fp = textfile, indent=4)
except:
pass
finally:
textfile.close()
return None
if __name__ == "__main__":
get_list_malicious_apis("C:\\Users\\marsh_000.LOrdinatueur\\Downloads\\container\\full_600")
# data1 = extract_apis_names("C:\\Users\\marsh_000.LOrdinatueur\\Desktop\\Code\\a")
# data1 = dict(Counter(data1))
# data1 = OrderedDict(sorted(data1.items(), key=lambda x: x[1], reverse=True))
# textfile = open("api_dataset.json", "w")
# json.dump(obj = data1, fp = textfile, indent=4)
# textfile.close()