sky's blog

过气的00截断

字数统计: 452阅读时长: 2 min
2017/09/06 Share

前记

为什么说%00过气了呢……
因为需要满足以下条件:

  1. php版本小于5.3.4
  2. php的magic_quotes_gpc为OFF状态

%00截断原理

截断的核心,就是chr(0)这个字符
先说一下这个字符,这个字符不为空(Null),也不是空字符(“”),更不是空格。
当程序在输出含有chr(0)变量时
chr(0)后面的数据会被停止,换句话说,就是误把它当成结束符,后面的数据直接忽略,这就导致漏洞产生

一些应用

读文件

1
2
3
4
5
<?php
$file = $_GET['file'].'PNG';
$contents = file_get_contents($file);
file_put_contents('put.txt', $contents);
?>

利用方式:
http://localhost/FileGetContents.php?file=password%00
此时可以看到当前目录put.txt是上面password中的内容。
原理很简单,%00截断了后面的.PNG

文件上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<html>
<body>

<h2>test</h2>
<form action="" method="post" enctype="multipart/form-data">
<label>文件:</label>
<input type="file" name = "file" >
<input type="submit" value="submit" name = "upload">
</form>

</body>
</html>

<?php
error_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("上传失败");
}

}

?>

问题就出在:
$targetPath = $_SERVER['DOCUMENT_ROOT'].$_REQUEST['jieduan'].rand(10, 99).date("YmdHis").".".$file_ext;
这里的$_REQUEST['jieduan']存在截断
可以上传文件名如下sky.php%00.jpg
$_REQUEST['jieduan']中,%00截断了后面的代码
带入后就成了
$targetPath = $_SERVER[‘DOCUMENT_ROOT’].sky.php;
再经过move_uploaded_file($tempFile,$targetPath)就成功传入了php恶意文件

点击赞赏二维码,您的支持将鼓励我继续创作!
CATALOG
  1. 1. 前记
  2. 2. %00截断原理
  3. 3. 一些应用
    1. 3.1. 读文件
    2. 3.2. 文件上传