> For the complete documentation index, see [llms.txt](https://meitoka.gitbook.io/stash/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://meitoka.gitbook.io/stash/tryhackme-stuffs/challenges-ctf/publisher.md).

# Publisher

Writeup by FzF\_StormZ

<figure><img src="https://1205247251-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKPO62FppgTaVmwp5axJJ%2Fuploads%2FU5qI5P1f6NRcQZcykqNT%2F618b3fa52f0acc0061fb0172-1718377893997.png?alt=media&amp;token=1abdc8be-78c5-463a-b341-b8c84e506e30" alt="" width="375"><figcaption><p>Publisher challenge</p></figcaption></figure>

## Introduction

### Context

The "**Publisher**" CTF machine is a simulated environment hosting some services. Through a series of enumeration techniques, including directory fuzzing and version identification, a vulnerability is discovered, allowing for Remote Code Execution (RCE). Attempts to escalate privileges using a custom binary are hindered by restricted access to critical system files and directories, necessitating a deeper exploration into the system's security profile to ultimately exploit a loophole that enables the execution of an unconfined bash shell and achieve privilege escalation.

{% hint style="info" %}
It's my first writeup for a TryHackMe challenge. So, please be kind if you find some errors or bad explaination during your reading :smile:
{% endhint %}

### Pentesting environment

I use Exegol inside WSL 2.0 Ubuntu to have a full and working environment to perfom the challenges

{% embed url="<https://exegol.readthedocs.io/en/latest/>" %}
Exegol is a community-driven hacking environment, powerful and yet simple enough to be used by anyone in day to day engagements.
{% endembed %}

## Enumeration

First of all, we will launch a complet **Nmap** scan to try to discover possible services:

<pre class="language-bash"><code class="lang-bash">nmap -sT -sC -sV -Pn <a data-footnote-ref href="#user-content-fn-1">10.10.212.9</a> -v
</code></pre>

<figure><img src="https://1205247251-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKPO62FppgTaVmwp5axJJ%2Fuploads%2FJA9UP5wLglWBW9wGgd0I%2Fimage.png?alt=media&amp;token=ffdecc3d-589b-4105-8741-a3f15d996288" alt=""><figcaption></figcaption></figure>

As you can see, we have 2 open ports (22 and 80). The SSH port can be interesting if we find credentials to have a legit shell with the machine. But, for now, we will focus on the 80 port.

When I have a website during a challenge, i like to run 2 commands:

```bash
gobuster dir -u http://10.10.212.9 -x php,txt,html \
-w /usr/share/wordlists/seclists/Discovery/Web-Content/directory-list-2.3-medium.txt
```

```bash
nikto -host http://10.10.212.9
```

During the scan, I will walk around on the website. Every buttons are fake. We can get some informations like possible authors. As you can see below, the article was posted by "**Admin**" and talk about technology like **Spip**. Keep in mind

<figure><img src="https://1205247251-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKPO62FppgTaVmwp5axJJ%2Fuploads%2FanyBG7rS5YIFxwdEnIsz%2Fimage.png?alt=media&amp;token=329cbd15-3f68-4424-8e0f-6302617fb730" alt="" width="342"><figcaption></figcaption></figure>

Go back to our commands. Gobuster find interesting directories to explore:

<figure><img src="https://1205247251-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKPO62FppgTaVmwp5axJJ%2Fuploads%2F1wV8kHyel5sdihkYANTJ%2Fimage.png?alt=media&amp;token=83db26f0-48b1-4294-bdf4-6e6cfe7b9a00" alt=""><figcaption></figcaption></figure>

Directories to explore:

* /images: nothing to continue to this path
* /spip: the entire blog with login page !

When you have a web techno like Spip, we try to know the version. With the Wappalyzer Firefox extension, we obtain this information: **Spip 4.2.0** !

## Exploitation

Everytime I have a version service, I like to run this command:

<pre class="language-bash"><code class="lang-bash"><a data-footnote-ref href="#user-content-fn-2">searchsploit</a> "spip 4.2.0"
</code></pre>

<figure><img src="https://1205247251-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKPO62FppgTaVmwp5axJJ%2Fuploads%2FmKVoaLY9YoLyMOB2EugW%2Fimage.png?alt=media&amp;token=a3322b45-d446-4ea4-a95c-02be96857242" alt=""><figcaption></figcaption></figure>

Perfecto ! There is an exploit to have a **RCE** ... <mark style="color:red;">UNAUTHENTICATED</mark> ! So we don't need to find or bruteforce an account to perfom this exploitation.

<figure><img src="https://1205247251-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKPO62FppgTaVmwp5axJJ%2Fuploads%2FmUMm71s8ooH1Bugnmu4c%2Fimage.png?alt=media&amp;token=f066cf03-0211-4904-b614-727425b7ad7f" alt=""><figcaption><p>Exploit details (CVSS 9.8 !!)</p></figcaption></figure>

Before trying to run the exploit with the script, we can search if the exploit is inside the database of Metasploit to gain time and maybe get a meterpreter session.

As you can see, we have an output result which is close to our exploit (the second one):

<figure><img src="https://1205247251-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKPO62FppgTaVmwp5axJJ%2Fuploads%2Fe1tDxwim1ItYfX4gyJRH%2Fimage.png?alt=media&amp;token=2c6b77da-3f39-42ea-89a8-c1eba812da2f" alt=""><figcaption></figcaption></figure>

Choose it, complete options like:

* TARGETURI = /spip/
* RHOSTS = \[IP target]
* LHOST = \[IP attacker]

And run it !

**We did it** :tada:&#x20;

We have now an access to the machine with the `www-data` user. We can get the <mark style="color:red;">user</mark> <mark style="color:red;">flag</mark> to go to the `/home/think` directory.

## Privilege escalation

Now, I will upload an automatique script to enumerate possible privilege escalation which are possible with the web user. I can also try to have the control to the think user which can have more privileges to reach the root user.

After running linpeas.sh, I have this crucial information:

<figure><img src="https://1205247251-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKPO62FppgTaVmwp5axJJ%2Fuploads%2FEc6kZUPBLfVxqTR7UeOw%2Fimage.png?alt=media&amp;token=46fbf814-a40b-48d3-b7d1-37986e748d3e" alt=""><figcaption></figcaption></figure>

We have the private SSH key of the think user.

We download the SSH key, change permissions on it and run this command:

```bash
 ssh -i id_rsa think@10.10.212.9
 think@publisher:~$
```

We have access to the think user now !

With the hint, they talked about "AppArmor". This is a system to control what the user can do (so, that's why we can't write with the `think` user.

## AppArmor profile restriction

We can check if there is really an AppArmor profile:

<figure><img src="https://1205247251-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKPO62FppgTaVmwp5axJJ%2Fuploads%2FqAL00i7yKs6HrngwLEYv%2Fimage.png?alt=media&amp;token=02367099-f7f9-4a2c-8a0f-e126d7456b92" alt=""><figcaption></figcaption></figure>

So, we have to do find a way to perform a privilege elevation by searching manually (we can't upload linpeas.sh because of the AppArmor).

The first command we can run is this one (to check SUID bit on a file):

<pre class="language-bash"><code class="lang-bash">find / -type f -perm <a data-footnote-ref href="#user-content-fn-3">-u=s</a> 2> /dev/null

> /usr/sbin/run_container // An uncommon binary ... This can be interesting
</code></pre>

We can donwload it and do some reverse engineering on it to firstly understand what actions this binary performs.

With `Ghidra` inside the `main` function, we can see some interesting strings (we can also just run `strings run_container`):

<figure><img src="https://1205247251-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKPO62FppgTaVmwp5axJJ%2Fuploads%2F9lqMbZ41qdAjmeDlqza4%2Fimage.png?alt=media&amp;token=12040f83-03b7-4ba3-833b-605989005a7a" alt=""><figcaption></figcaption></figure>

So, the `bash` binary and the `run_container.sh`script are executed. We can see the contents of the script to find something interesting.

The only thing you must see after reading it, it's the command `docker` use multiple times inside the script but not with tha absolute path. As an attacker, we think directly to a possible PATH injection. The problem is we can't write anywhere to create our custom binary `docker`...

Go back to our AppArmor profile. Because, we want to replace the script with a custom script for example but we don't have the permissions to achieve it.

We need to check our profile to know which restrictions are enabled:

<figure><img src="https://1205247251-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKPO62FppgTaVmwp5axJJ%2Fuploads%2FuTapDOk8VMJQwe9ZIlC3%2Fimage.png?alt=media&amp;token=3f3a1169-acf7-462c-adaf-a87ae3cc9955" alt="" width="431"><figcaption></figcaption></figure>

As you see above, we have specific `write` denies on specific directories like `/opt/**` . But, this is not the case for `/var/tmp/**`

Next steps:

1. PATH injection
2. create our `docker` custom binary inside the `/var/tmp`
3. run `/opt/run_container.sh`
4. with a bash shell now, replace `/opt/run_container.sh` with your `docker` binary
5. run `/usr/sbin/run_container` to have a bash shell with root privileges

Honestly, it was quite difficult to see this at the first sight. I lost a lot of time to really understand what we have to do to get a root shell.

## Conclusion

What we learned during this room:

1. SPIP CVE exploit RCE
2. AppArmor security kernel module
   1. understand the profile system
3. PATH injection

[^1]: Your IP machine can be different

[^2]: Find POC exploit

[^3]: SUID bit for the user
