# badwidth

There's also a video version available if you prefer [visual walkthroughs](https://www.youtube.com/watch?v=USsowXpk6i4).

**Port Scanning**

We begin our reconnaissance with a port scan using Rustscan to identify active services:

```
❯ rustscan -a 10.0.160.83
.----. .-. .-. .----..---.  .----. .---.   .--.  .-. .-.
| {}  }| { } |{ {__ {_   _}{ {__  /  ___} / {} \ |  `| |
| .-. \| {_} |.-._} } | |  .-._} }\     }/  /\  \| |\  |
`-' `-'`-----'`----'  `-'  `----'  `---' `-'  `-'`-' `-'
The Modern Day Port Scanner.
________________________________________
: http://discord.skerritt.blog           :
: https://github.com/RustScan/RustScan :
 --------------------------------------
Real hackers hack time ⌛

Open 10.0.160.83:1337
```

After identifying the open port, we use Nmap to gather detailed information about the service:

```
❯ nmap -p1337 -sCV -n -Pn -v 10.0.160.83 -oN scan
<SNIP>
Nmap scan report for 10.0.160.83
Host is up (0.15s latency).

PORT     STATE SERVICE VERSION
1337/tcp open  http    Node.js Express framework
|_http-title: Site doesn't have a title (text/plain; charset=utf-8).
| http-methods: 
|_  Supported Methods: POST GET HEAD OPTIONS

