> For the complete documentation index, see [llms.txt](https://hacktricks.xsx.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacktricks.xsx.tw/network-services-pentesting/pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedir-bypass/disable_functions-bypass-dl-function.md).

# disable\_functions bypass - dl function

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们独家的[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或在**Twitter**上关注我们 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

**重要提示:**

![image](https://user-images.githubusercontent.com/84577967/174675487-a4c4ca06-194f-4725-85af-231a2f35d56c.png)

**`dl`** 是一个PHP函数，可用于加载PHP扩展。如果该函数未被禁用，可能会被滥用以**绕过`disable_functions`并执行任意命令**。\
但是，它有一些严格的限制：

* `dl`函数必须**存在**于**环境**中且**未被禁用**
* PHP扩展必须**使用与服务器相同的主要版本**（PHP API版本）进行编译（您可以在phpinfo的输出中查看此信息）
* PHP扩展必须**位于**由\*\*`extension_dir`**指令定义的**目录\*\*中（您可以在phpinfo的输出中看到）。攻击者很难获得对此目录的写访问权限，因此这个要求可能会阻止您滥用此技术。

**如果您符合这些要求，请继续阅读帖子** [**https://antichat.com/threads/70763/**](https://antichat.com/threads/70763/) **以了解如何绕过`disable_functions`**。以下是摘要：

[dl函数](http://www.php.net/manual/en/function.dl.php)用于在脚本执行期间动态加载PHP扩展。PHP扩展通常用C/C++编写，增强PHP的功能。攻击者注意到`dl`函数未被禁用后，决定创建一个自定义PHP扩展来执行系统命令。

#### 攻击者采取的步骤：

1. **确定PHP版本：**

* 攻击者使用脚本（`<?php echo 'PHP Version is '.PHP_VERSION; ?>`）确定PHP版本。

2. **获取PHP源码：**

* 从官方[PHP网站](http://www.php.net/downloads.php)或[存档](http://museum.php.net)（如果版本较旧）下载PHP源码。

3. **本地PHP设置：**

* 在其系统上提取和安装特定的PHP版本。

4. **创建扩展：**

* 学习[创建PHP扩展](http://www.php.net/manual/en/zend.creating.php)并检查PHP源代码。
* 专注于复制位于`ext/standard/exec.c`的[exec函数](http://www.php.net/manual/en/function.exec.php)的功能。

#### 编译自定义扩展的注意事项：

1. **ZEND\_MODULE\_API\_NO：**

* `bypass.c`中的`ZEND_MODULE_API_NO`必须与当前Zend Extension Build匹配，可通过以下命令获取：

```bash
php -i | grep "Zend Extension Build" |awk -F"API4" '{print $2}' | awk -F"," '{print $1}'
```

2. **PHP\_FUNCTION修改：**

* 对于最新的PHP版本（5、7、8），可能需要调整`PHP_FUNCTION(bypass_exec)`。提供的代码片段详细说明了此修改。

#### 自定义扩展文件：

* **bypass.c**：
* 实现自定义扩展的核心功能。
* **php\_bypass.h**：
* 头文件，定义扩展属性。
* **config.m4**：
* 由`phpize`用于为自定义扩展配置构建环境。

#### 构建扩展：

1. **编译命令：**

* 使用`phpize`，`./configure`和`make`来编译扩展。
* 编译后的`bypass.so`然后位于modules子目录中。

2. **清理：**

* 编译后运行`make clean`和`phpize --clean`。

#### 上传并在受害主机上执行：

1. **版本兼容性：**

* 确保攻击者和受害者系统之间的PHP API版本匹配。

2. **加载扩展：**

* 利用`dl`函数，通过使用相对路径或脚本来自动化该过程来规避限制。

3. **脚本执行：**

* 攻击者将`bypass.so`和一个PHP脚本上传到受害者的服务器。
* 该脚本使用`dl_local`函数动态加载`bypass.so`，然后通过`cmd`查询参数传递命令调用`bypass_exec`。

#### 命令执行：

* 攻击者现在可以通过访问以下方式执行命令：`http://www.example.com/script.php?cmd=<command>`

这个详细的步骤概述了创建和部署PHP扩展以执行系统命令的过程，利用了`dl`函数，理想情况下应禁用以防止此类安全漏洞。

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们独家的[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或在**Twitter**上关注我们 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>
