[GWCTF 2019]枯燥的抽奖
打开网页,查看源代码,发现JavaScript脚本:
$(document).ready(function(){
$("#div1").load("check.php #p1");
$(".close").click(function(){
$("#myAlert").hide();
});
$("#button1").click(function(){
$("#myAlert").hide();
guess=$("input").val();
$.ajax({
type: "POST",
url: "check.php",
data: "num="+guess,
success: function(msg){
$("#div2").append(msg);
alertmsg = $("#flag").text();
if(alertmsg=="没抽中哦,再试试吧"){
$("#myAlert").attr("class","alert alert-warning");
if($("#new").text()=="")
$("#new").append(alertmsg);
}
else{
$("#myAlert").attr("class","alert alert-success");
if($("#new").text()=="")
$("#new").append(alertmsg);
}
}
});
$("#myAlert").show();
$("#new").empty();
$("#div2").empty();
});
});
发现check.php页面,输入url,访问check.php页面,发现源代码:
SWMWwWJOT3
<?php
#这不是抽奖程序的源代码!不许看!
header("Content-Type: text/html;charset=utf-8");
session_start();
if(!isset($_SESSION['seed'])){
$_SESSION['seed']=rand(0,999999999);
}
mt_srand($_SESSION['seed']);
$str_long1 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$str='';
$len1=20;
for ( $i = 0; $i < $len1; $i++ ){
$str.=substr($str_long1, mt_rand(0, strlen($str_long1) - 1), 1);
}
$str_show = substr($str, 0, 10);
echo "<p id='p1'>".$str_show."</p>";
if(isset($_POST['num'])){
if($_POST['num']===$str){x
echo "<p id=flag>抽奖,就是那么枯燥且无味,给你flag{xxxxxxxxx}</p>";
}
else{
echo "<p id=flag>没抽中哦,再试试吧</p>";
}
}
show_source("check.php");
查阅PHP手册:
mt_srand
(PHP 4, PHP 5, PHP 7, PHP 8)
mt_srand — 播下一个更好的随机数发生器种子
mt_rand
(PHP 4, PHP 5, PHP 7, PHP 8)
mt_rand — 生成更好的随机数,它可以产生随机数值的平均速度比 libc 提供的 rand() 快四倍。
使用php随机数碰撞脚本下载链接:
php_mt_seed - PHP mt_rand() seed cracker
在kail中在目录下打开终端使用make命令编译。只要知道了seed,那么随机数序列就是固定的,所以我们如果知道了随机数的前几位数字,就可以推断出seed是什么,恰好网站给了我们字符串的前几位,我们需要把它转化为php_mt_seed能够识别的格式。编写python脚本:
str1 = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
str2 = 'SWMWwWJOT3'
res = ''
for i in range(len(str2)):
for j in range(len(str1)):
if str2[i] == str1[j]:
res += str(j) + ' ' + str(j) + ' ' + '0' + ' ' + str(len(str1) - 1) + ' '
break
print(res)
运行后输出:
54 54 0 61 58 58 0 61 48 48 0 61 58 58 0 61 22 22 0 61 58 58 0 61 45 45 0 61 50 50 0 61 55 55 0 61 29 29 0 61
在kail中运行命令:
./php_mt_seed 54 54 0 61 58 58 0 61 48 48 0 61 58 58 0 61 22 22 0 61 58 58 0 61 45 45 0 61 50 50 0 61 55 55 0 61 29 29 0 61
得到结果:seed = 0x09b362ab = 162751147
(PHP 7.1.0+)
用php
脚本还原完整的字符串序列:
<?php
mt_srand(162751147);
$str_long1 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$str = '';
$len1 = 20;
for ( $i = 0; $i < $len1; $i++ )
$str .= substr($str_long1, mt_rand(0, strlen($str_long1) - 1), 1);
echo $str;
?>
用php -v
查看PHP
版本,如果不是php7+
版本,在系统环境变量中修改php
切换为PHP 7.3.4
,运行结果:SWMWwWJOT3EDKAfIjn8m
,输入在网站文本框,得到flag。
References