# Topology

{% embed url="<https://app.hackthebox.com/machines/546>" %}

The following post by 0xb0b is licensed under [CC BY 4.0<img src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" alt="" data-size="line"><img src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" alt="" data-size="line">](http://creativecommons.org/licenses/by/4.0/?ref=chooser-v1)

## Recon

By scanning our target with Nmap, we can discover just two open ports. We have SSH on port 22 with OpenSSH 8.2p1 and on port 80 running Apache httpd 2.4.41, a web server.

<figure><img src="/files/yKnt3v09qDHzoD0R7etH" alt=""><figcaption></figcaption></figure>

Since only a web server is present, the first thing to do is to enumerate possible subdomains using FFuF and possible directories via Gobuster. We are able to spot two subdomains: `dev` and `stats`. The subdomain dev looks promising. While Gobuster is running in the background, we move on to enumerate the web page manually.

<figure><img src="/files/dYofNVutJZ9yT5qjhpGH" alt=""><figcaption></figcaption></figure>

But upon visiting the page at `http://dev.topology.htb`, we have to provide some credentials. Let's move on to the page at `http://topology.htb`.

<figure><img src="/files/HdiRWwhkDUWYoQqDetn0" alt=""><figcaption></figcaption></figure>

Here we are welcomed to Topology, a site representing a research team and their work. There aren't any links directing to other sources, except for their software project, the `LaTeX Equation Generator`.

<figure><img src="/files/8sb4arsenDk1SwxjC9ej" alt=""><figcaption></figcaption></figure>

After clicking on it, we see another subdomain we were not able to enumerate with our wordlist. We update our `/etc/hosts` and refresh the page.

<figure><img src="/files/dHga48LE3l25DOTZ393z" alt=""><figcaption></figcaption></figure>

Here we are able to provide LateX code, especially code that is used in a math environment, to generate good-looking equations, rendered as a picture. Reading the text, we get two interesting hints. Firstly, only LaTeX code in inline math mode syntax is supported, and secondly, only one-liners can be evaluated.

<figure><img src="/files/r528mMOXWj1Cw9Zl5Uph" alt=""><figcaption></figcaption></figure>

## Command Injection Into LFI

Let's try the equation generator with a simple command `\cdot`.

<div align="center"><figure><img src="/files/4w6DtfhnX2Vx2pVUNICL" alt=""><figcaption></figcaption></figure></div>

It works...

<figure><img src="/files/9zCbQLWiXgq0DER1ab1n" alt=""><figcaption></figcaption></figure>

Let's check out how the mathmode looks from the following source:

{% embed url="<https://de.overleaf.com/learn/latex/Mathematical_expressions>" %}

Since only inline math mode is supported, our input is placed in one of the following environments:

* `\(...\)`
* `$...$`
* `\begin{math}...\end{math}`

So it should be possible to escape those and use other LaTeX commands. To test whether we are successful we place the command `\cdot`  between $ signs which is only evaluated in math mode.

Internally it would look like `$ $ 2\cdot 2 $ $`, we are outside the inline mathmode.<br>

<figure><img src="/files/oe61lbnlMPyWqYD6tlLx" alt=""><figcaption></figcaption></figure>

Since we received an error, it should work.

<figure><img src="/files/Q4qLyniLRFxxe3Zebgnv" alt=""><figcaption></figcaption></figure>

On further testing, we see our command can be injected. We are now able to execute commands outside of math mode.

<figure><img src="/files/2vMiWbn7VIbGyVZ8Kowk" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/IrIxObMGXPwtBdnGKOLn" alt=""><figcaption></figcaption></figure>

<div data-full-width="false"><figure><img src="/files/Vvn6ftMXcYIRxnpKsU73" alt=""><figcaption></figcaption></figure></div>

<figure><img src="/files/kcTOpMoc4AziKLelSfW4" alt=""><figcaption></figcaption></figure>

Next, we conduct common resources for payloads about command / LaTeX injection. After trying several payloads, we are only able to read files by using the `\lstinputlisting`. \
`\lstinputlisting{/path/to/desired/file}`:

{% embed url="<https://book.hacktricks.xyz/pentesting-web/formula-csv-doc-latex-ghostscript-injection#latex-injection>" %}

{% embed url="<https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/LaTeX%20Injection>" %}

The first successful attempt was to read the `/etc/passwd` file via&#x20;

`$ \lstinputlisting{/etc/passwd} $`.&#x20;

Here we are able to spot the user `vdaisley`.

<figure><img src="/files/94jK9UjZ6eKx38Usg2kY" alt=""><figcaption></figcaption></figure>

Furthermore, we were able to retrieve the contents of the `equation.php` at&#x20;

`/var/www/latex/equation.php`. But no credentials could be found in this folder.&#x20;

`$ \lstinputlisting{/var/www/latex/equation.php} $`

<figure><img src="/files/ldL9DkWKRGwTmIO6nWg8" alt=""><figcaption></figcaption></figure>

Recalling the subdomain `dev`, we were able to spot a `.htaccess` and `.htpasswd` file via&#x20;

`$ \lstinputlisting{/var/www/dev/.htaccess} $`

<figure><img src="/files/cFPiJM4OROYppc4RX94f" alt=""><figcaption></figcaption></figure>

From there we were able to retrieve the credentials for the user  `vdaisley`. But the password is hashed.

`$ \lstinputlisting{/var/www/dev/.htpasswd} $`

<figure><img src="/files/wk6YYMIZ5GYstjvIJxsz" alt=""><figcaption></figcaption></figure>

`$vdaisley:apr1$1ONUB/S2$58eeNVirnRDB5zAIbIxTYO`

The following link can be used to save the tedious copying text of the image:

{% embed url="<https://www.imagetotext.info/>" %}

By conducting the docs of hashcat we are able to determine the correct hash mode.

<figure><img src="/files/Hk2fbuP8RHyfAIHCzOIp" alt=""><figcaption></figcaption></figure>

Using hashcat with the wordlist `rockyou.txt` the clear text credentials of `vdaisley` could be retrieved:

`vdaisley:calculus20`

<figure><img src="/files/KbC5cnmbDxKPHLucIKPU" alt=""><figcaption></figcaption></figure>

The first idea was to visit `dev.topology.htb`, but there were nothing of interest.

<figure><img src="/files/NMqcUInaWfkmOdgKf3wX" alt=""><figcaption></figcaption></figure>

## Foothold

Recalling the Nmap scan, we know SSH is running on the machine, and the user `vdaisly` is also part of the system. Testing for credential reuse, we are able to log in via SSH.

<figure><img src="/files/lyp19ppeaXhP5uAqf8X5" alt=""><figcaption></figcaption></figure>

## Privilege Escalation

For privilege escalation, different scripts like `linpeas.sh` were used. But with `pspy64` we were able to spot two interesting cronjobs from root running a script in `/opt/gnuplot` and gnuplot itself.

<figure><img src="/files/EF9RBYSUwoa6oGJ3XScD" alt=""><figcaption></figcaption></figure>

Since we can't read what the script is doing, the gnuplot execution is more interesting, it seems like a part of `getdata.sh`. In short, the gnuplot execution looks for any `.plt` file in `/opt/gnuplot` that will then be executed running gnuplot.

<figure><img src="/files/iveBMf2ZQvAYkgbyhalQ" alt=""><figcaption></figcaption></figure>

We are not able to do anything with `getdata.sh`.

<figure><img src="/files/dMbOEAhyeMAU99LNyM8v" alt=""><figcaption></figcaption></figure>

But we are able to write to the folder `/opt/gnuplot`!

<figure><img src="/files/kbmtwUh3gwiJg2bYGic6" alt=""><figcaption></figcaption></figure>

After placing a reverse shell in `/opt/gnuplot/loadshell.plt` and setting up a listener via \
`nc -lvp 4445` on our attacker machine...

<figure><img src="/files/pPthcDdZmvsjqqgtg7q9" alt=""><figcaption></figcaption></figure>

... our cretated `.plt` file gets executed.

<figure><img src="/files/cLtbeUpN0v42th35h6B9" alt=""><figcaption></figcaption></figure>

We have a reverse shell as the user root and are able to read the root flag.

<figure><img src="/files/J3HNdNKsStLCkdi5BcUa" alt=""><figcaption></figcaption></figure>


---

# 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://0xb0b.gitbook.io/writeups/hackthebox/2023/topology.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.
