> For the complete documentation index, see [llms.txt](https://sayonara.gitbook.io/writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sayonara.gitbook.io/writeups/hackthebox/machines/awkward.md).

# Awkward

## <mark style="color:red;">Intro</mark>

In this article I’m going to be tackling `Awkward` machine, a medium difficulty Linux machine on `hackthebox.com`.

in this machine we will be preseneted with `vue js files` loaded with the page where we will search for routes and exploit a `jwt malformed token` to bypass authentication and get access to the hr dashboard, exploit `ssrf` to leak internal server resources where we will find an `api documentation` on port 3002, using this api we will exploit `awk` to get a `command injection` and extract a user's `ssh credentials` to gain initial foothold and from there exploit a `sed` command to get `command injection` and pivot to the www-data user which have access to a file that we will abuse through the `mail command` to get root privileges

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image000.png" alt="image"></div>

## <mark style="color:red;">Information Gathering</mark>

as always starting by doing nmap

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image001.png" alt="image"></div>

we see in the nmap scan output that only 2 ports are open `ssh` on port 22 and `http` on port 80 which is running `nginx server` let's take a look at the website running on port 80

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image002.png" alt="image"></div>

it redirects us to `http://hat-valley.htb` so lets add this to the `/etc/hosts` file so we can access this website

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image003.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image004.png" alt="image"></div>

Now you can be able to access the website

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image005.png" alt="image"></div>

using `wappalyzer` we can see that it's a nodejs application running on ubuntu's nginx server

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image011.png" alt="image"></div>

if we navigate the website we can see that an `online store` is under building so let's fuzz the website for subdomains that may lead us to find the online store website

![image](https://www.linkpicture.com/q/pasted_image008.png)

### <mark style="color:purple;">Subdomain FUZZING Using ffuf</mark>

`-mc all` => match all codes

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image006.png" alt="image"></div>

now let's filter by size and remove all responses with 132 size

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image007.png" alt="image"></div>

we have found the subdomain `store.hat-valley.htb`, let's add it to the `/etc/hosts` file so we can view the website

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image009.png" alt="image"></div>

the subdomain wants us to login

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image010.png" alt="image"></div>

the application uses php since the application runs normally with the `/index.php`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image012.png" alt="image"></div>

### <mark style="color:purple;">Directory Enumeration</mark>

enumerating directories on the root of the website

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image013.png" alt="image"></div>

enumerating directories on the store subdomain

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image014.png" alt="image"></div>

seems that enemerating directories doesn't bring somthing of value so let's try enemerating the website frontend files if we inspect the page we will find a bunch of files let's dig and find out if we are going to find something of interest

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image015.png" alt="image"></div>

this is a `vue application` (vue is javascript frontend framework)

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image016.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image017.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image019.png" alt="image"></div>

let's test out those routes, we can notice that the `/leave` and `/dashboard` routes are redirecting us to `/hr` route so login in is mendatory to acces the other routes

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image018.png" alt="image"></div>

let's look for other vue files maybe we can find other routes if we look at the directory called services we have 4 js files let's dig into these files and extract any useful informations so in all those files we get those routes :Cancel changes

`/api/all-leave` returns jwt malformed

`/api/submit-leave`

`/api/login`

`/api/staff-details` returns jwt malformed

`/api/store-status`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image020.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image021.png" alt="image"></div>

## <mark style="color:red;">Authentication Bypass by removing the Cookie</mark>

since It throws the error of `JWT token Malformed` I think it is a token error so let's pass this without token intercept the request using burp and we can see a token with guest value let's remove and send the request we get invalid user

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image022.png" alt="image"></div>

doing the same but with the `/api/staff-details` and we get a list of users data

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image023.png" alt="image"></div>

exfiltrating data from the `/api/staff-details` route in a nice format using curl and jq

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image024.png" alt="image"></div>

so we have passwords that are hashed using some algorithm we're going to use this website to know the type of the hashes `https://hashes.com/en/tools/hash_identifier` and looking at the result we can see that the hash is `SHA256`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image025.png" alt="image"></div>

or use the `hash-identifier` command on linux

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image026.png" alt="image"></div>

now let's crack the hashes first thing to do before using `hashcat` or `john` the ripper let's use `crackstation`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image027.png" alt="image"></div>

now let's login using those credentials in the `/hr` route that we have found previously

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image028.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image029.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image030.png" alt="image"></div>

let's intercept this using `burp suite`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image031.png" alt="image"></div>

`%22http:%2F%2Fstore.hat-valley.htb%22` => url decode : `"http://store.hat-valley.htb"`

&#x20;![image](https://www.linkpicture.com/q/pasted_image032.png)

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image033.png" alt="image"></div>

### Server Side Request Forgery SSRF leak internal resources

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image034.png" alt="image"></div>

### <mark style="color:purple;">**using ffuf to FUZZ all possible ports**</mark>

we are escaping the " because ffuf removes them if we didn't escape them

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image036.png" alt="image"></div>

`-fs 0` => filtering by size and removing empty responses

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image035.png" alt="image"></div>

let's take a look at the `internal port 3002`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image037.png" alt="image"></div>

let's open it in the browser and take a deeper look and this the `internal api documentation` of all the routes we found previously

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image039.png" alt="image"></div>

`/api/login` route is not vulnerable

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image040.png" alt="image"></div>

## <mark style="color:red;">Abusing Awk to get local file inclusion</mark>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image044.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image045.png" alt="image"></div>

i will show u how can this syntax `awk '\{user}\' /var/www/private/leave_request.csv` get u `File Disclosure Vunerability`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image048.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image049.png" alt="image"></div>

so if we find a way to `poison the user value` (token username) we could get a `File Disclosure Vulnerabilty`

### <mark style="color:purple;">Poisoning The User Token</mark>

copy the user token value from the `storage -> Cookies -> http://hat-valley.htb` so if we find a way to poison the user value (token username) we could get a file disclosure vulnerabilty

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image050.png" alt="image"></div>

and then navigate to the `jwt.io` website to decode the token

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image052.png" alt="image"></div>

in order to `poison the token` we need to make sure that the `signature is valid` and to do that we need to `crack the JWT token`

### <mark style="color:purple;">Cracking the JWT</mark>

cracking the JWT using `hashcat`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image053.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image054.png" alt="image"></div>

and now our token have a verified signature

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image055.png" alt="image"></div>

### <mark style="color:purple;">Python Exploit to poison the token and get local file disclosure vulnerability</mark>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image057.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image059.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image058.png" alt="image"></div>

so we can notice that `PWD=/var/www/hat-valley.htb` and since the application is a nodejs application there must be a `package.json` file so let's leak this file

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image060.png" alt="image"></div>

now we know the path to the `server.js` file let's leak this file

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image061.png" alt="image"></div>

discolsing `nginx configuration file` for the store subdomain `(nginx configuration files always stored in /etc/nginx/sites-available/*.conf)` what this does is if the store subdomain is directed to `/cart` or `/product-details` return 403 (forbidden) end with `.php` require a password which exist in `/etc/nginx/conf.d/.htpasswd`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image063.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image064.png" alt="image"></div>

let's identify the hash type using `hash-identifier`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image065.png" alt="image"></div>

now let's crack it using hashcat, but it couldn't crack it

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image066.png" alt="image"></div>

let's move on and look for the users that exectutes `/bin/bash`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image067.png" alt="image"></div>

let's take a look at the `.bashrc file` of the user bean (i couldn't discolse the same file for other users) analyzing the file we can notice an alias that backups the user bean's backup and this seems interesting

![image](https://www.linkpicture.com/q/pasted_image068.png)

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image069.png" alt="image"></div>

let's take a look at the backup\_home bash script

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image070.png" alt="image"></div>

let's extract the `bean_backup_final.tar.gz` from the victim machine, and to do that we need to modify the python script and read the content of the compressed backup as bytes and save it to a file so we can decompress it without errors

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image071.png" alt="image"></div>

now let's run the python script and extract the compressed GZIP data

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image072.png" alt="image"></div>

extracting the gzip using tar, and we have the user's bean backup home

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image073.png" alt="image"></div>

let's see if there is any hidden directories

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image074.png" alt="image"></div>

Discovering bean's credentials in his xpad directory, change directory to `.config/xpad` and read the `content-DS1ZS1` file which contains some `bean's credentials`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image075.png" alt="image"></div>

let's try to ssh using those credentials, and we are in cool

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image076.png" alt="image"></div>

## <mark style="color:red;">User Flag</mark>

we have found our first flag

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image077.png" alt="image"></div>

let's try if the password we have found in the .config/xpad `014mrbeanrules!#P` may be the password for the store the username is `admin` and the password may be `014mrbeanrules!#P`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image064.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image078.png" alt="image"></div>

and we are in

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image079.png" alt="image"></div>

now let's move to the directory where the store file are existed

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image080.png" alt="image"></div>

after reading the code in the files i found 3 dangerous system calls in the cart\_actions.php file where we may get command execution but if we look at the code we will find that there is blacklist on the special characters so it's not possible to get a command execution from the first 2 system calls but the last one using sed we may exploit it

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image081.png" alt="image"></div>

let's understand how sed can be exploited to inject commands here we have replaced the first line with the command id

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image082.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image087.png" alt="image"></div>

so we can inject in the `{$item_id} field`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image084.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image088.png" alt="image"></div>

let's see where is the add item in the code

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image085.png" alt="image"></div>

now let's go to the shop in the store web application (store.hat-valley.htb) and add some item to the cart and intercept this request using burp

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image086.png" alt="image"></div>

now we will modify the item parameter and the action parameter where the user will be used to inject the commands and action will be delete\_item so sed will be called and if we execute this we sleep for 3 seconds

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image091.png" alt="image"></div>

generate a `reverse shell script` and put it in the `tmp directory`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image098.png" alt="image"></div>

start listener

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image099.png" alt="image"></div>

add\_item first and then delete\_item with item is `1/d'+-e+"1e+/tmp/reverseshell.sh"+'` to execute the reverse shell

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image093.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image094.png" alt="image"></div>

<mark style="color:purple;">**Upgrading Simple Shells to Fully Interactive TTYs**</mark>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image096.png" alt="image"></div>

listing processes

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image097.png" alt="image"></div>

and this looks interesting it looks like its `monitoring the leave_requests.csv file`

![image](https://www.linkpicture.com/q/pasted_image100.png)

let's take a look at the leave\_requests.csv file

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image105.png" alt="image"></div>

let's use `PSPY` to `monitor linux processes without root privileges`, you can download it from this github repository `https://github.com/DominicBreuker/pspy` and then transfer it to the www-data session so we can use it there

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image101.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image103.png" alt="image"></div>

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image104.png" alt="image"></div>

when we update the leave\_request.csv file it invokes a `new process called mail` which runs with `root privileges` and based on what we get from the pspy we can see the mail command format that runs in the notify.sh script so the schema of the mail command could be this `mail -s "Leave Request: " $name christine`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image113.png" alt="image"></div>

to exploit the mail command go to `gtfobins` and search for mail to see how mail can be abused to execute scripts `echo 'bean --exec="\!/tmp/reverseshell.sh"' >> leave_requests.csv` before you execute this make sure you have `set up a listener to catch the reverse shell`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image109.png" alt="image"></div>

we have successfully executed the reverse shell script using the mail command and we have spawned a root shell

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image110.png" alt="image"></div>

## <mark style="color:red;">Root Flag</mark>

and here is the `root flag`

<div align="left"><img src="https://www.linkpicture.com/q/pasted_image112.png" alt="image"></div>

<div align="left"><figure><img src="https://1410593648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYI2noEqPw69jd0hR7Prp%2Fuploads%2FYrilNv8zF0nkSxMhUTDZ%2Fimage.png?alt=media&amp;token=e5292863-8d6d-4ed2-8e94-0085f66b75d6" alt=""><figcaption></figcaption></figure></div>

hope you found this walkthrough easy to understand and follow

Greeting From [<mark style="color:red;">Sayonara</mark>](https://github.com/ismail-arame)<br>
