-
Notifications
You must be signed in to change notification settings - Fork 265
/
jiagu.py
288 lines (233 loc) · 10 KB
/
jiagu.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
#!/usr/bin/env python
#coding:utf-8
import subprocess
import shutil
from xml.dom import minidom
import zipfile
import os
import re
import glob
import sys
import codecs
import random
import string
import time
from elf_header import ELF
'''
一、针对目标app不存在自定义application的情况
1.反编译目标app
apktool.bat d Target.apk
2.检测manifest文件是否有自定义的Application,并假设没有自定义Application
3.如果没有自定义Application,则复制smali文件夹,跟反编译后的app下的smali合并: cp -rf smali Target/
4.修改manifest文件,将自定义Application设定为“org.hackcode.ProxyApplication”
5.重打包目标app
6.提取重打包后的apk中的classes.dex文件,并压缩为TargetApk.zip文件,并将重打包的app命名为Target.modified.apk
7.合并TuokeApk项目下的classes.dex和TargetApk.zip(加固),生成classes.dex
8.将合并生成的新classes.dex文件与Target.modified.apk中的classes.dex替换
9.复制TuokeApk项目下的lib目录下的所有文件和文件夹到目标app中
10.将修改后的app重压缩成zip文件
11.签名
'''
def un_zip(file_name):
"""unzip zip file"""
zip_file = zipfile.ZipFile(file_name)
if os.path.isdir(file_name + "_files"):
pass
else:
os.mkdir(file_name + "_files")
for names in zip_file.namelist():
zip_file.extract(names,file_name + "_files/")
zip_file.close()
return file_name + "_files"
def zip_dir(dirname,zipfilename):
filelist = []
if os.path.isfile(dirname):
filelist.append(dirname)
else :
for root, dirs, files in os.walk(dirname):
for name in files:
filelist.append(os.path.join(root, name))
zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED)
for tar in filelist:
arcname = tar[len(dirname):]
zf.write(tar,arcname)
zf.close()
def recompile_TuokeApk_Project(application_name):
'''
1.修改 String appClassName = "com.targetapk.MyApplication";
2.重新编译
'''
file_path = 'TuokeApk/app/src/main/java/org/hackcode/ProxyApplication.java'
new_file_path = 'TuokeApk/app/src/main/java/org/hackcode/ProxyApplication2.java'
file_in = open(file_path)
file_out = open(new_file_path, 'w')
while 1:
line = file_in.readline()
if not line:
break
pattern = re.compile(r'.*String.*appClassName.*=.*\".*\";.*')
if re.search(pattern, line):
print '[+] Find \"String appClassName = ...\", replace it with \"' + application_name + '\"'
file_out.write('\t\t\tString appClassName = \"' + application_name + '\";\n')
else:
file_out.write(line)
file_in.close()
file_out.close()
os.remove(file_path)
os.rename(new_file_path, file_path)
# 重新编译TuokeApk工程
os.chdir('TuokeApk/')
subprocess.Popen(["gradle","clean"], shell=True, stdout=subprocess.PIPE).stdout.read()
out = subprocess.Popen(["gradle","build"], shell=True, stdout=subprocess.PIPE).stdout.read()
if out.find('BUILD SUCCESSFUL') < 0:
print 'Build error!'
return False
print '[+] Rebuild TuokeApk project successfully!'
os.chdir('../')
return True
def remove_without_exception(item, type):
if type == 'd':
try:
shutil.rmtree(item)
except Exception as e:
pass
else:
try:
os.remove(item)
except Exception as e:
pass
def clean():
remove_without_exception('Target', 'd')
remove_without_exception('Target.modified.apk_files', 'd')
remove_without_exception('Target.apk', 'f')
remove_without_exception('Target.modified.apk', 'f')
remove_without_exception('Target.modified.2.apk', 'f')
remove_without_exception('classes.dex', 'f')
remove_without_exception('TargetApk.zip', 'f')
remove_without_exception('tuoke.dex', 'f')
os.chdir('TuokeApk/')
subprocess.Popen(["gradle","clean"], shell=True, stdout=subprocess.PIPE).stdout.read()
os.chdir('../')
def genRandomStr(length):
chars=string.ascii_letters+string.digits
return ''.join([random.choice(chars) for i in range(length)])#得出的结果中字符会有重复的
def modify_ehdr_and_delete_shdr(apk_dir):
'''
修改ELF header(e_shoff和e_shnum属性)和删除section header table
TODO: 指定目标so文件
'''
for root, dirs, files in os.walk(apk_dir):
for name in files:
filepath = root + os.path.sep + name
if filepath.endswith('libhackcodejiagu.so'):
print ' - Modifying \"', filepath, '\" ELF header...'
dex = ELF(filepath)
file_size = os.path.getsize(filepath)
shdr_offset = dex.elf32_Ehdr.e_shoff
shdr_size = dex.elf32_Ehdr.e_shnum * dex.elf32_Ehdr.e_shentsize
src_file = file(filepath, 'rb')
dst_file = file(filepath + '2', 'wb')
# 1.破坏ELF Header
dst_file.write(src_file.read(32)) # 保存e_shoff之前的内容
src_file.read(4)
dst_file.write(genRandomStr(4)) # 修复e_shoff
dst_file.write(src_file.read(12)) # 保存e_shoff到e_shnum之间的内容
src_file.read(2)
dst_file.write(genRandomStr(2)) # 修复e_shnum
# 2.删除section header table
#读取section header table之前的内容
dst_file.write(src_file.read(shdr_offset - 50))
#读取section header table之后的内容
src_file.seek(shdr_offset + shdr_size, 0)
dst_file.write(src_file.read())
src_file.close()
dst_file.close()
shutil.move(filepath + '2', filepath)
def main(filepath = None):
clean()
if filepath:
input_filename = filepath
else:
input_filename = sys.argv[1]
shutil.copyfile(input_filename, 'Target.apk')
# Step1: 反编译目标app
out = subprocess.Popen('apktool.bat d Target.apk', stdout=subprocess.PIPE).stdout.read()
if out.find('error') > 0 or out.find('exception') > 0:
print '[Error] apktool decompiled error!'
return
print '[+] Apktool decompiled Target.apk successfully!'
# Step2: 检测manifest文件是否有自定义的Application
doc = minidom.parse('Target/AndroidManifest.xml')
root = doc.documentElement
application_node = root.getElementsByTagName('application')[0]
applicationName = application_node.getAttribute('android:name')
packageName = root.getAttribute('package')
if applicationName:
if not applicationName.startswith(packageName) and applicationName.startswith('.'):
applicationName = packageName + applicationName
print '[+] Target app\'s Application: ', applicationName
# Step3: 修改JiguApk工程中ProxyApplication中的applicationName变量为目标app的Application名称
recompile_TuokeApk_Project(applicationName)
else:
print '[+] Target.apk has no self-defined Application!'
applicationName = 'com.targetapk.MyApplication'
recompile_TuokeApk_Project(applicationName)
# Step3: 复制smali文件夹,跟反编译后的app下的smali合并
print '[+] Copy smali folder into Target folder...'
out = subprocess.Popen('cp -rf smali Target/', stdout=subprocess.PIPE).stdout.read()
# Step4: 修改manifest文件,将自定义Application设定为“org.hackcode.ProxyApplication”
print '[+] Modified AndroidManifest.xml...'
application_node.setAttribute('android:name', 'org.hackcode.ProxyApplication')
file_handle = codecs.open('Target/AndroidManifest.xml','w', 'utf-8')
root.writexml(file_handle)
file_handle.close()
# Step5: 重打包目标app
out = subprocess.Popen('apktool.bat b Target', stdout=subprocess.PIPE).stdout.read()
if out.find('error') > 0 or out.find('exception') > 0:
print '[Error] apktool recompiled error!'
return
print '[+] Apktool recompiled Target successfully!'
# Step6: 将重打包的app命名为Target.modified.apk,并提取重打包后的apk中的classes.dex文件,并压缩为TargetApk.zip文件
print '[+] Rename target app: \"Target.modified.apk\"'
shutil.copyfile('Target/dist/Target.apk', 'Target.modified.apk')
extracted_dir = un_zip('Target.modified.apk')
print '[+] Extracted classes.dex from Target.modifed.apk into TargetApk.zip'
shutil.copyfile(extracted_dir + '/classes.dex', 'classes.dex')
# 写入classes.dex
f = zipfile.ZipFile('TargetApk.zip', 'w', zipfile.ZIP_DEFLATED)
f.write('classes.dex')
f.close()
os.remove('classes.dex')
# Step7: 合并TuokeApk/bin/classes.dex和TargetApk.zip(加固),生成classes.dex
shutil.copyfile('TuokeApk/app/build/intermediates/transforms/dex/release/folders/1000/1f/main/classes.dex', 'tuoke.dex')
subprocess.Popen('java -jar JiaguApk.jar tuoke.dex TargetApk.zip', stdout=subprocess.PIPE).stdout.read()
# Step8: 将合并生成的新classes.dex文件与Target.modified.apk中的classes.dex替换
print '[+] Replace \"%s\" with \"classes.dex\"' % (extracted_dir + '/classes.dex', )
shutil.copyfile('classes.dex', extracted_dir + '/classes.dex')
# Step9: 复制TuokeApk/libs目录下的所有文件和文件夹到目标app中
print '[+] Copying TuokeApk/app/build/intermediates/ndk/release/lib/...'
if not os.path.exists(extracted_dir + '/lib/'):
os.mkdir(extracted_dir + '/lib/')
for item in os.listdir('TuokeApk/app/build/intermediates/ndk/release/lib/'):
if not os.path.exists(extracted_dir + '/lib/' + item):
shutil.copytree('TuokeApk/app/build/intermediates/ndk/release/lib/' + item, extracted_dir + '/lib/' + item)
else:
shutil.copyfile('TuokeApk/app/build/intermediates/ndk/release/lib/' + item + '/libhackcodejiagu.so', extracted_dir + '/lib/' + item + '/libhackcodejiagu.so')
else:
for item in os.listdir(extracted_dir + '/lib/'):
shutil.copyfile('TuokeApk/app/build/intermediates/ndk/release/lib/' + item + '/libhackcodejiagu.so', extracted_dir + '/lib/' + item + '/libhackcodejiagu.so')
# 破坏SO文件的ELF头部(删除 ELF header)
modify_ehdr_and_delete_shdr(extracted_dir)
# Step10: 将修改后的app重压缩成zip文件
print '[+] Compress %s folder into Target.modified.2.apk' % extracted_dir
zip_dir(extracted_dir, 'Target.modified.2.apk')
# Step11: 签名
print '[+] Signning...'
output_filename = input_filename[:input_filename.rfind('apk')] + 'signed.apk'
out = subprocess.Popen('java -jar sign/signapk.jar sign/testkey.x509.pem sign/testkey.pk8 Target.modified.2.apk ' + output_filename, stdout=subprocess.PIPE).stdout.read()
clean()
if __name__ == '__main__':
start = time.time()
main()
end = time.time()
print "Total time running %s seconds" %(str(end - start))