githubEdit

Attacking SQL Database

systems that store data in tables (rows and columns) and use SQL to manage and query it.

Database servers are prime targets because they hold sensitive stuff like user credentials, personal info (PII), business data, and payment details. Plus, they’re often set up with high-privilege accounts. If we get in, we can use those privileges to move around the network (lateral movement) or gain more power (privilege escalation).

Enumeration:

  • MSSQL uses TCP/1433 and UDP/1434 by default, or TCP/2433 if "hidden."

  • MySQL uses TCP/3306.

  • We use Nmap to scan: Output (example):

    nmap -Pn -sV -sC -p1433 10.10.10.125
    • Port 1433 open.

    • Version: Microsoft SQL Server 2017.

    • Hostname: mssql-test.

    • Domain: HTB.LOCAL.

This gives us key info like version and hostname to spot misconfigs or vulnerabilities.

Authentication:

  • MSSQL has two modes:

    1. Windows Authentication: Default, tied to Windows/AD accounts. No extra credentials if logged into Windows.

    2. Mixed Mode: Allows Windows or SQL Server-specific users/passwords.

  • MySQL: Supports username/password and Windows auth (with a plugin).

Misconfigs can let us in without credentials. An old MySQL vuln (CVE-2012-2122) let you bypass auth by spamming a wrong password, exploiting timing differences.

Misconfigurations:

If auth is open (anonymous access) or a user has no password, we’re in.

Privileges:

With high privileges, we can:

  • Read/edit database contents.

  • Execute system commands.

  • Read files.

  • Steal hashes.

  • Impersonate users.

Protocol Attacks:

Read/Change Data:

Once inside, we list databases, tables, and data. We pick tables with juicy info like passwords.

Connect:

  • MySQL:

  • MSSQL (Windows):

  • From Linux:

Execute Commands:

  • MSSQL: Use xp_cmdshell:

Enable it if off:

  • MySQL: No direct equivalent, but we can write executable files.

Write Files:

  • MySQL:

Check secure_file_priv:

  • MSSQL:

Read Files:

  • MSSQL:

  • MySQL:

Capture Hash:

  • MSSQL: Use **xp_dirtree** with Responder:

Impersonate Users:

  • MSSQL:

Talk to Other Databases:

  • MSSQL:

This covers the basics of attacking SQL databases!

Last updated