秋名山老司机 BugKuCTF

原题地址:https://ctf.bugku.com/challenges#秋名山老司机

测试网页:http://123.206.87.240:8002/qiumingshan/

根据服务器的行为,下面的代码是我对其源码的复现,或许略有差异,毕竟条条大路通罗马。

刚开始并不知道要POST什么值,要传入value 也只是猜出来的,在打CTF的时候,很多时候都需要盲猜的。

<?php
session_start();

$table = array('+', '-', '*');

function initial($table)
{
    // 设置值
    $formula = (string) mt_rand();

    for ($i = 0; $i < 10; $i++) {
        $formula .= $table[mt_rand(0, 2)] . (string) mt_rand();
    }

    $value = eval('return ' . $formula . ';');
    $_SESSION['value'] = $value;
    $_SESSION['formula'] = $formula;
    $_SESSION['time'] = date_timestamp_get(date_create());
    echo '<p style="text-align:center">亲请在2s内计算老司机的车速是多少</p>' . PHP_EOL;
    echo '<div>' . $formula . '=?;</div>' . PHP_EOL;
}

if (!isset($_SESSION['value'])) {
    initial($table);
} else {
    $diff = date_timestamp_get(date_create()) - $_SESSION['time'];

    if ($diff <= 2) {
        if (isset($_POST['value'])) {
            if ($_POST['value'] === (string) $_SESSION['value']) {
                echo 'You get the flag';
            }
        } else {
            echo 'Give me value post about ' . $_SESSION['formula'] . '=?';
        }
    } else {
        initial($table);
    }
}
?>

从上面的代码可以看出,只需要在两秒之内post正确的值即可获得flag。下面是我的脚本。

#! /usr/bin/python3
# -*- coding: utf-8 -*-

import requests
import re

# 打印信息
def print_response(res):
    for v in res.headers:
        print(v+': '+res.headers[v])

    print()
    print(res.content.decode())

url = 'http://123.206.87.240:8002/qiumingshan/'

s = requests.Session()

response = s.get(url)
print("The first response:")
print_response(response)

# 匹配formula(公式)
result = (re.search('(?<=<div>).+(?==)',response.content.decode()).group())

# 解析公式
result = eval(result)
print(result)

html = s.post(url,{'value':str(result)})

print("The second response:")
print_response(html)