Training: PHP LFI (Exploit, PHP, Training)
题目描述
Your mission is to exploit this code, which has obviously an LFI vulnerability:
您的任务是利用此代码,它显然具有LFI漏洞:
GeSHi`ed PHP code
$filename = 'pages/'.(isset($_GET["file"])?$_GET["file"]:"welcome").'.html';
include $filename;
There is a lot of important stuff in …/solution.php, so please include and execute this file for us.
在…/solution.php中有很多重要的内容,所以请为我们包含并执行此文件。
Here are a few examples of the script in action (in the box below):
以下是脚本运行的几个示例(在下面的框中):
index.php?file=welcome
index.php?file=news
index.php?file=forums
For debugging purposes, you may look at the whole source again, also as highlighted version.
出于调试目的,您可以再次查看整个源代码,也可以作为突出显示的版本。
The vulnerable script in action (pages/welcome.html 页面)
欢迎来到我的网站!
伙计,你被ZeroCool黑了联系我. 😄
The vulnerable script in action (pages/news.html 页面)
这是新闻页。
伙计,你被ZeroCool扎根了 xD
The vulnerable script in action (pages/forums.html 页面)
这是论坛页。
我想我也需要把 php 放在这里 😕
源代码
<?php
# Higlighter Plain
if (isset($_GET['show']) && $_GET['show'] === 'source')
{
header('Content-Type: text/plain; charset=utf8;');
echo file_get_contents('index.php');
die();
}
# Change dir to web root
chdir('../../../../../');
# Print the website header
define('GWF_PAGE_TITLE', 'Local File Inclusion');
require_once('challenge/html_head.php');
if (false === ($chall = WC_Challenge::getByTitle('Training: PHP LFI'))) {
$chall = WC_Challenge::dummyChallenge('Training: PHP LFI', 2, 'challenge/training/php/lfi/up/index.php', false);
}
$chall->showHeader();
# Highlighter BBCode
if (isset($_GET['highlight']) && $_GET['highlight'] === 'christmas')
{
echo GWF_Message::display('[PHP]'.file_get_contents($_SERVER['SCRIPT_FILENAME']).'[/PHP]');
require_once('challenge/html_foot.php');
return;
}
###############################
### Here is your exploit :) ###
###############################
$code = '$filename = \'pages/\'.(isset($_GET["file"])?$_GET["file"]:"welcome").\'.html\';';
$code_emulate_pnb = '$filename = Common::substrUntil($filename, "\\0");'; # Emulate Poison Null Byte for PHP>=5.3.4
$code2 = 'include $filename;';
### End of exploit ###
# Show the mission box
$url = 'index.php?file=';
$ex = array('welcome', 'news', 'forums');
$showsrc1 = 'index.php?show=source';
$showsrc2 = 'index.php?highlight=christmas';
foreach ($ex as $i => $e) { $ex[$i] = htmlspecialchars($url.$e); }
echo GWF_Box::box($chall->lang('info', array(GWF_Message::display('[PHP]'.$code.PHP_EOL.$code2.'[/PHP]'), '../solution.php', $showsrc1, $showsrc2, $ex[0], $ex[1], $ex[2])), $chall->lang('title'));
# Execute the code, using eval.
GWF_Debug::setDieOnError(false);
GWF_Debug::setMailOnError(false);
eval($code.$code_emulate_pnb); # eval the first line
echo '<div class="box">'.PHP_EOL;
echo '<div class="box_t">'.$chall->lang('example_title').' ('.htmlspecialchars($filename).')'.'</div>'.PHP_EOL;
echo '<div class="box_c">'.PHP_EOL;
if (lfiIsSafeDir($filename) === true) { eval($code2); } # Eval the second line, when safe.
else { echo GWF_HTML::error('LFI', $chall->lang('err_basedir'), false); }
echo '</div>'.PHP_EOL;
echo '</div>'.PHP_EOL;
GWF_Debug::setMailOnError(true);
GWF_Debug::setDieOnError(true);
# Show credits box
if (false !== ($minus = GWF_User::getByName('minus')))
{
echo GWF_Box::box($chall->lang('credits', array($minus->displayProfileLink())));
}
# Show end of website
echo $chall->copyrightFooter();
require_once('challenge/html_foot.php');
### Safety first ###
function lfiIsSafeDir($filename)
{
$valid = array(
'pages',
'pages/../..',
'pages/..',
);
$d = dirname($filename);
return in_array($d, $valid, true);
}
?>
解:
关于LFI(本地文件包含漏洞)这里就不做多解释。(PHP语言的一大高危漏洞,用户可以控制被包含的文件)。什么都要学
$_GET(PHP $GET)用法,如图在地址栏(index.php)后面加?highlight=christmas就表示将christmas传入highlight,然后执行代码,来到代码高亮的页面。
自己可以尝试一下。
根据题目要求:先将 ../solution.php传入file中。
可以看到在传入的file后面有.html,所以这里就要想办法对.html进行截断(利用%00截断)。
访问之后发现提示文件或目录不存在,无法打开要包含的“pages/../solution.php”(总结起来就是solution目录错啦),第54行代码有提示,当安全时eval函数执行,往下有安全判断函数。
关于dirname和in array函数。安全判断就是在valid数组中搜索filename的路径,找到返回true。(即告诉你../solution在这三个中的一个目录里面)都访问试试。
../solution.php最开始就已经访问了。
wechall相关链接:我的wechall之旅??!