xp_cmdshell 自己收藏

 
一、简介
        xp_cmdshell 扩展存储过程将命令字符串作为操作系统命令 shell 执行,并以文本行的形式返回所有输出。
 
二、安全隐患
        由于xp_cmdshell 可以执行任何操作系统命令,所以一旦SQL Server管理员帐号(如sa)被攻破,那么攻击者就可以利用xp_cmdshell 在SQL Server中执行操作系统命令,如:创建系统管理员,也就意味着系统的最高权限已在别人的掌控之中。
 
三、SQL Server 2005中的xp_cmdshell
        由于存在安全隐患,所以在SQL Server 2005中, xp_cmdshell 默认是关闭的。
        此时,如果执行 xp_cmdshell 将会提示服务未开启:
 
        exec xp_cmdshell 'dir c:\'

消息 15281,级别 16,状态 1,过程 xp_cmdshell,第 1 行
SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'xp_cmdshell'。有关启用 'xp_cmdshell' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"。

四、开启xp_cmdshell
        打开外围应用配置器—>
        功能的外围应用配置器—>
        实例名\Database Engine\xp_cmdshell—>
        启用

        或运行   sp_configure   系统存储过程来启用它,如下面的代码示例所示:   
 1 --   To   allow   advanced   options   to   be   changed.   
 2  EXEC   sp_configure   'show   advanced   options',   1   
 3  GO   
 4  --   To   update   the   currently   configured   value   for   advanced   options.   
 5  RECONFIGURE   
 6  GO   
 7  --   To   enable   the   feature.   
 8  EXEC   sp_configure   'xp_cmdshell',   1   
 9  GO   
10  --   To   update   the   currently   configured   value   for   this   feature.   
11  RECONFIGURE   
12  GO   
13
 
五、使用xp_cmdshell
        显示C盘根目录下的所有目录和文件:
 
        exec xp_cmdshell 'dir c:\'
 
output
------------------------------------------------------------------------
 驱动器 C 中的卷没有标签。
 卷的序列号是 F8C5-1B8C
NULL
 c:\ 的目录
NULL
2007-01-29  14:08                 0 AUTOEXEC.BAT
2007-01-29  14:08                 0 CONFIG.SYS
2007-01-29  14:20    <DIR>          dell
2007-01-29  14:13    <DIR>          Documents and Settings
2007-07-11  09:57    <DIR>          Inetpub
2007-07-11  14:31    <DIR>          Program Files
2007-06-07  07:35    <DIR>          TEMP
2007-07-05  15:08             1,506 testout.txt
2007-07-05  15:10             1,506 testout01.txt
2007-07-11  13:32    <DIR>          WINDOWS
2007-01-29  14:09    <DIR>          wmpub
2007-06-25  15:00    <DIR>          ~CUL2
               4 个文件          3,012 字节
               8 个目录    420,556,800 可用字节
NULL
(20 行受影响)








附:一个使用例子

方法:
1、建立过程
CREATE PROCEDURE sp_textcopy (
  @srvname    varchar (30),
  @login      varchar (30),
  @password    varchar (30),
  @dbname      varchar (30),
  @tbname      varchar (30),
  @colname    varchar (30),
  @filename    varchar (30),
  @whereclause varchar (40),
  @direction  char(1))
AS
DECLARE @exec_str varchar (255)
SELECT @exec_str =
        'textcopy /S ' + @srvname +
        ' /U ' + @login +
        ' /P ' + @password +
        ' /D ' + @dbname +
        ' /T ' + @tbname +
        ' /C ' + @colname +
        ' /W "' + @whereclause +
        '" /F ' + @filename +
        ' /' + @direction
EXEC master..xp_cmdshell @exec_str 

2、建表和初始化数据
create table 表名 (编号 int,image列名 image)
go
insert 表名 values(1,0x)    -- 必须的,且不是null
insert 表名 values(2,0x)    -- 必须的,且不是null
go

3、读入
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','I' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','I' --注意条件是 编号=2

go

4、读出成文件
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','O' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','O' --注意条件是 编号=2
go

如果报textcopy不是可执行文件的话,你就到
C:\Program Files\Microsoft SQL Server\MSSQL\Binn
目录下拷备 textcopy.exe到:
C:\Program Files\Microsoft SQL Server\80\Tools\Binn


posted @ 2007-08-02 15:15  Boy Xie  阅读(1461)  评论(2编辑  收藏  举报