网络安全系列 之 MySQL数据库安全

数据库安全使用规则

1. 数据库版本及运行要求

  1. 使用稳定的解决了已知漏洞的版本,如5.6.39或5.7.20 (更新 于2018.2)
  2. 以--skip-symbolic-links选项启动数据库:禁止在创建索引和创建表的时候,将索引文件和数据文件链接到其他文件。

2. 通用加固项

  1. 删除冗余数据库, 如test。
    drop database if exists ${dbname};
    
  2. 清除无用用户(没有用户名的用户)。
    drop user ''
    
  3. 修改超级用户的密码。
    set password for <user>@<hostname> = password('<yourpassword>')
    
  4. 配置密码复杂度
    使用validate_password.so插件:
    Installing the MySQL Password Validation Plugin
    配置后,举例:
mysql> create user testuser;
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> CREATE USER 'test'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements        
mysql> grant all privileges on *.* to testuser@"%" Identified by "123456";
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> use mysql; update user set password =PASSWORD('123456') where user ='system';
Database changed
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

自问: 使用insert能够绕过 !?

mysql> insert into user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject)  values('%','testuser','123456','BLOB','BLOB','BLOB');
Query OK, 1 row affected (0.00 sec)

自答:
. insert直接插入的123456虽然可以插入成功但表中保存的应是加密后的内容,不能用123456进行登录;
. 使用PASSWORD('123456'),PASSWORD方法会进行校验,校验通过情况下insert方式(insert后记得flush privileges;)添加用户有效!
. 补充:即便使用UPPER(SHA1(UNHEX(SHA1('123456')))) ,插入其加密之后的值,也不能使用“123456”进行登录。

mysql> insert into user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject)  values('%','testuser',PASSWORD('123456'),'BOLOB','BLOB','BLOB');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> update user set password=UPPER(SHA1(UNHEX(SHA1('123456')))) where user='testuser';
...
mysql> select user,host,password from user;
+-------------+-----------+-------------------------------------------+
| user        | host      | password                                  |
+-------------+-----------+-------------------------------------------+
| root        | 127.0.0.1 | *936A04AEF6CF3742E2F327C1970D875C588546F2 |
| testuser    | %         | 6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9|
| system      | %         | *197D257ACCF6E52954DAA9A406D6B58EA95FCF45|
+-------------+-----------+-------------------------------------------+
# mysql -utestuser -p123456
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'testuser'@'localhost' (using password: YES)

3. 用户权限

Mysql提供的用户权限参考: Permissible Privileges for GRANT and REVOKE

  1. mysql.user表只有管理员(root)用户才能操作
    说明:实际应用中,有修改mysql.user的用户都算超级用户,都禁止远程连接数据库。

    【mysql用户权限管理体系】
    mysql.user表:记录用户的全局操作权限。GRANT ALL ON .和REVOKE ALL ON .
    mysql.db表:记录用户对指定数据库的操作权限。 GRANT ALL ON db_name.和REVOKE ALL ON db_name.
    mysql.talbes_priv表: 记录用户对指定表的操作权限。 GRANT ALL ON db_name.tbl_name和REVOKE ALL ON db_name.tbl_name
    mysql.columns_priv表: 记录用户对表中具体列的操作权限。
    注意: user表中没有相应的操作权限时,才会继续检查对应的数据库或表是否有操作权限。

  2. 避免以管理员用户创建存储过程和函数

  3. 以下权限只能赋予超级用户(root):
    Shutdown_priv、Process_priv、File_priv、Grant_priv、Reload_priv、Super_priv 、Create_user_priv 、Repl_slave_priv

    update mysql.user set Shutdown_priv='N',Process_priv='N',File_priv='N',Grant_priv='N',Reload_priv='N',Super_priv ='N',Create_user_priv ='N',Repl_slave_priv='N' where mysql.user.User!='root';
    

遗留:多实例情况下用户 multi_admin 也算管理员用户? 和root用户拥有一样的权限?

  1. 更改MySQL root用户的名字
    update mysql.user set user='<your_account>' where user='root'
    flush privileges
    

4. 连接设置

  1. 监听地址不允许包含*,0.0.0.0,::

    [mysqld] bind-address=<’ServerIP’>
    
  2. 用户主机名不使用通配符%

  3. 超级管理员只能本地登录

    验证:
    select count(*) from mysql.user where Super_priv = 'Y' and host not in ('localhost','127.0.0.1','::1')
    
  4. 限制数据库连接闲置等待时间

    [mysqld] wait_timeout、interactive_timeout、slave-net-timeout
    
  5. 防暴力破解
    Installing Connection Control Plugins

    修改my.cnf配置文件:
    plugin-load-add="connection_control.so"
    connection-control=FORCE_PLUS_PERMANENT
    connection-control-failed-login-attempts=FORCE_PLUS_PERMANENT
    connection-control-failed-connections-threshold=3
    connection-control-min-connection-delay=1000
    connection-control-max-connection-delay=2147483647
    

