all groups > sql server programming > august 2007 >
You're in the

sql server programming

group:

xp_cmdshell permission denied SQL 2005


xp_cmdshell permission denied SQL 2005 jose.mendez22 NO[at]SPAM gmail.com
8/22/2007 11:32:09 PM
sql server programming:
My issue. I'm running a web app which makes command calls to a DB
using SQL Authentication. One task the app will have to do is utilize
th "xp_cmdshell" to populate an Excel spreadsheet and move the file
from location A to B which is a linked server. Since the DB user
executing all my sql commands is not a default sysadmin. My app
encounters the the following error "...EXECUTE permission denied on
object 'xp_cmdshell', database 'mssqlsystemresource'..." I've
investigated on possible ways to resolve this issue however I've had
no luck on resolving this issue; below are the step's I taken so far.

1) I created a windows acct - "ADomain\AUserAcct"
2) Created a Login acct for this user ("ADomain\AUserAcct") in SQL
2005 and granted user access to this DB. User access for this account
is "dbowner"
3) Executed the "sp_xp_cmdshell_proxy_account" sproc for this new acct
a. EXEC sp_xp_cmdshell_proxy_account "ADomain\AUserAcct",
"pwd"
4) Granted execute rights on sys.xp_cmdshell for this user
a. GRANT EXECUTE ON sys.xp_cmdshell TO "ADomain\AUserAcct

After these steps, I tried to execute my process again but ecountered
the same error msg. so

1) I modified the stored sproc to execute with the new user's acct.
but had the same error
a.Create proc myprocname with Execute As ADomain\AUserAcct

Note: when I execute the proc. Before any of these steps thru sql 2005
IDE with a sysadmin account all works great.

If anyone has any suggestions on how to resolve this or pointers on
where I went wrong, I would greatly appreciate it.

Thanks,
Re: xp_cmdshell permission denied SQL 2005 Uri Dimant
8/23/2007 12:00:00 AM
Jose

USE master;

GRANT EXECUTE on xp_cmdshell to [ADomain\AUserAcct]









[quoted text, click to view]

Re: xp_cmdshell permission denied SQL 2005 Tibor Karaszi
8/23/2007 12:00:00 AM
[quoted text, click to view]

That is not how xp_cmdshell security works. See my other reply.

There are basically three things involved here.

1. Execution of xp_cmdshell need to be allowed at the instance level.

2. The user need permissions in SQL Server to execute xp_cmdshell.

3. SQL Server then need to open a process (the process spawned by xp_cmdshell) in a certain Windows
context (every process in Windows need to execute in a certain Windows user context).

1:
Use "Surface Area Configuration" or sp_configure to allow usage of xp_cmdshell:

EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE


2. You need to have a user in the *master* database which has execute permission on xp_cmdshell. If
you are uncertain about the difference between login and user, you should read up in BOL on
security:

USE master
CREATE LOGIN x WITH PASSWORD = 'jlkw#.6('

--Note, we are in the master database!!!
CREATE USER x FROM LOGIN x
GO

--Run as login x
EXECUTE AS login = 'x'
--Below fails, no execute permission on xp_cmdshell
EXEC xp_cmdshell 'DIR C:\*.*'
REVERT

--Note, we are in the master database!!!
GRANT EXECUTE ON xp_cmdshell TO x

--Try again
EXECUTE AS login = 'x'
--Execution of xp_cmdshell is allowed.
--But I haven't configured the proxy account...
EXEC xp_cmdshell 'DIR C:\*.*'
REVERT

The execution of xp_cmdshell gave below error message:

Msg 15153, Level 16, State 1, Procedure xp_cmdshell, Line 1
The xp_cmdshell proxy account information cannot be retrieved or is invalid. Verify that the
'##xp_cmdshell_proxy_account##' credential exists and contains valid information.

Above is expected. So for the final step:
3.
Now we need to say what Windows account should be used when a non-sysadmin user is executing
xp_cmdshell:
EXEC sp_xp_cmdshell_proxy_account 'domain\WinUser','pwd'
EXECUTE AS login = 'x'
--Execution of xp_cmdshell is allowed.
--And executes successfully!!!
EXEC xp_cmdshell 'DIR C:\*.*'
REVERT

And below is a clean-up of what I've done:
EXEC sp_xp_cmdshell_proxy_account null

DROP USER JohnDoe
DROP LOGIN JohnDoe
EXEC sp_configure 'xp_cmdshell', 0
RECONFIGURE


I've blogged about this in
http://sqlblog.com/blogs/tibor_karaszi/archive/2007/08/23/xp-cmdshell-and-permissions.aspx. The blog
basically say what I said above.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


