> 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/authentication/2fa-bypass-via-brute-force.md).

# 通过暴力破解绕过 2FA

通过暴力破解绕过 2FA，PortSwigger Web Security Academy 实验笔记，包括利用流程、载荷测试、Burp Suite 验证以及修复上下文。

### 使用暴力破解攻击绕过2FA

此实验有两个因子可被暴力破解攻击利用。/ 用户ID已知，但2FA验证码无法获取。/ 目标是强制获取MFA代码，以访问以下账户 **Carlos**.

* **受害者**: `carlos`
* **密码**: `montoya`

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3834412bbcf44d707207510096f72ba0b0eccada%2F%7B4DED2AEE-EA63-4072-9E04-BAF3CFC206DC%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**2FA 身份验证上下文**

在使用用户名和密码成功连接后，应用程序会要求输入MFA代码。

服务器端请求如下所示：

```bash
csrf=pqkmb1Cny2T3wbbgKEdqZPweWFpyCv0A&mfa-code=1234
```

**遇到的问题**

* 在测试多个MFA代码时，服务器会提示 **CSRF令牌已不再有效**.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-964d0e1a2d6d3bd6103dc107200e6915e9773250%2F%7BC01D1654-0FBB-418B-9281-E7AE950854A1%7D.png?alt=media" alt="" width="464"><figcaption></figcaption></figure>

* 随后，应用程序会自动重定向到登录页面。

每次重新访问登录页面时，都会生成一个 **新的CSRF令牌** 。

这会阻止对 `mfa-code` 参数中指定管理员的用户 ID 来获得管理员权限。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7201e3354f2bd7840af1ba91304b8ddd869057b6%2F%7BAA71B106-2316-4CD6-ADDA-6F8FA67512DD%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**设置解决方案（Burp Suite）**

为绕过此保护，使用一个 **宏** 以便在每次尝试时自动重新生成有效的CSRF令牌。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-89547f86ba355f76c71e4433af94aa50d6ea96e1%2F%7BCC2731F5-E07D-487D-8B38-81D8336FEA6B%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1e764a2d92dac7dde3dd174cf8b5dcdc87e39fab%2F%7B3EF5EB33-F316-4FF5-AFDA-E9FEFFC38E36%7D.png?alt=media" alt="" width="461"><figcaption></figcaption></figure>

**步骤：**

1. 创建一个新的 **宏** 在 Burp Suite 中。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f3b8e88622c250b5d11f0576c296d378414bf96d%2F%7B595C3E70-CED1-4912-A6E8-C88D680E9903%7D.png?alt=media" alt="" width="459"><figcaption></figcaption></figure>

* 在宏中包含以下请求：
* `GET /login`
* `POST /login`
* `GET /login2`

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c3f201c93c6c856f7f18662c8ea82fa2c003e8de%2F%7B3863FEBE-6FF4-468F-B4D3-A7747BA24BD0%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

1. 验证该宏是否允许提取一个 **新的CSRF令牌** 在每次执行时。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-525be5a3d11a9b8834309c181841d449d13c765d%2F%7BC488BB59-A9A6-4DFB-B938-5A3750FFBD80%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

该宏现在已准备就绪并可正常运行。

**MFA 代码暴力破解攻击**

1. 拦截 `POST` 包含 `mfa-code` 参数中指定管理员的用户 ID 来获得管理员权限。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3672181bbd5df883883eae782d5a77203cd591b5%2F%7BE49939DA-088C-4E64-96CC-34CD2A46A4F4%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* 将此请求发送到 **Intruder**.
* 在 `mfa-code` 字段上设置有效载荷。
* 设置一个数值范围，从 **0000 到 9999**，格式为 **4位必填数字**.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-78278bbb025425207b01cc78dbea415d4ea11e67%2F%7BC0CE4BB8-36E4-47F9-BC97-82B1FFB7FB68%7D.png?alt=media" alt="" width="394"><figcaption></figcaption></figure>

关联宏，以便 **CSRF会自动更新** 到每个请求中。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c9e9c533009cc059e9deb568e51cd7b30d58893d%2F%7BC167ABD2-994F-4D27-B3BE-B95DFB48A301%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**结果**

* CSRF令牌会随着每次尝试正确变化。
* 当测试到有效的MFA代码时，服务器响应会有所不同。
* 正确的代码可通过一个 **`302 已找到`** 响应识别出来，表明已成功重定向到用户账户。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-229a1a4f28b1aea19f7b3b757caf5ec08da82557%2F%7BD4634311-3A29-461E-9A0E-79F7D5407B5F%7D.png?alt=media" 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 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/authentication/2fa-bypass-via-brute-force.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.
