-
Notifications
You must be signed in to change notification settings - Fork 125
/
ApkDetecter.py
282 lines (230 loc) · 11 KB
/
ApkDetecter.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
#-*-encoding:utf-8-*-
__author__ = 'Andy'
import os
import time
import tempfile
import io
import sys
import hashlib
import thread
import shutil
from PyQt4 import QtCore, QtGui
from AnalysisXML.AXML import AXML
from DexInfo import DexInfoForm
from ApkInfo import MyApkInfoForm
from AnalysisDEX.InitDEX import InitDEX
from CheckProtect import CheckProtect
from AnalysisCSN.CSN import CSN
from GUI.apkdetecter_ui import Ui_APKDetecter
class ApkDetecterForm(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
#super(ApkDetecterForm, self).__init__(parent)
self._want_to_close = True
self.dexheader = {}
self.loadfile_path = ""
#self.unpackDir = tempfile.mktemp()
self.unpackDir = ur"c:\APK"
isExists = os.path.exists(self.unpackDir)
if not isExists:
os.makedirs(ur"c:\APK")
self.ui = Ui_APKDetecter()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.file_open, QtCore.SIGNAL("clicked()"), self.file_dialog)
self.ui.apk_info.clicked.connect(self.apkinfo_dialog)
self.ui.extend_info.clicked.connect(self.extendinfo_dialog)
def closeEvent(self, evnt):
if self._want_to_close:
super(ApkDetecterForm, self).closeEvent(evnt)
self.clearfiles(self.unpackDir)
print "Andy"
def probar_thread(self, no, interval):
for a in range(no, interval):
time.sleep(0.1)
self.ui.progressBar.setValue(a)
thread.exit_thread()
def unzip(self, apkpath):
apkpath = unicode(apkpath, "utf8")
cmd = "tool\\7z.exe x %s -y -o%s *.dex AndroidManifest.xml lib META-INF assets"
print cmd % (apkpath, self.unpackDir)
self.ui.progressBar.setMaximum(29)
thread.start_new_thread(self.probar_thread, (3, 30))
os.system(cmd % (apkpath, self.unpackDir))
def Init_Main_text(self):
self.ui.te_dex_flag.clear()
self.ui.te_dexheader_size.clear()
self.ui.te_endiantag.clear()
self.ui.te_file_size.clear()
self.ui.te_linkoff.clear()
self.ui.te_linksize.clear()
self.ui.te_protect.clear()
def Init_Apkinfo_text(self):
self.apkinfo.ui.edt_file.setText("")
self.apkinfo.ui.edt_serial_num.setText("")
self.apkinfo.ui.edt_publisher.setText("")
self.apkinfo.ui.edt_issuer.setText("")
self.apkinfo.ui.edt_dexmd5.setText("")
self.apkinfo.ui.edt_apkmd5.setText("")
self.apkinfo.ui.edt_package.setText("")
self.apkinfo.ui.edt_version.setText("")
self.apkinfo.ui.edt_version_num.setText("")
self.apkinfo.ui.edt_version_need.setText("")
def Init_DexInfo_text(self):
self.dexinfo.ui.text_magic.setText("")
self.dexinfo.ui.text_checksum.setText("")
self.dexinfo.ui.text_file_size.setText("")
self.dexinfo.ui.text_header_size.setText("")
self.dexinfo.ui.text_endian_tag.setText("")
self.dexinfo.ui.text_link_size.setText("")
self.dexinfo.ui.text_link_off.setText("")
self.dexinfo.ui.text_map_off.setText("")
self.dexinfo.ui.text_string_ids_size.setText("")
self.dexinfo.ui.text_string_ids_off.setText("")
self.dexinfo.ui.text_type_ids_size.setText("")
self.dexinfo.ui.text_type_ids_off.setText("")
self.dexinfo.ui.text_proto_ids_size.setText("")
self.dexinfo.ui.text_proto_ids_off.setText("")
self.dexinfo.ui.text_field_ids_size.setText("")
self.dexinfo.ui.text_field_ids_off.setText("")
self.dexinfo.ui.text_method_ids_size.setText("")
self.dexinfo.ui.text_method_ids_off.setText("")
self.dexinfo.ui.text_class_defs_size.setText("")
self.dexinfo.ui.text_class_defs_off.setText("")
self.dexinfo.ui.text_data_size.setText("")
self.dexinfo.ui.text_data_off.setText("")
self.dexinfo.ui.text_sha.setText("")
def clearfiles(self, delDir):
delList = []
delList = os.listdir(delDir)
for f in delList:
filePath = os.path.join(delDir, f)
if os.path.isfile(filePath):
os.remove(filePath)
elif os.path.isdir(filePath):
shutil.rmtree(filePath, True)
shutil.rmtree(delDir)
def file_dialog(self):
fd = QtGui.QFileDialog(self)
self.Init_Main_text()
self.loadfile_path = u""
self.loadfile_path = fd.getOpenFileName()
#self.loadfile_path = unicode(self.loadfile_path, "utf8")
self.loadfile_path = self.loadfile_path.replace("/", os.path.sep)
if self.loadfile_path != u"":
self.clearfiles(self.unpackDir)
self.ui.te_path.setText(self.loadfile_path)
self.unzip(self.loadfile_path)
obj = CheckProtect(self.unpackDir)
protect_flag = obj.check_protectflag()
dexobj = InitDEX()
self.dexheader ={}
self.dexheader = dexobj.getDexInfo(self.unpackDir + os.path.sep + "classes.dex")
self.ui.te_dex_flag.setText(self.dexheader["header_magic"])
self.ui.te_dexheader_size.setText(self.dexheader["header_headerSize"])
self.ui.te_endiantag.setText(self.dexheader["header_endianTag"])
self.ui.te_file_size.setText(self.dexheader["header_fileSize"])
self.ui.te_linkoff.setText(self.dexheader["header_linkOff"])
self.ui.te_linksize.setText(self.dexheader["header_linkSize"])
self.ui.te_protect.setText(protect_flag)
else:
return
def GetFileMd5(self, path):
try:
file = open(path, 'rb')
md5 = hashlib.md5()
strRead = ""
while True:
strRead = file.read(8096)
if not strRead:
break
md5.update(strRead)
#read file finish
strMd5 = md5.hexdigest()
file.close()
return strMd5
except:
return u"Sorry,计算出错!"
def apkinfo_dialog(self):
self.apkinfo = MyApkInfoForm()
self.Init_Apkinfo_text()
csn_path = self.unpackDir + os.path.sep + "META-INF"
if os.path.isdir(csn_path):
f_list = os.listdir(csn_path)
for file_name in f_list:
if os.path.splitext(file_name)[1] == '.RSA' or os.path.splitext(file_name)[1] == '.DSA':
csn_path = csn_path + os.path.sep + file_name
csn = CSN(csn_path)
self.apkinfo.ui.edt_file.setText(str(csn.get_size()))
self.apkinfo.ui.edt_serial_num.setText(str(csn.getCertificateSN()).upper())
self.apkinfo.ui.edt_publisher.setText(str(csn.getCertificateIDN()))
self.apkinfo.ui.edt_issuer.setText(str(csn.getCertificateSDN()))
break
dex_path = self.unpackDir + os.path.sep + "classes.dex"
if os.path.exists(dex_path):
m = hashlib.md5()
file = io.FileIO(dex_path, 'r')
bytes = file.read(1024)
while(bytes != b''):
m.update(bytes)
bytes = file.read(1024)
file.close()
dexmd5value = m.hexdigest()
#print str(dexmd5value).upper()
self.apkinfo.ui.edt_dexmd5.setText(str(dexmd5value).upper())
apkpath = unicode(self.loadfile_path, "utf8")
if os.path.isfile(apkpath) and self.loadfile_path != "":
pass
if self.loadfile_path != "":
#apkpath = unicode(self.loadfile_path, "utf8")
apkmd5value = self.GetFileMd5(self.loadfile_path)
# m = hashlib.md5()
# file = io.FileIO(self.loadfile_path, 'r')
# bytes = file.read(1024)
# while(bytes != b''):
# m.update(bytes)
# bytes = file.read(1024)
# file.close()
# apkmd5value = m.hexdigest()
self.apkinfo.ui.edt_apkmd5.setText(apkmd5value.upper())
path = self.unpackDir + os.path.sep +"AndroidManifest.xml"
if os.path.exists(path):
axml_analysis = AXML(self.unpackDir + os.path.sep +"AndroidManifest.xml")
if axml_analysis.get_filename_abs() == 'AndroidManifest':
self.apkinfo.ui.edt_package.setText(axml_analysis.get_package())
self.apkinfo.ui.edt_version.setText(axml_analysis.get_androidversion_name())
self.apkinfo.ui.edt_version_num.setText(axml_analysis.get_androidversion_code())
self.apkinfo.ui.edt_version_need.setText(axml_analysis.getMinSdkVersion())
self.apkinfo.show()
def extendinfo_dialog(self):
self.dexinfo = DexInfoForm()
self.Init_DexInfo_text()
if self.dexheader.has_key("header_magic"):
self.dexinfo.ui.text_magic.setText(self.dexheader["header_magic"])
self.dexinfo.ui.text_checksum.setText(self.dexheader["header_checksum"])
self.dexinfo.ui.text_file_size.setText(self.dexheader["header_fileSize"])
self.dexinfo.ui.text_header_size.setText(self.dexheader["header_headerSize"])
self.dexinfo.ui.text_endian_tag.setText(self.dexheader["header_endianTag"])
self.dexinfo.ui.text_link_size.setText(self.dexheader["header_linkSize"])
self.dexinfo.ui.text_link_off.setText(self.dexheader["header_linkOff"])
self.dexinfo.ui.text_map_off.setText(self.dexheader["header_mapOff"])
self.dexinfo.ui.text_string_ids_size.setText(self.dexheader["header_stringIdsSize"])
self.dexinfo.ui.text_string_ids_off.setText(self.dexheader["header_stringIdsOff"])
self.dexinfo.ui.text_type_ids_size.setText(self.dexheader["header_typeIdsSize"])
self.dexinfo.ui.text_type_ids_off.setText(self.dexheader["header_typeIdsOff"])
self.dexinfo.ui.text_proto_ids_size.setText(self.dexheader["header_protoIdsSize"])
self.dexinfo.ui.text_proto_ids_off.setText(self.dexheader["header_protoIdsOff"])
self.dexinfo.ui.text_field_ids_size.setText(self.dexheader["header_fieldIdsSize"])
self.dexinfo.ui.text_field_ids_off.setText(self.dexheader["header_fieldIdsOff"])
self.dexinfo.ui.text_method_ids_size.setText(self.dexheader["header_methodIdsSize"])
self.dexinfo.ui.text_method_ids_off.setText(self.dexheader["header_methodIdsOff"])
self.dexinfo.ui.text_class_defs_size.setText(self.dexheader["header_classDefsSize"])
self.dexinfo.ui.text_class_defs_off.setText(self.dexheader["header_classDefsOff"])
self.dexinfo.ui.text_data_size.setText(self.dexheader["header_dataSize"])
self.dexinfo.ui.text_data_off.setText(self.dexheader["header_dataOff"])
self.dexinfo.ui.text_sha.setText(self.dexheader["header_signature"])
self.dexinfo.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = ApkDetecterForm()
myapp.show()
sys.exit(app.exec_())