🔮
P4n1cBook
  • 🏴‍☠️Welcome!
    • 🔮P4n1cBook
    • 📚Bookmarks
    • 🚨Licence and Disclaimer
  • Fundamentals
    • Starter Kit
      • Linux
      • PowerShell
      • Git
      • 💾Regex
      • Network Analysis
        • curl
        • tcpdump
        • Nmap
        • 🦈Wireshark
      • Metasploit
    • 🌐Network Protocols
      • ICMP
      • SSH
      • Telnet
      • DNS
      • FTP
      • HTTP/HTTPS
      • SMB
      • SNMP
      • SMTP
      • NFS
      • IPP
      • WinRM
      • LLMNR
      • JDWP
    • Code
      • Python Essentials
      • C & C++
    • Web APIs
      • GraphQL
    • Shells/TTYs
    • Dorks
    • Cryptography
    • Reverse Engineering
      • GDB
      • Binaries
  • Web Exploitation
    • Web Enumeration
      • User Endpoints
      • Web Fuzzing
        • ffuf
        • feroxbuster
        • Gobuster
        • GoWitness
      • Web Servers
        • Apache
        • Nginx
        • Werkzeug
      • Databases
        • MySQL
        • NoSQL
          • MongoDB
          • Redis
      • Web Services/Frameworks
        • Wordpress
        • Laravel
        • Express
        • Magento
        • AIOHTTP
        • HashiCorp Vault
        • Tiny File Manager
        • Joomla
        • CMS Made Simple
        • 🌵Cacti
        • Tomcat
        • Zabbix
        • OpenNetAdmin
        • ImageMagick
    • Vulnerabilities
      • Arbitrary File Read
      • Session Hijacking
      • SSRF
      • Eval Injection
      • Template Manipulation
      • Path Traversal
      • Prototype Pollution
      • XXE
      • Deserialization
      • Log Poisoning
      • Arbitrary Command Execution
      • SQLi
        • SQLmap
      • SSI
      • SSTI
      • LFI
      • XSS
    • Java-based web application
      • Struts
      • .WAR
      • pd4ml.jar
  • Cloud Exploitation
    • Kubernetes
    • AWS
  • Post Exploitation
    • File Transfer
      • Exfiltration
    • Credential Dumping
      • Thunderbird
    • Lateral Movement
    • Persistence
    • Linux Privilege Escalation
      • Static Binaries
      • Enumeration
      • Hijacks
      • Command Injection
      • Jailbreaks
      • Binary Exploitation - Linux
      • Kernel Exploits
      • Buffer Overflow - Linux
      • Docker
      • Abusing Wildcards
  • Wireless Exploitation
    • NFC
Powered by GitBook
On this page
Edit on GitHub
  1. Web Exploitation
  2. Web Enumeration
  3. Databases

MySQL

MySQL

  • MySQL Connector/Python automatically appends a semicolon at the end of your queries.


Operator Precedence

Highest to lowest:

  1. Parentheses (): Operations inside parentheses are evaluated first.

  2. Unary Operators (highest precedence):

    • + (positive)

    • - (negation)

    • ~ (bitwise NOT)

    • ! (logical NOT)

  3. Multiplication, Division, Modulus: These operators are evaluated next and have the same precedence:

    • * (multiplication)

    • / (division)

    • % (modulus)

  4. Addition and Subtraction: These operators have the next level of precedence:

    • + (addition)

    • - (subtraction)

  5. 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)

  6. Logical NOT:

    • ! (logical NOT)

  7. Logical AND:

    • && (AND)

  8. Logical OR:

    • || (OR)


General Commands

Connect to the database
mysql -u USER -h HOST -P PORT -p
Print available databases
show databases;
Connect to the database
use databasename;

Tables

Print tables from the database
show tables;
Print info about the table
describe table_name;
Add values to table
INSERT INTO table_name VALUES (value_1,..);
Add values to column in a table
INSERT INTO table_name(column2, ...) VALUES (column2_value, ..);
Update table values
UPDATE table_name SET column1=newvalue1, ... WHERE <condition>;

Columns

Show all columns in a table
select * from table_name;
Show columns from a table
select name,username,password from sd4fg_users;
Delete a table
DROP TABLE tablename;
Add a new column
ALTER TABLE logins ADD newColumn INT;
Rename column
ALTER TABLE logins RENAME COLUMN newColumn TO oldColumn;
Change column datatype
ALTER TABLE logins MODIFY oldColumn DATE;
Delete column
ALTER TABLE logins DROP oldColumn;

Output

Sort By column
SELECT * FROM logins ORDER BY column_1;
Sort by column in descending order
SELECT * FROM logins ORDER BY column_1 DESC;
Sort by column in Ascending order
SELECT * FROM logins ORDER BY column_1 DESC, id ASC;
Sort by two-columns
SELECT * FROM logins LIMIT 2;
Only show first two results starting from index 2
SELECT * FROM logins LIMIT 1, 2;
List results that meet a condition
SELECT * FROM table_name WHERE <condition>;
List results where the name is similar to a given string
SELECT * FROM logins WHERE username LIKE 'admin%';

pymysql

  • The pymysql library is a Python client for interacting with MySQL databases.

Steps to Exploit

  1. Identify the Connection Script

  2. Extract Database Credentials

  3. Execute SQL Queries


  • The following script dynamically executes SQL queries on a target database using credentials extracted from application settings:

#!/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"

PreviousDatabasesNextNoSQL

Last updated 3 months ago