Skip to content
Account takeover Disclosed report Intermediate

Bypassing password authentication of users that have 2FA enabled

When a user has two-factor authentication enabled, the sign-in flow could be tricked into verifying a one-time code against a different account by adding a login parameter to the request. An attacker who knows a victim's username and has a valid code for their own account could sign in as the victim without knowing the victim's password.

jobert Follow

Originally disclosed on HackerOne

  • Triaged in 16 hours
  • Fixed in 11 days
  • Disclosed after 14 days

Summary by the researcher

This vulnerability allowed password authentication to be bypassed when two-factor authentication was enabled for a user. @GitLab resolved this 2 days after I reported it to them. The commit that fixed the bug can be found at https://gitlab.com/gitlab-org/gitlab-ce/commit/00da609cfd8bf1105fe433dfc92ab263d6205eaf. The bug has been fixed in GitLab version 8.6.5.

The report

Proof of Concept

When a user has 2FA enabled, it's possible to sign in as that user without the need to know its password. To reproduce this attack, you need two users that both have 2FA enabled. For the sake of this PoC, lets call them Jane and John. Jane is the attacker and wants to get access to John's account. John his username is john. Jane knows John's username. Here's how you can reproduce it:

  • as Jane, go to the sign in page and enter your username and password
  • in the background, it sets Jane's user ID in session[:otp_user_id]
  • you now need to enter Jane's 2FA code in order to get access to the account
  • now intercept all your network traffic with a tool like Burp Suite and capture the request that is send when you submit the 2FA token - it looks like this:
> POST /users/sign_in HTTP/1.1
> Host: 159.xxx.xxx.xxx
> ...

> ----------1881604860
> Content-Disposition: form-data; name="user[otp_attempt]"
> 
> 212421
> ----------1881604860--
  • now add the login header to the request - the request now looks like:
> POST /users/sign_in HTTP/1.1
> Host: 159.xxx.xxx.xxx
> ...

> ----------1881604860
> Content-Disposition: form-data; name="user[otp_attempt]"
> 
> 212421
> ----------1881604860
> Content-Disposition: form-data; name="user[login]"
> 
> john
> ----------1881604860--
  • now, instead of 212421, send a valid OTP code for john to the server
  • Jane is now signed in as John by entering her own password and John's OTP code - Jane still doesn't know John's password

Impact

The OTP codes are 6 numbers that change every 30 seconds. I haven't looked whether the server allows time drift. This would increase the chance that an attacker guesses the right OTP code for the account. As a PoC, I could run a small attack against GitLab.com, but I haven't been able to reach @sytsem to ask for permission. ;)

Origin of the issue

This issue originates from the find_user method in the SessionsController. It returns a User object in two different ways: the first returns the object based on params[:login] parameter. The second one if sessions[:otp_user_id]. The params[:login] parameter takes precedence over the ID stored in the session. This means that if the params[:login] is specified in the request when the 2FA code needs to be verified, a different user can be selected to verify the code against. Here's the method:

Ruby
# app/controllers/sessions_controller.rb:58
def find_user
  if user_params[:login]
    User.by_login(user_params[:login])
  elsif user_params[:otp_attempt] && session[:otp_user_id]
    User.find(session[:otp_user_id])
  end
end

Fix

Here's a fix (needs specs to proof that it works): F83019.

The original report as published on HackerOne, by jobert. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

GitLab's sign-in flow supports two-factor authentication. When a user with 2FA enabled signs in, the application first checks the username and password, then asks for a one-time password (OTP) code before granting access. The report describes a flaw in that second step: the code that verifies the OTP could be made to verify it against a different user than the one who had just passed the password check.

The researcher reported the issue to GitLab, which resolved it two days later. The fix landed in GitLab version 8.6.5, and the commit is at https://gitlab.com/gitlab-org/gitlab-ce/commit/[redacted].

Setup

To reproduce the attack you need two accounts, both with 2FA enabled. In the proof of concept they are called Jane and John. Jane is the attacker and wants access to John's account. John's username is john, and Jane knows it. Jane does not know John's password.

