我想执行EXEC master..xp_cmdshell @bcpquery
但是我收到以下错误:
SQL Server阻止访问组件“xp_cmdshell”的过程“sys.xp_cmdshell”,因为此组件已作为此服务器的安全配置的一部分关闭。系统管理员可以使用sp_configure启用“xp_cmdshell”。有关启用“xp_cmdshell”的详细信息,请参阅SQL Server联机丛书中的“表面区域配置”。
有没有办法激活它,或在启用该功能之前执行某些操作?
如何解决?
答案 0 :(得分:353)
您需要启用它。查看xp_cmdshell MSDN docs的权限部分:
http://msdn.microsoft.com/en-us/library/ms190693.aspx:
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
答案 1 :(得分:37)
您还可以在重新配置后再次隐藏高级选项:
-- show advanced options
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
-- enable xp_cmdshell
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
-- hide advanced options
EXEC sp_configure 'show advanced options', 0
GO
RECONFIGURE
GO
答案 2 :(得分:14)
答案 3 :(得分:9)
如其他答案中所列,诀窍(在SQL 2005或更高版本中)是按顺序将show advanced options
和xp_cmdshell
的全局配置设置更改为1
。
除此之外,如果要保留以前的值,可以先从sys.configurations
读取它们,然后在最后以相反的顺序应用它们。我们还可以避免不必要的reconfigure
来电:
declare @prevAdvancedOptions int
declare @prevXpCmdshell int
select @prevAdvancedOptions = cast(value_in_use as int) from sys.configurations where name = 'show advanced options'
select @prevXpCmdshell = cast(value_in_use as int) from sys.configurations where name = 'xp_cmdshell'
if (@prevAdvancedOptions = 0)
begin
exec sp_configure 'show advanced options', 1
reconfigure
end
if (@prevXpCmdshell = 0)
begin
exec sp_configure 'xp_cmdshell', 1
reconfigure
end
/* do work */
if (@prevXpCmdshell = 0)
begin
exec sp_configure 'xp_cmdshell', 0
reconfigure
end
if (@prevAdvancedOptions = 0)
begin
exec sp_configure 'show advanced options', 0
reconfigure
end
请注意,这取决于SQL Server 2005或更高版本(原始问题是2008年)。
答案 4 :(得分:4)
虽然接受的答案大部分时间都有效,但我遇到过(仍然不知道为什么)某些情况并非如此。使用WITH OVERRIDE
中的RECONFIGURE
对查询稍作修改即可获得解决方案
Use Master
GO
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
GO
预期输出
配置选项'显示高级选项'从0更改为1.运行RECONFIGURE语句进行安装 配置选项' xp_cmdshell'从0更改为1.运行RECONFIGURE语句进行安装。
答案 5 :(得分:1)
即使这个问题已解决,我也想补充一下我的建议。...因为我忽略了开发人员。
重要的是要知道我们正在谈论 启用的MSSQL xp_cmdshell 对于安全至关重要,如消息警告中所示:
Blockquote SQL Server阻止访问组件“ xp_cmdshell”的过程“ sys.xp_cmdshell”,因为该组件已作为该服务器的 安全配置 的一部分而关闭。 [...]
启用该服务是一种弱点,例如,在Web应用程序中,该弱点可以反映并执行攻击者的SQL命令。
流行的CWE-89:SQL Injection
可能是我们软件的弱点,因此这些类型的场景可以为可能的攻击铺平道路,例如CAPEC-108:Command Line Execution through SQL Injection
< / p>
我希望做的事愉快,我们开发人员和工程师在做事时要有意识,我们会更加安全!
答案 6 :(得分:0)
对我来说,SQL 2008 R2的唯一方法是:
EXEC sp_configure 'Show Advanced Options', 1
RECONFIGURE **WITH OVERRIDE**
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE **WITH OVERRIDE**
答案 7 :(得分:-1)