forked from yeling/ApkShield
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shield.py
92 lines (77 loc) · 2 KB
/
Shield.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
import os
import sys
import xml.dom.minidom
import codecs
import shutil
import zipfile
def decypt(path):
decypt = 'java -jar apktool.jar d -f -o out ' + path
print decypt
os.system(decypt)
def encypt(path):
encypt = 'java -jar apktool.jar b out -o .\out\shield.apk';
print encypt
os.system(encypt)
def sign(path):
sign = 'java -jar signapk.jar platform.x509.pem platform.pk8 .\out\shield.apk' + ' shield' + path
print sign
os.system(sign)
def install(path):
ins = 'adb install -r '+' shield' + path
print ins
os.system(ins)
#解析xml文件,增加application
def parseXml():
path = '.\out\AndroidManifest.xml'
pathout = '.\out\AndroidManifest.xml'
tree = xml.dom.minidom.parse(path)
app = tree.getElementsByTagName('application')[0]
applicationName = app.getAttribute('android:name')
if len(applicationName) > 0:
print "Already has application"
return
else:
print "Normal"
print app
app.setAttribute('android:name','com.apk.shield.ShieldApplication')
f=file(pathout, 'w')
writer = codecs.lookup('utf-8')[3](f)
tree.writexml(writer, encoding='utf-8')
writer.close()
def copyFile(src):
shieldsrc = '.\\ApkShield\\smali\\com\\apk\\shield\\'
shielddst = '.\\out\\smali\\com\\apk\\shield\\'
shutil.rmtree('.\\out\\smali\\')
shutil.copytree(shieldsrc,shielddst)
#dstapk = '.\\out\\assets\\ex07.apk'
#shutil.copy(src,dstapk)
#将apk中dex解析出来并且拷贝到对应的目录
def extractDex(src):
zipFile = zipfile.ZipFile(src)
dstpath = '.\\out\\assets\\'
zipFile.extract('classes.dex',dstpath)
shutil.copy(dstpath + 'classes.dex',dstpath + 'ex07.apk')
#===================================================================================
print 'Begin decypt'
print 'Begin Shield'
args = ""
for i in range(1,len(sys.argv)):
args += sys.argv[i]
print args
src = args
#解开apk
decypt(args)
#deal xml file
parseXml()
#
copyFile(src)
#
extractDex(src)
#apk打包
encypt(args)
#apk签名
sign(args)
shutil.rmtree('.\\out')
#install(src)
print 'End decypt'
print 'End Shield'