```

Nmap command options explained:

* `-p <ports>`: scan only a specific port.
* `-sCV`: combines the options -sC and -sV.
* `-sC`: equivalent to --script=default.
* `-sV`: Probe open ports to determine service/version info.
* `-n`: Never do DNS resolution.
* `-v`: Increase verbosity level.
* `-oN <file>`: Output scan in normal format. The scan reveals a web server running on port 1337. Let's investigate the website's content using a web browser:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2F8OF40pTxFny7tEXDRytg%2FPasted%20image%2020240831112916.png?alt=media&#x26;token=2e164d79-dba8-4fc1-aefa-d66837944abd" alt=""><figcaption></figcaption></figure>

When accessing the web server on port 1337, we encounter a message indicating the need to send an `ifname` parameter via a POST request.

**Gaining Initial Access**

To send the POST request, we'll use `curl` as follows:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FMbx2ZNIsmh3UnfpPb12e%2FPasted%20image%2020240831113110.png?alt=media&#x26;token=f7535c6d-71ec-43ed-9ef7-0e375cf9b138" alt=""><figcaption></figcaption></figure>

I didn't specify a method because curl uses POST by default when using the `-d` flag.

The response simply says "Used interface" and echoes the content we sent using the ifname parameter, regardless of what we sent.

**Command Injection**

After trying various techniques, I realized we can inject commands, but we are unable to see the output of those commands. We can use one of two techniques to detect if the command is being executed:

* Ping usage:

  We can use `tcpdump` to "listen" for ICMP packets through the tun0 interface as shown in the following image:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FQ8suFqZnDpp8HoUL7Ypd%2FPasted%20image%2020240831142029.png?alt=media&#x26;token=f4943536-3844-4ae6-98ea-3136697e7358" alt=""><figcaption></figcaption></figure>

Then we make a ping from the target to our IP.

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FdsrwYVgOla2ZQTpR3tDr%2FPasted%20image%2020240831142236.png?alt=media&#x26;token=58090c27-5cc5-4135-8da1-2870b6fbaf47" alt=""><figcaption></figcaption></figure>

We then receive the ping coming from the target.

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FOn0Zhsra9MNy9rFVT1Rf%2FPasted%20image%2020240831142330.png?alt=media&#x26;token=50097108-528b-4a58-a1d8-dca85a2c20d7" alt=""><figcaption></figcaption></figure>

* HTTP server:

  We can set up a basic HTTP server using Python with the command `python3 -m http.server 80` then use curl on the target to make a request to our server, like this:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FXIMzp2ecNCVxzyH9Q9Qj%2FPasted%20image%2020240831142522.png?alt=media&#x26;token=fff1e338-fc0a-4f82-af8c-14f7c3ac719a" alt=""><figcaption></figcaption></figure>

And we have received the request:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FQ1IC2eObYKwqBUiwCQOc%2FPasted%20image%2020240831142550.png?alt=media&#x26;token=7959944b-c28e-4a61-8372-4ac05f65cc8c" alt=""><figcaption></figcaption></figure>

**Gaining Initial Access**

Now that we know we can execute commands, let's get a reverse shell. First, set up a netcat listener on your machine with the command `nc -nlvp 443`. Then send the shell using the command injection:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FKgSKUWJLxMvxWBfuDb1s%2FPasted%20image%2020240831142715.png?alt=media&#x26;token=7ecac28a-c225-42f1-af3f-db578c3a1705" alt=""><figcaption></figcaption></figure>

We have received the connection from the target.

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FniiVyBqbiaAhA7gOvZV5%2FPasted%20image%2020240831142744.png?alt=media&#x26;token=78daf92c-b77c-4412-a7c3-d8b85415a227" alt=""><figcaption></figcaption></figure>

Here, we need to upgrade this to a fully interactive shell, so follow these steps:

1. Execute: `script /dev/null -qc bash`
2. Press `CTRL + Z`
3. Run: `stty raw -echo;fg`
4. Execute: `reset xterm` and press Enter
5.

```
<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FZuq2JNA1hv1gPOKSoCKG%2FPasted%20image%2020240831143221.png?alt=media&#x26;token=da6cc207-f0dd-477c-949a-343a9f79b6c4" alt=""><figcaption></figcaption></figure>
```

6. Finally execute `export TERM=xterm`. With this we have a fully interactive shell as ETSCTF:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FKuVw7tpwoVCZQoEb6nq7%2FPasted%20image%2020240831143348.png?alt=media&#x26;token=feb51f4b-cacd-49d4-9793-c6f8a3f2a65b" alt=""><figcaption></figcaption></figure>

**Privilege escalation**

If we execute `sudo -l`: The output of the command indicates that we can execute `/usr/local/bin/n158` as any user without a password.

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2F7Uqcf4uxid6iu4GdxfVc%2FPasted%20image%2020240831143454.png?alt=media&#x26;token=aeca3ae1-4da1-4c96-a9fa-e65e6083cf51" alt=""><figcaption></figcaption></figure>

Searching for vulnerabilities related to this, we found [this vulnerability](https://security.snyk.io/vuln/SNYK-JS-N158-3183746).

There's a PoC (Proof of Concept) that demonstrates how to inject commands:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FodkF4TllWnXwiWPdbHwZ%2FPasted%20image%2020240831152536.png?alt=media&#x26;token=95399fc5-6708-4c24-84c9-10bcfbe32ed3" alt=""><figcaption></figcaption></figure>

Let's use this to set the SETUID and SETGID permissions on bash, which will allow us to execute it in privileged mode:&#x20;

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FdGcq5EOhnXSjQo3ULiEE%2FPasted%20image%2020240831152715.png?alt=media&#x26;token=43f4c76a-e80b-4bb1-bd65-777d2751ec6d" alt=""><figcaption></figcaption></figure>

The bash binary now has these permissions:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FoyJZjc5BUdDT3bM2HPIJ%2FPasted%20image%2020240831152803.png?alt=media&#x26;token=51bba791-684e-46fb-a37c-b30a8ad806b3" alt=""><figcaption></figcaption></figure>

By simply executing `bash -p`, we have now become root:

<figure><img src="https://3863537643-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDdYSqYDpT0Aiur7jvsM1%2Fuploads%2FusdURYR6Bx51TCcilU4r%2FPasted%20image%2020240831152843.png?alt=media&#x26;token=401afaba-5793-4d41-a60a-a1448c320d21" alt=""><figcaption></figcaption></figure>

**Flag Locations**

The flags can be found in the following locations:

* /root
* /etc/passwd
* /etc/shadow


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://b13ss3d.gitbook.io/hackmex-prep/readme/badwidth.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
