# Bypass

{% embed url="<https://tryhackme.com/r/room/bypass>" %}

The following post by 0xb0b is licensed under [CC BY 4.0<img src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" alt="" data-size="line"><img src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" alt="" data-size="line">](http://creativecommons.org/licenses/by/4.0/?ref=chooser-v1)

***

## Recon

We start with a Nmap scan and discover three open ports, SSH on port 22 and an `Apache/2.4.41` web server on ports 80 and 443, each of which provides a web page. After a version and default script scan, we only see that the results return a 403 forbidden and the certificate information on 443.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FQVeP91S9detbc2JNt7om%2Fgrafik.png?alt=media&#x26;token=403970f5-12fb-40ea-92ca-e8e496dc3076" alt=""><figcaption></figcaption></figure>

Calling the index page on port `80` only gave a `403` forbidden response, which we get on any requested resources. On the index page on port `443` we receive a login prompt that expects a password.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FkEMUg0YYbAvkhu4obcL9%2Fgrafik.png?alt=media&#x26;token=66b1540e-b5f8-47c6-9db7-831537e3e5fd" alt=""><figcaption></figcaption></figure>

If we look at the source, we discover an interesting comment that refers to the endpoint `/mail`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FKMTjJcBrKbL9P29xJzTi%2Fgrafik.png?alt=media&#x26;token=f0a58640-94d8-4772-bbdf-4428f15aa67e" alt=""><figcaption></figcaption></figure>

On this endpoint, we find a directory listing with the file `dump.txt.`

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FqGAYedEKXylvPVMo4ZUb%2Fgrafik.png?alt=media&#x26;token=536f4f06-0581-4d3e-acea-471642686f6d" alt=""><figcaption></figcaption></figure>

Here we find instructions to recover the password for the login on index. Each request on `/fpassword.php?id` recovers a part of the flag. To retrieve the flags at `/fpassword.php?id` one needs to send specially crafted packets first and then request the source. We will discuss each individual requirement below.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FDZVDjIVans1kP7fSKElW%2Fgrafik.png?alt=media&#x26;token=4a620aa0-3b0f-4fa6-9aff-b0045a2df387" alt=""><figcaption></figcaption></figure>

## First Login

As already stated, we have to restore parts of the password.

{% code overflow="wrap" %}

```
After receiving all the flags, you can visit the MACHINE IP that will ask you for the password. 
The first password will be concatenated values of all five flags you have received above.
```

{% endcode %}

### cctv.thm/fpassword.php?id=1

> ```
> Make a UDP request to the machine with source port number 5000. 
> Once done, you can fetch the flag through /fpassword.php?id=1
> ```

To make a UDP request, we use the command line tool nc with the flag `-u` to specify that we want to use UDP. Furthermore, we define the source and destination port as `5000` and additionally send some data to that port, even though the requested port seems closed on the nmap scan.

```bash
echo -n "Crafted packet for password recovery" | nc -u -w1 -p 5000 cctv.thm 5000
```

After submitting the command, we are able to retrieve the first flag at `https://cctv.thm/fpassword.php?id=1`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FlRqooHXsFDZRg60ZDzbL%2Fgrafik.png?alt=media&#x26;token=8c271a27-75c9-4cf7-a59a-9ccec2f7318a" alt=""><figcaption></figcaption></figure>

### cctv.thm/fpassword.php?id=2

> ```
> Make a TCP request to fpassword.php?id=2 with user-agent set as "I am Steve Friend". 
> Once done, you can fetch the flag through /fpassword.php?id=2
> ```

Here we have to make a TCP request with a special user-agent. Since HTTP operates on TCP, we just make an HTTP request using cURL with a custom user agent specified via parameter `-A`.

```bash
curl -A "I am Steve Friend" http://cctv.thm/password.php?id=2
```

After submitting the command, we are able to retrieve the second flag at `https://cctv.thm/fpassword.php?id=2`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F0J6BvpZtAIGvFe8i9hal%2Fgrafik.png?alt=media&#x26;token=15354afd-6200-44c2-9b58-71f8f848adfa" alt=""><figcaption></figcaption></figure>

### cctv.thm/fpassword.php?id=3

> ```
> Send a ping packet to the machine appearing as Mozilla browser 
> (Hint: packet content with user agent set as Mozilla). 
> Once done, you can fetch the flag through /fpassword.php?id=3
> ```

Next, for the third flag, we need to send data via an ICMP ping. For this we use the ping command using the `-p` option which allows you to specify the pattern used to fill the payload of the ICMP echo request packets. When you use `-p` followed by a pattern, such as `-p ff`, it fills the packet with the specified pattern.<br>

{% embed url="<https://stackoverflow.com/questions/31857419/how-to-send-a-message-with-ping>" %}

But we only have 16 bytes available, and the payload must be hex encoded. We use CyberChef to decode Mozilla to its hex representation `4d6f7a696c6c61`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FYyQi3f7kYg7sutkvqQ5M%2Fgrafik.png?alt=media&#x26;token=76e71d40-6ecd-4eee-b477-f93e8d072e3d" alt=""><figcaption></figcaption></figure>

```bash
ping -p 4d6f7a696c6c61 cctv.thm
```

After submitting the command, we are able to retrieve the third flag at `https://cctv.thm/fpassword.php?id=3`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FC8GYJQ5DDfXH6ZCtRkKa%2Fgrafik.png?alt=media&#x26;token=3c9a850e-9671-4f57-a95e-3f92af3a7051" alt=""><figcaption></figcaption></figure>

### cctv.thm/fpassword.php?id=4

> {% code overflow="wrap" %}
>
> ```
> Attempt to login to the FTP server with content containing the word "user" in it. 
> Once done, you can fetch the flag from /fpassword.php?id=4
> ```
>
> {% endcode %}

Now we need to make a login attempt to the ftp server with the content containing the word user. For this, just issuing the following command is sufficient.

```bash
ftp cctv.thm
```

After submitting the command, we are able to retrieve the fourth flag at `https://cctv.thm/fpassword.php?id=4`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FgXOdylpzmz16ePek9lBU%2Fgrafik.png?alt=media&#x26;token=b62144ab-cb1d-44e6-8bf1-a2eb333eb9cd" alt=""><figcaption></figcaption></figure>

### cctv.thm/fpassword.php?id=5

> {% code overflow="wrap" %}
>
> ```
> Send TCP request to flagger.cgi endpoint with a host header containing more than 50 characters. 
> Once done, you can fetch the flag from /fpassword.php?id=5
> ```
>
> {% endcode %}

Lastly, we need to make a TCP request on endpoint flagger.cgi with a host header containing more than 50 characters. Like in the second flag, a cURL request is sufficient, but this time we specify the host via the `-H` header parameter.

{% code overflow="wrap" %}

```bash
curl -H "Host: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" http://cctv.thm/flagger.cgi
```

{% endcode %}

After submitting the command, we are able to retrieve the fifth flag at `https://cctv.thm/fpassword.php?id=5`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FAdLdQovN6YIsTQ0ZXCom%2Fgrafik.png?alt=media&#x26;token=1e38f70a-ad12-453a-9974-5ddb210cd866" alt=""><figcaption></figcaption></figure>

### Logging in

Now we have all five parts of the password together, we can concat those to craft the password. Don't forget that the password consists of the whole flag parts.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FcqRdPrj23OD5UdltgaGC%2Fgrafik.png?alt=media&#x26;token=3489bcbb-4c69-4838-a029-338147834c17" alt=""><figcaption></figcaption></figure>

After submitting the password, we log in and find another login field, requiring username and password. Furthermore, we see a timestamp, which looks like a dropdown with a single entry.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FgZ9RZsUgiwmMiIJKQtoK%2Fgrafik.png?alt=media&#x26;token=97b76c9b-e58d-415a-9ab1-cae76491b94a" alt=""><figcaption></figcaption></figure>

## Second Login

For the second login we only need the username since we have the password already. The username is the machines' hostname. The developer excludes the possibility of command injection for his application. This will probably not be the case.

> {% code overflow="wrap" %}
>
> ```
> For the second layer of security, I have enabled a wholly sandboxed login environment with no connection to the database and no possibility of command execution. The username is the computer's hostname, and the password is the same as the previous password. I will SMS you the details as well.
> ```
>
> {% endcode %}

We look at the source of the page and see that the drop-down is part of a form. It is very suspicious that commands can be selected here. The value of an option element is probably the command that is executed.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fc25U1cAhPmUWaLI6Gd8K%2Fgrafik.png?alt=media&#x26;token=5fee7d7e-699d-4266-9f20-477fadd316df" alt=""><figcaption></figcaption></figure>

The JavaScript code listens for the `DOMContentLoaded` event and attaches an event listener to the dropdown element. When the dropdown selection changes, it submits the form associated with the form element named `myform`. So all we have to do is expand the form with commands that we want to execute.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FxfQqW2txcGB1mBjNwB3k%2Fgrafik.png?alt=media&#x26;token=9d0438b8-b896-4e39-9a0b-a396bcf3ed7c" alt=""><figcaption></figcaption></figure>

### What is the username for the CCTV web panel?

So, let's get the username, I mean hostname of the machine.

> ```
>  The username is the computer's hostname, [...]
> ```

This is how the element could look like.

```
<option value="hostname">Get Hostname</option>
```

We can add this by making use of the development tools of Firefox or chrome. We just duplicate the node and edit its value content. To get the hostname, we just change the value to hostname. After having the dropdown prepared, we trigger it by selecting the second item.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fy4J8GrWIHYg65YU3xuet%2Fgrafik.png?alt=media&#x26;token=888b4883-5b0d-422b-907a-e9cec0fc1a02" alt=""><figcaption></figcaption></figure>

Here is the procedure:

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FtOGzwx8uDZUfyKAG6PdD%2Fhostname.gif?alt=media&#x26;token=7474f1ce-7d43-415f-8e67-977766ec4a08" alt=""><figcaption></figcaption></figure>

### What is the lsb\_release -r -s command output from the attached machine?

We repeat the process to get the Linux standard base.

```
<option value="lsb_release -r -s">Get Release Number</option>
```

### What is the flag value after logging into the CCTV web panel?

After logging in ...

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FVwJWREgfzVSoUehkTRAf%2Fgrafik.png?alt=media&#x26;token=e140017d-48b8-40c6-bad0-45a1d6e6442f" alt=""><figcaption></figcaption></figure>

... we have access to the cctvs and the final flag.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FGv2IELi79diFhg5GLmGr%2Fgrafik.png?alt=media&#x26;token=e41d9835-1350-4bc9-b5e4-bb37506c2e58" alt=""><figcaption></figcaption></figure>

## Bonus: Initial Foothold + Privilege Escalation

Being curious, we try to get on the machine using a reverse shell.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FAaoxmdo6hBlsKBaflAtq%2Fgrafik.png?alt=media&#x26;token=f8ec6daf-7221-459b-933d-47614857d4f3" alt=""><figcaption></figcaption></figure>

After setting up a listener and triggering the selection, we are able to catch a reverse shell using busybox.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fxco3HgB7nop5I38MmCpe%2Fgrafik.png?alt=media&#x26;token=6125fb41-f2c0-4fe0-a432-6d6e4ae06a2c" alt=""><figcaption></figcaption></figure>

We are dealing with the kernel `5.15.0-1041`. Maybe we could get root privileges with a kernel exploit.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fdlz7EsBOlhzEbUe9WqlM%2Fgrafik.png?alt=media&#x26;token=88fa0fd9-c42c-4514-a9d2-0d1773756782" alt=""><figcaption></figcaption></figure>

The poc of `CVE-2024-1086` is a promising one. We compile the following and bring that to the machine.

{% embed url="<https://github.com/Notselwyn/CVE-2024-1086?tab=readme-ov-file>" %}

After executing the exploit we get root privileges.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FRhyU05UeTpXJ7yfR1Zeg%2Fgrafik.png?alt=media&#x26;token=b361af71-9afa-426b-8a6f-761a7fbc8c33" alt=""><figcaption></figcaption></figure>
