-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_mappings_converter.py
164 lines (126 loc) · 4.83 KB
/
api_mappings_converter.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
import os
import sqlite3
axplorer = 'mappings/axplorer/permissions/'
pscout = 'mappings/PScout/permissions/'
PRIMITIVE_TYPES = {
'void': 'V',
'boolean': 'Z',
'byte': 'B',
'short': 'S',
'char': 'C',
'int': 'I',
'long': 'J',
'float': 'F',
'double': 'D'
}
# Converts Java types to Smali
def to_smali_type(java_type):
ret = None
narrays = 0
# check if it's an array
if java_type.endswith('[]'):
narrays = java_type.count('[]')
java_type = java_type.rstrip('[]')
# if it's a class
if '.' in java_type:
smali_type = 'L' + java_type.replace('.', '/') + ';'
# if it's a simple type
elif java_type in PRIMITIVE_TYPES:
smali_type = PRIMITIVE_TYPES[java_type]
# if it's an array - add [ before type
ret = ('[' * narrays) + smali_type
return ret
def parse_pscout_line(line):
ret = {}
if line.startswith('Permission'):
ret['permission'] = line.strip().split(':')[-1]
elif line.startswith('<'):
strs = line.split(' ')
# class name
class_name = to_smali_type(strs[0].strip('<:'))
if not class_name.startswith('Landroid'):
return ret
# return type
ret_type = to_smali_type(strs[1])
# method := name + args
method = strs[2].rstrip('>')
method_name, args = method.rstrip(')').split('(')
arg_list = []
if len(args) != 0:
for arg in args.split(','):
smali_arg = to_smali_type(arg)
arg_list.append(smali_arg)
smali_api = class_name + '->' + method_name +\
'(' + ' '.join(arg_list) + ')' + ret_type
ret['api'] = smali_api
return ret
def parse_axplorer_line(line):
api, permissions = map(str.strip, line.split('::'))
strs = api.split('(')
args, ret_type = strs[1].split(')')
# return type + axplorer bug fix
if ret_type.endswith('[]'):
ret_type = '[' * ret_type.count('[]') + ret_type.rstrip('[]')
else:
ret_type = to_smali_type(ret_type)
# arguments
arg_list = []
if len(args) != 0:
for arg in args.split(','):
# argument type conversion + axplorer bug fix
if arg.startswith('['):
smali_arg = '[' * arg.count('[') + to_smali_type(arg.lstrip('['))
else:
smali_arg = to_smali_type(arg)
arg_list.append(smali_arg)
# class and method name
strs = strs[0].split('.')
method_name = strs[-1]
class_name = to_smali_type('.'.join(strs[:-1]))
smali_api = class_name + '->' + method_name +\
'(' + ' '.join(arg_list) + ')' + ret_type
ret = {'permissions': permissions.split(', '), 'api': smali_api}
return ret
def create_table_with_pk(cursor, table_name, header, pk):
query = 'CREATE TABLE IF NOT EXISTS {} ({}, PRIMARY KEY({}))'.format(
table_name, ','.join(header), ','.join(pk))
cursor.execute(query)
def insert_row(cursor, table_name, values):
query = 'INSERT INTO {} VALUES ({})'.format(table_name, ','.join(values))
cursor.execute(query)
def pscout_to_sqlite(sqlite3_cursor):
for pscout_api in os.listdir(pscout):
pscout_mappings = os.path.join(pscout, pscout_api, 'allmappings')
table_name = '_'.join(['PSCOUT', pscout_api])
create_table_with_pk(sqlite3_cursor, table_name, ['api', 'permission'], ['api', 'permission'])
with open(pscout_mappings) as f:
current_perm = None
for line in f:
ret = parse_pscout_line(line)
if 'permission' in ret:
current_perm = ret['permission']
elif 'api' in ret:
api = ret['api']
insert_row(sqlite3_cursor, table_name, ['\'' + api + '\'', '\'' + current_perm + '\''])
print('PScout: %s is done' % pscout_api)
def axplorer_to_sqlite(sqlite3_cursor):
for axplorer_api in os.listdir(axplorer):
axplorer_mappings = os.path.join(axplorer, axplorer_api,
'sdk-map-' + axplorer_api.split('_')[-1] + '.txt')
table_name = '_'.join(['AXPLORER', axplorer_api])
create_table_with_pk(sqlite3_cursor, table_name, ['api', 'permission'], ['api', 'permission'])
with open(axplorer_mappings) as f:
for line in f:
ret = parse_axplorer_line(line)
for perm in ret['permissions']:
insert_row(sqlite3_cursor, table_name, ['\'' + ret['api'] + '\'', '\'' + perm + '\''])
print('axplorer: %s is done' % axplorer_api)
if __name__ == '__main__':
with sqlite3.connect('mapping.db') as conn:
cursor = conn.cursor()
# PScout
pscout_to_sqlite(cursor)
conn.commit()
# axplorer
axplorer_to_sqlite(cursor)
conn.commit()