The researcher used a tool such as Burp Suite to intercept network traffic and capture the request sent when the 2FA token is submitted.

Finding it

When a user with 2FA enabled signs in, the application stores that user's ID in the session under session[:otp_user_id]. The OTP verification step is supposed to look up the user from that session value. The find_user method in the SessionsController instead gives precedence to a login parameter supplied in the request, so a request that carries both the session ID and a login value selects the user named in login for OTP verification. That means the password check performed for one account can be paired with the OTP check for another.

Exploitation

Step 1: Sign in as Jane

As Jane, go to the sign-in page and enter your own username and password. In the background, the application sets Jane's user ID in session[:otp_user_id].

Step 2: Reach the 2FA prompt

You now need to enter Jane's 2FA code in order to get access to the account. At this point the application is waiting for an OTP for Jane.

Step 3: Capture the OTP submission

Intercept all your network traffic with a tool like Burp Suite and capture the request that is sent when you submit the 2FA token. It looks like this:

HTTP
> POST /users/sign_in HTTP/1.1
> Host: 159.xxx.xxx.xxx
> ...

> ----------1881604860
> Content-Disposition: form-data; name="user[otp_attempt]"
>
> 212421
> ----------1881604860--

The request is a POST to /users/sign_in. The multipart body carries a single field, user[otp_attempt], whose value is the OTP code being submitted.

Step 4: Add the login field

Add the login header to the request. The request now looks like:

HTTP
> POST /users/sign_in HTTP/1.1
> Host: 159.xxx.xxx.xxx
> ...

> ----------1881604860
> Content-Disposition: form-data; name="user[otp_attempt]"
>
> 212421
> ----------1881604860
> Content-Disposition: form-data; name="user[login]"
>
> john
> ----------1881604860--

The added part is a second multipart field, user[login], set to the victim's username, john. The session still holds Jane's otp_user_id, but the request now also names John.

Step 5: Submit John's OTP

Instead of 212421, send a valid OTP code for john to the server. Jane is now signed in as John by entering her own password and John's OTP code. Jane still does not know John's password.

Root cause

The issue originates from the find_user method in the SessionsController. It returns a User object in two different ways: the first returns the object based on the params[:login] parameter, and the second returns it based on session[:otp_user_id]. The params[:login] parameter takes precedence over the ID stored in the session. This means that if params[:login] is specified in the request when the 2FA code needs to be verified, a different user can be selected to verify the code against. Here is the method:

Ruby
# app/controllers/sessions_controller.rb:58
def find_user
  if user_params[:login]
    User.by_login(user_params[:login])
  elsif user_params[:otp_attempt] && session[:otp_user_id]
    User.find(session[:otp_user_id])
  end
end

The password check and the OTP check can therefore bind to different identities: the OTP is verified for whatever user the request names in login. The session value that was meant to pin the second factor to the first is simply ignored when login is present.

Impact

An attacker who knows a victim's username and has a valid OTP code for that victim can sign in as the victim without knowing the victim's password. The attacker needs their own account with 2FA enabled to start the flow, and needs the victim's OTP at the moment of submission.

OTP codes are 6 numbers that change every 30 seconds. The researcher had not looked into whether the server allows time drift, which would increase the chance that an attacker guesses the right OTP code for the account. As a proof of concept, the researcher could run a small attack against GitLab.com but was not able to reach @sytsem to ask for permission.

Fix

GitLab fixed the bug, and the fix is in GitLab version 8.6.5; the commit is linked above. The researcher also proposed a fix, noting that it needs specs to prove that it works, and referenced it as F83019.

Variants to test

  • Multi-step login flows that keep the pending user in the session. Try adding a login, username or user_id field to the second-step request and see whether the OTP is checked against the named account instead of the session account. A positive result is a successful sign-in as the named user using your own password and that user's OTP.
  • Parameter precedence in user lookup helpers. Look for methods that branch on request parameters before falling back to session state, as find_user does. A positive result is any path where a request parameter overrides a value the server set itself.
  • OTP drift windows. Where the server accepts codes outside the current 30-second window, measure how many codes are valid at once. A wider window raises the chance of guessing a victim's code during the attack above.
  • Other second-factor endpoints. Password reset, e-mail confirmation and step-up authentication prompts often reuse the same session-pinned user ID. Try supplying an alternative identifier in the request and check whether the factor is validated against the wrong account.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 12 min read.

