uroboros-diversification/src/disasm/extern_symbol_process.py
def pltgot(filepath):
    """
    Handle library functions linked through .plt.got and substitute them with correct symbols
    :param filepath: path to target executable
    """
    with open('plts.info') as f:
        f.seek(-2, os.SEEK_END)
        while f.read(1) != '\n': f.seek(-2, os.SEEK_CUR)
        lastplt = f.readline().split()
    lastplt = (int(lastplt[0],16), re.escape(lastplt[1].rstrip('>:')))

    pltgotsym = check_output('readelf -r ' + filepath + ' | awk \'/_GLOB_DAT/ {print $1,$5}\' | grep -v __gmon_start__ | cat', shell=True).strip()
    if len(pltgotsym) == 0: return
    def pltsymmapper(l):
        items = l.strip().split()
        return (int(items[0], 16), items[1].split('@')[0])
    pltgotsym = dict(map(pltsymmapper, pltgotsym.split('\n')))

    pltgottargets = check_output(config.objdump + ' -Dr -j .plt.got ' + filepath + ' | grep jmp | cut -f1,3', shell=True)
    def pltgotmapper(l):
        items = l.strip().split()
        dest = int(items[4] if '#' in items else items[2].lstrip('*'), 16)
        return (int(items[0].rstrip(':'), 16), dest)
    pltgottargets = dict(map(pltgotmapper, pltgottargets.strip().split('\n')))
    pltgottargets = {e[0]: '<' + pltgotsym[e[1]] + '@plt>' for e in pltgottargets.iteritems() if e[1] in pltgotsym}
    if len(pltgottargets) == 0: return

    pltgotre = re.compile(lastplt[1] + '\+(0x[0-9a-f]+)\>', re.I)
    def calldesmapper(l):
        m = pltgotre.search(l)
        if m:
            dest = lastplt[0] + int(m.group(1), 16)
            if dest in pltgottargets: return pltgotre.sub(pltgottargets[dest], l)
        return l
    with open(filepath + '.temp') as f:
        lines = map(calldesmapper, f)

    with open(filepath + '.temp', 'w') as f:
        f.writelines(lines)

    Handle library functions linked through .plt.got and substitute them with correct symbols
    :param filepath: path to target executable
    
Handle library functions linked through .plt.got and substitute them with correct symbols
Handle library functions linked through . plt . got and substitute them with correct symbols
def pltgot(filepath):
    with open('plts.info') as f:
        f.seek(-2, os.SEEK_END)
        while f.read(1) != '\n': f.seek(-2, os.SEEK_CUR)
        lastplt = f.readline().split()
    lastplt = (int(lastplt[0],16), re.escape(lastplt[1].rstrip('>:')))
    pltgotsym = check_output('readelf -r ' + filepath + ' | awk \'/_GLOB_DAT/ {print $1,$5}\' | grep -v __gmon_start__ | cat', shell=True).strip()
    if len(pltgotsym) == 0: return
    def pltsymmapper(l):
        items = l.strip().split()
        return (int(items[0], 16), items[1].split('@')[0])
    pltgotsym = dict(map(pltsymmapper, pltgotsym.split('\n')))
    pltgottargets = check_output(config.objdump + ' -Dr -j .plt.got ' + filepath + ' | grep jmp | cut -f1,3', shell=True)
    def pltgotmapper(l):
        items = l.strip().split()
        dest = int(items[4] if '#' in items else items[2].lstrip('*'), 16)
        return (int(items[0].rstrip(':'), 16), dest)
    pltgottargets = dict(map(pltgotmapper, pltgottargets.strip().split('\n')))
    pltgottargets = {e[0]: '<' + pltgotsym[e[1]] + '@plt>' for e in pltgottargets.iteritems() if e[1] in pltgotsym}
    if len(pltgottargets) == 0: return
    pltgotre = re.compile(lastplt[1] + '\+(0x[0-9a-f]+)\>', re.I)
    def calldesmapper(l):
        m = pltgotre.search(l)
        if m:
            dest = lastplt[0] + int(m.group(1), 16)
            if dest in pltgottargets: return pltgotre.sub(pltgottargets[dest], l)
        return l
    with open(filepath + '.temp') as f:
        lines = map(calldesmapper, f)
    with open(filepath + '.temp', 'w') as f:
        f.writelines(lines)
def pltgot ( filepath ) : with open ( 'plts.info' ) as f : f . seek ( - 2 , os . SEEK_END ) while f . read ( 1 ) != '\n' : f . seek ( - 2 , os . SEEK_CUR ) lastplt = f . readline ( ) . split ( ) lastplt = ( int ( lastplt [ 0 ] , 16 ) , re . escape ( lastplt [ 1 ] . rstrip ( '>:' ) ) ) pltgotsym = check_output ( 'readelf -r ' + filepath + ' | awk \'/_GLOB_DAT/ {print $1,$5}\' | grep -v __gmon_start__ | cat' , shell = True ) . strip ( ) if len ( pltgotsym ) == 0 : return def pltsymmapper ( l ) : items = l . strip ( ) . split ( ) return ( int ( items [ 0 ] , 16 ) , items [ 1 ] . split ( '@' ) [ 0 ] ) pltgotsym = dict ( map ( pltsymmapper , pltgotsym . split ( '\n' ) ) ) pltgottargets = check_output ( config . objdump + ' -Dr -j .plt.got ' + filepath + ' | grep jmp | cut -f1,3' , shell = True ) def pltgotmapper ( l ) : items = l . strip ( ) . split ( ) dest = int ( items [ 4 ] if '#' in items else items [ 2 ] . lstrip ( '*' ) , 16 ) return ( int ( items [ 0 ] . rstrip ( ':' ) , 16 ) , dest ) pltgottargets = dict ( map ( pltgotmapper , pltgottargets . strip ( ) . split ( '\n' ) ) ) pltgottargets = { e [ 0 ] : '<' + pltgotsym [ e [ 1 ] ] + '@plt>' for e in pltgottargets . iteritems ( ) if e [ 1 ] in pltgotsym } if len ( pltgottargets ) == 0 : return pltgotre = re . compile ( lastplt [ 1 ] + '\+(0x[0-9a-f]+)\>' , re . I ) def calldesmapper ( l ) : m = pltgotre . search ( l ) if m : dest = lastplt [ 0 ] + int ( m . group ( 1 ) , 16 ) if dest in pltgottargets : return pltgotre . sub ( pltgottargets [ dest ] , l ) return l with open ( filepath + '.temp' ) as f : lines = map ( calldesmapper , f ) with open ( filepath + '.temp' , 'w' ) as f : f . writelines ( lines )
Handle library functions linked through .plt.got and substitute them with correct symbols
Handle library functions linked through . plt . got and substitute them with correct symbols
"""
    Handle library functions linked through .plt.got and substitute them with correct symbols
    :param filepath: path to target executable
    """
filepath
path to target executable
path to target executable