Use SQL Server rights, impersonation and command facilities
Needs: Correct SQL versus Windows authentication; database role and configuration must permit the requested operation
Shell commands
# Baseline
impacket-mssqlclient 'USER@TARGET'
impacket-mssqlclient 'DOMAIN/USER@TARGET' -windows-auth
At the SQL prompt
-- At the SQL prompt
SELECT @@VERSION;
SELECT SYSTEM_USER;
SELECT IS_SRVROLEMEMBER('sysadmin');
SELECT name FROM sys.databases;
USE DATABASE;
SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES;
SELECT TOP 20 * FROM SCHEMA.TABLE;
-- MSSQL variant — command-execution boundary
EXEC xp_cmdshell 'whoami';
-- MSSQL linked-server and login-context mapping
SELECT SYSTEM_USER AS current_login,
ORIGINAL_LOGIN() AS original_login,
SUSER_SNAME() AS effective_login,
IS_SRVROLEMEMBER('sysadmin') AS is_sysadmin;
SELECT name, product, provider, data_source, is_linked
FROM sys.servers
WHERE is_linked = 1;
EXEC master.dbo.sp_helplinkedsrvlogin;
-- MSSQL linked-server and login-context mapping
EXEC ('SELECT SYSTEM_USER AS remote_login,
ORIGINAL_LOGIN() AS remote_original_login,
IS_SRVROLEMEMBER(''sysadmin'') AS remote_is_sysadmin')
AT [LINKED_SERVER];
xp_cmdshellis disabled by default on new installations. Enabling it generally requires high privilege and changes server configuration; do not enable it merely to enumerate. When available, a sysadmin's child process normally runs as the SQL Server service account; an authorized non-sysadmin may use a configured proxy. Verify returned identity before choosing the next action.
MSSQL linked-server and login-context mapping
If RPC execution is disabled but data access is allowed, use a read-only pass-through query instead:
-- MSSQL linked-server and login-context mapping
SELECT *
FROM OPENQUERY([LINKED_SERVER],
'SELECT SYSTEM_USER AS remote_login,
ORIGINAL_LOGIN() AS remote_original_login');
MSSQL linked-server and login-context mapping
Decide: a returned remote identity enables only the operations granted to that mapped login.
RPC OUT is disabled, login-mapping failures and data-access failures rule out different mechanisms; they do not prove the server is unreachable. Ifenum_impersonateidentifies a specific permitted login, test only that login and always return to the original context:
-- MSSQL linked-server and login-context mapping
EXECUTE AS LOGIN = 'LOGIN';
SELECT SYSTEM_USER, ORIGINAL_LOGIN(), SUSER_SNAME(), IS_SRVROLEMEMBER('sysadmin');
REVERT;
Do not enable RPC, create mappings or change server configuration merely to enumerate. A remote SQL query is not OS execution; independently validate any file or command facility and the operating-system identity behind it.
Verify → Verify SQL identity and any OS effect with hostname/whoami; record service/proxy execution identity.
If it fails → Login success with denied configuration changes is a rights boundary. Do not repeatedly enable an unavailable facility. More detail ↓
Explanation & examples
Use SQL Server rights, impersonation and command facilities
- USE WHEN
- SQL Server credentials or a Windows identity accepted by the database
- STARTING ACCESS
- A confirmed reachable application; anonymous context or one identified credential
- RESULT
- Database data, a more useful SQL security context or verified server command execution
Recognize the clue
Once SQL Server authentication works, inspect actual login/server role and supported impersonation context.
SELECT SYSTEM_USER, ORIGINAL_LOGIN(), @@VERSION;
SELECT IS_SRVROLEMEMBER('sysadmin');
SYSTEM_USER: DOMAIN\USER
ORIGINAL_LOGIN: DOMAIN\USER
IS_SRVROLEMEMBER: 0Means: A valid login with sysadmin=0 is data/rights evidence, not permission to enable xp_cmdshell.
Confirm: Enumerate actual database/server grants and useful impersonation; independently verify any OS facility and its identity.
- After validation
- Required role/configuration and OS effect are verified: use the supported database-to-command method.
- Ruled out in this context
- No facility/impersonation right rules out that proposed OS chain while readable data may remain.
- Still inconclusive
- A failed role query or unknown proxy context needs a narrower rights check.
Troubleshooting: Login success with denied configuration changes is a rights boundary. Do not repeatedly enable an unavailable facility.
Let the tool prompt for the password. At the SQL prompt:
Interpret:
IS_SRVROLEMEMBER('sysadmin')returns1,0orNULL;NULLis not success. Impacket also exposes helpers such asenum_impersonateandxp_cmdshelldepending on version and rights.
MSSQL variant — command-execution boundary
xp_cmdshell is disabled by default on new installations. Enabling it generally requires high privilege and changes server configuration; do not enable it merely to enumerate. When available, a sysadmin's child process normally runs as the SQL Server service account; an authorized non-sysadmin may use a configured proxy. Verify returned identity before choosing the next action.
MSSQL linked-server and login-context mapping
Treat a linked server as another authentication and authorization boundary. The local SQL login, its mapped remote login, remote SQL role and RPC/data-access settings can all differ.
Interpret:
sys.serversproves configuration, not remote access.sp_helplinkedsrvloginshows local-to-remote mappings; aNULLlocal login can represent a mapping that applies to all local logins. Preserve the local and remote identities separately.
After selecting one named link, test the smallest remote query supported by its configuration:
If RPC execution is disabled but data access is allowed, use a read-only pass-through query instead:
Decide: a returned remote identity enables only the operations granted to that mapped login.
RPC OUT is disabled, login-mapping failures and data-access failures rule out different mechanisms; they do not prove the server is unreachable. Ifenum_impersonateidentifies a specific permitted login, test only that login and always return to the original context:
Do not enable RPC, create mappings or change server configuration merely to enumerate. A remote SQL query is not OS execution; independently validate any file or command facility and the operating-system identity behind it.
Sources: Microsoft sys.servers, sp_helplinkedsrvlogin, EXECUTE AS, and OPENQUERY.
Why it works
SQL login, sysadmin/impersonation rights and xp_cmdshell configuration are separate conditions. OS commands run under the SQL service or configured proxy context, not automatically as domain administrator.
Worked example · hypothetical
Observation: Windows authentication succeeds to SQL Server. Test: inspect role and allowed impersonation. Illustrative output: only a low-privilege database user. Interpretation: enumerate permitted data and relationships before proposing xp_cmdshell.
New identity, privilege, host or route? Re-enumerate → All topic tasks →