[docs]defconvert_info_sharedlibrary(fname):""" Convert a dump from gdb's ``info sharedlibrary`` command to a set of options that can be passed to CLE to replicate the address space from the gdb session :param fname: The name of a file containing the dump :returns: A dict appropriate to be passed as ``**kwargs`` for ``angr.Project`` or ``cle.Loader`` """return_parse_gdb_map(fname,True)
[docs]defconvert_info_proc_maps(fname):""" Convert a dump from gdb's ``info proc maps`` command to a set of options that can be passed to CLE to replicate the address space from the gdb session :param fname: The name of a file containing the dump :returns: A dict appropriate to be passed as ``**kwargs`` for ``angr.Project`` or ``cle.Loader`` """return_parse_gdb_map(fname,False)
def_parse_gdb_map(gdb_map,gdb_fix):ifnotos.path.isfile(gdb_map):raiseCLEFileNotFoundError("gdb mapping file %s does not exist"%gdb_map)withopen(gdb_map)asf:data=f.readlines()gmap={}forlineindata:iflinein("\n","\r\n"):continueline_items=line.split()# Get rid of all metadata, just extract lines containing addressesif"0x"notinline_items[0]:continueifline_items[-1].startswith("["):continuetry:int(line_items[-1],16)exceptValueError:passelse:continueaddr,objfile=int(line_items[0],16),line_items[-1].strip()# Get the smallest address of each libs' mappingstry:gmap[objfile]=min(gmap[objfile],addr)exceptKeyError:gmap[objfile]=addr# Find lib names# libnames = filter(lambda n: '.so' in n, gmap.keys())# Find base addr for each lib (each lib is mapped to several segments,# we take the segment that is loaded at the smallest address).lib_opts={}main_opts={}force_load_libs=[]smallest_addr=min(gmap.values())forlib,addringmap.items():ifaddr==smallest_addrandnotgdb_fix:# this is the main binaryopts=main_optselse:# if not os.path.exists(lib):# lib = _simple_search(lib)force_load_libs.append(lib)opts={}lib_opts[lib]=opts# address of .text -> base address of the libraryifgdb_fix:found=_simple_search(lib)iffound:addr=addr-get_text_offset(found)log.info("gdb_plugin: mapped %s to %#x",lib,addr)opts["base_addr"]=addrreturn{"force_load_libs":force_load_libs,"main_opts":main_opts,"lib_opts":lib_opts}GDB_SEARCH_PATH=["/lib","/usr/lib"]def_simple_search(libname):dirs=list(GDB_SEARCH_PATH)whiledirs:dirname=dirs.pop(0)try:fornameinos.listdir(dirname):ifnamein(".",".."):continuefull=os.path.join(dirname,name)ifos.path.isdir(full):iffull.count("/")<12:# don't go too deepdirs.append(full)ifos.path.isfile(full)andname==libname:returnfullexceptOSError:passreturnlibname