# Event Horizon

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

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)

***

In this challenge we are provided the following files:

```
powershell.DMP  traffic.pcapng
```

## The attacker was able to find the correct pair of credentials for the email service. What were they? Format: email:password

We scroll down the network traffic until we reach the SMTP traffic. There we are able to spot some brute force happening. Around packet `4665`, we identify a successful login.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FPSrfi6s6SGuMsB3zFHnX%2Fgrafik.png?alt=media&#x26;token=45af627f-deba-4f8d-86af-77f8f013eba8" alt=""><figcaption></figcaption></figure>

We follow the TCP traffic and are able to extract the base64-encoded credentials which lead to a successful login.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FmB7XvEVTErrWSOGLfBoU%2Fgrafik.png?alt=media&#x26;token=f2efe742-e126-46a6-982b-8f42b2540a91" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F3YIkCSuJJt58Nl52z3yL%2Fgrafik.png?alt=media&#x26;token=536f42f6-1cab-499d-8572-92e345f31075" alt=""><figcaption></figcaption></figure>

## What was the body of the email that was sent by the attacker?

We inspect the followed TCP traffic and are able to extract the body of the email sent by the attacker.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FIKnEfjAxUajRen2Ehkus%2Fgrafik.png?alt=media&#x26;token=83cb346e-f237-44cd-9fec-e4f94ef07086" alt=""><figcaption></figcaption></figure>

## What command initiated the malicious script download?

To identify what command initiated the malicous script download we extract the base64 encoded file in the attachment.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FFdyuPffL5moZNU37KdiQ%2Fgrafik.png?alt=media&#x26;token=cc67bfe5-45ce-444b-a60f-337f9e6400b7" alt=""><figcaption></figcaption></figure>

We decode the content and on the very end of the script we see the command used to download the malicious file. The malicious file downloaded is `radius.ps1`. Next , we look for the download of that file in the PCAP.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fs8GiVoDELJOs0tkcypQ3%2Fgrafik.png?alt=media&#x26;token=d5ebc703-b315-47df-8946-ef4beaf175ba" alt=""><figcaption></figcaption></figure>

## What is the initial AES key that is used for decrypting the C2 traffic?

{% hint style="info" %}
Until now it was kinda easy, we are passing now the event horizon
{% endhint %}

We look for the malicious file download, and spot it on packet no. `4722`:

