My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /androdd.py
a7887d5d8446 vs. a954a482ac43 Compare: vs.  Format:
Revision a954a482ac43
Go to: 
Project members, sign in to write a code review
/androdd.py   a7887d5d8446 /androdd.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, re 21 import sys, os, cmd, threading, re
22 22
23 from optparse import OptionParser 23 from optparse import OptionParser
24 24
25 import androguard, analysis, misc 25 import androguard, analysis, misc
26 from bytecode import method2dot, method2format 26 from bytecode import method2dot, method2format
27 27
28 option_0 = { 'name' : ('-i', '--input'), 'help' : 'file : use this filename', 'nargs' : 1 } 28 option_0 = { 'name' : ('-i', '--input'), 'help' : 'file : use this filename', 'nargs' : 1 }
29 option_1 = { 'name' : ('-o', '--output'), 'help' : 'base directory to output all files', 'nargs' : 1 } 29 option_1 = { 'name' : ('-o', '--output'), 'help' : 'base directory to output all files', 'nargs' : 1 }
30 30
31 option_2 = { 'name' : ('-d', '--dot'), 'help' : 'write the method in dot format', 'action' : 'count' } 31 option_2 = { 'name' : ('-d', '--dot'), 'help' : 'write the method in dot format', 'action' : 'count' }
32 option_3 = { 'name' : ('-f', '--format'), 'help' : 'write the method in specific format (png, ...)', 'nargs' : 1 } 32 option_3 = { 'name' : ('-f', '--format'), 'help' : 'write the method in specific format (png, ...)', 'nargs' : 1 }
33 33
34 option_4 = { 'name' : ('-v', '--version'), 'help' : 'version of the API', 'action' : 'count' } 34 option_4 = { 'name' : ('-v', '--version'), 'help' : 'version of the API', 'action' : 'count' }
35 35
36 options = [option_0, option_1, option_2, option_3, option_4] 36 options = [option_0, option_1, option_2, option_3, option_4]
37 37
38 def valid_class_name( class_name ): 38 def valid_class_name( class_name ):
39 if class_name[-1] == ";" : 39 if class_name[-1] == ";" :
40 return class_name[1:-1] 40 return class_name[1:-1]
41 return class_name 41 return class_name
42 42
43 def create_directory( class_name, output ) : 43 def create_directory( class_name, output ) :
44 output_name = output 44 output_name = output
45 if output_name[-1] != "/" : 45 if output_name[-1] != "/" :
46 output_name = output_name + "/" 46 output_name = output_name + "/"
47 47
48 try : 48 try :
49 os.makedirs( output_name + class_name ) 49 os.makedirs( output_name + class_name )
50 except OSError : 50 except OSError :
51 pass 51 pass
52 52
53 def create_directories( a, output ) : 53 def create_directories( a, output ) :
54 for vm in a.get_vms() : 54 for vm in a.get_vms() :
55 for class_name in vm.get_classes_names() : 55 for class_name in vm.get_classes_names() :
56 create_directory( valid_class_name( class_name ), output ) 56 create_directory( valid_class_name( class_name ), output )
57 57
58 def export_apps_to_format( a, output, dot=None, _format=None ) : 58 def export_apps_to_format( a, output, dot=None, _format=None ) :
59 output_name = output 59 output_name = output
60 if output_name[-1] != "/" : 60 if output_name[-1] != "/" :
61 output_name = output_name + "/" 61 output_name = output_name + "/"
62 62
63 for vm in a.get_vms() : 63 for vm in a.get_vms() :
64 x = analysis.VM_BCA( vm ) 64 x = analysis.VMAnalysis( vm )
65 for method in vm.get_methods() : 65 for method in vm.get_methods() :
66 filename = output_name + valid_class_name( method.get_class_name() ) 66 filename = output_name + valid_class_name( method.get_class_name() )
67 if filename[-1] != "/" : 67 if filename[-1] != "/" :
68 filename = filename + "/" 68 filename = filename + "/"
69 69
70 descriptor = method.get_descriptor() 70 descriptor = method.get_descriptor()
71 descriptor = descriptor.replace(";", "") 71 descriptor = descriptor.replace(";", "")
72 descriptor = descriptor.replace(" ", "") 72 descriptor = descriptor.replace(" ", "")
73 descriptor = descriptor.replace("(", "-") 73 descriptor = descriptor.replace("(", "-")
74 descriptor = descriptor.replace(")", "-") 74 descriptor = descriptor.replace(")", "-")
75 descriptor = descriptor.replace("/", "_") 75 descriptor = descriptor.replace("/", "_")
76 76
77 filename = filename + method.get_name() + descriptor 77 filename = filename + method.get_name() + descriptor
78 78
79 79
80 buff = method2dot( x.get_method( method ) ) 80 buff = method2dot( x.get_method( method ) )
81 81
82 if dot : 82 if dot :
83 fd = open( filename + ".dot", "w") 83 fd = open( filename + ".dot", "w")
84 fd.write( buff ) 84 fd.write( buff )
85 fd.close() 85 fd.close()
86 86
87 if _format : 87 if _format :
88 method2format( filename + "." + _format, _format, raw = buff ) 88 method2format( filename + "." + _format, _format, raw = buff )
89 89
90 def main(options, arguments) : 90 def main(options, arguments) :
91 if options.input != None and options.output != None : 91 if options.input != None and options.output != None :
92 a = androguard.Androguard( [ options.input ] ) 92 a = androguard.Androguard( [ options.input ] )
93 93
94 create_directories( a, options.output ) 94 create_directories( a, options.output )
95 95
96 if options.dot != None or options.format != None : 96 if options.dot != None or options.format != None :
97 export_apps_to_format( a, options.output, options.dot, options.format ) 97 export_apps_to_format( a, options.output, options.dot, options.format )
98 98
99 elif options.version != None : 99 elif options.version != None :
100 print "Androdd version %s" % misc.VERSION 100 print "Androdd version %s" % misc.VERSION
101 101
102 if __name__ == "__main__" : 102 if __name__ == "__main__" :
103 parser = OptionParser() 103 parser = OptionParser()
104 for option in options : 104 for option in options :
105 param = option['name'] 105 param = option['name']
106 del option['name'] 106 del option['name']
107 parser.add_option(*param, **option) 107 parser.add_option(*param, **option)
108 108
109 options, arguments = parser.parse_args() 109 options, arguments = parser.parse_args()
110 sys.argv[:] = arguments 110 sys.argv[:] = arguments
111 main(options, arguments) 111 main(options, arguments)
Powered by Google Project Hosting