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

# 2FA Bypass via Brute Force

2FA Bypass via Brute Force notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### 2FA Bypass Using a Brute-force Attack

This lab has two factors that are vulnerable to brute force attack. / User IDs are already known, but the 2FA verification code is not accessible. / The objective is to force the MFA code in order to access the account of **Carlos**.

* **Victime**: `carlos`
* **Password**: `montoya`

<figure><img src="https://131707722-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 Authentication Context**

After a successful connection with the username and password, the application requires an MFA code.

The server-side query looks like this:

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

**Problem met**

* When testing several MFA codes, the server indicates that the **token CSRF is no longer valid**.

<figure><img src="https://131707722-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>

* The application then automatically redirects to the login page.

With each new access to the login page, a **new token CSRF** is generated.

This prevents a direct brute force attack on the `mfa-code` parameter.

<figure><img src="https://131707722-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>

**Setting up the solution (Burp Suite)**

To circumvent this protection, a **macro** is used to automatically regenerate a valid CSRF token at each attempt.

<figure><img src="https://131707722-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://131707722-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>

**Steps:**

1. Create a new **macro** in Burp Suite.

<figure><img src="https://131707722-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>

* Include the following requests in the macro:
* `GET /login`
* `POST /login`
* `GET /login2`

<figure><img src="https://131707722-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. Verify that the macro allows to extract a **new token CSRF** at each execution.

<figure><img src="https://131707722-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>

The macro is now ready and functional.

**MFA Code Brute-Force Attack**

1. Intercept the `POST` query containing the `mfa-code` parameter.

<figure><img src="https://131707722-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>

* Send this request to **Intruder**.
* Set payload on the `mfa-code` field.
* Set a range of values from **0000 to 9999**, with a format of **4 required digits**.

<figure><img src="https://131707722-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>

Associate the macro so that **CSRF is automatically updated** to each query.

<figure><img src="https://131707722-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>

**Result**

* The CSRF token changes correctly with each attempt.
* When a valid MFA code is tested, the server response differs.
* The correct code is identifiable by a **`302 Found`** response, indicating a successful redirect to the user account.

<figure><img src="https://131707722-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/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.