What happened, in plain words

GitLab is a website where teams keep their code and work on it together. To get into your account there, you type a username and a password. Many people add a second lock on top of that, called two-factor authentication (2FA). With 2FA, after the password is accepted the site asks for a short code from a phone app. The idea is that a stolen password alone is not enough: the thief also needs the code, which changes every 30 seconds.

This report describes a bug in that second step. When you sign in with 2FA, GitLab remembers which account has just passed the password check, and then waits for that account's code. The researcher found that the request which submits the code could also carry a field naming a different account. When that field was present, GitLab checked the code against the named account instead of the one that had just typed the password. So an attacker could type their own password, then send the victim's code, and end up signed in as the victim, without ever knowing the victim's password.

The researcher reported it to GitLab, and GitLab fixed it two days later. The fix is in GitLab version 8.6.5, and the commit that fixed the bug is at https://gitlab.com/gitlab-org/gitlab-ce/commit/[redacted].

A few words you will meet below. A session is how a website remembers you between page loads: the server keeps a small record for you and gives your browser a short identifier so it can find that record again. Think of a cloakroom ticket: the ticket itself is not your coat, it just tells the attendant which coat is yours. A request is the message your browser sends to the server when you click something or submit a form. A parameter (or field) is one named piece of data inside that request, like a line on a paper form: a name and a value. An OTP (one-time password) is the short code from the phone app. Burp Suite is a program that sits between your browser and the website and lets you look at, and change, the requests before they are sent.

Setting up the test

To reproduce the attack you need two accounts, both with 2FA enabled. In the proof of concept they are called Jane and John. Jane is the attacker and wants access to John's account. John's username is john, and Jane knows it. Jane does not know John's password.

That setup matters: the attacker needs an account of their own with 2FA turned on, because the attack starts by signing in normally as themselves. The only thing they need to know about the victim is the username.

The researcher used a tool such as Burp Suite to intercept network traffic and capture the request sent when the 2FA token is submitted. Intercepting means the tool pauses each request on its way out so you can read it and edit it before it reaches the server.

Finding it

When a user with 2FA enabled signs in, the application stores that user's ID in the session under session[:otp_user_id]. In plain terms: after the password step, the server writes a note in your cloakroom record saying "the person waiting for a code is user number X". The OTP verification step is supposed to look up the user from that session value, so the code is always checked against the person who just typed the password.

The find_user method in the SessionsController instead gives precedence to a login parameter supplied in the request. A method is a named block of code that does one job; SessionsController is the part of the application that handles signing in and out. So a request that carries both the session ID and a login value selects the user named in login for OTP verification. That means the password check performed for one account can be paired with the OTP check for another: the first lock was opened by Jane, the second lock was opened for John, and the door opened for John.

Exploitation

Step 1: Sign in as Jane

As Jane, go to the sign-in page and enter your own username and password. In the background, the application sets Jane's user ID in session[:otp_user_id].

This step is deliberately ordinary. Jane is not doing anything unusual yet; she is proving she knows her own password, which she does. The point is to get the server into the state where it is waiting for a code, with Jane's ID written in the session.

Step 2: Reach the 2FA prompt

You now need to enter Jane's 2FA code in order to get access to the account. At this point the application is waiting for an OTP for Jane.

Jane does not actually want to finish signing in as herself. She only needs the page where the code is typed, because that is the request she is going to tamper with.

Step 3: Capture the OTP submission

Intercept all your network traffic with a tool like Burp Suite and capture the request that is sent when you submit the 2FA token. It looks like this:

HTTP
> POST /users/sign_in HTTP/1.1
> Host: 159.xxx.xxx.xxx
> ...

