/
apk.py
285 lines (238 loc) · 8.54 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# This file is part of Androguard.
#
# Copyright (C) 2010, Anthony Desnos <desnos at t0t0.fr>
# All rights reserved.
#
# Androguard is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Androguard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Androguard. If not, see <http://www.gnu.org/licenses/>.
import bytecode
from axmlprinter import AXMLPrinter
from bytecode import SV
import zipfile, StringIO
from struct import pack, unpack
from xml.dom import minidom
try:
import chilkat
ZIPMODULE = 0
# UNLOCK: change it with your valid key !
try:
CHILKAT_KEY = open("key.txt", "rb").read()
except Exception:
CHILKAT_KEY = "testme"
except ImportError:
ZIPMODULE = 1
class APK:
"""APK manages apk file format"""
def __init__(self, filename, raw=False):
"""
@param filename: specify the path of the file, or raw data
@param raw: specify (boolean) if the filename is a path or raw data
"""
self.filename = filename
self.xml = {}
self.package = ""
self.androidversion = {}
self.permissions = []
self.valid_apk = False
if raw == True:
self.__raw = filename
else:
fd = open(filename, "rb")
self.__raw = fd.read()
fd.close()
if ZIPMODULE == 0:
self.zip = ChilkatZip(self.__raw)
else:
self.zip = zipfile.ZipFile(StringIO.StringIO(self.__raw))
# CHECK if there is only one embedded file
#self._reload_apk()
for i in self.zip.namelist():
if i == "AndroidManifest.xml":
self.xml[i] = minidom.parseString(AXMLPrinter(self.zip.read(i)).getBuff())
self.package = self.xml[i].documentElement.getAttribute("package")
self.androidversion["Code"] = self.xml[i].documentElement.getAttribute("android:versionCode")
self.androidversion["Name"] = self.xml[i].documentElement.getAttribute("android:versionName")
for item in self.xml[i].getElementsByTagName("uses-permission"):
self.permissions.append(str(item.getAttribute("android:name")))
self.valid_apk = True
def is_valid_apk(self):
return self.valid_apk
#def _reload_apk(self):
# if len(files) == 1:
# if ".apk" in files[0]:
# self.__raw = self.zip.read(files[0])
# if ZIPMODULE == 0:
# self.zip = ChilkatZip(self.__raw)
# else:
# self.zip = zipfile.ZipFile(StringIO.StringIO(self.__raw))
def get_filename(self):
"""
Return the filename of the APK
"""
return self.filename
def get_package(self):
"""
Return the name of the package
"""
return self.package
def get_androidversion_code(self):
"""
Return the android version code
"""
return self.androidversion["Code"]
androidversion_code = property(get_androidversion_code)
def get_androidversion_name(self):
"""
Return the android version name
"""
return self.androidversion["Name"]
androidversion_name = property(get_androidversion_name)
def get_files(self):
"""
Return the files inside the APK
"""
return self.zip.namelist()
files = property(get_files)
def get_files_types(self):
"""
Return the files inside the APK with their types (by using python-magic)
"""
try:
import magic
except ImportError:
return {}
l = {}
builtin_magic = 0
try:
getattr(magic, "Magic")
except AttributeError:
builtin_magic = 1
if builtin_magic:
ms = magic.open(magic.MAGIC_NONE)
ms.load()
for i in self.get_files():
l[ i ] = ms.buffer(self.zip.read(i))
else:
m = magic.Magic()
for i in self.get_files():
l[ i ] = m.from_buffer(self.zip.read(i))
return l
files_types = property(get_files_types)
def get_raw(self):
"""
Return raw bytes of the APK
"""
return self.__raw
raw = property(get_raw)
def get_file(self, filename):
"""
Return the raw data of the specified filename
"""
try:
return self.zip.read(filename)
except KeyError:
return ""
def get_dex(self):
"""
Return the raw data of the classes dex file
"""
return self.get_file("classes.dex")
dex = property(get_dex)
def get_elements(self, tag_name, attribute):
"""
Return elements in xml files which match with the tag name and the specific attribute
@param tag_name: a string which specify the tag name
@param attribute: a string which specify the attribute
"""
l = []
for i in self.xml:
for item in self.xml[i].getElementsByTagName(tag_name):
value = item.getAttribute(attribute)
if len(value) > 0:
if value[0] == ".":
value = self.package + value
else:
v_dot = value.find(".")
if v_dot == 0:
value = self.package + "." + value
elif v_dot == -1:
value = self.package + "." + value
l.append(str(value))
return l
def get_element(self, tag_name, attribute):
"""
Return element in xml files which match with the tag name and the specific attribute
@param tag_name: a string which specify the tag name
@param attribute: a string which specify the attribute
"""
l = []
for i in self.xml:
for item in self.xml[i].getElementsByTagName(tag_name):
value = item.getAttribute(attribute)
if len(value) > 0:
return value
return None
def get_activities(self):
"""
Return the android:name attribute of all activities
"""
return self.get_elements("activity", "android:name")
activities = property(get_activities)
def get_services(self):
"""
Return the android:name attribute of all services
"""
return self.get_elements("service", "android:name")
services = property(get_services)
def get_receivers(self):
"""
Return the android:name attribute of all receivers
"""
return self.get_elements("receiver", "android:name")
receivers = property(get_receivers)
def get_providers(self):
"""
Return the android:name attribute of all providers
"""
return self.get_elements("provider", "android:name")
providers = property(get_providers)
def get_permissions(self):
"""
Return permissions
"""
return self.permissions
permissions = property(get_permissions)
def get_min_sdk_version(self):
"""
Return the android:minSdkVersion attribute
"""
return self.get_element("uses-sdk", "android:minSdkVersion")
min_sdk_version = property(get_min_sdk_version)
def get_target_sdk_version(self):
"""
Return the android:targetSdkVersion attribute
"""
return self.get_element("uses-sdk", "android:targetSdkVersion")
target_sdk_version = property(get_target_sdk_version)
def get_libraries(self):
"""
Return the android:name attributes for libraries
"""
return self.get_elements("uses-library", "android:name")
libraries = property(get_libraries)
def show(self):
print "FILES: ", self.get_files_types()
print "ACTIVITIES: ", self.get_activities()
print "SERVICES: ", self.get_services()
print "RECEIVERS: ", self.get_receivers()
print "PROVIDERS: ", self.get_providers()