MySQL
MySQL Connector/Python automatically appends a semicolon at the end of your queries.
Operator Precedence - Highest to lowest
Parentheses()
Operations inside parentheses are evaluated first.
Unary Operators
+ (positive)
- (negation)
~ (bitwise NOT)
! (logical NOT)
Multiplication, Division, Modulus: These operators are evaluated next and have the same precedence:
* (multiplication)
/ (division)
% (modulus)
Addition and Subtraction: These operators have the next level of precedence:
+ (addition)
- (subtraction)
Comparison Operators: All of these operators have the same precedence:
= (equal to)
!= (not equal to)
> (greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
LIKE (pattern matching)
Logical NOT
!
Logical AND
&&
Logical OR
||
General Commands
mysql -u USER -h HOST -P PORT -pshow databases;use databasename;Tables
show tables;describe table_name;INSERT INTO table_name VALUES (value_1,..);INSERT INTO table_name(column2, ...) VALUES (column2_value, ..);UPDATE table_name SET column1=newvalue1, ... WHERE <condition>;Columns
select * from table_name;select name,username,password from sd4fg_users;DROP TABLE tablename;ALTER TABLE logins ADD newColumn INT;ALTER TABLE logins RENAME COLUMN newColumn TO oldColumn;ALTER TABLE logins MODIFY oldColumn DATE;ALTER TABLE logins DROP oldColumn;Output
SELECT * FROM logins ORDER BY column_1;SELECT * FROM logins ORDER BY column_1 DESC;SELECT * FROM logins ORDER BY column_1 DESC, id ASC;SELECT * FROM logins LIMIT 2;SELECT * FROM logins LIMIT 1, 2;SELECT * FROM table_name WHERE <condition>;SELECT * FROM logins WHERE username LIKE 'admin%';pymysql
The
pymysqllibrary is a Python client for interacting with MySQL databases.
#!/usr/bin/env python
import pymysql
import sys
from craft_api import settings
# Test connection to MySQL database
connection = pymysql.connect(
host=settings.MYSQL_DATABASE_HOST,
user=settings.MYSQL_DATABASE_USER,
password=settings.MYSQL_DATABASE_PASSWORD,
db=settings.MYSQL_DATABASE_DB,
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
sql = sys.argv[1]
cursor.execute(sql)
result = cursor.fetchall()
print(result)
finally:
connection.close()Use Cases in Exploitation
python myscript.py "SHOW TABLES"python myscript.py "SELECT * FROM user"python myscript.py "SHOW GRANTS FOR CURRENT_USER()"python myscript.py "DESCRIBE user"Last updated