> ----------1881604860
> Content-Disposition: form-data; name="user[otp_attempt]"
>
> 212421
> ----------1881604860--

Line by line:

  • POST /users/sign_in HTTP/1.1 is the first line of the request. POST is the method used when you send data to the server, as opposed to just asking for a page. /users/sign_in is the address on the site that handles sign-in. HTTP/1.1 is the version of the language browsers and servers use to talk.
  • Host: 159.xxx.xxx.xxx says which server the request is for. The digits are an IP address, the numeric address of a machine on the internet, with the last parts hidden here.
  • ... stands for the other headers, the extra lines of information the browser normally attaches, which the report does not show.
  • The lines starting with ----------1881604860 are boundaries. When a form contains several fields, the browser sends them glued together in one body, separated by a made-up marker string so the server can tell where one field ends and the next begins. This format is called multipart form data.
  • Content-Disposition: form-data; name="user[otp_attempt]" names the field: user[otp_attempt]. The square brackets are a naming convention many web frameworks use to group fields under an object called user.
  • 212421 is the value of that field: the six-digit code Jane typed.
  • The final boundary with two trailing dashes, ----------1881604860--, marks the end of the body.

So the whole request says: "here is a code, please check it". The server will check it against whoever the session says is waiting, which at this moment is Jane.

Step 4: Add the login field

Add the login header to the request. The request now looks like:

HTTP
> POST /users/sign_in HTTP/1.1
> Host: 159.xxx.xxx.xxx
> ...

> ----------1881604860
> Content-Disposition: form-data; name="user[otp_attempt]"
>
> 212421
> ----------1881604860
> Content-Disposition: form-data; name="user[login]"
>
> john
> ----------1881604860--

Line by line, compared with the previous request:

  • The first line, the Host line and the ... are unchanged: same address, same server, same hidden headers.
  • The first field is unchanged: user[otp_attempt] still holds 212421.
  • A new boundary line separates the first field from the second.
  • Content-Disposition: form-data; name="user[login]" adds a second field called user[login].
  • john is its value: the victim's username.
  • The closing boundary ends the body as before.

The added part is a second multipart field, user[login], set to the victim's username, john. The session still holds Jane's otp_user_id, but the request now also names John. This is the heart of the attack: the request carries two identities at once, one in the session (Jane) and one in the body (John), and the bug is that the body wins.

Step 5: Submit John's OTP

Instead of 212421, send a valid OTP code for john to the server. Jane is now signed in as John by entering her own password and John's OTP code. Jane still does not know John's password.

Notice what Jane had to have: her own password, which she knows, and John's current code, which she does not normally have. The bug does not hand her the code; it removes the password requirement and lets her combine her own first factor with John's second factor. That is why the impact section below talks about how hard it is to obtain or guess a code.

Root cause

The issue originates from the find_user method in the SessionsController. It returns a User object in two different ways: the first returns the object based on the params[:login] parameter, and the second returns it based on session[:otp_user_id]. The params[:login] parameter takes precedence over the ID stored in the session. This means that if params[:login] is specified in the request when the 2FA code needs to be verified, a different user can be selected to verify the code against. Here is the method:

Ruby
# app/controllers/sessions_controller.rb:58
def find_user
  if user_params[:login]
    User.by_login(user_params[:login])
  elsif user_params[:otp_attempt] && session[:otp_user_id]
    User.find(session[:otp_user_id])
  end
end

Line by line:

  • # app/controllers/sessions_controller.rb:58 is a comment giving the file and line where the method lives. It is a note for humans, not code the computer runs.
  • def find_user starts the definition of a method called find_user. Its job, as the name says, is to find the user the current request is about.
  • if user_params[:login] asks: did the request include a login value? user_params is the collection of fields the request sent under the user name, and [:login] picks the one called login. If it is there, the next line runs.
  • User.by_login(user_params[:login]) looks up and returns the account whose username matches that value. This is the branch the attacker triggers by adding the field.
  • elsif user_params[:otp_attempt] && session[:otp_user_id] is the fallback, reached only when there is no login value. It asks: is there a code in the request, and is there a user ID stored in the session? The && means both must be true.
  • User.find(session[:otp_user_id]) returns the account for the ID stored in the session. This is the branch the developer intended for the second step of a 2FA sign-in.
  • end closes the if, and the second end closes the method.

