def test(data): # Digest C informations text = """ struct human { unsigned short age; unsigned int height; char name[50]; }; struct ll_human { struct ll_human* next; struct human human; }; """ my_types = CTypeAMD64_unk() types_mngr = CTypesManagerNotPacked(my_types.types) types_mngr.add_c_decl(text) # Analyze binary cont = Container.fallback_container(data, None, addr=0) machine = Machine("x86_64") dis_engine, ira = machine.dis_engine, machine.ira mdis = dis_engine(cont.bin_stream, symbol_pool=cont.symbol_pool) addr_head = 0 blocks = mdis.dis_multibloc(addr_head) lbl_head = mdis.symbol_pool.getby_offset(addr_head) ir_arch_a = ira(mdis.symbol_pool) for block in blocks: ir_arch_a.add_bloc(block) open('graph_irflow.dot', 'w').write(ir_arch_a.graph.dot()) # Main function's first argument's type is "struct ll_human*" void_ptr = types_mngr.void_ptr ll_human = types_mngr.get_type(('ll_human',)) ptr_llhuman = ObjCPtr('noname', ll_human, void_ptr.align, void_ptr.size) arg0 = ExprId('ptr', 64) ctx = {ir_arch_a.arch.regs.RDI: arg0} expr_types = {arg0.name: ptr_llhuman} mychandler = MyCHandler(types_mngr, expr_types) for expr in get_funcs_arg0(ctx, ir_arch_a, lbl_head): print "Access:", expr target_types = mychandler.expr_to_types(expr) for target_type in target_types: print '\tType:', target_type c_strs = mychandler.expr_to_c(expr) for c_str in c_strs: print "\tC access:", c_str print
def parse_types(self): """Extract the prototype of the targeted function and associated type""" ctype_manager = CTypesManagerNotPacked(CAstTypes(), CTypeAMD64_unk()) with open(self.header_filename) as fdesc: data = fdesc.read() self.headerfile = HeaderFile(data, ctype_manager) self.prototype = self.headerfile.functions[self.functionname] self.types = ctype_manager self.logger.info("Found prototype: %s" % self.prototype)
def get_types_mngr(headerFile): text = open(headerFile).read() base_types = CTypeAMD64_unk() types_ast = CAstTypes() # Add C types definition types_ast.add_c_decl(text) types_mngr = CTypesManagerNotPacked(types_ast, base_types) return types_mngr
def test(): """ C manipulation example """ # Digest C informations text = """ struct line { char color[20]; int size; }; struct rectangle { unsigned int width; unsigned int length; struct line* line; }; """ # Type manager for x86 64: structures not packed my_types = CTypeAMD64_unk() types_mngr = CTypesManagerNotPacked(my_types.types) # Add C types definition types_mngr.add_c_decl(text) # Create the ptr variable with type "struct rectangle*" void_ptr = types_mngr.void_ptr rectangle = types_mngr.get_type(('rectangle',)) ptr_rectangle = ObjCPtr('noname', rectangle, void_ptr.align, void_ptr.size) ptr = ExprId('ptr', 64) expr_types = {ptr.name: ptr_rectangle} mychandler = CHandler(types_mngr, expr_types) # Parse some C accesses c_acceses = ["ptr->width", "ptr->length", "ptr->line", "ptr->line->color", "ptr->line->color[3]", "ptr->line->size" ] for c_str in c_acceses: expr = mychandler.c_to_expr(c_str) c_type = mychandler.c_to_type(c_str) print 'C access:', c_str print '\tExpr:', expr print '\tType:', c_type
def __init__(self, *args, **kwargs): super(TestHeader, self).__init__(*args, **kwargs) ctype_manager = CTypesManagerNotPacked(CAstTypes(), CTypeAMD64_unk()) hdr = HeaderFile(self.header, ctype_manager) proto = hdr.functions[self.func] self.c_handler = CHandler( hdr.ctype_manager, { 'arg%d_%s' % (i, name): proto.args[name] for i, name in enumerate(proto.args_order) }) self.cache_sizeof = {} self.cache_trad = {} self.cache_field_addr = {}
def get_types_mngr(headerFile, arch): text = open(headerFile).read() if arch == "AMD64_unk": base_types = CTypeAMD64_unk() elif arch == "X86_32_unk": base_types = CTypeX86_unk() else: raise NotImplementedError("Unsupported arch") types_ast = CAstTypes() # Add C types definition types_ast.add_c_decl(text) types_mngr = CTypesManagerNotPacked(types_ast, base_types) return types_mngr
def __init__(self, *args, **kwargs): super(TestHeader, self).__init__(*args, **kwargs) # Requirement check if pycparser is None: raise ImportError( "pycparser module is needed to launch tests based" "on header files") ctype_manager = CTypesManagerNotPacked(CAstTypes(), CTypeAMD64_unk()) hdr = HeaderFile(self.header, ctype_manager) proto = hdr.functions[self.func] self.c_handler = CHandler( hdr.ctype_manager, { 'arg%d_%s' % (i, name): proto.args[name] for i, name in enumerate(proto.args_order) }) self.cache_sizeof = {} self.cache_trad = {} self.cache_field_addr = {}
data = open(sys.argv[1]).read() # Digest C informations text = """ struct human { unsigned short age; unsigned int height; char name[50]; }; struct ll_human { struct ll_human* next; struct human human; }; """ base_types = CTypeAMD64_unk() types_ast = CAstTypes() types_ast.add_c_decl(text) types_mngr = CTypesManagerNotPacked(types_ast, base_types) # Analyze binary cont = Container.fallback_container(data, None, addr=0) machine = Machine("x86_64") dis_engine, ira = machine.dis_engine, machine.ira mdis = dis_engine(cont.bin_stream, symbol_pool=cont.symbol_pool) addr_head = 0 blocks = mdis.dis_multiblock(addr_head) lbl_head = mdis.symbol_pool.getby_offset(addr_head)