-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapk.py
238 lines (180 loc) · 7.1 KB
/
apk.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from db_manager import insert_apk, insert_features
import os
import re
class APK:
def __init__(self, path: str, dataset: str, malignity: int = None):
self.manifest_features = ManifestFeatures()
self.dex_features = DexFeatures()
self.malignity = malignity
self.name = self.get_apk_name(path)
self.path = path
self.dataset = dataset
self.score = None
def set_manifest_features(self, manifest_features):
if type(manifest_features) == ManifestFeatures:
self.manifest_features = manifest_features
def set_dex_features(self, dex_features):
if type(dex_features) == DexFeatures:
self.dex_features = dex_features
def get_apk_name(self, path: str) -> str:
basename = os.path.basename(path)
match = re.search(r'([\w.]+)\.apk', path)
if match:
return match.group(1)
return basename
def save_apk_to_db(self):
row_id = insert_apk(self)
if row_id:
insert_features(self.get_features(row_id))
pass
else:
raise Exception('apk not saved : row_id == None')
def get_features(self, row_id=None) -> list:
features = self.get_dex_features().get_features()
features.extend(self.get_manifest_features().get_features())
return [
{'apk_id': row_id,
'name': feature['feature_name'],
'type': feature['feature_type']}
for feature in features
]
@staticmethod
def get_feature_types():
feature_types = ManifestFeatures.get_feature_types()
feature_types.extend(DexFeatures.get_feature_types())
return feature_types
def get_dataset(self):
return self.dataset
def get_manifest_features(self):
return self.manifest_features
def get_dex_features(self):
return self.dex_features
def get_name(self):
return self.name
def get_malignity(self):
return self.malignity
def add_restricted_api(self, api_call):
self.dex_features.add_restricted_api(api_call)
def add_suspicious_api(self, api_call):
self.dex_features.add_suspicious_api(api_call)
def add_used_permissions(self, permissions):
self.dex_features.add_used_permissions(permissions)
def add_url(self, url):
self.dex_features.add_url(url)
def add_defined_permissions(self, permissions):
self.manifest_features.add_permissions(permissions)
def add_activities(self, activities):
self.manifest_features.add_activities(activities)
def add_receivers(self, receivers):
self.manifest_features.add_receivers(receivers)
def add_services(self, services):
self.manifest_features.add_services(services)
def add_providers(self, providers):
self.manifest_features.add_providers(providers)
def add_hardwares(self, hardwares):
self.manifest_features.add_hardwares(hardwares)
def add_intents(self, intents):
self.manifest_features.add_intents(intents)
def get_path(self):
return self.path
class DexFeatures:
RESTRICTED_API = 'restricted_api'
USED_PERM = 'used_perm'
SUSPICIOUS_API = 'suspicious_api'
URL = 'url'
def __init__(self):
self.restricted_apis = set()
self.used_permissions = set()
self.suspicious_apis = set()
self.urls = set()
self.features = {DexFeatures.RESTRICTED_API: self.restricted_apis,
DexFeatures.USED_PERM: self.used_permissions,
DexFeatures.SUSPICIOUS_API: self.suspicious_apis,
DexFeatures.URL: self.urls}
def get_features(self):
return [{'feature_type': feature_type,
'feature_name': feature_name} for feature_type, feature_set in self.features.items()
for feature_name in feature_set if len(feature_set) != 0]
@staticmethod
def get_feature_types():
return [DexFeatures.RESTRICTED_API,
DexFeatures.USED_PERM,
DexFeatures.SUSPICIOUS_API,
DexFeatures.URL]
def get_restricted_apis(self):
return self.restricted_apis
def get_used_permissions(self):
return self.used_permissions
def get_suspicious_apis(self):
return self.suspicious_apis
def get_url(self):
return self.urls
def add_restricted_api(self, api: str):
self.restricted_apis.add(api)
def add_used_permissions(self, permissions: list):
self.used_permissions.update(permissions)
def add_suspicious_api(self, api: str):
self.suspicious_apis.add(api)
def add_url(self, url: str):
self.urls.add(url)
class ManifestFeatures:
DEFINED_PERM = 'defined_perm'
HARDWARE = 'hardware'
INTENT = 'intent'
ACTIVITY = 'activity'
RECEIVER = 'receiver'
SERVICE = 'service'
PROVIDER = 'provider'
def __init__(self):
self.permissions = set()
self.hardware = set()
self.intents = set()
# app components
self.activities = set()
self.receivers = set()
self.services = set()
self.providers = set()
self.features = {ManifestFeatures.DEFINED_PERM: self.permissions,
ManifestFeatures.HARDWARE: self.hardware,
ManifestFeatures.INTENT: self.intents,
ManifestFeatures.ACTIVITY: self.activities,
ManifestFeatures.RECEIVER: self.receivers,
ManifestFeatures.SERVICE: self.services,
ManifestFeatures.PROVIDER: self.providers}
def get_features(self):
return [{'feature_type': feature_type,
'feature_name': feature_name} for feature_type, feature_set in self.features.items()
for feature_name in feature_set if len(feature_set) != 0]
@staticmethod
def get_feature_types():
return [ManifestFeatures.DEFINED_PERM, ManifestFeatures.HARDWARE, ManifestFeatures.INTENT,
ManifestFeatures.ACTIVITY, ManifestFeatures.RECEIVER,
ManifestFeatures.SERVICE, ManifestFeatures.PROVIDER]
def get_activities(self):
return self.activities
def get_receivers(self):
return self.receivers
def get_services(self):
return self.services
def get_providers(self):
return self.providers
def get_hardware(self):
return self.hardware
def get_intents(self):
return self.intents
def get_permissions(self):
return self.permissions
def add_permissions(self, permissions):
self.permissions.update(permissions)
def add_hardwares(self, hardwares):
self.hardware.update(hardwares)
def add_intents(self, intents):
self.intents.update(intents)
def add_activities(self, activities):
self.activities.update(activities)
def add_receivers(self, receivers):
self.receivers.update(receivers)
def add_services(self, services):
self.services.update(services)
def add_providers(self, providers):
self.providers.update(providers)