Pwn題中修改程序連接庫的方法

做Pwn的時候有時候會遇到題目的環境和本地環境不一致的情況。如果要裝和遠程環境相同的虛擬機就會比較麻煩,下面可以通過修改binary動態連接庫的方法,強行讓程序鏈接到與遠程環境相同的庫上。

1.首先下載好和遠程環境相同的動態庫,如libc-2.23.so;然後下載與之相符的鏈接器ld-2.23.so。有兩個項目可以實現自動下載 libc:https://github.com/niklasb/libc-database和https://github.com/matrix1001/glibc-all-in-one。
2.運行下面腳本。這個腳本修改了二進制文件的頭部信息,將其連接器位置修改成你想替換的連接器,然後修改環境變量LD_PRELOAD,去加載動態庫。

import os
from pwn import *

def change_ld(binary, ld):
    """
    Force to use assigned new ld.so by changing the binary
    """
    if not os.access(ld, os.R_OK): 
        log.failure("Invalid path {} to ld".format(ld))
        return None
 
         
    if not isinstance(binary, ELF):
        if not os.access(binary, os.R_OK): 
            log.failure("Invalid path {} to binary".format(binary))
            return None
        binary = ELF(binary)
 
    for segment in binary.segments:
        if segment.header['p_type'] == 'PT_INTERP':
            size = segment.header['p_memsz']
            addr = segment.header['p_paddr']
            data = segment.data()
            if size <= len(ld):
                log.failure("Failed to change PT_INTERP from {} to {}".format(data, ld))
                return None
            binary.write(addr, ld.ljust(size, '\0'))
            if not os.access('./Pwn', os.F_OK): os.mkdir('./Pwn')
            path = './Pwn/{}_debug'.format(os.path.basename(binary.path))
            if os.access(path, os.F_OK): 
                os.remove(path)
                info("Removing exist file {}".format(path))
            binary.save(path)    
            os.chmod(path, 0b111000000) #rwx------
    success("PT_INTERP has changed from {} to {}. Using temp file {}".format(data, ld, path)) 
    return ELF(path)

elf=change_ld('./pwn','./ld-2.23.so')
p = elf.process(env={'LD_PRELOAD':'./libc-2.23.so'})
p.interactive()

3.如果要動態調試,在運行二進制之後,attach這個進程即可。

參考連接:關於不同版本glibc強行加載的方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章