File: androapi_format.py

package info (click to toggle)
androguard 2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 30,620 kB
  • sloc: python: 173,336; ansic: 8,884; java: 1,702; cpp: 1,372; xml: 308; makefile: 243
file content (140 lines) | stat: -rwxr-xr-x 4,640 bytes parent folder | download
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
#!/usr/bin/env python

# This file is part of Androguard.
#
# Copyright (C) 2010, Anthony Desnos <desnos at t0t0.org>
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os, sys, re, string

from dvm_permissions_unformatted import PERMISSIONS
from permissions_by_hand import PERMISSIONS_BY_HAND

BASIC_TYPES = {
   "byte" : "B",
   "char" : "C",
   "double" : "D",
   "float" : "F",
   "int" : "I",
   "long" : "J",
   "short" : "S",
   "boolean" : "B",
   "void" : "V",
}

ADVANCED_TYPES = {
    "String" : "Ljava/lang/String;",
    "List" : "Ljava/util/List;",
    "AccountManagerFuture" : "Landroid/accounts/AccountManagerFuture;",
    "CellLocation" : "Landroid/telephony/CellLocation;",
    "Uri" : "Landroid/net/Uri;",
    "Cursor" : "Landroid/database/Cursor;",
    "Set" : "Ljava/util/Set;",
    "BluetoothServerSocket" : "Landroid/bluetooth/BluetoothServerSocket;",
    "BluetoothSocket" : "Landroid/bluetooth/BluetoothSocket;",
    "DownloadManager.Request" : "Landroid/app/DownloadManager/Request;",
    "PendingIntent" : "Landroid/app/PendingIntent;",
    "SmsManager" : "Landroid/telephony/SmsManager;",
    "Bitmap" : "Landroid/graphics/Bitmap;",
    "IBinder" : "Landroid/os/IBinder;",
}

def translateDescParams( desc_params ):
    desc_params = desc_params.replace(" ", "")
    buff = ""

    for elem in desc_params.split(","):
        if elem != "":
            tab = ""
            if "[" in elem:
                tab = "[" * string.count(elem, "[")

                elem = elem[ : tab.find("[") - 2 ]

            if elem not in BASIC_TYPES:
                if elem in ADVANCED_TYPES:
                    buff += tab + ADVANCED_TYPES[ elem ] + " "
                else:
                    buff += tab + "L" + elem.replace(".", "/") + "; "
            else:
                buff += tab + BASIC_TYPES[ elem ] + " "

    buff = buff[:-1]
    return buff

def translateDescReturn( desc_return ):
    buff = ""
    for elem in desc_return.split(" "):
        tab = ""
        if "[" in elem:
            tab = "[" * string.count(elem, "[")
            elem = elem[ : tab.find("[") - 2 ]

        if elem in BASIC_TYPES:
            buff += tab + BASIC_TYPES[ elem ] + " "
        else:
            if elem in ADVANCED_TYPES:
                buff += tab + ADVANCED_TYPES[ elem ] + " "
            else:
                if "." in elem:
                    buff += tab + "L" + elem.replace(".", "/") + "; "

    buff = buff[:-1]
    return buff

def translateToCLASS( desc_params, desc_return ):
    print desc_params, desc_return,

    buff = "(" + translateDescParams( desc_params[ desc_params.find("(") + 1 : -1 ] ) + ")" + translateDescReturn( desc_return )
    print "----->", buff

    return [ desc_params[ : desc_params.find("(") ], buff ]

def translateToCLASS2( constant_name, desc_return ):
    return [ constant_name, translateDescReturn( desc_return ) ]

PERMISSIONS.update( PERMISSIONS_BY_HAND )

for perm in PERMISSIONS:
    for package in PERMISSIONS[perm]:
        for element in PERMISSIONS[perm][package]:
            if element[0] == "F":
                element.extend( translateToCLASS( element[1], element[2] ) )
            elif element[0] == "C":
                element.extend( translateToCLASS2( element[1], element[2] ) )

with open("./core/bytecodes/api_permissions.py", "w") as fd:
    fd.write("DVM_PERMISSIONS_BY_PERMISSION = {\n")

    for perm in PERMISSIONS:
        fd.write("\"%s\" : {\n" % perm)

        for package in PERMISSIONS[perm]:
            fd.write("\t\"L%s;\" : [\n" % package.replace(".", "/"))

            for element in PERMISSIONS[perm][package]:
                fd.write("\t\t(\"%s\", \"%s\", \"%s\"),\n" % (element[0], element[-2], element[-1]) )

            fd.write("\t],\n")
        fd.write("},\n")
    fd.write("}\n\n")


    fd.write("DVM_PERMISSIONS_BY_ELEMENT = { \n")
    for perm in PERMISSIONS:
        for package in PERMISSIONS[perm]:
            for element in PERMISSIONS[perm][package]:
                fd.write("\t\"L%s;-%s-%s\" : \"%s\",\n" % (package.replace(".", "/"), element[-2], element[-1], perm))
    fd.write("}\n")