2FA Bypass using OAuth2 apps to generate access tokens
A user who has not set up two-factor authentication (2FA) can still authorise an OAuth2 application and obtain an access token. This token can then be used with the REST API to access 2FA-protected groups and projects, bypassing the 2FA requirement.
GitLab
Originally disclosed on GitLab
- Fixed in 41 days
- Disclosed after 41 days
HackerOne report #900323 by fushbey on 2020-06-17, assigned to @jeremymatos:
Summary
Hi, I want to report a 2FA bypass, this time using OAuth2 apps. When creating 2FA enabled groups one of my test accounts was redirected to the 2FA setup page, there was theoretically no way to go anywhere unless I set up 2FA first, I looked at a bypass, I found PAT is still valid to interact with the REST API, however, that is not a bug, but a by design decision, ok, so if using PAT is not considered a vulnerability, let's generate one PAT at https://gitlab.com/profile/personal_access_tokens, that does not work because we are redirected to the 2FA setup page, we can't move from this page until we set it up, however, there is another way to generate access tokens, using OAuth2 apps.
An attacker compromising a user can still get full access to any Gitlab feature the user is allowed to without 2FA by creating a OAuth2 app in the compromised user account(because it turns out https://gitlab.com/profile/applications is not restricted) or in another account, it does not matter.
Then the compromised user will accept the created app, complete the OAuth2 flow which will lead to the generation of an access token, ready to be used with the REST API, as per the docs, the access tokens work regardless of 2FA enabled or disabled so here we have a 2FA bypass.
Steps to reproduce
1 - userA creates a private group.
2 - userA makes the private group a 2FA enabled group, so its access is restricted to users with 2FA configured.
3 - userA adds userB to the private group created above.
4 - At this point, userB can't access almost any page on Gitlab as he gets redirected to the 2FA setup page at https://gitlab.com/profile/two_factor_auth
5 - However, he can access https://gitlab.com/profile/applications which would allow him(or the attacker who just compromised him) to create OAuth2 apps, but most importantly, userB can authorize OAuth2 apps, and this is where the vulnerability is, so go ahead and create an OAuth2 app in any account, for simplicity create it on userB itself, add all scopes and a redirect URI you have access to for example http://localhost:9090
6 - Build the consent page URL for that application you just created, it has the following shape:
https://gitlab.com/oauth/authorize?client_id=APP_ID&redirect_uri=REDIRECT_URI&response_type=code&state=YOUR_UNIQUE_STATE_HASH
7 - userB visits the above consent page URL, accepts the app
8 - He gets redirected to http://localhost:9090/?code=[OAuth2Code]
9 - Exchange the code with an access token using the following template:
curl -X POST https://gitlab.com/oauth/token -d 'client_id=APP_ID&client_secret=APP_SECRET&code=RETURNED_CODE&grant_type=authorization_code&redirect_uri=REDIRECT_URI'
10 - Now you have an access token, you can use it to interact with the REST API as you want, The Web UI is restricting you, however with this access token you can do anything using the API.
This vector may be used to escalate further any other attack, because, for example, I reported other 2FA bypasses and showed how to push code without 2FA setup on a 2FA enabled group, however, there was a limitation, userB did not know the name of the protected projects in order to push code to them, well, using this vector the user can get the name of the projects using the API as explained above.
The process would look like:
- List groups userB belongs to:
$ curl -H 'Authorization: Bearer ACCESS_TOKEN...' https://gitlab.com/api/v4/groups | jq
- Once the 2FA protected group is located, list projects on it:
$ curl -H 'Authorization: Bearer ACCESS_TOKEN.....' https://gitlab.com/api/v4/groups/:group_id/projects | jq
Impact
Bypass 2FA protection layer, do anything as if the 2FA was not there.
Examples
(If the bug is project related, please create an example project and export it using the project export feature)
(If you are using an older version of GitLab, this will also help determine whether the bug has been fixed in a more recent version)
(If the bug can be reproduced on GitLab.com without violating the Rules of Engagement as outlined in the program policy, please provide the full path to the project.)
What is the current bug behavior?
A user that has not set up 2FA is allowed to authorize OAuth2 apps to act on his behalf allowing him to self escalate privileges and interact with 2FA enabled projects using the REST API.
What is the expected correct behavior?
- The creation of access tokens(and OAuth2 applications) should not be allowed to users who have not set up 2FA
- The user should not be allowed to grant access to OAuth2 apps when he has a 2FA setup process pending, he should first set up 2FA.
Impact
Read above.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the application does not enforce the 2FA setup requirement on the OAuth2 authorisation endpoint, allowing users with pending 2FA setup to grant access tokens.
Impact
An attacker who has compromised a user account can bypass the 2FA protection layer and perform any action the user is allowed to, including accessing 2FA-enabled groups and projects via the API.
What the attacker needed
Attacker has compromised a user account; victim user has not set up 2FA; victim user is member of a 2FA-enabled group
The fix
The application should prevent users who have not completed 2FA setup from creating OAuth2 applications or authorising them to generate access tokens.
How it was found
- Create a private group and enable 2FA requirement for it.
- Add a second user to the group; that user is redirected to the 2FA setup page and cannot access most pages.
- The second user can still access the OAuth2 applications page and create an OAuth2 app with all scopes and a redirect URI.
- Build the consent page URL for the app and visit it as the second user, then accept the app.
- The user is redirected to the redirect URI with an authorisation code.
- Exchange the code for an access token using the token endpoint, then use the token to call the REST API.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Two-factor authentication (2FA) adds a second step to logging in, like a code from your phone. The report says that even if you haven't set up 2FA, you can still create an OAuth2 app (a way for another program to access your account) and get a special key (access token). That key lets you use GitLab's API (a way to interact with the site programmatically) to do things you shouldn't be able to do until you set up 2FA.
What is Account takeover?
Getting into someone else's account through a flaw in sign-in, password reset, second factor, single sign-on or session handling.