扩展

5. ssl安全认证

  1. 确保对所有远端用户均设置了ssl_type参数
    GRANT USAGE ON *.* TO <my_user>@<host> REQUIRE SSL;
    
    验证:
    SELECT user, host, ssl_type FROM mysql.user WHERE NOT HOST IN ('::1', '127.0.0.1', 'localhost'); 
    
>**注意**:服务端、客户端都需要配置SSL证书!

## 6. 涉及操作系统相关配置
### 6.1 系统资源
1. 禁止数据库启动用户交互式登录
    /etc/passwd文件,在mysql用户对应行添加/sbin/nologin或/bin/false.
    ```bash
    usermod -s /sbin/nologin mysql
    ```

### 6.2 文件权限
1. 禁止mysql_history文件记录信息
    ```bash
    rm <your_path>/.mysql_history 
    ln -s /dev/null <your_path>/.mysql_history
    sed -i '$a readonly MYSQL_HISTFILE=/dev/null' /etc/profile    
    ```
2. 限制安装文件属主和权限
    MySQL初始安装后,安装目录和文件属主要求为MySQL运行用户,目录权限要求为700,二进制文件为500,库文件为500,启动文件(一般位于/etc/init.d目录下)为500。
3. 限制数据库数据文件属主和权限
    数据目录和文件属主为mysql用户,数据目录为700,数据文件为600。
    ```bash
    #chmod 700 <data_dir Value>
    #chown mysql:mysql <data_dir Value>
    #chmod 600 <data_dir Value>/*
    #chown mysql:mysql <data_dir Value>/*
    ```
4. 限制日志文件(含binlog)属主(mysql)和权限(600)
    MySQL数据库中常见日志文件有:
    -  **错误日志**(log_error):
            记录了MySQL的启停和运行过程。——有问题首先看这个文件。
            'log_error=/opt/mysql/data/aaa.err'      
            
    -  **二进制日志**(log_bin):
            记录了对数据库执行更改的所有操作。
            作用:数据恢复、复制、日志审计(如判断有无注入攻击)。
            ```
            log_bin [=name]                         # 默认关闭,需要配置此参数开启。不指定name默认文件名为'主机名.日志序号'。
            ```
            备注:bin_log.00001即是二进制日志,bin_log.index为索引文件。二进制日志可使用自带的**mysqlbinlog**工具查看。

    -  慢查询日志(slow_query_log_file):
            记录运行慢的sql,帮助进行sql优化。
            ```
            slow_query_log=1                     # 默认关闭,1启用,0禁用     [log_slow_queries]
            slow_query_log_file=slow.log    # 指定文件路径和名字;默认值是'主机名-slow.log',位于datadir目录  
            long_query_time=3                   # 执行时间超过设置阈值(s)的sql语句会被记录在slow_query_log_file中
            log_output=FILE,TABLE           # 指定慢查询的输出方式,动态参数。 FILE:慢查询日志;TABLE:mysql.slow_log表
            ```
            Tips: 可以借助**mysqldumpslow**命令帮助分析慢查询日志。

    -  查询日志(general_log_file)
            记录了所有对数据库请求的信息。
            ```  
            general_log=1          # 默认关闭,配置此参数开启查询日志。
            general_log_file=/opt/mysql/data/general.log  
            ```
            按照log=/opt/mysql/data/xxx.log 配置,mysql服务启动失败。                 
            可以动态开启:
            ```
            mysql>set global general_log_file='/tmp/general.log';
            mysql>set global general_log=on;  
            mysql>set global general_log=off;  
            ```
            与slow_log一样,可以将查询日志放入mysql.general_log表中。
            

5. 限制my.cnf文件属主和权限
    ```bash
    #chmod 600 /xxx/my.cnf
    #chown mysql:mysql /xxx/my.cnf  
    ```
6. 限制Plugin目录及其文件权限
    取得Plugin目录路径:
     ```
    show variables where variable_name = 'plugin_dir';
    ```
    目录权限为500, 目录下的文件权限为400:
    ```bash
     #chmod 500 <plugin_dir Value>
    #chown mysql:mysql <plugin_dir Value>
    #chmod 400 <plugin_dir Value>/*    
    #chown mysql:mysql <plugin_dir Value>/*
    ```
7. 限制SSLfile文件权限
    数据库运行用户,权限是否为400。
    取得ssl_ca、ssl_cert、ssl_crl、ssl_key文件路径:
    ```bash
    show variables like 'ssl%';
    ```
原文地址:https://www.cnblogs.com/eaglediao/p/8470085.html