```
http://10.0.2.45/radius.ps1
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FOpnIh0YO2d6npX8Q3KAy%2Fgrafik.png?alt=media&#x26;token=2b86a3e0-2814-4b39-9f2b-4507b6f6b86d" alt=""><figcaption></figcaption></figure>

We follow the TCP traffic, and see that the script downloaded contains a base64 encoded payload which also uses `IO.Compression.DeflateStream`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FPK9qShkM2u99EAh0PERk%2Fgrafik.png?alt=media&#x26;token=501d7264-8f91-4e0a-8c19-74ef10b810ac" alt=""><figcaption></figcaption></figure>

We use CyberChef to decode the blob and apply the `Raw Inflate` recipe to reverse the compression.

The blob is actually a ms-dos executeable, which we can identify by the MZ magic bytes.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FXrUsJbkAZCvKA4mUTLdC%2Fgrafik.png?alt=media&#x26;token=754d02c7-2611-4040-9503-d1af5b306756" alt=""><figcaption></figcaption></figure>

We save the output of our CyberChef decoding into a file and calculate a md5 hash.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FtDxEUmuYToSbLZOc5t0i%2Fgrafik.png?alt=media&#x26;token=d6c857d5-48a3-4916-954a-695ecb215b75" alt=""><figcaption></figcaption></figure>

We provide that hash to Virustotal and we see its something malicious that is known in the wild and is somewhat related to Covenant.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F2Cb9jBEYlnSSiKjMICXV%2Fgrafik.png?alt=media&#x26;token=3c0380d1-3d72-4277-96b9-cd26846f6d33" alt=""><figcaption></figcaption></figure>

In the details section we see its a PE32 executable that uses .NET Libraries.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FHehfbbQp7KJga2nWvT0v%2Fgrafik.png?alt=media&#x26;token=dda481bd-0058-4d24-898b-7191c5ace881" alt=""><figcaption></figcaption></figure>

We see that it is a `.NET` assembly. It is possible to decompile and get the information we need via Ghirda, but I was not able to do so, same with the room `Chrome`. Since it is a `.NET` binary we can make use of decompilers specific fot `.NET` binaries like `dnSPY.exe,` which allows us to do this very easily

It is probably possible to use `dnSpy.exe` via Wine, but this did not work properly on my machine. If you still want to try it, you can do this as follows: `WINEPREFIX=$HOME/.<INSERT YOUR PREFIX> wine dnSpy.exe`. The executable can be obtained from the following resource:

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

{% hint style="info" %}
There is an alternative to running dnSpy on a Windows VM.

We can use the plugin ILSpy which is not only available on Visual Studio but also on Visual Studio Code to decompile .NET binaries.
{% endhint %}

If you have not setup Visual Studio Code yet checkout the following resource:

{% embed url="<https://code.visualstudio.com/docs/setup/linuxcode.visualstudio.com>" %}

We are using the ILSpy extension on VSCode to decompile the binary

The `ILSpy` plugin can be easily installed via the marketplace.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FWRvxW7WvoGoYm76YnCVp%2Fgrafik.png?alt=media&#x26;token=63ded87d-1e92-4a51-9c7a-1626dab8e071" alt=""><figcaption></figcaption></figure>

After having the plugin installed hit `CTRL+SHIFT+P` and enter `ILSpy: Pick assembly from file system` to pick the `evidence.exe`. In the Execute Stager Class we are able to spot the key used.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FZJywxpgTQnrUVxTE7kCV%2Fgrafik.png?alt=media&#x26;token=ea1169a8-24a7-415d-995d-da30499dc9dc" alt=""><figcaption></figcaption></figure>

## What is the Administrator NTLM hash that the attacker found?

Upon researching about Covenant we immediatly come across the CovenantDecrypter. Which is designed to decrypt the communication data of Covenant traffic. From the decompilation and the results from VirusTotal we can tell it might be a Covenant C2 Agent.&#x20;

{% embed url="<https://github.com/naacbin/CovenantDecryptor>" %}

And all the data neceessary to decrypt the Covenant traffic (stage0 POST data and a minidump file of an infected process) is provided by the challenge:

> ### What do you need ?
>
> * The data traffic of Covenant is extracted from a network capture and stored in a separate file.
> * The AES key, which is embedded in the stage 0 binary, employed at the beginning of the communication.
> * A minidump file of an infected process.

The following quoted section from the CovenantDecryptor repository shows how the Covenant communication is setup.

> The Covenant communication initialization consists of 3 stages :
>
> * Stage0 :
>   1. The infected agent initiates an RSA session by transmitting a public key encrypted using the `SetupAESKey`, which is embedded in a malicious executable. Before sending, it formats the text as described in [GruntHTTPStager](https://github.com/cobbr/Covenant/blob/master/Covenant/Data/Grunt/GruntHTTP/GruntHTTPStager.cs#L59) with the type set to 0.
>   2. The C2 transfers a `SessionKey`, encrypted with the RSA public key, for subsequent communication.
> * Stage1 :
>   1. The infected agent employs the `SetupAESKey` to decrypt the message, and then leverages the RSA private key to decrypt the `SessionKey`. Afterwards, it encrypts 4 randomly generated bytes with the `SessionKey` and transmits them. Before sending, it formats the text as described in [GruntHTTPStager](https://github.com/cobbr/Covenant/blob/master/Covenant/Data/Grunt/GruntHTTP/GruntHTTPStager.cs#L142)with the type set to 1.
>   2. The C2 decrypts the 4 bytes using the `SessionKey`, appends 4 additional randomly generated bytes and transfers the resulting 8 bytes data to the infected agent.
> * Stage2 :
>   1. The infected agent decrypts the 8 bytes with the `SessionKey`. Subsequently, it checks if the first 4 bytes match the data it had previously transmitted, and proceeds transfer the last 4 bytes back to the C2. Before sending, it formats the text as described in [GruntHTTPStager](https://github.com/cobbr/Covenant/blob/master/Covenant/Data/Grunt/GruntHTTP/GruntHTTPStager.cs#L179) with the type set to 2.
>   2. The C2 decrypts the 4 bytes and verifies if they correspond to those it had transmitted earlier.
>
> Once verification is complete, data can be exchanged.

> CovenantDecryptor is composed of two utilities. The `extract_privatekey` script retrieves the p and q primes from a minidump file to construct an RSA private key by employing the public modulus. The `decrypt_covenant_traffic` script consists of 3 commands `modulus`, `key` and `decrypt`. The first command extracts the modulus from Covenant communication, while the second recovers the AES key used for encrypting data traffic. Lastly, the third command decrypts the traffic.

Next, we follow the steps shown in the repository to decrypt the communication of the C2.&#x20;

But first we need to extract the Stage0 POST data. We find the Stage0 POST request which starts with packet no. `4742`:

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FZmhl1TFTejcfz4T6jaXf%2Fgrafik.png?alt=media&#x26;token=9fd551ee-2f1e-445a-9565-a0d92a8b4c96" alt=""><figcaption></figcaption></figure>

We follow the HTTP stream, and save the content to a file called `traffic.txt`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fh3k5a1VxobCtcX5KwS2G%2Fgrafik.png?alt=media&#x26;token=fc8c4014-7535-4aef-9ba1-7dfd33ed7cfe" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FnTNWb5gtioOg7yOTcUP6%2Fgrafik.png?alt=media&#x26;token=ed04ac90-2bb9-4520-84fa-0b87499b9873" alt=""><figcaption></figcaption></figure>

Next, we follow the steps shown in the repository.

### Extract the modulus from the stage 0 request of an infected host:

{% code overflow="wrap" %}

```
python3 CovenantDecryptor/decrypt_covenant_traffic.py modulus -i traffic.txt -k "REDACTED" -t base64
```

{% endcode %}

Unfortuntely it fails, but trying only the POST data manually it works.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FQGqS3unZN62wK1Mvw56Z%2Fgrafik.png?alt=media&#x26;token=96031e05-69a8-4f7e-b259-ee877ac41662" alt=""><figcaption></figcaption></figure>

So for the workaround we use tshark to only extract the POST data from the traffic and work with that from now on.

{% code overflow="wrap" %}

```
tshark -r traffic.pcapng -Y "http.request.method == POST" -T fields -e http.file_data > post_data.txt
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FnO4ui4VhvaCgoEMX2BzC%2Fgrafik.png?alt=media&#x26;token=1dd5f363-8957-475c-8ee3-acbaa2f1e178" alt=""><figcaption></figcaption></figure>

