代码审计,打开页面,就发现了
$(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,我就是给你源码,有本事你咬我啊
<?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");
看起来是考的伪随机数,其实就是一个有伪随机数的爆破问题,只要有固定的种子,那么生成随机数序列是固定的。这也就是为什么你刷新页面这个随机数都不会变的原因
session里的种子我们读不到,但是可以爆破
str1 ='OYn87LsZj5'
str2 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
result =''
length = str(len(str2)-1)
for i in range(0,len(str1)):
for j in range(0,len(str2)):
if str1[i] == str2[j]:
result += str(j) + ' ' +str(j) + ' ' + '0' + ' ' + length + ' '
break
print(result)
这里是对根据代码原先的逻辑进行处理,j是随机出的值,length代表取数区间
得到:50 50 0 61 60 60 0 61 13 13 0 61 34 34 0 61 33 33 0 61 47 47 0 61 18 18 0 61 61 61 0 61 9 9 0 61 31 31 0 61
然后根据求出的值利用php_mt_seed进行爆破,爆破出种子
./php_mt_seed 50 50 0 61 60 60 0 61 13 13 0 61 34 34 0 61 33 33 0 61 47 47 0 61 18 18 0 61 61 61 0 61 9 9 0 61 31 31 0 61
结果输出php7.1.0版本往上爆破出种子299287931(随机数算法和php版本也有关)
有了种子,我们就可以顺利反推了
<?php
mt_srand(299287931);
$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 "<p id='p1'>".$str."</p>";
?>
顺利得到结果
OYn87LsZj59lqv9q0Z92
和运期结果一样,顺利提交flag
参考视频链接:https://www.bilibili.com/video/BV1NZ4y1C7gf/