[quoted text, click to view]
Re: xp_cmdshell permission denied SQL 2005 Kalen Delaney
9/11/2007 1:27:00 PM
Hi Audrey

It looks like you missed something in Tibor's instructions.

You should not issue the GRANT command after setting EXECUTE AS.
The EXECUTE AS tells SQL Server to run the following commands as if you were
logged in as JohnDoe. And then as JohnDoe you try to grant permission to
JohnDoe. You cannot grant permission to yourself, as the error message
indicates.

Tibor's script had a REVERT at some point after the EXECUTE AS, to revert
your identity back to the sysadmin, who can grant permission.

The EXECUTE AS is only needed to test the permission that has been granted
to JohnDoe, but not to actually grant the permission. So try this:

USE master
CREATE LOGIN JohnDoe WITH PASSWORD = 'somepassword'

CREATE USER ohnDoe FROM LOGIN ohnDoe

GRANT EXECUTE ON xp_cmdshell TO JohnDoe


EXECUTE as login = 'JohnDoe'

--Now, as JohnDoe, try executing xp_cmdshell:
EXEC sp_cmdshell 'your command here....'

--
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://sqlblog.com


[quoted text, click to view]
Re: xp_cmdshell permission denied SQL 2005 audrey.nsh NO[at]SPAM gmail.com
9/11/2007 3:29:23 PM
On Aug 23, 3:42 am, "Tibor Karaszi"
[quoted text, click to view]

Ok, I followed all your instructions above and I am still getting the
the following error "...EXECUTE permission denied on
object 'xp_cmdshell', database 'mssqlsystemresource'..."

This is what I have done:

(1) Created a login (server role=public)
(2) Created a user from login in the master database
(3) Gave it Execute permission on xp_cmdshell
(4) exec sp_xp_cmdshell_proxy_account 'Domain\WinAccount','pwd'

and it doesn't work.

what am i doing wrong??

BTW, I am using xp_cmdshell off a trigger,
do I always have to specify exec sp_xp_cmdshell_proxy_account 'Domain
\WinAccount','pwd', exec as login = 'Login' whenever the trigger gets
invoked?
Re: xp_cmdshell permission denied SQL 2005 audrey.nsh NO[at]SPAM gmail.com
9/11/2007 4:59:12 PM
On Sep 11, 12:12 pm, "Tibor Karaszi"
[quoted text, click to view]

in the trigger, i run

exec xp_cmdshell

Re: xp_cmdshell permission denied SQL 2005 Tibor Karaszi
9/11/2007 6:12:34 PM
[quoted text, click to view]

I would suspect that you aren't using the login you created in stepo 1 when you execute xp_cmdshell.



[quoted text, click to view]

No, that is a one-time configuration. And, of course, you need to specify a valif login name and
password for your domain...

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


[quoted text, click to view]
Re: xp_cmdshell permission denied SQL 2005 Tibor Karaszi
9/11/2007 7:13:47 PM
I'm sorry, but I'm out of ideas... :-(

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


[quoted text, click to view]
Re: xp_cmdshell permission denied SQL 2005 audrey.nsh NO[at]SPAM gmail.com
9/11/2007 8:15:59 PM
On Sep 11, 1:13 pm, "Tibor Karaszi"
[quoted text, click to view]
Re: xp_cmdshell permission denied SQL 2005 audrey.nsh NO[at]SPAM gmail.com
9/11/2007 8:30:06 PM
On Sep 11, 1:13 pm, "Tibor Karaszi"
[quoted text, click to view]
Re: xp_cmdshell permission denied SQL 2005 audrey.nsh NO[at]SPAM gmail.com
9/11/2007 8:41:55 PM
[quoted text, click to view]
Re: xp_cmdshell permission denied SQL 2005 audrey.nsh NO[at]SPAM gmail.com
9/11/2007 8:49:45 PM
[quoted text, click to view]
Re: xp_cmdshell permission denied SQL 2005 Tibor Karaszi
9/11/2007 11:00:30 PM
[quoted text, click to view]

My guess is that the service account for the SQL Server servce is lacking some windows priviliges.
Search Books Online for "level token" and you will find topics that lists the windows priviliges
that the service account needs.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


[quoted text, click to view]
Re: xp_cmdshell permission denied SQL 2005 vijay
9/12/2007 12:00:00 AM
On Sep 12, 2:00 am, "Tibor Karaszi"
[quoted text, click to view]
Re: xp_cmdshell permission denied SQL 2005 audrey.nsh NO[at]SPAM gmail.com
9/12/2007 12:00:00 AM
[quoted text, click to view]