# Lookup

{% embed url="<https://tryhackme.com/r/room/lookup>" %}

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

We start with a Nmap scan and find two open ports. On port `22` we have SSH, and on port `80` we have an Apache httpd `2.4.41` web server.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FhMClHWf4EuteDyOX49SG%2Fgrafik.png?alt=media&#x26;token=863fd94e-ff51-456f-90d0-747e8cf4331f" alt=""><figcaption></figcaption></figure>

The index page reveals a login page.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FUtUxFViqxcJHPoUXtNVb%2Fgrafik.png?alt=media&#x26;token=dc61765a-c526-4488-a920-84f69ad83682" alt=""><figcaption></figcaption></figure>

Further directories and pages could not be found by using Feroxbuster. Only that it is a PHP server, with the login.php page that is used for the login.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FODx1xFReoSx1atTPct1J%2Fgrafik.png?alt=media&#x26;token=b752b59d-065e-4407-823f-443e98ad76b2" alt=""><figcaption></figcaption></figure>

## Web Access

First of all, we intercept a login request to see how it is structured in order to brute-force logins using SQLMap or Hydra, for example. Standard SQL Injection payloads were unsuccessful. If an incorrect entry is made, we only see a generic response that either the username or password is incorrect. At first glance, no password or username enumeration seems possible.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F3XT5okfADEGtiOLUxRYA%2Fgrafik.png?alt=media&#x26;token=1764495d-03d1-4f2e-b931-37d345943884" alt=""><figcaption></figcaption></figure>

We continue with a list of SQL Login Bypass payloads referenced at hacktricks.xyz. We had previously used the same in the Injectics room.

{% embed url="<https://github.com/HackTricks-wiki/hacktricks/blob/master/pentesting-web/login-bypass/sql-login-bypass.md>" %}

Unfortunately, this did not produce any functioning payloads. SQLMap did not produce anything either. For the user `admin`, we get a different response size than for the other payloads. Perhaps this is a valid user.

{% code overflow="wrap" %}

```
ffuf -w bypass.txt -X POST -u http://lookup.thm/login.php -d 'username=FUZZ&password=asdf' -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8"  -fw 10
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FHBwSgPE5cq3lvUupBu2t%2Fgrafik.png?alt=media&#x26;token=3d5689b4-65c8-48a1-8909-f87215bcaeb3" alt=""><figcaption></figcaption></figure>

We are now fuzzing for possible passwords for the user `admin`. And have a hit. The response size is different here than for others. Like earlier with our payload fuzzing.

{% code overflow="wrap" %}

```
ffuf -w /usr/share/wordlists/SecLists/Passwords/2020-200_most_used_passwords.txt -X POST -u http://lookup.thm/login.php -d 'username=admin&password=FUZZ' -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8"  -fw 8
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FbHMH9s5w2RW8ChyfsFgw%2Fgrafik.png?alt=media&#x26;token=7d94ca61-c684-47ea-b774-4fa1fd852632" alt=""><figcaption></figcaption></figure>

If we use the password for the user admin, we get the generic answer as mentioned earlier.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FXouSf1FKeJ7SMUAVbAzY%2Fgrafik.png?alt=media&#x26;token=351ceda8-b9c7-4340-90e3-d5f1dbb981f1" alt=""><figcaption></figcaption></figure>

If we enter a different password, we are informed that the password is incorrect. That's strange. Maybe we get feedback for the correct password for the wrong user with our found password from before.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FJZsBCq0WhEdMejNboTMH%2Fgrafik.png?alt=media&#x26;token=d649a402-7f9c-4d81-b466-85b8c5234ce0" alt=""><figcaption></figcaption></figure>

So, let's fuzz for other users and use the `xato-net-10-million-usernames.txt`. And we find a user who provides a `302` status code for the previously found password. We have found a credential pair.

{% code overflow="wrap" %}

```
ffuf -w /usr/share/wordlists/SecLists/Usernames/xato-net-10-million-usernames.txt -X POST -u http://lookup.thm/login.php -d 'username=FUZZ&password=REDACTED' -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8"  -fw 8
```

