[問題] shellcode會core dump

看板C_and_CPP作者 (體重突破所有均線)時間15年前 (2010/12/29 19:11), 編輯推噓1(105)
留言6則, 2人參與, 最新討論串1/2 (看更多)
以下是程式碼 =================== #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那行也被執行了 所以就是這樣寫應該是有問題的 請大神們指教一下 我卡了好久啊… ※ 編輯: cobrasgo 來自: 220.130.51.128 (12/29 19:12)

12/29 20:50, , 1F
為什麼 shellcode 是 exit ? btw, 你改了 return addr
12/29 20:50, 1F

12/29 20:50, , 2F
的確是要 test() 執行完, return 的時候才會跳到別的地方
12/29 20:50, 2F

12/29 20:50, , 3F
所以 printf 會執行到很正常
12/29 20:50, 3F

12/29 21:40, , 4F
shellcode是exit的原因是因為我本來寫了一串,但是不work
12/29 21:40, 4F

12/29 21:40, , 5F
就一個一個拿掉,最後剩這個,看看問題在哪裡
12/29 21:40, 5F

12/29 21:41, , 6F
printf會執行的確是正常的,我沒想清楚,應該是放在main
12/29 21:41, 6F
文章代碼(AID): #1D6nTlik (C_and_CPP)
文章代碼(AID): #1D6nTlik (C_and_CPP)