[BJDCTF2020]Cookie is so stable && [GWCTF 2019]枯燥的抽奖

[BJDCTF2020]Cookie is so stable

进入环境后看到有hint,点击之后查看源代码

 

 提示我们cookie有线索

flag页面是:

 

 需要输入一个username,或许这道题目是cookie伪造,随便输入一个username

 

 输入的结果显示在了页面上,很容易能想到注入,抓包

 

 cookie里面有我们最开始输入的username

可以猜到后端的代码应该是类似于 这样的 

1
2
<code-pre class="code-pre" id="pre-nHKSe6"><code-line class="line-numbers-rows"></code-line>Hello %s
</code-pre>

  将用户cookie里面传过来的user字符串通过模板渲染在页面上,处理不当的话就会导致模板注入

因为既然是通过模板渲染,我们也可以按照模板引擎的语法来输入,然后让模板把我们的恶意输入成功渲染

 

 

 

 可以看到存在确实存在模板注入,然后题目环境对应的模板,上payload任意命令执行就行了

1
2
<code-pre class="code-pre" id="pre-zfRapW"><code-line class="line-numbers-rows"></code-line>{{_self.env.registerUndefinedFilterCallback("exec")}}{{_self.env.getFilter("cat /flag")}};
</code-pre>

  

 

 好像还必须在burpsuite里面才可以

 

关于SSTI模板注入的详解可以看:https://www.k0rz3n.com/2018/11/12/%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0%E5%B8%A6%E4%BD%A0%E7%90%86%E8%A7%A3%E6%BC%8F%E6%B4%9E%E4%B9%8BSSTI%E6%BC%8F%E6%B4%9E/

 

[GWCTF 2019]枯燥的抽奖

 

 给出了10位字母,一共20位,每一位是大小写字母+数字,即36个可能的字符,暴力猜后十位肯定没戏,看源代码

 

 里面暴露了存在check.php,访问,发现了抽奖程序的源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<code-pre class="code-pre" id="pre-fXaB58"><code-line class="line-numbers-rows"></code-line><?php
<code-line class="line-numbers-rows"></code-line>#这不是抽奖程序的源代码!不许看!
<code-line class="line-numbers-rows"></code-line>header("Content-Type: text/html;charset=utf-8");
<code-line class="line-numbers-rows"></code-line>session_start();
<code-line class="line-numbers-rows"></code-line>if(!isset($_SESSION['seed'])){
<code-line class="line-numbers-rows"></code-line>$_SESSION['seed']=rand(0,999999999);
<code-line class="line-numbers-rows"></code-line>}
<code-line class="line-numbers-rows"></code-line>
<code-line class="line-numbers-rows"></code-line>mt_srand($_SESSION['seed']);
<code-line class="line-numbers-rows"></code-line>$str_long1 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
<code-line class="line-numbers-rows"></code-line>$str='';
<code-line class="line-numbers-rows"></code-line>$len1=20;
<code-line class="line-numbers-rows"></code-line>for ( $i = 0; $i < $len1; $i++ ){
<code-line class="line-numbers-rows"></code-line>    $str.=substr($str_long1, mt_rand(0, strlen($str_long1) - 1), 1);      
<code-line class="line-numbers-rows"></code-line>}
<code-line class="line-numbers-rows"></code-line>$str_show = substr($str, 0, 10);
<code-line class="line-numbers-rows"></code-line>echo "<p id='p1'>".$str_show."</p>";
<code-line class="line-numbers-rows"></code-line>
<code-line class="line-numbers-rows"></code-line>
<code-line class="line-numbers-rows"></code-line>if(isset($_POST['num'])){
<code-line class="line-numbers-rows"></code-line>    if($_POST['num']===$str){x
<code-line class="line-numbers-rows"></code-line>        echo "<p id=flag>抽奖,就是那么枯燥且无味,给你flag{xxxxxxxxx}</p>";
<code-line class="line-numbers-rows"></code-line>    }
<code-line class="line-numbers-rows"></code-line>    else{
<code-line class="line-numbers-rows"></code-line>        echo "<p id=flag>没抽中哦,再试试吧</p>";
<code-line class="line-numbers-rows"></code-line>    }
<code-line class="line-numbers-rows"></code-line>}
<code-line class="line-numbers-rows"></code-line>show_source("check.php");
</code-pre>

  而当mt_srand使用同一个seed时,生成的随机数是可以爆破出seed种子的,使用到的工具是:https://www.openwall.com/php_mt_seed/

