def _analyze(self) : for i in self.__files : #print "processing ", i if ".class" in i : bc = jvm.JVMFormat( self.__orig_raw[ i ] ) elif ".jar" in i : x = jvm.JAR( i ) bc = x.get_classes() elif ".dex" in i : bc = dvm.DalvikVMFormat( self.__orig_raw[ i ] ) elif ".apk" in i : x = apk.APK( i ) bc = dvm.DalvikVMFormat( x.get_dex() ) else : ret_type = androconf.is_android( i ) if ret_type == "APK" : x = apk.APK( i ) bc = dvm.DalvikVMFormat( x.get_dex() ) elif ret_type == "DEX" : bc = dvm.DalvikVMFormat( open(i, "rb").read() ) else : raise( "Unknown bytecode" ) if isinstance(bc, list) : for j in bc : self.__bc.append( (j[0], BC( jvm.JVMFormat(j[1]) ) ) ) else : self.__bc.append( (i, BC( bc )) )
def __init__(self, adp): self.__adp = adp for i in self.__adp.getFilesBuffer(): try: print "0x%x :" % (i[0]) j = jvm.JVMFormat(i[1]) for method in j.get_methods(): print "\t -->", method.get_class_name(), method.get_name( ), method.get_descriptor() # if (method.get_class_name() == "Test2" and method.get_name() == "main") : # print "patch" # code = method.get_code() #code.remplace_at( 51, [ "bipush", 20 ] ) # code.show() # print "\t\t-> %x" % (len(j.save())) # self.__adp.write( i[0], j.save() ) except Exception, e: print e
#!/usr/bin/env python import sys PATH_INSTALL = "./" sys.path.append(PATH_INSTALL + "/core") sys.path.append(PATH_INSTALL + "/core/bytecodes") sys.path.append(PATH_INSTALL + "/core/analysis") import jvm, analysis TEST = "./examples/java/test/orig/Test1.class" j = jvm.JVMFormat(open(TEST).read()) x = analysis.VMAnalysis(j) # SHOW CLASS (verbose and pretty) #j.pretty_show( x ) # SHOW METHODS for i in j.get_methods(): print i i.pretty_show(x)
#!/usr/bin/env python import sys, random, string PATH_INSTALL = "./" sys.path.append(PATH_INSTALL + "/core") sys.path.append(PATH_INSTALL + "/core/bytecodes") import jvm TEST = "./examples/java/test/orig/Test1.class" TEST_REF = "./examples/java/Hello.class" TEST_OUTPUT = "./examples/java/test/new/Test1.class" j = jvm.JVMFormat(open(TEST).read()) j2 = jvm.JVMFormat(open(TEST_REF).read()) # Insert a craft method :) j.insert_craft_method("toto", ["ACC_PUBLIC", "[B", "[B"], [["aconst_null"], ["areturn"]]) # Insert a method with no dependances methods j.insert_direct_method("toto2", j2.get_method("test3")[0]) # SAVE CLASS fd = open(TEST_OUTPUT, "w") fd.write(j.save()) fd.close()
def _create_methods(self): fd = open("./VM.java", "w") fd.write("class VM {\n") fd.write("public ") if self.__type_return == "": fd.write("void ") else: fd.write("%s " % self.__type_return) fd.write("%s() {\n" % self.__vm_name) self.__VAR_REG = "REG" self.__VAR_IREG = "IREG" self.__VAR_IDX = "idx" self.__VAR_C_IDX = "c_idx" self.__VAR_BC = "BC" fd.write("int[] %s = new int[%d];\n" % (self.__VAR_REG, len(self.__RR))) fd.write("int[] %s = {" % self.__VAR_IREG) buff = "" for i in self.__RR: buff += "%d,%s," % (self.__RR[i], self.__ORR[self.__RR[i]]) fd.write(buff[:-1]) fd.write("};\n") buff = "" fd.write("int[][] %s = {" % self.__VAR_BC) for i in self.__bytecodes: buff += "{%s}," % str(i)[1:-1] fd.write(buff[:-1]) fd.write("};\n") fd.write("int %s = 0;\n" % self.__VAR_IDX) fd.write("int %s = -1;\n" % self.__VAR_C_IDX) if self.__debug: fd.write( "System.out.println(\"================START VM===============\");\n" ) fd.write("while(idx < BC.length) {\n") self.__VAR_T = "T" self.__VAR_S = "S" fd.write("int[] %s = new int[3];\n" % self.__VAR_T) fd.write("int[] %s = new int[3];\n" % self.__VAR_S) fd.write("T[0] = (BC[idx][0] & 0x00FF0000) >> 16;\n") fd.write("T[1] = (BC[idx][0] & 0x0000FF00) >> 8;\n") fd.write("T[2] = (BC[idx][0] & 0x000000FF);\n") fd.write("S[0] = (BC[idx][1] & 0x00FF0000) >> 16;\n") fd.write("S[1] = (BC[idx][1] & 0x0000FF00) >> 8;\n") fd.write("S[2] = (BC[idx][1] & 0x000000FF);\n") if self.__debug: fd.write("System.out.println( \"IDX \" + idx );\n") fd.write( "System.out.println( \"TYPE \" + T[0] + \" \" + T[1] + \" \" + T[2] );\n" ) fd.write( "System.out.println( \"SIZE \" + S[0] + \" \" + S[1] + \" \" + S[2] );\n" ) fd.write( "System.out.println( \"BC \" + BC[idx][3] + \" \" + BC[idx][4] + \" \" + BC[idx][5] );\n" ) #FIXME for i in self.__OP: fd.write("if (BC[idx][2] == %d) {\n" % self.__OP[i]) fd.write(REIL_TO_JAVA(i, self, self.__debug).get_raw()) fd.write("}\n") if self.__debug: fd.write("System.out.printf(\"REG = \");\n") fd.write("for(int j=0; j < REG.length; j++) {\n") fd.write("System.out.printf(\"%d \", REG[j]);\n") fd.write("}\n") fd.write("System.out.println(\"\");\n") fd.write("if (%s != -1) {\n" % self.__VAR_C_IDX) fd.write("%s = %s;\n" % (self.__VAR_IDX, self.__VAR_C_IDX)) fd.write("%s = -1;\n" % (self.__VAR_C_IDX)) fd.write("}\n") fd.write("else {\n") fd.write("idx = idx + 1;\n") fd.write("}\n") fd.write("}\n") if self.__debug: fd.write( "System.out.println(\"================STOP VM================\\n\");\n" ) fd.write("return REG[%s];\n" % self.__ORR[self.__RR[self.__register_return.get_name()]]) fd.write("}\n") fd.write("}\n") fd.close() compile = Popen(["/usr/bin/javac", "VM.java"], stdout=PIPE, stderr=STDOUT) stdout, stderr = compile.communicate() print "COMPILATION RESULTS", stdout, stderr if stdout != "": raise ("oops") # print "COMPILATION RESULTS", "SWAP ...." self.__vm_format = jvm.JVMFormat(open("VM.class").read())