实验吧之【拐弯抹角】(url伪静态)

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

题目地址:http://ctf5.shiyanbar.com/indirection/

打开后给了源码 

<?php 
// code by SEC@USTC 

echo '<html><head><meta http-equiv="charset" content="gbk"></head><body>'; 

$URL = $_SERVER['REQUEST_URI']; 
//echo 'URL: '.$URL.'<br/>'; 
$flag = "CTF{???}"; 

$code = str_replace($flag, 'CTF{???}', file_get_contents('./index.php')); 
$stop = 0; 

//这道题目本身也有教学的目的 
//第一,我们可以构造 /indirection/a/../ /indirection/./ 等等这一类的 
//所以,第一个要求就是不得出现 ./ 
if($flag && strpos($URL, './') !== FALSE){ 
    $flag = ""; 
    $stop = 1;        //Pass 
} 

//第二,我们可以构造 \ 来代替被过滤的 / 
//所以,第二个要求就是不得出现 ../ 
if($flag && strpos($URL, '\\') !== FALSE){ 
    $flag = ""; 
    $stop = 2;        //Pass 
} 

//第三,有的系统大小写通用,例如 indirectioN/ 
//你也可以用?和#等等的字符绕过,这需要统一解决 
//所以,第三个要求对可以用的字符做了限制,a-z / 和 . 
$matches = array(); 
preg_match('/^([0-9a-z\/.]+)$/', $URL, $matches); 
if($flag && empty($matches) || $matches[1] != $URL){ 
    $flag = ""; 
    $stop = 3;        //Pass 
} 

//第四,多个 / 也是可以的 
//所以,第四个要求是不得出现 // 
if($flag && strpos($URL, '//') !== FALSE){ 
    $flag = ""; 
    $stop = 4;        //Pass 
} 

//第五,显然加上index.php或者减去index.php都是可以的 
//所以我们下一个要求就是必须包含/index.php,并且以此结尾 
if($flag && substr($URL, -10) !== '/index.php'){ 
    $flag = ""; 
    $stop = 5;        //Not Pass 
} 

//第六,我们知道在index.php后面加.也是可以的 
//所以我们禁止p后面出现.这个符号 
if($flag && strpos($URL, 'p.') !== FALSE){ 
    $flag = ""; 
    $stop = 6;        //Not Pass 
} 

//第七,现在是最关键的时刻 
//你的$URL必须与/indirection/index.php有所不同 
if($flag && $URL == '/indirection/index.php'){ 
    $flag = ""; 
    $stop = 7;        //Not Pass 
} 
if(!$stop) $stop = 8; 

echo 'Flag: '.$flag; 
echo '<hr />'; 
for($i = 1; $i < $stop; $i++) 
    $code = str_replace('//Pass '.$i, '//Pass', $code); 
for(; $i < 8; $i++) 
    $code = str_replace('//Pass '.$i, '//Not Pass', $code); 


echo highlight_string($code, TRUE); 

echo '</body></html>';

简单分析:

分7个步骤判断URL是否满足条件,如果不满足就把$flag弄掉,当你不满足条件就会看到Not Pass。
如果是正常访问,$URL = $_SERVER['REQUEST_URI']; 这里$URL一般会得到/indirection/或者/indirection/index.php
需要满足不能出现./\大写或者其他符号、//、p.,而且要求末尾是/index.php,这6个步骤对于/indirection/index.php都可以满足,但是最后一条又告知不能等于/indirection/index.php

这种其实就是伪静态技术(pseudo-static),又名URL重写(URL rewriting)。举个最简单的应用,例如你原本想弄的是index.php?id=123,但你想隐藏其真实的文件路径,你通过URL重写技术,可以达到访问test/123.html而实际上在访问index.php?id=123

这题的关键在于伪静态,比如url中含有xxxx.php/xx/x,那么.php后的xx就会被当成参数名,x会被当成参数

所以这里的payload可以这么构造:http://ctf5.shiyanbar.com/indirection/index.php/x/index.php

猜你喜欢

转载自blog.csdn.net/qq_17204441/article/details/93551525