November 20, 2026
Lab: 2FA bypass using a brute-force attack | PortSwigger | Authentication
Category: Authentication Difficulty: Expert

By Irfan
3 min read
Lab Overview:
Platform: PortSwigger Web Security Academy Category: Authentication Difficulty: Expert Tool: Burp Suite (Proxy, Macros, Session Handling Rules and Intruder)
In this lab, I explored how an improperly implemented 2FA mechanism can be bypassed through brute-force testing, even when the application terminates the session after two incorrect attempts.
The application provides default credentials (carlos:montoya), but the 2FA verification code is unknown. The main challenge is that the application logs out the user after two incorrect verification attempts, making a traditional brute-force approach ineffective.
To overcome this restriction, Burp Suite Macros and Session Handling Rules are used to automate the authentication process and maintain the required session information during the attack.
Author's Note :
The key challenge in this lab is handling the automatic logout mechanism after two incorrect 2FA attempts.
Instead of sending repeated verification requests using a normal brute-force approach, we configure a macro in Burp Suite to automate the authentication workflow.
A macro is a predefined sequence of HTTP requests that Burp Suite automatically executes in a specific order.
While configuring the macro, carefully select the following three HTTP requests in the correct order:
GET Login Page: Retrieves the initial login page and its required security parameters. POST Login Request: Submits the username and password. GET 2FA Page: Retrieves the two-factor authentication page and the associated security parameters.
Make sure all three requests are selected correctly and in the proper sequence. Selecting the wrong requests can cause the macro to fail or return incorrect session information.
Configuring the Macro:
Navigate to Settings → Sessions → Session Handling Rules.
Click Add to create a new session handling rule.
Under Rule Actions, select Run a macro. Configure the URL scope by selecting Include all URLs.
Restrict the tool scope to Intruder only.
Select the macro containing the three required HTTP requests in the correct order.
Testing the Macro
After configuring the macro, use the Test Macro option to verify that the automated sequence works correctly. The macro executed successfully and returned the required CSRF token. One important observation is that the CSRF token changes whenever the macro is executed. This is expected behaviour when the application generates a new token dynamically.
Important step & idea:
CSRF Token Extraction: Before launching Intruder, configure the macro (
GET /login2) to extract the dynamically generatedcsrftoken from the HTML response and pass it to thePOST /login2request. While Burp's Cookie Jar automatically updates thesessioncookie in the background, thecsrfbody parameter must be dynamically overwritten on every single request so the server doesn't reject it as expired.
Configuring Intruder:
Send the POST /login2 request to Intruder. Note that any existing csrf value or session cookie currently visible in the request tab only acts as a placeholder—Burp's Session Handling Rule and Macro will automatically overwrite both with fresh values right before each payload is sent.
-
Payload Position: Selected the 2FA verification code parameter.
-
Payload Type: Selected Numbers or Brute Forcer (remove alphabets only numbers included)
-
Payload Range: If Numbers Configured the payload range from
0000to9999, it generates 10,000 possible four-digit verification codes. -
Resource Pool: Created a custom resource pool with maximum concurrent requests set to 1 to enforce sequential, single-threaded request execution and avoid rate-limiting or state conflicts.
-
Now start attack.
Wait for 302 request,
we can use negative search and filtering option for easy pupose i use "incorrect security code" to get response easily I also filtered to show only 3xx status codes unsuccessful responses are hidden.
After identifying the 302 response, I right-clicked on the corresponding successful request and selected Show in browser.
paste that on the portswigger lab page in the browser you can see the lab is solved.
Conclusion:
In this lab why session-level defenses alone cannot stop automated brute-force attacks. Even though the application logs users out after two failed attempts and regenerates a single-use CSRF token on every request, chaining Burp Suite Macros with Session Handling Rules allows us to automatically re-authenticate, extract a fresh CSRF token, and inject it into each Intruder payload.
In real-world applications, developers should implement account-level rate limiting, progressive delays after failed attempts, strict OTP expiration, replay prevention, and proper server-side attempt tracking. Passkeys can provide stronger phishing-resistant authentication compared to traditional OTP-based methods.
Thank you for reading!