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 __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 = {}
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) ir_arch_a = ira(mdis.symbol_pool) for block in blocks: ir_arch_a.add_block(block)
struct rectangle { unsigned int width; unsigned int length; struct line* line; }; """ # Type manager for x86 64: structures not packed base_types = CTypeAMD64_unk() types_ast = CAstTypes() # Add C types definition types_ast.add_c_decl(text) types_mngr = CTypesManagerNotPacked(types_ast, base_types) # Create the ptr variable with type "struct rectangle*" ptr_rectangle = types_mngr.get_objc(CTypePtr(CTypeStruct('rectangle'))) ptr = ExprId('ptr', 64) c_context = {ptr.name: ptr_rectangle} mychandler = CHandler(types_mngr, {}) # 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:
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 asmcfg = mdis.dis_multiblock(addr_head) lbl_head = mdis.symbol_pool.getby_offset(addr_head) ir_arch_a = ira(mdis.symbol_pool) for block in asmcfg.blocks: ir_arch_a.add_block(block)
text_2 = """ struct test_context { int a; struct test_st test; int b; }; """ base_types = CTypeAMD64_unk() types_ast = CAstTypes() # Add C types definition types_ast.add_c_decl(text_1) types_ast.add_c_decl(text_2) types_mngr = CTypesManagerNotPacked(types_ast, base_types) for type_id, type_desc in types_mngr.types_ast._types.iteritems(): print type_id obj = types_mngr.get_objc(type_id) print obj print repr(obj) types_mngr.check_objc(obj) for type_id, type_desc in types_mngr.types_ast._typedefs.iteritems(): print type_id obj = types_mngr.get_objc(type_id) print obj print repr(obj) types_mngr.check_objc(obj)
struct test_context { int a; struct test_st test; int b; }; """ base_types = CTypeAMD64_unk() types_ast = CAstTypes() # Add C types definition types_ast.add_c_decl(text_1) types_ast.add_c_decl(text_2) types_mngr = CTypesManagerNotPacked(types_ast, base_types) for type_id, type_desc in types_mngr.types_ast._types.iteritems(): print type_id obj = types_mngr.get_objc(type_id) print obj print repr(obj) types_mngr.check_objc(obj) for type_id, type_desc in types_mngr.types_ast._typedefs.iteritems(): print type_id obj = types_mngr.get_objc(type_id) print obj print repr(obj) types_mngr.check_objc(obj)
struct rectangle { unsigned int width; unsigned int length; struct line* line; }; """ # Type manager for x86 64: structures not packed base_types = CTypeAMD64_unk() types_ast = CAstTypes() # Add C types definition types_ast.add_c_decl(text) types_mngr = CTypesManagerNotPacked(types_ast, base_types) # Create the ptr variable with type "struct rectangle*" ptr_rectangle = types_mngr.get_objc(CTypePtr(CTypeStruct('rectangle'))) ptr = ExprId('ptr', 64) c_context = {ptr.name: ptr_rectangle} mychandler = CHandler(types_mngr, {}) # Parse some C accesses c_acceses = ["ptr->width", "ptr->length", "ptr->line", "ptr->line->color", "ptr->line->color[3]", "ptr->line->size"