-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
148 lines (128 loc) · 4.58 KB
/
run.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
#!/usr/bin/python
import os
import sys
import subprocess
import getopt
from utils.PermissionInfo import PermissionInfo
from utils.PermissionParser import PermissionParser
from utils.ProtectionLevelParser import ProtectionLevelParser
from configs.Constant import Constant as configs
#DBUG config
DEBUG = False
DEFAULT_SDK = configs.getDefSdkLv()
def usage():
print 'USAGE:\n'
print sys.argv[0]+' [-h | --help]\n'
print sys.argv[0]+' [-d] [-l] [-g] [-t <target-sdk>] -i <path-to-apk>'
print '\t-d: list all the protection level decription'
print '\t-l: list all permissions'
print '\t-g: group the use-permission by protection level'
print '\t-t <target-sdk>: set the codebase target-sdk'
def getPermsDict(sdk_lv):
xml= configs.getRes(sdk_lv)
if xml is None:
xml = base.getRes(DEFAULT_SDK)
parser = PermissionParser(xml)
return parser.getPermList()
def printUnknownPerm(perm):
print " Permission"
print " Name: "+perm
print " Group: unknown"
print " Protection Level: unknown"
def main(argv):
targetApk = ''
sdkLevel = DEFAULT_SDK #defaul use Android N (7.1.1)
err = False;
groupByLevel = False
listLevelDesc = False
listAllPerms = False
#getopt Ref: https://docs.python.org/2/library/getopt.html
try:
opts, args = getopt.getopt(sys.argv[1:],'dlghi:t:', ["help"] )
for opt, arg in opts:
if (DEBUG == True):
print opt+": "+arg
if (opt == '-t'):
sdkLevel = arg
elif (opt in ("-h", "--help")):
usage()
sys.exit()
elif (opt == '-g'):
groupByLevel = True
elif (opt == '-d'):
listLevelDesc = True
elif (opt == '-l'):
listAllPerms = True
elif (opt == '-i'):
targetApk = arg
else:
assert False, "unhandled option"
permParser = ProtectionLevelParser()
levels = permParser.parsingProtectionLevels()
if (listLevelDesc == True):
for lv in levels:
print lv+':'
print '\tValue: '+permParser.getProtectionLevelValue(lv)
print '\tDescription: '+permParser.getProtectionLevelDesc(lv)
print '\n'
permissionsDict = getPermsDict(sdkLevel)
if (listAllPerms == True):
for perm in permissionsDict.values():
perm.dump()
if (targetApk is ''):
print "No target APK file path!"
sys.exit(2)
out = subprocess.check_output("./utils/unpackApk.sh "+targetApk, shell=True)
usePerms = {}
for perm in out.split('\n'):
if (DEBUG == True):
print perm
permInfo = permissionsDict.get(perm)
usePerms[perm] = permInfo
print '\n============================================================='
print 'Permission Status'
print 'TARGET APK:'+targetApk
print '=============================================================\n'
if (groupByLevel != True):
#output use-permissions list
for perm in usePerms.keys():
info = usePerms[perm]
if info is not None:
info.dump()
else:
printUnknownPerm(perm)
else:
permsGroupByLv = {}
unknownPerms = []
for lv in levels:
perms = []
for permName in usePerms.keys():
permInfo = usePerms[permName]
if permInfo is None:
if permName is not '' and permName not in unknownPerms:
unknownPerms.append(permName)
elif lv in permInfo.protectionLevel:
perms.append(permName)
permsGroupByLv[lv] = perms
permsGroupByLv['unknown'] = unknownPerms
#result output
for level, perms in permsGroupByLv.items():
if len(perms) is 0:
continue
print "\n* "+level+" permissions:"
for perm in perms:
if (level is 'unknown'):
printUnknownPerm(perm)
else:
usePerms[perm].dump()
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
if __name__ == '__main__':
if (DEBUG == True):
print sys.argv
if (len(sys.argv) < 2):
usage()
else:
main(sys.argv)