Skip to content
Xp Cmdshell Transact Sql

Xp Cmdshell Transact Sql

learn.microsoft.com • November 27, 2026

Access to this page requires authorization. You can try signing in or changing directories .

Access to this page requires authorization. You can try changing directories .

Applies to: SQL Server

Spawns a Windows command shell and passes in a string for execution. Any output is returned as rows of text.

Transact-SQL syntax conventions

Arguments for extended stored procedures must be entered in the specific order as described in the Syntax section. If the parameters are entered out of order, an error message occurs.

The string that contains a command to be passed to the operating system. command_string is varchar(8000) or nvarchar(4000) , with no default. command_string can't contain more than one set of double quotation marks. A single pair of quotation marks is required if any spaces are present in the file paths or program names referenced in command_string . If you have trouble with embedded spaces, consider using FAT 8.3 file names as a workaround.

An optional parameter, specifying that no output should be returned to the client.

0 (success) or 1 (failure).

Executing the following xp_cmdshell statement returns a directory listing of the current directory.

The rows are returned in an nvarchar(255) column. If the NO_OUTPUT option is used, only the following output is returned:

The Windows process spawned by xp_cmdshell has the same security rights as the SQL Server service account.

xp_cmdshell is a powerful feature and disabled by default. xp_cmdshell can be enabled and disabled by using Policy-Based Management or by executing sp_configure . For more information, see Surface area configuration and xp_cmdshell (server configuration option) . Using xp_cmdshell can trigger security audit tools.

xp_cmdshell operates synchronously. Control isn't returned to the caller until the command-shell command is completed. If xp_cmdshell is executed within a batch and returns an error, the batch will fail.

xp_cmdshell proxy account

When it's called by a user that isn't a member of the sysadmin fixed server role, xp_cmdshell connects to Windows by using the account name and password stored in the credential named ##xp_cmdshell_proxy_account## . If this proxy credential doesn't exist, xp_cmdshell fails.

The proxy account credential can be created by executing sp_xp_cmdshell_proxy_account . As arguments, this stored procedure takes a Windows user name and password. For example, the following command creates a proxy credential for Windows domain user SHIPPING\KobeR . Replace with a strong password.

For more information, see sp_xp_cmdshell_proxy_account .

Because malicious users sometimes attempt to elevate their privileges by using xp_cmdshell , xp_cmdshell is disabled by default. Use sp_configure or Policy Based Management to enable it. For more information, see xp_cmdshell Server Configuration Option .

When first enabled, xp_cmdshell requires CONTROL SERVER permission to execute and the Windows process created by xp_cmdshell has the same security context as the SQL Server service account. The SQL Server service account often has more permissions than are necessary for the work performed by the process created by xp_cmdshell . To enhance security, access to xp_cmdshell should be restricted to highly privileged users.

To allow non-administrators to use xp_cmdshell , and allow SQL Server to create child processes with the security token of a less-privileged account, follow these steps:

Create and customize a Windows local user account or a domain account with the least privileges that your processes require.

Create and customize a Windows local user account or a domain account with the least privileges that your processes require.

Use the sp_xp_cmdshell_proxy_account system procedure to configure xp_cmdshell to use that least-privileged account. Note You can also configure this proxy account using SQL Server Management Studio by right-clicking Properties on your server name in Object Explorer, and looking on the Security tab for the Server proxy account section.

Use the sp_xp_cmdshell_proxy_account system procedure to configure xp_cmdshell to use that least-privileged account.

You can also configure this proxy account using SQL Server Management Studio by right-clicking Properties on your server name in Object Explorer, and looking on the Security tab for the Server proxy account section.

In Management Studio, using the master database, execute the following Transact-SQL statement to give specific non- sysadmin users the ability to execute xp_cmdshell . The specified user must exist in the master database. GRANT exec ON xp_cmdshell TO N' ';

In Management Studio, using the master database, execute the following Transact-SQL statement to give specific non- sysadmin users the ability to execute xp_cmdshell . The specified user must exist in the master database.

Now non-administrators can launch operating system processes with xp_cmdshell and those processes run with the permissions of the proxy account that you configured. Users with CONTROL SERVER permission (members of the sysadmin fixed server role) continue to receive the permissions of the SQL Server service account for child processes that are launched by xp_cmdshell .

To determine the Windows account being used by xp_cmdshell when launching operating system processes, execute the following statement:

To determine the security context for another login, execute the following Transact-SQL code:

A. Return a list of executable files

The following example shows the xp_cmdshell extended stored procedure executing a directory command.

The following example uses xp_cmdshell to execute a command string without returning the output to the client.

In the following example, the xp_cmdshell extended stored procedure also suggests return status. The return code value is stored in the variable @result .

D. Write variable contents to a file

The following example writes the contents of the @var variable to a file named var_out.txt in the current server directory.

E. Capture the result of a command to a file

The following example writes the contents of the current directory to a file named dir_out.txt in the current server directory.

General extended stored procedures (Transact-SQL)

Server configuration: xp_cmdshell

Surface area configuration

sp_xp_cmdshell_proxy_account (Transact-SQL)

Was this page helpful?

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Last updated on 2025-06-23

The full story

This article is one source in a clustered incident — the cluster page carries the summary, timeline and every other outlet covering it.

Suspected Vietnam-linked WeaveLotus cluster breaches Thai and Philippine government networks SOCRadar's Threat Research Unit reports a previously undocumented cluster it tracks as WeaveLotus, running a campaign it calls RedOps, which targeted military, government and legislative organizations in Southeast Asia between August and September 2026. SOCRadar confirmed five intrusions in Thailand and the Philippines and reconnaissance against more than 30 Indonesian government organizations that did not progress to compromise. Initial access was reportedly gained through web-application weaknesses and default credentials, including GeoServer (CVE-2024-36401), an osTicket SQL injection (CVE-2021-42235), default JasperReports credentials, and a Laravel application with debug mode enabled. The operators then used reverse SSH tunnels and lateral movement; SOCRadar reports that a Thai legislative JasperReports server was used to reach a backend Microsoft SQL Server, and that a third-party document management system yielded 1,502 user records and metadata covering 88,575 documents. Reporting says 19 GB was exfiltrated, which SOCRadar says it verified against transfer logs. Some outlets report nearly 350,000 government records extracted, a figure not reflected in SOCRadar's account quoted in the other articles. SOCRadar assesses the actor as Vietnam-linked based on Vietnamese-language code, activity during UTC+07:00 working hours, and regional targeting, but says the link is only somewhat certain and that a connection to OceanLotus (APT32) is weak. The stated objective is intelligence on South China Sea defense and military agreements, amid ASEAN–China negotiations on a Code of Conduct. The named governments have not, according to one analyst note, issued public confirmations of these specific intrusions. 8 sources 75

Extracted Entities

Platforms (2)

Tools (1)