My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /androlyze.py
5a30a50f74f9 vs. a954a482ac43 Compare: vs.  Format:
Revision a954a482ac43
Go to: 
Project members, sign in to write a code review
/androlyze.py   5a30a50f74f9 /androlyze.py   a954a482ac43
1 #!/usr/bin/env python2.6 1 #!/usr/bin/env python2.6
2 2
3 # This file is part of Androguard. 3 # This file is part of Androguard.
4 # 4 #
5 # Copyright (C) 2010, Anthony Desnos <desnos at t0t0.org> 5 # Copyright (C) 2010, Anthony Desnos <desnos at t0t0.org>
6 # All rights reserved. 6 # All rights reserved.
7 # 7 #
8 # Androguard is free software: you can redistribute it and/or modify 8 # Androguard is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Lesser General Public License as published by 9 # it under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or 10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version. 11 # (at your option) any later version.
12 # 12 #
13 # Androguard is distributed in the hope that it will be useful, 13 # Androguard is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Lesser General Public License for more details. 16 # GNU Lesser General Public License for more details.
17 # 17 #
18 # You should have received a copy of the GNU Lesser General Public License 18 # You should have received a copy of the GNU Lesser General Public License
19 # along with Androguard. If not, see <http://www.gnu.org/licenses/>. 19 # along with Androguard. If not, see <http://www.gnu.org/licenses/>.
20 20
21 import sys, os, cmd, threading, code, re 21 import sys, os, cmd, threading, code, re
22 22
23 from optparse import OptionParser 23 from optparse import OptionParser
24 24
25 from androguard import * 25 from androguard import *
26 from bytecode import * 26 from bytecode import *
27 from jvm import * 27 from jvm import *
28 from dvm import * 28 from dvm import *
29 from apk import * 29 from apk import *
30 from analysis import * 30 from analysis import *
31 from diff import * 31 from diff import *
32 32
33 from misc import * 33 from misc import *
34 34
35 import IPython.ipapi 35 import IPython.ipapi
36 from IPython.Shell import IPShellEmbed 36 from IPython.Shell import IPShellEmbed
37 37
38 from cPickle import dumps, loads 38 from cPickle import dumps, loads
39 39
40 option_0 = { 'name' : ('-i', '--input'), 'help' : 'file : use this filename', 'nargs' : 1 } 40 option_0 = { 'name' : ('-i', '--input'), 'help' : 'file : use this filename', 'nargs' : 1 }
41 41
42 option_1 = { 'name' : ('-d', '--display'), 'help' : 'display the file in human readable format', 'action' : 'count' } 42 option_1 = { 'name' : ('-d', '--display'), 'help' : 'display the file in human readable format', 'action' : 'count' }
43 43
44 option_2 = { 'name' : ('-m', '--method'), 'help' : 'display method(s) respect with a regexp', 'nargs' : 1 } 44 option_2 = { 'name' : ('-m', '--method'), 'help' : 'display method(s) respect with a regexp', 'nargs' : 1 }
45 45
46 option_3 = { 'name' : ('-f', '--field'), 'help' : 'display field(s) respect with a regexp', 'nargs' : 1 } 46 option_3 = { 'name' : ('-f', '--field'), 'help' : 'display field(s) respect with a regexp', 'nargs' : 1 }
47 47
48 option_4 = { 'name' : ('-s', '--shell'), 'help' : 'open a shell to interact more easily with objects', 'action' : 'count' } 48 option_4 = { 'name' : ('-s', '--shell'), 'help' : 'open a shell to interact more easily with objects', 'action' : 'count' }
49 49
50 option_5 = { 'name' : ('-v', '--version'), 'help' : 'version of the API', 'action' : 'count' } 50 option_5 = { 'name' : ('-v', '--version'), 'help' : 'version of the API', 'action' : 'count' }
51 51
52 option_6 = { 'name' : ('-p', '--pretty'), 'help' : 'pretty print !', 'action' : 'count' } 52 option_6 = { 'name' : ('-p', '--pretty'), 'help' : 'pretty print !', 'action' : 'count' }
53 53
54 option_7 = { 'name' : ('-t', '--type_pretty'), 'help' : 'set the type of pretty print (0, 1) !', 'nargs' : 1 } 54 option_7 = { 'name' : ('-t', '--type_pretty'), 'help' : 'set the type of pretty print (0, 1) !', 'nargs' : 1 }
55 55
56 option_8 = { 'name' : ('-x', '--xpermissions'), 'help' : 'show paths of permissions', 'action' : 'count' } 56 option_8 = { 'name' : ('-x', '--xpermissions'), 'help' : 'show paths of permissions', 'action' : 'count' }
57 57
58 options = [option_0, option_1, option_2, option_3, option_4, option_5, option_6, option_7, option_8] 58 options = [option_0, option_1, option_2, option_3, option_4, option_5, option_6, option_7, option_8]
59 59
60 def save_session(l, filename) : 60 def save_session(l, filename) :
61 fd = open(filename, "w") 61 fd = open(filename, "w")
62 fd.write( dumps(l, -1) ) 62 fd.write( dumps(l, -1) )
63 fd.close() 63 fd.close()
64 64
65 def load_session(filename) : 65 def load_session(filename) :
66 return loads( open(filename, "r").read() ) 66 return loads( open(filename, "r").read() )
67 67
68 def interact() : 68 def interact() :
69 ipshell = IPShellEmbed(banner="Androlyze version %s" % VERSION) 69 ipshell = IPShellEmbed(banner="Androlyze version %s" % VERSION)
70 ipshell() 70 ipshell()
71 71
72 def AnalyzeAPK(filename, raw=False) :
73 a = APK(filename, raw)
74
75 d = DalvikVMFormat( a.get_dex() )
76 dx = VMAnalysis( d )
77
78 ExportVMToPython( d )
79
80 return a, d, dx
81
72 def main(options, arguments) : 82 def main(options, arguments) :
73 if options.shell != None : 83 if options.shell != None :
74 interact() 84 interact()
75 85
76 elif options.input != None : 86 elif options.input != None :
77 _a = AndroguardS( options.input ) 87 _a = AndroguardS( options.input )
78 88
79 if options.type_pretty != None : 89 if options.type_pretty != None :
80 bytecode.set_pretty_show( int( options.type_pretty ) ) 90 bytecode.set_pretty_show( int( options.type_pretty ) )
81 91
82 if options.display != None : 92 if options.display != None :
83 if options.pretty != None : 93 if options.pretty != None :
84 _a.ianalyze() 94 _a.ianalyze()
85 _a.pretty_show() 95 _a.pretty_show()
86 else : 96 else :
87 _a.show() 97 _a.show()
88 98
89 elif options.method != None : 99 elif options.method != None :
90 for method in _a.get("method", options.method) : 100 for method in _a.get("method", options.method) :
91 if options.pretty != None : 101 if options.pretty != None :
92 _a.ianalyze() 102 _a.ianalyze()
93 method.pretty_show( _a.get_analysis() ) 103 method.pretty_show( _a.get_analysis() )
94 else : 104 else :
95 method.show() 105 method.show()
96 106
97 elif options.field != None : 107 elif options.field != None :
98 for field in _a.get("field", options.field) : 108 for field in _a.get("field", options.field) :
99 field.show() 109 field.show()
100 110
101 elif options.xpermissions != None : 111 elif options.xpermissions != None :
102 _a.ianalyze() 112 _a.ianalyze()
103 perms_access = _a.get_analysis().tainted_packages.get_permissions( [] ) 113 perms_access = _a.get_analysis().tainted_packages.get_permissions( [] )
104 for perm in perms_access : 114 for perm in perms_access :
105 print "PERM : ", perm 115 print "PERM : ", perm
106 for path in perms_access[ perm ] : 116 for path in perms_access[ perm ] :
107 print "\t%s %s %s (@%s-0x%x) ---> %s %s %s" % ( path.get_method().get_class_name(), path.get_method().get_name(), path.get_method().get_descriptor(), \ 117 print "\t%s %s %s (@%s-0x%x) ---> %s %s %s" % ( path.get_method().get_class_name(), path.get_method().get_name(), path.get_method().get_descriptor(), \
108 path.get_bb().get_name(), path.get_bb().start + path.get_idx(), \ 118 path.get_bb().get_name(), path.get_bb().start + path.get_idx(), \
109 path.get_class_name(), path.get_name(), path.get_descriptor()) 119 path.get_class_name(), path.get_name(), path.get_descriptor())
110 120
111 elif options.version != None : 121 elif options.version != None :
112 print "Androlyze version %s" % VERSION 122 print "Androlyze version %s" % VERSION
113 123
114 if __name__ == "__main__" : 124 if __name__ == "__main__" :
115 parser = OptionParser() 125 parser = OptionParser()
116 for option in options : 126 for option in options :
117 param = option['name'] 127 param = option['name']
118 del option['name'] 128 del option['name']
119 parser.add_option(*param, **option) 129 parser.add_option(*param, **option)
120 130
121 options, arguments = parser.parse_args() 131 options, arguments = parser.parse_args()
122 sys.argv[:] = arguments 132 sys.argv[:] = arguments
123 main(options, arguments) 133 main(options, arguments)
Powered by Google Project Hosting