<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Think In Geek &#187; security</title>
	<atom:link href="http://thinkingeek.com/tag/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkingeek.com</link>
	<description>In geek we trust</description>
	<lastBuildDate>Sat, 19 Jun 2010 22:00:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Controlling the commands executed with xp_cmdshell on SQL Server 2005</title>
		<link>http://thinkingeek.com/2008/11/13/controlling-the-commands-executed-with-xp_cmdshell-on-sql-server-2005/</link>
		<comments>http://thinkingeek.com/2008/11/13/controlling-the-commands-executed-with-xp_cmdshell-on-sql-server-2005/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 17:04:44 +0000</pubDate>
		<dc:creator>brafales</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sql server]]></category>

		<guid isPermaLink="false">http://thinkingeek.com/?p=81</guid>
		<description><![CDATA[SQL Server has a special extended stored procedure called xp_cmdshell. This procedure has a lot of power: it allows to execute any command line code on the machine hosting the SQL Server. Imagine you want to list all the files on C: on the SQL Server Windows host: you could write a T-SQL statement like [...]]]></description>
			<content:encoded><![CDATA[<p>
SQL Server has a special extended stored procedure called <em>xp_cmdshell</em>. This procedure has a lot of power: it allows to execute any command line code on the machine hosting the SQL Server.
</p>
<p>
Imagine you want to list all the files on <em>C:</em> on the SQL Server Windows host: you could write a T-SQL statement like this one:
</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">EXECUTE master<span style="color: #66cc66;">..</span>xp_cmdshell <span style="color: #ff0000;">'dir c:'</span></pre></div></div>

<p>
This stored procedure, however, is a very dangerous one, as it would allow to execute harmful code. This is the reason why it&#8217;s disabled by default. Even when enabled, only users on the <em>sysadmin</em> role can use it.
</p>
<p>
If you ever need some users the ability to run only some specific commands with xp_cmdshell, you can use the method I&#8217;ll explain below, making use of the <em>EXECUTE AS</em> modifier of the stored procedure definitions in T-SQL.
</p>
<p><span id="more-81"></span></p>
<p>
The proposed solution involves five steps:</p>
<ul>
<li>Enabling the <em>xp_cmdshell</em> extended procedure.</li>
<li>Adding a procedure on the database with the <em>EXECUTE AS</em> modifier as an administrator, controlling which commands are allowed to be executed.</li>
<li>Modifying or creating the <em>xp_cmdshell_proxy_account</em>, associating it to a user with <em>sysadmin</em> privileges.</li>
<li>Giving the user(s) you want the <em>EXECUTE</em> privileges to the procedure.</li>
<li>Grant the proxy account user the privilege to log on as a batch in the Windows server.</li>
</ul>
<p>
The execution of <em>xp_cmdshell</em> must be enabled on the SQL Server. This can be done through the SQL Surface Area Configuration utility or by code. Refer to Figure below on how to activate xp_cmdshell through the SQL Surface Area Configuration.
</p>
<div id="attachment_82" class="wp-caption aligncenter" style="width: 310px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/sql1.gif"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/sql1-300x226.gif" alt="SQL Surface Area" title="sqlsurface" width="300" height="226" class="size-medium wp-image-82" /></a><p class="wp-caption-text">SQL Surface Area</p></div>
<p>
To enable <em>xp_cmdshell</em> using SQL code, use the sentences below:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">EXEC master<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>sp_configure <span style="color: #ff0000;">'show advanced options'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span>
RECONFIGURE
EXEC master<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>sp_configure <span style="color: #ff0000;">'xp_cmdshell'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span>
RECONFIGURE</pre></div></div>

</p>
<p>
<b>This will allow users of the <em>sysadmin</em> role, and no one else, to execute <em>xp_cmdshell</em>.</b>
</p>
<p>
Now we have to create a special stored procedure that will control the actions used as parameters to <em>xp_cmdshell</em>. This will allow the administrators of the database to have control over which commands they allow to be executed on their servers. The most important part of this procedure is the <em>EXECUTE AS OWNER</em> modifier. By using this modifier, everyone that runs that procedure will be able to run it as if it was the owner of the database, thus having execute permissions to <em>xp_cmdshell</em> (we&#8217;re assuming the procedure will be created in the <em>master</em> schema. By granting execute permissions on that procedure, you will allow specific users an indirect way to call the <em>xp_cmdshell</em>.
</p>
<p>
Using this method, only the users of the <em>sysadmin</em> role will be able to execute <em>xp_cmdshell</em>, and only the users you grant <em>EXECUTE</em> permissions on the stored procedure will be able to execute the specific commands that you allow.
</p>
<p>
To insert the store procedure, log in as a <em>sysadmin</em> on the database and create it with the <a href="http://msdn.microsoft.com/en-us/library/ms188354.aspx" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/ms188354.aspx?referer=');"><em>EXECUTE AS OWNER</em></a> modifier on it.
</p>
<p>
For the above procedure to work on non <em>sysadmin</em> accounts there is another step that has to be done. By default, even if you have permissions on the store procedure, you won’t be able to execute it if you’re not on the <em>sysadmin</em> role. This is because those users need a proxy account that is used as the account in which the <em>xp_cmdshell</em> is executed.
</p>
<p>
So, for this procedure to work, you must create or modify the <em>xp_cmdshell_proxy_account</em> with a user within the <em>sysadmin</em> role. To setup this account, proceed with the code below:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">EXEC sp_xp_cmdshell_proxy_account <span style="color: #ff0000;">'MyDomain<span style="color: #000099; font-weight: bold;">\M</span>yUserName'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'myDomainPassword'</span></pre></div></div>

</p>
<p>
If the above code does not work, try this one:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> credential <span style="color: #808080; font-style: italic;">##xp_cmdshell_proxy_account## with identity = 'Domain\DomainUser', secret =  password'</span></pre></div></div>

</p>
<p>
After the procedure and the proxy account have been set, the users we want to be able to execute the procedure must be granted <em>EXECUTE</em> permission on it. To do so, execute this statement for every user you want to grant permissions:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">GRANT</span> EXECUTE <span style="color: #993333; font-weight: bold;">ON</span> Tango_xp_cmdshell <span style="color: #993333; font-weight: bold;">TO</span> <span style="color: #66cc66;">&lt;</span>username<span style="color: #66cc66;">&gt;</span>;
GO</pre></div></div>

</p>
<p>
To grant this permission, use the <em>Local Security Settings</em> on the <em>Administrative Tools</em> interface of the <em>Windows Control Panel</em>. Once there locate the property shown on the screenshot and add the user you gave permissions to the user list.
</p>
<div id="attachment_83" class="wp-caption aligncenter" style="width: 310px"><a href="http://thinkingeek.com/wp-content/uploads/2008/11/sql2.png"  rel="lightbox[roadtrip]"><img src="http://thinkingeek.com/wp-content/uploads/2008/11/sql2-300x211.png" alt="Local Security Policy" title="LocalSecurityPolicies" width="300" height="211" class="size-medium wp-image-83" /></a><p class="wp-caption-text">Local Security Policy</p></div>
<p>
<b>Note that enabling the <em>xp_cmdshell</em> command may still have some security implications, so try to avoid it when possible.</b></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fthinkingeek.com%2F2008%2F11%2F13%2Fcontrolling-the-commands-executed-with-xp_cmdshell-on-sql-server-2005%2F&amp;linkname=Controlling%20the%20commands%20executed%20with%20xp_cmdshell%20on%20SQL%20Server%202005" onclick="pageTracker._trackPageview('/outgoing/www.addtoany.com/share_save?linkurl=http_3A_2F_2Fthinkingeek.com_2F2008_2F11_2F13_2Fcontrolling-the-commands-executed-with-xp_cmdshell-on-sql-server-2005_2F_amp_linkname=Controlling_20the_20commands_20executed_20with_20xp_cmdshell_20on_20SQL_20Server_202005&amp;referer=');"><img src="http://thinkingeek.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://thinkingeek.com/2008/11/13/controlling-the-commands-executed-with-xp_cmdshell-on-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