We run the command again against the `post_data.txt` and are able to extract the modulus this time.

{% code overflow="wrap" %}

```
python3 CovenantDecryptor/decrypt_covenant_traffic.py modulus -i post_data.txt -k "REDACTED" -t base64
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FmkJ8yyWLHyqaPZutXG9c%2Fgrafik.png?alt=media&#x26;token=901efa2e-e28b-4b41-a1ee-7123c1185de9" alt=""><figcaption></figcaption></figure>

### Retrieve the RSA private key from a minidump file of an infected Covenant process:

Next, we extract the private key from the process dump using the modulus.

{% code overflow="wrap" %}

```
python3 CovenantDecryptor/extract_privatekey.py -i powershell.DMP -m $(cat mod.txt) -o ./
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FQeYJk454etAQTkBINaVG%2Fgrafik.png?alt=media&#x26;token=ac20c251-2cf6-4a8c-9709-4cbf623064b8" alt=""><figcaption></figcaption></figure>

### Recover the `SessionKey` from the stage 0 response of Covenant C2, which is employed to encrypt network traffic:

Then, we recover the `SessionKey` from the stage 0 response of Covenant C2, which is employed to encrypt network traffic:

{% code overflow="wrap" %}

```
python3 CovenantDecryptor/decrypt_covenant_traffic.py key -i post_data.txt --key "REDACTED" -t base64 -r privkey1.pem -s 1
```

{% endcode %}

But it errors for some reason.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fe5eFSXQ9k8P0Cp7QmHe0%2Fgrafik.png?alt=media&#x26;token=d96c4e86-b246-4f73-9a26-febe3aa4cc08" alt=""><figcaption></figcaption></figure>

We just need the response from stage0 which is on packet 4745:

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FpH8yCxuryHSVDWdhXxDz%2Fgrafik.png?alt=media&#x26;token=2dbcdecd-4d89-4d68-812c-1eedcdb075ea" alt=""><figcaption></figcaption></figure>

We copy the response to a file called `response.txt`, and execute the following command to retrieve the AES key.

{% code overflow="wrap" %}

```
python3 CovenantDecryptor/decrypt_covenant_traffic.py key -i response.txt --key "REDACTED" -t base64 -r privkey1.pem 
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F5Pd5xdJj0d3BJ192XwX2%2Fgrafik.png?alt=media&#x26;token=3005c6c3-879e-4dfe-bec8-a57430bd043c" alt=""><figcaption></figcaption></figure>

### Decrypt the Covenant communication:

To decrypt the Covenant communication we use the last command from the instruction and use our `post_data.txt` file again. We have now decrypted the traffic and are able to spot the ntlm hash.

{% code overflow="wrap" %}

```
python3 CovenantDecryptor/decrypt_covenant_traffic.py decrypt -i post_data.txt -k "REDACTED AES KEY" -t hex
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FVSWrpDPJw2c3yw2zJqN3%2Fgrafik.png?alt=media&#x26;token=06f92413-45bc-45f0-807c-f74f49ce6244" alt=""><figcaption></figcaption></figure>

## What is the flag?

From our previous decryption we noticed that message 8 contains a big chunk of base64 encoded data followed by another one. We use CyberChef to decode it and see that it is actually an image by the magic bytes.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F9kwDq3KjQVkD6oOsMPq5%2Fgrafik.png?alt=media&#x26;token=dfa1ba4b-96d2-4119-8075-2a189ac8ba55" alt=""><figcaption></figcaption></figure>

Next, we render that image using CyberChaef and see that it is a capture of the desktop of the machine containing the final flag.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fl05d1zhFtRZtB7RwJRjk%2Fgrafik.png?alt=media&#x26;token=ec19886e-5c69-4a3c-bc17-de40b0e27daa" alt=""><figcaption></figcaption></figure>