The password check and the OTP check can therefore bind to different identities: the OTP is verified for whatever user the request names in login. The session value that was meant to pin the second factor to the first is simply ignored when login is present. In cloakroom terms, the attendant checks your ticket for one coat, then hands you a different coat because you also said a name out loud.

Impact

An attacker who knows a victim's username and has a valid OTP code for that victim can sign in as the victim without knowing the victim's password. The attacker needs their own account with 2FA enabled to start the flow, and needs the victim's OTP at the moment of submission.

OTP codes are 6 numbers that change every 30 seconds. The researcher had not looked into whether the server allows time drift, which would increase the chance that an attacker guesses the right OTP code for the account. Time drift here means accepting codes from slightly before or after the current 30-second window; the wider that window, the more codes are valid at any moment, and the easier a guess becomes. As a proof of concept, the researcher could run a small attack against GitLab.com but was not able to reach @sytsem to ask for permission.

Fix

GitLab fixed the bug, and the fix is in GitLab version 8.6.5; the commit is linked above. The researcher also proposed a fix, noting that it needs specs to prove that it works, and referenced it as F83019. Specs are automated tests that check a piece of code behaves as intended, so the note means the proposed change should come with tests before being trusted.

Variants to test

These are the same ideas applied to other places a site might make the same mistake.

  • Multi-step login flows that keep the pending user in the session. Try adding a login, username or user_id field to the second-step request and see whether the OTP is checked against the named account instead of the session account. A positive result is a successful sign-in as the named user using your own password and that user's OTP.
  • Parameter precedence in user lookup helpers. Look for methods that branch on request parameters before falling back to session state, as find_user does. A positive result is any path where a request parameter overrides a value the server set itself.
  • OTP drift windows. Where the server accepts codes outside the current 30-second window, measure how many codes are valid at once. A wider window raises the chance of guessing a victim's code during the attack above.
  • Other second-factor endpoints. Password reset, e-mail confirmation and step-up authentication prompts often reuse the same session-pinned user ID. Try supplying an alternative identifier in the request and check whether the factor is validated against the wrong account.

Glossary

  • 2FA (two-factor authentication): a second check at sign-in, usually a short code from a phone app, on top of the password.
  • Session: the server's memory of you between requests, found again through a short identifier your browser holds.
  • Session ID: the value that lets the server find your session record.
  • Request: the message your browser sends to a server when you load a page or submit a form.
  • POST: the request method used to send data to the server.
  • Header: an extra line of information attached to a request, such as the server's address.
  • Parameter / field: one named piece of data in a request, with a name and a value.
  • Multipart form data: a way of packing several form fields into one request body, separated by boundary markers.
  • Boundary: the made-up marker string that separates fields in multipart form data.
  • OTP (one-time password): a short code that is valid only briefly, here 6 digits changing every 30 seconds.
  • Token: a value submitted to prove something, here the OTP code.
  • Burp Suite: a tool that sits between browser and server so you can read and edit requests.
  • Intercept: pause a request on its way out so you can inspect or change it.
  • Controller: the part of a web application that handles a kind of request, such as signing in.
  • Method: a named block of code that performs one job.
  • Parameter precedence: which of two supplied values a piece of code uses when both are present; here the request value beat the session value.
  • Root cause: the underlying coding mistake that makes an attack possible.
  • Time drift: accepting OTP codes from slightly outside the current 30-second window.
  • Specs: automated tests that check code behaves as intended.
  • Commit: a recorded change to a codebase, identified by a long hash.
  • IP address: the numeric address of a machine on the internet.
  • Username: the public name that identifies an account, as opposed to its password.

More by jobert

View all

67 more disclosed reports, best first.

More from GitLab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Account takeover

The lesson behind this report.

Getting into someone else's account through a flaw in sign-in, password reset, second factor, single sign-on or session handling.