Django Privilege Escalation – Zero To Superuser
A user profile update endpoint accepted extra JSON fields that were not meant to be editable, allowing the attacker to set their own username and privilege flags. By sending is_superuser and is_staff as true, the attacker gained superuser access to the Django admin panel.
Published on seanmelia.files.wordpress.com
Django Privilege Escalation – Zero To Superuser Sean Melia June 1 2017 I was invited to a bug bounty program on 5/31. I initially kicked off dirsearch on their dev environment which they wanted tested.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Sean Melia was invited to a bug bounty program on 5/31. The program had a development environment that they wanted tested. The application exposed a Django admin login interface at /admin/. The main application had a REST API under /api/v1/, including a user resource at /api/v1/user/<id>/. The application also had a regular signup page and a profile update feature that allowed users to change their first name, last name and similar details. The profile update did not offer an option to change the username or to upgrade or downgrade privileges.
Setup
The researcher created an account via the regular signup page using his email address. He did not know at the time that regular user accounts were stored in the same database as the admin accounts for the admin panel. He used a browser session with the main user application and also visited /admin/. No specific tools, versions, or other preconditions are mentioned beyond the use of dirsearch to discover /admin/.
Finding it
The researcher initially ran dirsearch on the development environment, which pointed him to /admin/. He found a Django admin login interface exposed to the internet. He wrote a report about locking this down to certain IPs and not exposing it to the Internet. The program replied that the internal console was locked down, but the login page was still visible. He decided to test that claim.
After creating an account and logging in, he found several bugs. One was an authorization bug on the second or third request after signing in that allowed him to enumerate other user account data including names, emails, password hashes, etc. The value after the /user/ path was vulnerable to parameter manipulation. The REST API endpoint was:
/api/v1/user/872/
In the response were two values: "is_superuser": "false" and "is_staff": "false". He found that interesting but continued to test other parts of the application and report other bugs.
He then went back to /admin/ in a browser where he had a session with the main user application and noticed the login page had changed to include that he was attempting to login with X account and he was not authorized. This alerted him to the fact that his session from the main app also corresponded to the Django admin panel.
Exploitation
Step 1: Identify the profile update endpoint and its parameters
On another page in the application, it allows you to update your profile information including your first/last name, etc. However there is no option to change the username or upgrade/downgrade privileges. The researcher decided to take the JSON POST data and add in another parameter "username":
{"firstName":"meals","lastName":"meals","email":"[redacted]", "smsPhone":"7035555666","username":"admin"}
In the response he noticed his username was changed from his email address to admin. The response was:
{"email": "[redacted]", "firstName": "meals", "first_name": "meals", "id": 872, "lastName": "meals", "last_login": "redacted", "last_name": "meals", "resource_uri": "/api/v1/user/872/", "smsPhone": "7035556666", ”username": "admin"}
He was able to pass in additional parameters which were then actually processed correctly on the backend.
Step 2: Attempt to escalate privileges with a superuser parameter
He went back to /admin/ and noticed the response had changed. He was attempting to login as admin however he did not have access to the panel still. He then decided to include the parameter "superuser": "true" which resulted in no change.
Step 3: Try is_superuser with a string value
He then tried "is_superuser": "true" and the response from the application stated:
{"error": "[\"'true' value must be either True or False.\"]"}
This indicated that the backend expected a boolean value, not a string.
Step 4: Supply boolean values for is_superuser and is_staff
He then supplied "is_superuser" : "True" and "is_staff": "True" and the request was processed. The response was:
{"date_joined": "redacted", "email": "[redacted]", "firstName": "meals", "first_name": "meals", "id": 872, "is_active": true, "is_staff": true, "is_superuser": true, "lastName": "meals", "last_login": "redacted", "last_name": "meals", "password": "redacted", "resource_uri": "/api/v1/user/872/", "smsPhone": "7035556666"username": "admin"}
He then navigated back to the /admin/ directory and was subsequently logged in.
Root cause
The application's profile update endpoint accepted additional JSON parameters beyond those exposed in the user interface. The backend processed these additional parameters, allowing the user to modify their own username, is_superuser and is_staff fields. The endpoint accepted fields the interface did not offer, and the backend processed them. The error message for "is_superuser": "true" revealed that the backend expected a boolean, which guided the researcher to use "True" (a string that the backend interpreted as a boolean). The report does not state whether any other defences were in place.
Impact
An attacker with a regular user account could escalate their privileges to superuser and staff, gaining access to the Django admin panel. This gave access to the Django admin panel. The attack required that the attacker have a valid account and that the profile update endpoint be accessible. The report does not specify the exact impact beyond gaining admin access.
Fix
Based on the root cause, the fix should be to enforce server-side authorization on the profile update endpoint, allowing only permitted fields to be updated and preventing users from modifying their own is_superuser, is_staff, or username fields unless explicitly authorized.
Variants to test
- Test other API endpoints that accept JSON for mass assignment vulnerabilities. Look for endpoints that return user objects with fields like
is_superuser,is_staff,role,permissions, etc. Try adding these fields to update requests and observe if they are processed. - Try changing the
usernameto an existing admin username to see if account takeover is possible. A positive result would be the ability to log in as that admin or to receive a response indicating the username was changed. - Attempt to set
is_activetofalseon other users to disable their accounts, or totrueon your own if it was disabled. A positive result would be the field value changing in the response. - Test for boolean type confusion by sending
"true"as a string,1,0,"True","False", etc. and observe error messages or successful processing. A positive result would be the field being set to the desired boolean value. - Check if the same mass assignment works on other user-related endpoints, such as registration or password reset, by adding extra fields. A positive result would be the extra fields being reflected in the response or stored.
What happened, in plain words
A security researcher was invited to test a company's development environment, a copy of their website used for testing rather than for real customers. While looking around, he found an administrative login page exposed to the public internet. He reported it, the company said the sensitive part behind it was locked down, and he decided to check whether that was really true.
He signed up for a normal account, like any visitor would. Then he discovered that the page where you edit your own profile would quietly accept extra pieces of information that the website's form never shows you. By adding a few of those extra fields to the request his browser sends when saving a profile, he was able to rename himself to admin and then flip two hidden switches that mark an account as a superuser and as staff. After that, the administrative panel let him in. A regular user had turned himself into the most powerful kind of account the application has.
This kind of flaw is called privilege escalation: going from a low level of access to a higher one without being given it. The specific mechanism, where a program accepts fields the user interface never offered, is often called mass assignment.
Background concepts
Before walking through the steps, here are the ideas you need.
- Server and browser. The browser on your computer asks for pages; the server is the computer that stores the website and answers. Anything the server decides is trustworthy, because the browser can be manipulated by the person using it.
- URL and path. A URL is a web address. The part after the site name, such as
/admin/, is the path, and it points at a particular page or feature on the server. - Django. A framework for building websites in the Python programming language. It comes with a ready-made administration panel, a private area where site staff manage data. That panel normally lives at
/admin/. - API. A way for programs to talk to each other. Instead of returning a pretty web page, an API returns raw data, usually in a format called JSON. Think of it as the difference between a restaurant's printed menu (the web page, made for humans) and the order slip the waiter hands to the kitchen (the API, made for machines).
- REST API. An API organised around addresses that represent things.
/api/v1/user/872/means "version 1 of the API, the user whose number is 872". - JSON. A text format for structured data, made of names and values in curly braces, like
{"name": "value"}. It is how the browser sends a profile update to the server and how the server sends data back. - GET and POST. Two kinds of request a browser makes. GET asks for something ("show me this page"). POST sends something to be saved or acted on ("here is my updated profile").
- Parameter. A named piece of data in a request. In
"firstName":"meals", the parameter name isfirstNameand its value ismeals. - Session and cookie. When you log in, the server gives your browser a small piece of data, stored in a cookie, that acts like a wristband at an event. On every later request the browser shows the wristband, and the server knows who you are without asking for your password again.
- Authorization versus authentication. Authentication is proving who you are (logging in). Authorization is what you are allowed to do once you are known. A bug where you are allowed to do something you should not be is an authorization bug.
- Privilege. A permission level. A normal user has few privileges; a superuser has all of them.
- Boolean. A value that is either true or false, nothing else. Some systems are strict about the difference between the word
true(a real boolean) and the text"true"(a string of letters that merely looks like it). - dirsearch. A tool that tries a long list of common paths against a website to see which ones exist, a bit like trying every door in a corridor to see which are unlocked.
- Bug bounty program. An arrangement where a company invites outside researchers to look for security problems and report them, often for a reward.
Finding the admin page
The researcher was invited to the program on 5/31. The company wanted its development environment tested. He ran dirsearch against it, and the tool pointed him at /admin/. Visiting that path showed a Django admin login interface, reachable from the open internet.
He wrote a report suggesting the page be restricted to certain IP addresses rather than exposed publicly. An IP address is the numeric label a computer has on a network, so restricting by IP means only allowing connections from known machines. The company replied that the internal console was locked down, but that the login page was still visible. He decided to put that claim to the test.
Creating an account and finding a data leak
He created an account through the regular signup page, using his own email address. At that point he did not know that ordinary user accounts and admin accounts live in the same database, the same organised store of data. At that point he did not know that ordinary user accounts and admin accounts live in the same database.
He logged in and found several bugs. One, on the second or third request after signing in, was an authorization bug that let him read other users' account data: names, emails, password hashes and more. A password hash is a scrambled form of a password; it is not the password itself, but it should never be handed out to other users. The flaw was that the number after /user/ in the address could simply be changed to another user's number. The endpoint was:
/api/v1/user/872/
Reading this line: it is the address of one user record in the API. api says this is the machine-readable interface, v1 is the version, user is the kind of thing, and 872 is the identifier of one particular user. The value after /user/ could be manipulated to reach other users' records. This is often called an insecure direct object reference: the server trusts the number you supply instead of checking that the record belongs to you.
In that response were two values, "is_superuser": "false" and "is_staff": "false". These are the two switches that decide whether an account can use the admin panel. He found them interesting, but carried on testing other parts of the application and reporting other bugs.
Noticing the shared session
He went back to /admin/ in the same browser where he was logged in to the main application. The login page had changed: it now said he was attempting to log in with a particular account and was not authorized. That told him something important. His session from the main app was also recognised by the admin panel. In other words, the wristband from the front door also works at the staff entrance; the only thing missing was the permission stamp on it.
Step 1: adding a field the form never shows
Another page in the application lets you update your profile: first name, last name and similar details. The page offers no option to change your username, and no option to upgrade or downgrade your privileges. That is the intended behaviour, and it is enforced only by what the page chooses to display.
He took the JSON data that the profile page sends when you save, and added one more parameter, "username", setting it to admin:
{"firstName":"meals","lastName":"meals","email":"[redacted]", "smsPhone":"7035555666","username":"admin"}
Line by line, this is one JSON object with five name/value pairs. firstName and lastName are the names shown on the profile. email is the contact address. smsPhone is a phone number for text messages. The last pair, username, is the addition: the profile form never sends it, but he typed it in by hand. The idea is simple: the form is only a suggestion of what to send, and nothing stops a person from editing the request before it leaves the browser.
The response showed that his username had changed from his email address to admin:
{"email": "[redacted]", "firstName": "meals", "first_name": "meals", "id": 872, "lastName": "meals", "last_login": "redacted", "last_name": "meals", "resource_uri": "/api/v1/user/872/", "smsPhone": "7035556666", ”username": "admin"}
This is the server's reply, the updated user record. email, firstName, first_name, lastName and last_name are the profile names, some stored twice under slightly different spellings. id is the user's number, 872. last_login is when the account last signed in. resource_uri is the address of this record in the API. smsPhone is the phone number. username is now admin. The key lesson: the server accepted and saved a field that the interface never offered. It trusted the client to send only the fields it was supposed to send.
Step 2: trying a superuser parameter
He returned to /admin/ and saw the response had changed. He was now attempting to log in as admin, but still had no access to the panel. The name alone does not grant rights; the switches do.
He then added the parameter "superuser": "true". Nothing changed. Nothing changed.
Step 3: the error message that gave the game away
He tried the correct field name, "is_superuser": "true", and the application answered with an error:
{"error": "[\"'true' value must be either True or False.\"]"}
This is an error message wrapped in JSON. The outer braces hold one pair, error, whose value is a piece of text. Inside that text, the server is complaining that the value 'true' must be either True or False. In plain words: the server was expecting a real boolean, a true/false value, and received a string, a piece of text that happens to spell "true". The quotes around it are the giveaway. Error messages like this are gold to a tester, because they describe exactly what the server wants.
Step 4: sending real boolean values
He supplied "is_superuser" : "True" and "is_staff": "True", and this time the request was processed. The response was:
{"date_joined": "redacted", "email": "[redacted]", "firstName": "meals", "first_name": "meals", "id": 872, "is_active": true, "is_staff": true, "is_superuser": true, "lastName": "meals", "last_login": "redacted", "last_name": "meals", "password": "redacted", "resource_uri": "/api/v1/user/872/", "smsPhone": "7035556666"username": "admin"}
Reading the important parts: date_joined is when the account was created. id is still 872, the same account. is_active is true, meaning the account is enabled. is_staff is now true, which grants access to the admin panel. is_superuser is now true, which grants full rights over the data. password is the stored password value. username is admin. The two switches that were false at the start are now true, and the server accepted the change without asking whether this user was allowed to make it.
He navigated back to /admin/ and was logged in.
Why it worked
The profile update endpoint accepted extra JSON parameters beyond the ones the interface shows. The backend, the part of the application that does the actual work, processed those parameters without checking whether this user was allowed to change them. The only thing stopping a normal user from setting is_superuser was that the form did not display the field. Anything the browser can send, a person can send, so a check that lives only in the page is no check at all. The error message about "true" versus True also revealed the expected type, which is what pointed him at the value that finally worked.
What an attacker could do with it
Someone with an ordinary account could raise themselves to superuser and staff, and walk into the Django admin panel. From there they had access to the Django admin panel. The requirements were modest: a valid account and access to the profile update feature.
How to fix it
The report does not say what fix the company applied. The lesson for defenders is to enforce permissions on the server: accept only the fields a user is permitted to change, and refuse any attempt to modify sensitive fields such as is_superuser, is_staff or username unless the user is explicitly authorised. Never rely on the interface to hide a field, because the interface is under the attacker's control.
Other places to look
- Try other API endpoints that accept JSON, looking for the same mass assignment pattern. Endpoints that return user objects with fields like
is_superuser,is_staff,roleorpermissionsare worth adding those fields to in an update request, to see whether they are processed. - Try setting
usernameto an existing admin's name, to see whether account takeover is possible. A positive result would be logging in as that admin, or a response showing the name was changed. - Try setting
is_activetofalseon other users to disable their accounts, or totrueon your own if it was disabled. A positive result is the value changing in the response. - Test how the server handles true/false values: send
"true"as text, then1,0,"True","False", and watch the error messages or the successful processing. A positive result is the field ending up with the value you wanted. - Check whether the same trick works on other user-related endpoints, such as registration or password reset, by adding extra fields. A positive result is those fields appearing in the response or being stored.
Glossary
- API: an interface that lets programs exchange data, usually returning JSON rather than web pages.
- Authentication: proving who you are, typically by logging in.
- Authorization: what you are allowed to do once you are known.
- Backend: the part of an application on the server that does the real work and stores data.
- Boolean: a value that is either true or false.
- Bug bounty program: an arrangement where a company invites outside researchers to find and report security problems.
- Cookie: a small piece of data a site stores in your browser, often used to remember your session.
- Database: an organised store of data, such as all user accounts.
- dirsearch: a tool that tries many common paths on a website to discover which exist.
- Django: a Python framework for building websites, which includes an admin panel at
/admin/. - Endpoint: a specific address in an API that performs a particular action.
- GET: a request that asks the server to return something.
- IP address: the numeric label a computer has on a network.
- JSON: a text format for structured data made of name/value pairs.
- Mass assignment: a flaw where a program accepts and saves fields the user interface never offered.
- Password hash: a scrambled form of a password, stored so the real password need not be kept.
- POST: a request that sends data to the server to be saved or acted on.
- Privilege: a permission level, such as normal user or superuser.
- Privilege escalation: raising your own access level without being granted it.
- REST API: an API organised around addresses that represent things, such as one user record.
- Session: the server's memory of your logged-in state, kept alive by a cookie.
- Superuser: the highest-privilege account type, with full rights over the data.
- Staff: an account flag that grants access to the admin panel.