{% endcode %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FuK8ADCxQOsAU8C2O8YRc%2Fgrafik.png?alt=media&#x26;token=951346cc-d86a-4f34-becb-c22772afa16d" alt=""><figcaption></figcaption></figure>

The login takes some time. We intercept the login request with the correct login credentials, `jose:REDACTED` for checking, and follow the redirection. We see that it is redirected to `files.lookup.thm.`

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FNBMl0I8qOkhwPXgJS2is%2Fgrafik.png?alt=media&#x26;token=3f96c9d5-3569-4e05-b367-7cf7974d3251" alt=""><figcaption></figcaption></figure>

We add this to our`/etc/hosts` file and log in again.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F3GdpwfFklpayZx7IkVAS%2Fgrafik.png?alt=media&#x26;token=825ba5e0-8b4d-41c7-923a-e33854922029" alt=""><figcaption></figcaption></figure>

We see something like a file manager. It is elFinder, an open-source file manager for web applications that allows users to manage files and directories.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FyDN7g7jE8IHWFeBlBZQS%2Fgrafik.png?alt=media&#x26;token=3c6f848c-990f-4941-a317-0fcf9a77c222" alt=""><figcaption></figcaption></figure>

## Shell as www-data

We see it is the Version `2.1.47`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fne7yJmSvD6lzSsiWNyO6%2Fgrafik.png?alt=media&#x26;token=8e63fd99-3924-48ac-9c3e-f3d6852c5db9" alt=""><figcaption></figcaption></figure>

With a little research we find an exploit to a command injection vulnerability.

{% embed url="<https://github.com/Studio-42/elFinder/issues/2868>" %}

elFinder <= 2.1.47 - Command Injection vulnerability in the PHP connector.

{% embed url="<https://www.exploit-db.com/exploits/46481>" %}

This exploit requires `Python 2.7` and accesses the `connect.minimal.php` to upload files. We test if it is reachable without a login, since the exploits available do not handle any authentication. But we do not need to adapt the script. The endpoint to `connect.minimal.php` is reachable without authentication.

The vulnerability lies in the filename, which allows commands to be injected. The exploit creates a PHP web shell into the upload directory while uploading an arbitrary image.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F4J9HwjqbwzWTqwJya7lS%2Fgrafik.png?alt=media&#x26;token=828160e2-6b1b-420c-804f-89000ed9ef66" alt=""><figcaption></figcaption></figure>

The exploits require and JPG, so we chose any.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FULZfpZMi7tvpEaH5HxAY%2Fgrafik.png?alt=media&#x26;token=91a69798-d796-4a3e-9284-a015214f7847" alt=""><figcaption></figcaption></figure>

Next, we need to run the exploit with the correct URL. The endpoint is vulnerable; we have a pseudoshell.

```
python2.7 elfinder.py http://files.lookup.thm/elFinder/
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FhB32vtEQ1cExYmPGycyp%2Fgrafik.png?alt=media&#x26;token=75dae5ed-19ea-479b-82d2-dab690df6a7b" alt=""><figcaption></figcaption></figure>

Now we create a payload to establish an interactive shell. For this, we use revshell.com. We chose the `nc mkfifo` one and URL encode the payload since we have a web shell.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FqKkFpu01gUMHE4gEVXy8%2Fgrafik.png?alt=media&#x26;token=fb35ddca-8113-4da3-8a8d-4ea00987072a" alt=""><figcaption></figcaption></figure>

We set up a listener on our desired port and run the payload.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FvGxZgWXbWwxyXgV9JymL%2Fgrafik.png?alt=media&#x26;token=98fff264-463a-46de-b83c-22d97027ce69" alt=""><figcaption></figcaption></figure>

We get a connection back and are `www-data`. Next, we upgrade our shell using the commands provided in the following resource.

{% embed url="<https://0xffsec.com/handbook/shells/full-tty/>" %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FxHkwlPnq2fUapme630tw%2Fgrafik.png?alt=media&#x26;token=eee802a8-3ac7-4dc0-b5cb-15343ecdee43" alt=""><figcaption></figcaption></figure>

## Shell as think

While enumerating the target manually and with LinPEAS we find the user `think`, where we are able to list the files in the home directory of the user. There is a possible valuable file present here, the `.passwords`, which we aren't allowed to read... yet.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FsUgQi3Vmodu5ROwJLJv8%2Fgrafik.png?alt=media&#x26;token=6cadc4bc-1743-4e80-aabe-9ecd61b2c80e" alt=""><figcaption></figcaption></figure>

In the LinPEAS output, we are able to spot an unknown SUID binary `pwm` owned by `root`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FJgSOwyAxaUIWkEz1ggs6%2Fgrafik.png?alt=media&#x26;token=9642a8eb-f405-4b35-b732-d40a058158d4" alt=""><figcaption></figcaption></figure>

Running it reveals that it checks the `id`, from which the current user is derived, and then used to locate the `.password` file of that user in the home directory. So it might be some sort of a password manager.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FiWfhNcGJAP6DEStRFSUq%2Fgrafik.png?alt=media&#x26;token=9e0b8724-d7b2-4537-9df3-01fbf8b2eaaa" alt=""><figcaption></figcaption></figure>

We copy the file to the web root folder to download it to our machine and analyze it.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FyRCVZKDjanWyhMFNYchk%2Fgrafik.png?alt=media&#x26;token=11286824-b245-49d8-a133-8616237d8666" alt=""><figcaption></figcaption></figure>

```
http://lookup.thm/pwm
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fi8mPQuBaPDmlq2JPgnHi%2Fgrafik.png?alt=media&#x26;token=e307e0c2-f4ad-4828-8bd2-7b468af29229" alt=""><figcaption></figcaption></figure>

With a quick look using `strings`, we see the mechanism by which the username is extracted from and output that might be the `id` command.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FnyASIqqOtgKsU3YHR5Xy%2Fgrafik.png?alt=media&#x26;token=fbe5b7a9-6aa3-4b29-a46f-96273477ea12" alt=""><figcaption></figcaption></figure>

The path is then complemented with the extracted username.&#x20;

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fu5ZgorwgS1F0PZPrITCN%2Fgrafik.png?alt=media&#x26;token=63883d8f-c6f4-4fbb-8d38-66819844ecdf" alt=""><figcaption></figcaption></figure>

Next, we analyze the binary using BinaryNinja. Here we set the language to pseudo c first. In the main function, we see that `data_204e` might hold the path to the id command.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FozuzYYeeRgjW0jQ5XbR9%2Fgrafik.png?alt=media&#x26;token=1a7f55ce-3925-4bbc-b620-265c50d22d96" alt=""><figcaption></figcaption></figure>

By clicking on the variable `data_204e`, we see that it stored just `id`. So a relative path, not an absolute path, is used. Which makes it possible to point to a different id executable using the `PATH` environment variable to fool the binary that we are the user `think`.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FlYxCPKf8BUa4UGMd3Arw%2Fgrafik.png?alt=media&#x26;token=7a528087-ff60-4cdd-b7c1-333f722d931e" alt=""><figcaption></figcaption></figure>

For this, we create a simple bash script called `id`, that mimics the output of the original `id` command and place it into `/tmp/id` and make it executable.

```
echo '#!/bin/bash' > id
echo 'echo "uid=1000(think) gid=1000(think) groups=1000(think)"' >> id
chmod +x id

```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F3CU9bwREbkelw7jQWAAa%2Fgrafik.png?alt=media&#x26;token=83f26bb0-1991-49ad-9197-70e7c03e95d4" alt=""><figcaption></figcaption></figure>

Next we prepend `/tmp` to our `PATH` variable so that binary is looked up first there when any binary like `id` is called.

```
 export PATH=/tmp:$PATH
```

Now after running `/usr/sbin/pwm` we get the contents of `.passwords`. We could now  try each of the passwords, but one stands out. &#x20;

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2F2KU1WflrvZubDpWxboZd%2Fgrafik.png?alt=media&#x26;token=370bf97c-46f6-45c4-9c68-9cdd93e4b9d3" alt=""><figcaption></figcaption></figure>

Alternatively, we could try to brute-force the ssh log in using Hydra:

```
hydra -l think -P .passwords lookup.thm ssh
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fern0pMXfKgyJV8TADGOz%2Fgrafik.png?alt=media&#x26;token=ff1bba9f-4db1-450a-a441-a158d6e3fe47" alt=""><figcaption></figcaption></figure>

We use the found password to switch to the user `think` using `su` and are able to read the first flag in the user's home directory.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FV3R3WgUq6rtixs4If3pt%2Fgrafik.png?alt=media&#x26;token=d6f51443-2f34-44a5-aeb0-c325685e9992" alt=""><figcaption></figcaption></figure>

## Shell as root

Equipped with the password, we are able to run `sudo -l`. We see that the user is able to use look with `sudo` permissions for the `root` user. The binary `look` performs a dictionary lookup to display lines from a sorted list of words that begin with a specified prefix.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FSePoG66v0UDKnI7CiQvi%2Fgrafik.png?alt=media&#x26;token=094b340e-a3dd-4a63-9cde-c3afa8ef474c" alt=""><figcaption></figcaption></figure>

Looking up that binary at GTFObins we are able to actually read any files as `root` using `sudo`.

{% embed url="<https://gtfobins.github.io/gtfobins/look/#sudo>" %}

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FzUJRu4S3K7nurJB8ZgDt%2Fgrafik.png?alt=media&#x26;token=99932788-9f51-4c47-b128-ece0b6642983" alt=""><figcaption></figcaption></figure>

We try to read the private ssh key of the root user located at `/root/.ssh/id_rsa` using the following commands displayed at GTFOBins. We are able to read the ssh key.

```
LFILE=/root/.ssh/id_rsa
sudo /usr/bin/look '' "$LFILE"
```

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2Fhew3tBOnxtQUB5LIE9F7%2Fgrafik.png?alt=media&#x26;token=93426955-ccfb-4220-979b-e08abe0287a9" alt=""><figcaption></figcaption></figure>

We store the contents of the SSH key on our machine and adjust the permission to be able to use it.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FVoFnxWmFr3nA4TKpaqQZ%2Fgrafik.png?alt=media&#x26;token=81045a08-abcf-4716-a7ac-81a887e69ab9" alt=""><figcaption></figcaption></figure>

Next, we use it to log in as `root` and find the final flag in the root home directory.

<figure><img src="https://2148487935-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoqaFccsCrwKo1CHmLRKW%2Fuploads%2FFlAfScN8lCfe1TpLOoVn%2Fgrafik.png?alt=media&#x26;token=e4e56066-2ede-4b73-9191-537ffe3541cc" alt=""><figcaption></figcaption></figure>
