-
Notifications
You must be signed in to change notification settings - Fork 5
/
ndk_cmd.py
408 lines (333 loc) · 9.15 KB
/
ndk_cmd.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# -*- coding: UTF-8 -*-
'''
auther:
Loopher
source :
http://www.qingpingshan.com/rjbc/az/222774.html
http://zke1ev3n.me/2016/01/18/%E5%9F%BA%E4%BA%8ELLVM%E7%9A%84%E4%BB%A3%E7%A0%81%E6%B7%B7%E6%B7%86/
'''
import sys,os
import re
import fileinput
###
TOOL_TAG=r"NDK_TOOLCHAIN_VERSION := "
LOCAL_CFLAGS=r"LOCAL_CFLAGS := "
###
FLAG=r":="
##
APP_MK="Application.mk"
Androi_MK="Android.mk"
###
llvm_flag_dict={"fla":"-mllvm -fla",
"sub":"-mllvm -sub",
"bcf":"-mllvm -bcf"
}
##
llvm_percent_dict={
"preFLA":"-mllvm -perFLA=20",
"loop":"-mllvm -boguscf-loop=3",
"prob":"-mllvm -boguscf-prob=40"
}
llvm_flagpercent_dict={
"fla":"-mllvm -fla",
"sub":"-mllvm -sub",
"bcf":"-mllvm -bcf",
"preFLA":"-mllvm -perFLA=20",
"loop":"-mllvm -boguscf-loop=3",
"prob":"-mllvm -boguscf-prob=40"
}
all_obfuscator_dict={
"1":llvm_flag_dict,
"2":llvm_percent_dict,
"3":llvm_flagpercent_dict
}
'''
do main job for me
'''
class NDK(object):
def __init__(self,num="0"):
self.num=num
self.items=["1","2","3"]
self.flag=False
self.flag_app=False
self.flag_android=False
self.fileList=[]
self.root_path=""
self.app_list=[]
self.android_list=[]
def find_ndk_root(self):
print "check NDK_ROOT... "
ndk_root_path=os.getenv("LLVMr11c")
return ndk_root_path
def execut_cmd(self):
ndk_path = self.find_ndk_root()
if not ndk_path:
print "not found $NDK_ROOT,check it ~~"
return
#get item
#
# print "fileList ",self.fileList
# self.read_srcfile_tolist(self.fileList)
self.ready_files()# ready files
if self.num in self.items:
print "find num ,and use num for ndk-build ",self.num
self.changeContent()
self.execCmd()
#default
else:
#do change and execute cmd
print "use default item not add toolchain for building ~~~"
# self.do_modified_()
for file in self.fileList:
self.read_list_tosrcfile(file)
self.execCmd()
def read_list_tosrcfile(self,file):
try:
name = os.path.basename(file)
fp=open(file,"w+")
if name == APP_MK:
for e in self.app_list:
fp.write(e)
if name == Androi_MK:
for e in self.android_list:
fp.write(e)
fp.close()
print "write back to files ok"
self.flag=True
except Exception as e:
raise e
# record src content
def read_srcfile_tolist(self,fileList):
try:
for file in fileList:
name=os.path.basename(file)
fp=open(file,"r")
if name == APP_MK:
self.app_list= fp.readlines()
fp.close()
if name == Androi_MK:
self.android_list=fp.readlines()
fp.close()
#print "".join(str(e) for e in self.app_list)
print "**" * 10
#print "".join(str(e) for e in self.android_list)
except Exception as e:
raise e
#use self.num for modified Application.mk Android.mk
def changeContent(self):
self.change_file_content(self.fileList)
# self.flag=True
if self.flag_android and self.flag_app:
self.flag=True
def ready_files(self):
current_path = os.getcwd()
dirs=os.listdir(current_path)
for sub_dir in dirs:
if sub_dir == "jni":
print "--" * 10
print "find jni ... ready to change Application.mk,Android.mk file ~~"
print "--" * 10
self.fileList=self.scan_file(sub_dir,postfix=".mk")
#copy src file
self.read_srcfile_tolist(self.fileList)
break
else:
print "--" * 10
print "not found jni ,plz create jni and try again ~~~"
break
def execCmd(self):
if self.flag:
print "*****" * 10
os.system("ndk-build -B")
print "build project ok ~~~~~"
self.write_back_srcContent()
print "*****" * 10
else:
print "modified Application.mk and Android.mk was failed . "
def write_back_srcContent(self):
if self.fileList:
if self.app_list and self.android_list:
for file in self.fileList:
#print "write file "
fp=open(file,"w+")
name = os.path.basename(file)
#print "write file ",name
if name == APP_MK:
for line in self.app_list:
fp.writelines(line)
if name == Androi_MK:
for line in self.android_list:
fp.writelines(line)
fp.close()
print "write back !!! "
def scan_file(self,directory,prefix=None,postfix=None):
print "scanning file "
file_list=[]
for root,sub_dirs,files in os.walk(directory):
for special_file in files:
if postfix:
if special_file.endswith(postfix):
file_list.append(os.path.join(root,special_file))
elif prefix:
if special_file.startswith(prefix):
file_list.append(os.path.join(root,special_file))
else:
file_list.append(os.path.join(root,special_file))
current_path=os.path.join(root,special_file)
self.root_path=root
return file_list
def change_file_content(self,files):
fileName=""
if not files:
#print "file list is None"
return
for file in files:
fileName=os.path.basename(file)
#print "file name --- ", fileName
if fileName == APP_MK:
#print "App.mk "
# self.chang_App_text(file)
self.create_ApplicationMK(file)
#
if fileName == Androi_MK:
#print "Android.mk !!! "
# self.change_android_text(file)
mlist=self.create_mlist()
#print "get list ",mlist
if not mlist:
#print "command list is None ~"
return
self.create_AndroidMk(file,mlist)
def create_mlist(self):
tmp_list=[]
for key in all_obfuscator_dict.keys():
print "key",key
if key == self.num:
# print "dict key ",key
mdict=all_obfuscator_dict.get(key)
for mkey in mdict.keys():
tmp_list.append(mdict.get(mkey))
else:
continue
return tmp_list
#default TOOL_TAG :obfuscator3.6
def create_ApplicationMK(self,file,obftype="obfuscator3.6"):
mpath=""
toolchain=TOOL_TAG+ " "+obftype
try:
#print " pp ",os.path.join(file)
mpath= os.path.join(self.root_path,file)#os.getcwd()
#print "666 ",mpath
flag=0
newfile=mpath
srcfp=open(file,"r")
lines=srcfp.readlines()
# print "src android .mk -------------- ",lines
dstfp=open(os.path.join(newfile),"w+")
for line in lines:
# print "** ------ --- ---- ** "
if line.startswith(TOOL_TAG):
#print "find ************"
dstfp.writelines(toolchain)
flag=1
else:
# print "writing "
dstfp.writelines(line)
#print "flag ",flag
if not flag:
#print "add "
dstfp.writelines(toolchain)
#print "add "
srcfp.close()
dstfp.close()
print"file Application.mk create ok"
self.flag_app=True
except Exception as e:
print"create Application.mk failed exception", e
def create_AndroidMk(self,file,mlist):
include ="include $(BUILD_SHARED_LIBRARY)"
tmp_list=mlist
flag=False
strflag1=" ".join(str(e) for e in tmp_list)
# print "strflag1 ",strflag1
strflag2=LOCAL_CFLAGS+" ".join(str(e) for e in tmp_list)
# print "strflag2 ",strflag2
position=0
try:
mpath=os.path.join(self.root_path,file)
srcfile=open(file,"r")
lines=srcfile.readlines()
# print "andrid .mk ======== ",lines
newfile=mpath
dstfile=open(os.path.join(newfile),"w+")
for line in lines:
#add text into tail
# print"android line ",line
if line.startswith(LOCAL_CFLAGS):
print "write local_cflags "
line=line.strip("\n")
if line[-1] =="\\":
dstfile.writelines(line)
dstfile.writelines("\n"+strflag1)
dstfile.writelines("\n")
flag =True
else:
dstfile.writelines(line +" \\")
dstfile.writelines("\n"+strflag1)
dstfile.writelines("\n")
flag =True
# dstfile.writelines("\n")
#add text
elif not flag and line.startswith(include):
print "not found local cflags"
dstfile.writelines("\n")
dstfile.writelines(strflag2)
dstfile.writelines("\n")
dstfile.writelines(include)
dstfile.writelines("\n")
else:# normal write
dstfile.writelines(line)
srcfile.close()
dstfile.close()
print"create Android.mk ok "
self.flag_android=True
except Exception as e:
print "create Android.mk was failed ,exception ",e
def execut_cmd(ndk_path):
if ndk_path:
print " path is ok "
os.system(" ndk-build -v ")
else:
print " path not ok "
def find_ndk_root():
print "executing ~~"
ndk_root_path=os.getenv("LLVMr11c")
print "HOME =%s "%(os.getenv("HOME"))
return ndk_root_path
def main():
print "*" *10
current_path=os.getcwd()
print "current_path" ,current_path
path=find_ndk_root()
execut_cmd(path)
iterator_dir(current_path)
panduan()
if __name__ == '__main__':
# main()
print "--**--"
print "if you want to build obfuscator by ollvm ,"\
"Firs of all,you should set ndk to the PAHT variable as 'LLVMr11c =path/android-ndk-r11c ' "\
"and check the ndk path call 'ndk-build -v' "\
"you also need install Python 2.7.x or highset level, "\
" and keep the script with jni directory as same directory "\
"if everything is ok ,now you can run script as 'python ndk_cmd.py '"\
"please enter the number 1-3,the others number will"\
" be select to the default \n"\
"1 : -mllvm -fla -mllvm -sub -mllvm -bcf\n"\
"2 : -mllvm -perFLA=20 -mllvm -boguscf-loop=3 -mllvm -boguscf-prob=40 \n"\
"3 : both 1 and 2 \n"
print "--**--"
num = raw_input("please enter the number(1-3) :" )
mtype=str(num)
ndk=NDK(mtype)
ndk.execut_cmd()