Module androguard
[hide private]
[frames] | no frames]

Source Code for Module androguard

  1  # This file is part of Androguard. 
  2  # 
  3  # Copyright (C) 2010, Anthony Desnos <desnos at t0t0.org> 
  4  # All rights reserved. 
  5  # 
  6  # Androguard is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser General Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # Androguard is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of   
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU Lesser General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU Lesser General Public License 
 17  # along with Androguard.  If not, see <http://www.gnu.org/licenses/>. 
 18   
 19  import sys 
 20   
 21  PATH_INSTALL = "./"  
 22   
 23  sys.path.append(PATH_INSTALL + "/core") 
 24  sys.path.append(PATH_INSTALL + "/core/bytecodes") 
 25  sys.path.append(PATH_INSTALL + "/core/predicates") 
 26  sys.path.append(PATH_INSTALL + "/core/analysis") 
 27  sys.path.append(PATH_INSTALL + "/core/vm") 
 28   
 29  import bytecode, jvm, dvm, misc, analysis, opaque, vm 
 30   
31 -class BC :
32 - def __init__(self, bc) :
33 self.__bc = bc
34
35 - def get_bc(self) :
36 return self.__bc
37
38 - def _get(self, val, name) :
39 l = [] 40 r = getattr(self.__bc, val)(name) 41 for i in r : 42 l.append( i ) 43 return l
44
45 - def _gets(self, val) :
46 l = [] 47 r = getattr(self.__bc, val)() 48 for i in r : 49 l.append( i ) 50 return l
51
52 - def gets(self, name) :
53 return self._gets("get_" + name)
54
55 - def get(self, val, name) :
56 return self._get("get_" + val, name)
57
58 - def insert_direct_method(self, name, method) :
59 return self.__bc.insert_direct_method(name, method)
60
61 - def insert_craft_method(self, name, proto, codes) :
62 return self.__bc.insert_craft_method( name, proto, codes)
63
64 - def show(self) :
65 self.__bc.show()
66
67 - def save(self) :
68 return self.__bc.save()
69
70 - def __getattr__(self, value) :
71 return getattr(self.__bc, value)
72
73 -class Androguard :
74 """Androguard is the main object to abstract and manage differents formats 75 76 @param files : a list of filenames (filename must be terminated by .class or .dex) 77 """
78 - def __init__(self, files, config=None) :
79 self.__files = files 80 self.__bc = [] 81 self._analyze()
82
83 - def _analyze(self) :
84 for i in self.__files : 85 if ".class" in i : 86 bc = jvm.JVMFormat( open(i).read() ) 87 elif ".dex" in i : 88 bc = dvm.DalvikVMFormat( open(i).read() ) 89 else : 90 raise( "Unknown bytecode" ) 91 92 self.__bc.append( (i, BC( bc )) )
93
94 - def analysis(self, name) :
95 for file_name, bc in self.__bc : 96 r = getattr(bc, "get_method")(name) 97 for i in r : 98 analysis.JBCA( bc, i )
99
100 - def get_raw(self) :
101 """Return raw format of all file""" 102 l = [] 103 for _, bc in self.__bc : 104 l.append( bc._get_raw() ) 105 return l
106 107
108 - def get(self, name, val) :
109 if name == "file" : 110 for file_name, bc in self.__bc : 111 if file_name == val : 112 return bc 113 114 return None 115 else : 116 l = [] 117 for file_name, bc in self.__bc : 118 l.append( bc.get( name, val ) ) 119 120 return l
121
122 - def gets(self, name) :
123 l = [] 124 for file_name, bc in self.__bc : 125 l.append( bc.gets( name ) ) 126 127 return l
128
129 - def show(self) :
130 for _, bc in self.__bc : 131 bc.show()
132
133 -class AndroguardS :
134 """AndroguardS is the main object to abstract and manage differents formats but only per filename. In fact this class is just a wrapper to the main class Androguard 135 136 @param filename : the filename to use (filename must be terminated by .class or .dex) 137 """
138 - def __init__(self, filename) :
139 a = Androguard( [ filename ] ) 140 self.__a = a.get( "file", filename )
141
142 - def __getattr__(self, value) :
143 return getattr(self.__a, value)
144 145 VM_INT_AUTO = 0 146 VM_INT_BASIC_MATH_FORMULA = 1 147 VM_INT_BASIC_PRNG = 2
148 -class VM_int :
149 """VM_int is the main high level Virtual Machine object to protect a method by remplacing all integer contants 150 151 @param andro : an L{Androguard} / L{AndroguardS} object to have full access to the desired information 152 @param method_name : the name of the method to protect 153 @param vm_int_type : the type of the Virtual Machine 154 """
155 - def __init__(self, andro, method_name, vm_int_type) :
156 method = andro.get("method", method_name)[0] 157 code = method.get_code() 158 159 class_manager = andro.get_class_manager() 160 161 # LOOP until integers constant ! 162 iip = True 163 while iip == True : 164 idx = 0 165 end_iip = True 166 for bc in code.get_bc().get() : 167 if bc.get_name() == "bipush" : 168 169 if vm_int_type == VM_INT_BASIC_MATH_FORMULA : 170 vi = vm.VM_int_basic_math_formula( class_manager.get_this_class_name(), code, idx ) 171 elif vm_int_type == VM_INT_BASIC_PRNG : 172 vi = vm.VM_int_basic_prng( class_manager.get_this_class_name(), code, idx ) 173 else : 174 raise("oops") 175 176 for new_method in vi.get_methods() : 177 andro.insert_direct_method( new_method.get_name(), new_method ) 178 method.show() 179 vi.patch_code() 180 181 end_iip = False 182 183 break 184 idx += 1 185 186 # We have patch zero integers, it's the end my friend ! 187 if end_iip == True : 188 iip = False
189