1. 程式人生 > 其它 >[GWCTF 2019]枯燥的抽獎

[GWCTF 2019]枯燥的抽獎

[GWCTF 2019]枯燥的抽獎

知識點:

種子爆破

https://www.openwall.com/php_mt_seed/

PHP的mt_rand函式作為一個隨機數生成工具在程式中被廣泛使用,但是我們不難發現它生成的隨機數不是真正的隨機數而是偽隨機。既然是偽隨機就有可能利用。

安裝 php_mt_seed

  1. 【下載地址】
  2. 下載以後解壓,然後到資料夾下 make 就會生成一個可執行檔案
  3. 安裝需要 gcc 環境

解題:

進入check.php檢視原始碼:

cMUrE9Gapk

<?php
#這不是抽獎程式的原始碼!不許看!
header("Content-Type: text/html;charset=utf-8");
session_start();
if(!isset($_SESSION['seed'])){
$_SESSION['seed']=rand(0,999999999);
}

mt_srand($_SESSION['seed']);	// 使用 Mersenne Twister 演算法返回隨機整數。
$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");

發現mt_scrand()mt_rand()這倆函式; 並且session是用的隨機數設定的;

先用指令碼將偽隨機數轉換成php_mt_seed可以識別的資料

str1='abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
str2='cMUrE9Gapk'
length = len(str2)
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)

得到:

2 2 0 61 48 48 0 61 56 56 0 61 17 17 0 61 40 40 0 61 35 35 0 61 42 42 0 61 0 0 0 61 15 15 0 61 10 10 0 61

然後丟到php_mt_seed裡:

再根據這道題的指令碼,把數轉為字串就好啦。

<?php
mt_srand(896280733);
$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>";
?>