[問題] shellcode會core dump
以下是程式碼
===================
#include <stdio.h>
#include <string.h>
char shellcode[]="\x31\xdb\x89\xd8\x40\xcd\x80";
unsigned long sp(void){
__asm__(
"movl %esp, %eax;"
);
}
void test(){
int* ret;
ret = (int*)&ret + 2;
(*ret) = (int)shellcode;
printf("test\n");
}
int main(int argc, char** argv){
test();
return 0;
}
===================
shellcode的內容就是很簡單的exit()
這個程式的目的只是要測試一下這個程式是否能work
預期的行為是printf("test\n");這行不會被執行直接結束程式
不過一執行就死
我用gdb trace的流程是這樣
首先在test()設break point
接下來一行一行印東西來看
Breakpoint 1, test () at missingPrintf.c:15
15 ret = (int*)&ret + 2;
(gdb) x/16aw ret
0x1: Cannot access memory at address 0x1
(gdb) n
16 (*ret) = (int)shellcode;
(gdb) x/16aw ret
0xbfffe82c: 0x804826c <main+22> 0x3 0xbfffe850 0xbfffea68
0xbfffe83c: 0x80484b8 <__libc_start_main+552> 0x20615 0x3 0xbfffea68
0xbfffe84c: 0x80484b8 <__libc_start_main+552> 0x1 0xbfffea94 0xbfffea9c
0xbfffe85c: 0x0 0x0 0x694c0000 0x78756e
(gdb) n
17 printf("test\n");
(gdb) x/16aw ret
0xbfffe82c: 0x80bd084 <shellcode> 0x3 0xbfffe850 0xbfffea68
0xbfffe83c: 0x80484b8 <__libc_start_main+552> 0x20615 0x3 0xbfffea68
0xbfffe84c: 0x80484b8 <__libc_start_main+552> 0x1 0xbfffea94 0xbfffea9c
0xbfffe85c: 0x0 0x0 0x694c0000 0x78756e
(gdb)
看得出來return address(main+22)已經被shellcode的address蓋掉
請問一下為什麼這樣子還會core dump?
而且test那行也被執行了
所以就是這樣寫應該是有問題的
請大神們指教一下
我卡了好久啊…
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.130.51.128
※ 編輯: cobrasgo 來自: 220.130.51.128 (12/29 18:53)
→
01/01 00:46, , 1F
01/01 00:46, 1F
→
01/01 00:46, , 2F
01/01 00:46, 2F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 4 篇):