根据check.php源代码得到满足php_mt_seed工具要求的参数,搬运其他师傅的逆向python代码:

1
2
3
4
5
6
7
8
9
10
11
12
<code-pre class="code-pre" id="pre-QA28MA"><code-line class="line-numbers-rows"></code-line>str1='abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
<code-line class="line-numbers-rows"></code-line>str2='BW3hV5WMFP'
<code-line class="line-numbers-rows"></code-line>str3 = str1[::-1]
<code-line class="line-numbers-rows"></code-line>length = len(str2)
<code-line class="line-numbers-rows"></code-line>res=''
<code-line class="line-numbers-rows"></code-line>for i in range(len(str2)):
<code-line class="line-numbers-rows"></code-line>    for j in range(len(str1)):
<code-line class="line-numbers-rows"></code-line>        if str2[i] == str1[j]:
<code-line class="line-numbers-rows"></code-line>            res+=str(j)+' '+str(j)+' '+'0'+' '+str(len(str1)-1)+' '
<code-line class="line-numbers-rows"></code-line>            break
<code-line class="line-numbers-rows"></code-line>print res
</code-pre>

  str2就是显示出来的十位字符串,需要自己修改为自己看到的

得到

 

 在Ubuntu下使用工具php_mt_seed,因为是第一次下载,还需要make一下

 

 然后运行:

1
2
<code-pre class="code-pre" id="pre-cPcT7x"><code-line class="line-numbers-rows"></code-line>$ ./php_mt_seed 37 37 0 61 58 58 0 61 29 29 0 61 7 7 0 61 57 57 0 61 31 31 0 61 58 58 0 61 48 48 0 61 41 41 0 61 51 51 0 61
</code-pre>

  获取随机种子,虚拟机漫长的等待。。

 

 吃了个饭回来看随机种子为: 794200984,对应的PHP版本是PHP 7.1.0+

将check.php的代码改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
<code-pre class="code-pre" id="pre-nwheFB"><code-line class="line-numbers-rows"></code-line><?php
<code-line class="line-numbers-rows"></code-line>#这不是抽奖程序的源代码!不许看!
<code-line class="line-numbers-rows"></code-line>header("Content-Type: text/html;charset=utf-8");
<code-line class="line-numbers-rows"></code-line>
<code-line class="line-numbers-rows"></code-line>mt_srand(794200984);
<code-line class="line-numbers-rows"></code-line>$str_long1 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
<code-line class="line-numbers-rows"></code-line>$str='';
<code-line class="line-numbers-rows"></code-line>$len1=20;
<code-line class="line-numbers-rows"></code-line>for ( $i = 0; $i < $len1; $i++ ){
<code-line class="line-numbers-rows"></code-line>    $str.=substr($str_long1, mt_rand(0, strlen($str_long1) - 1), 1);      
<code-line class="line-numbers-rows"></code-line>}
<code-line class="line-numbers-rows"></code-line>echo $str;
</code-pre>

  在对应版本的PHP下运行就可以输出20位的字符串了,但是phpstudy没有这么高版本的PHP,如果使用低版本的php运行,得到的字符串时错误的,去找了找PHP的在线运行工具:https://code.y444.cn/php

可选php7.1,运行之后拿到字符串,猜对数字

 

 

 

 填入获得flag

 

 

参考链接:

https://kit4y.github.io/2019/12/03/wei-sui-ji-shu/

https://www.cnblogs.com/wangtanzhi/p/12288687.html

https://www.cnblogs.com/20175211lyz/p/12198535.html

https://www.kingkk.com/2018/02/php%E7%9A%84%E4%BC%AA%E9%9A%8F%E6%9C%BA%E6%95%B0/

 


__EOF__

本文作者春告鳥
本文链接https://www.cnblogs.com/Cl0ud/p/12801182.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   春告鳥  阅读(322)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示