首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux 系统与数据库安全

Linux 系统与数据库安全

作者头像
netkiller old
发布2018-03-05 18:21:10
3.4K0
发布2018-03-05 18:21:10
举报
文章被收录于专栏:NetkillerNetkiller

Linux 系统与数据库安全

目录

  • 1. 帐号安全
    • 1.1. Shell 安全
    • 1.2. .history 文件
  • 2. 临时文件安全
  • 3. 其他安全问题
  • 4. 防火墙配置
  • 5. 数据库安全
    • 5.3.1. 数据备份
    • 5.3.2. 数据恢复
    • 5.2.1. bind-address
    • 5.2.2. mysql 管理
    • 5.2.3. ~/.mysql_history
    • 5.1. 数据库程序安全
    • 5.2. 数据库客户端安全
    • 5.3. mysqldump 安全
    • 5.4. crontab 定时备份脚本于安全
    • 5.5. 数据库归档文件
    • 5.6. 开发与测试环境的数据库安全问题
    • 5.7. 与数据库有关的服务器安全问题

1. 帐号安全

帐号权限安全

1.1. Shell 安全

需求:限制用户权限,仅提供一些linux常用命令,用户监控linux系统于网络运行情况,不允许用户ssh登录后随意运行linux命令

  1. 用户不能进入到Shell环境 例如普通用户一旦登录web服务器可以看到web程序中的数据库配置
  2. 用户可以了解OS工作状态如内存,cpu,网络等等 例如:ping, tracepath, top, free, netstat
  3. 可以查看系统部分日志 例如:access.log, error.log, php-error.log ...

使用mgmt替代bash

			#!/bin/bash
TITLE="Client"

#USER=$(whiptail --inputbox "User:" 8 60 --title "$TITLE" 3>&1 1>&2 2>&3)

#PASSWD=$(whiptail --title "$TITLE" --passwordbox "Passsword:" 8 60 3>&1 1>&2 2>&3)

COMMAND=$(whiptail --title "$TITLE" --menu "Administrator Tools" 22 50 10 \
"ping" "ping" \
"tracepath" "tracepath" \
"top" "top" \
"free" "free"  \
"ps" "ps"  \
"netstat" "netstat"  \
"lsof" "lsof"  \
"iftop" "iftop"  \
"log" "log" \
3>&1 1>&2 2>&3)

function option(){
OPTION=$(whiptail --inputbox "COMMAND-LINE Options: " 8 60 --title "$TITLE" 3>&1 1>&2 2>&3)
}

function weblog(){
LOG=$(whiptail --title "$TITLE" --menu "Logs" 22 50 8 \
"/var/log/messages" "message"  \
"/var/log/syslog" "syslog"  \
"/var/log/nginx/access.log" "access.log" \
"/var/log/nginx/error.log" "error.log"  \
3>&1 1>&2 2>&3)

}

case $COMMAND in
ping)
    option
    $COMMAND $OPTION
    ;;
tracepath)
    option
    $COMMAND $OPTION
    ;;
free)
    $COMMAND -m
    read
    ;;
top|iftop)
    $COMMAND
    ;;
log)
    weblog
    tail -f $LOG
    ;;
ps|lsof)
    option
    $COMMAND $OPTION
    read
    ;;
netstat)
    option
    $COMMAND $OPTION
    read
    ;;
*)
    exit $?
esac			

Shell 启动文件,主要用户隐藏 /srv/sbin/mgmt 文件(针对菜鸟)

			$ cat shell.c
#include <stdlib.h>
main()
{
	for (;;){
		system("/srv/sbin/mgmt");
	}
}			

编译.c文件

gcc shell.c -o /bin/nsh			

添加Shell到/etc/shells

echo /bin/nsh >> /etc/shells			

将用户shell更改为我们刚刚创建的nsh

$ vim /etc/passwd

www:x:33:33:www:/var/www:/bin/nsh 			

现在来作一个测试,如果正确应该现在为下面的TUI界面

			ssh www@example.com

              ┌───────────────────┤ Client ├───────────────────┐
              │ Administrator Tools                            │
              │                                                │
              │               ping      ping                   │
              │               tracepath tracepath              │
              │               top       top                    │
              │               free      free                   │
              │               ps        ps                     │
              │               netstat   netstat                │
              │               lsof      lsof                   │
              │               iftop     iftop                  │
              │               log       log                    │
              │                                                │
              │                                                │
              │           <Ok>               <Cancel>          │
              │                                                │
              └────────────────────────────────────────────────┘			

提示

这里采用的方式是给用户提供一个界面的方式,另外还有更好的方案,你可以些一个Shell的外壳,你需要实现

  1. 与Shell相同的提示符
  2. 提供TAB补齐
  3. 上下光标键翻看历史命令,左右光标改变位置,Home/End 键到行首与行尾
  4. Ctrl+R 搜索, Ctrl+D 退出
  5. Ctrl+S,Ctrl+Q 等等

流程

用户输入 -> 关键字过滤 -> 放行				

例如用户输入 cd / 经过过滤器后, cd /home/usr/

例如用户输入 cd /aaa 经过过滤器后, cd /home/usr/aaa

rm -rf /usr/local 提示拒绝等等

我已经使用python实现上面的大部分功能(因为python受到很多限制)如果使用C可以100%实现,需要你的想想力了

1.2. .history 文件

SA的操作记录问题

通过~/.bash_history文件记录系统管理员的操作记录,定制.bash_history格式

HISTSIZE=1000
HISTFILESIZE=2000
HISTTIMEFORMAT="%Y-%m-%d-%H:%M:%S "
export HISTTIMEFORMAT			

看看实际效果

$ history | head
    1  2012-02-27-09:10:45 do-release-upgrade
    2  2012-02-27-09:10:45 vim /etc/network/interfaces
    3  2012-02-27-09:10:45 vi /etc/network/interfaces
    4  2012-02-27-09:10:45 ping www.163.com			

2. 临时文件安全

临时文件不应该有执行权限

/tmp

/dev/sda3 /tmp ext4 nosuid,noexec,nodev,rw 0 0		

同时使用符号连接将/var/tmp 指向 /tmp

/dev/shm

none /dev/shm tmpfs defaults,nosuid,noexec,rw 0 0		

3. 其他安全问题

/etc/sudoers

		Cmnd_Alias WEBMASTER = /usr/local/webserver/nginx/sbin/nginx, /usr/local/webserver/php/sbin/php-fpm, !/usr/local/webserver/mysql/bin/*
www localhost = NETWORKING, SERVICES, DELEGATING, PROCESSES, WEBMASTER

Cmnd_Alias Database = /usr/bin/mysqldump, /srv/mysql/bin/mysql, /u01/oracle/10.x.x/bin/sqlplus
oralce localhost = NETWORKING, SERVICES, DELEGATING, PROCESSES, WEBMASTER, Database		

使用www用户测试登录,无误后修改SSH配置文件,禁止root登录。

		vim /etc/ssh/sshd_config
PermitRootLogin no		

然后在测试从www su 到root

4. 防火墙配置

封锁22等端口,避免相互跳转

lokkit --enabled
iptables -F
iptables -A OUTPUT -p tcp -m multiport --dports 22,21,2049 -j REJECT
/etc/init.d/iptables save
iptables -L -n		

web 服务器禁止使用ssh,作为跳板机

用户将不能使用ssh命令登陆到其他电脑

5. 数据库安全

我们以MySQL为例,讲解怎样控制DBA权限。稍加修改即可用于oracle等服务器

  1. DBA 没有系统SSH帐号,只有数据库帐号
  2. 系统管理员只能有SSH系统帐号,没有数据库帐号
  3. DBA 可备份数据库,还原数据库指定的备份文件,但是接触不到备份文件
  4. DBA 有权重启数据库以及修复损坏库/表文件,通过工具完成,而不是登录SSH运行命令

5.1. 数据库程序安全

rpm, deb 等等包安装mysql后默认权限是 755

$ ll /usr/bin/mysql*
-rwxr-xr-x 1 root root  132132 2012-02-28 01:33 /usr/bin/mysql*
-rwxr-xr-x 1 root root  111572 2012-02-28 01:31 /usr/bin/mysqlaccess*
-rwxr-xr-x 1 root root   32468 2012-02-28 01:33 /usr/bin/mysqladmin*
-rwxr-xr-x 1 root root 2030768 2011-09-14 23:04 /usr/bin/mysql-admin*
lrwxrwxrwx 1 root root      10 2012-02-28 01:33 /usr/bin/mysqlanalyze -> mysqlcheck*
-rwxr-xr-x 1 root root  147288 2012-02-28 01:33 /usr/bin/mysqlbinlog*
-rwxr-xr-x 1 root root   12006 2012-02-28 01:31 /usr/bin/mysqlbug*
-rwxr-xr-x 1 root root   24940 2012-02-28 01:33 /usr/bin/mysqlcheck*
-rwxr-xr-x 1 root root  451016 2012-02-28 01:33 /usr/bin/mysql_client_test*
-rwxr-xr-x 1 root root 7246484 2012-02-28 01:33 /usr/bin/mysql_client_test_embedded*
-rwxr-xr-x 1 root root    4245 2012-02-28 01:31 /usr/bin/mysql_convert_table_format*
-rwxr-xr-x 1 root root   23943 2012-02-28 01:31 /usr/bin/mysqld_multi*
-rwxr-xr-x 1 root root   16642 2012-02-28 01:32 /usr/bin/mysqld_safe*
-rwxr-xr-x 1 root root  101636 2012-02-28 01:33 /usr/bin/mysqldump*
-rwxr-xr-x 1 root root    7402 2012-02-28 01:31 /usr/bin/mysqldumpslow*
-rwxr-xr-x 1 root root    3315 2012-02-28 01:31 /usr/bin/mysql_find_rows*
-rwxr-xr-x 1 root root    1261 2012-02-28 01:31 /usr/bin/mysql_fix_extensions*
-rwxr-xr-x 1 root root    5834 2012-02-28 01:31 /usr/bin/mysql_fix_privilege_tables*
-rwxr-xr-x 1 root root   32477 2012-02-28 01:31 /usr/bin/mysqlhotcopy*
-rwxr-xr-x 1 root root   24584 2012-02-28 01:33 /usr/bin/mysqlimport*
-rwxr-xr-x 1 root root   14657 2012-02-28 01:31 /usr/bin/mysql_install_db*
lrwxrwxrwx 1 root root      10 2012-02-28 01:33 /usr/bin/mysqloptimize -> mysqlcheck*
-rwxr-xr-x 1 root root 2006884 2011-09-14 23:04 /usr/bin/mysql-query-browser*
lrwxrwxrwx 1 root root      10 2012-02-28 01:33 /usr/bin/mysqlrepair -> mysqlcheck*
-rwxr-xr-x 1 root root   39016 2012-02-28 01:32 /usr/bin/mysqlreport*
-rwxr-xr-x 1 root root    8066 2012-02-28 01:31 /usr/bin/mysql_secure_installation*
-rwxr-xr-x 1 root root   17473 2012-02-28 01:31 /usr/bin/mysql_setpermission*
-rwxr-xr-x 1 root root   23716 2012-02-28 01:33 /usr/bin/mysqlshow*
-rwxr-xr-x 1 root root   45884 2012-02-28 01:33 /usr/bin/mysqlslap*
-rwxr-xr-x 1 root root  208148 2012-02-28 01:33 /usr/bin/mysqltest*
-rwxr-xr-x 1 root root 6960852 2012-02-28 01:33 /usr/bin/mysqltest_embedded*
-rwxr-xr-x 1 root root 1334028 2012-02-28 01:33 /usr/bin/mysql_tzinfo_to_sql*
-rwxr-xr-x 1 root root   64728 2012-02-28 01:33 /usr/bin/mysql_upgrade*
-rwxr-xr-x 1 root root  149836 2012-02-28 01:33 /usr/bin/mysql_waitpid*
-rwxr-xr-x 1 root root    2108 2012-02-22 01:28 /usr/bin/mysql-workbench*
-rwxr-xr-x 1 root root 9885312 2012-02-22 01:29 /usr/bin/mysql-workbench-bin*
-rwxr-xr-x 1 root root    3888 2012-02-28 01:31 /usr/bin/mysql_zap*			

从安全角度考虑我们需要如下更改

chown mysql:mysql /usr/bin/mysql*
chmod 700 /usr/bin/mysql*			

mysql用户是DBA专用用户

5.2. 数据库客户端安全

DBA不需要通过SSH登录数据库服务器,然后运行mysql/sqlplus在登录数据库

5.2.1. bind-address

如果web与database 在一台机器上

bind-address = 127.0.0.1				
5.2.2. mysql 管理
				$ cat ../database/mysqltui
#!/bin/bash
TITLE="MySQL Client"

HOST=$(whiptail --title "$TITLE" --menu "MySQL Host" 22 50 8 \
"127.0.0.1" "localhost" \
"172.16.0.1" "MySQL Master" \
"172.16.0.2" "MySQL Slave 1" \
"172.16.0.3" "MySQL Slave 2"  \
3>&1 1>&2 2>&3)



USER=$(whiptail --inputbox "MySQL User:" 8 60 --title "$TITLE" 3>&1 1>&2 2>&3)

PASSWD=$(whiptail --title "$TITLE" --passwordbox "MySQL Password:" 8 60 3>&1 1>&2 2>&3)

#DATABASE=$(mysqlshow -h$HOST -u$USER | egrep -o "|\w(.*)\w|" | grep -v "Databases" |awk '{print "\""$1"\" \""$1"\""}')
#DATABASE=$(mysqlshow -h$HOST -u$USER | egrep -o "|\w(.*)\w|" | grep -v "Databases" |awk "{print \"$1\" \"$1\"}")

#DB=$(whiptail --title "$TITLE" --menu "MySQL DATABASE" 22 50 8 $DATABASE  3>&1 1>&2 2>&3)

DATABASE=$(whiptail --inputbox "MySQL Database:" 8 60 --title "$TITLE" 3>&1 1>&2 2>&3)

echo $HOST $USER $PASSWD $DATABASE

mysql -h$HOST -u$USER -p$PASSWD $DATABASE				
             ┌───┤ MySQL Adminstrator ├───┐
             │ Menu                       │
             │                            │
             │       1 MySQL Manager      │
             │       2 MySQL Backup       │
             │       2 MySQL Restore      │
             │                            │
             │                            │
             │    <Ok>        <Cancel>    │
             │                            │
             └────────────────────────────┘				
        ┌────────┤ MySQL Adminstrator ├────────┐
        │ Database Host                        │
        │                                      │
        │        127.0.0.1  localhost          │
        │        172.16.0.1 mysql master       │
        │        172.16.0.2 mysql slave        │
        │                                      │
        │       <Ok>           <Cancel>        │
        │                                      │
        └──────────────────────────────────────┘				

/etc/php5/fpm/pool.d/www.conf

        ┌────────┤ MySQL Adminstrator ├────────┐
        │ User                                 │
        │                                      │
        │ root________________________________ │
        │                                      │
        │       <Ok>           <Cancel>        │
        │                                      │
        └──────────────────────────────────────┘

        ┌────────┤ MySQL Adminstrator ├────────┐
        │ Password                             │
        │                                      │
        │ ****________________________________ │
        │                                      │
        │       <Ok>           <Cancel>        │
        │                                      │
        └──────────────────────────────────────┘				

进入mysql客户端

				Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 503
Server version: 5.1.58-1ubuntu1 (Ubuntu)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>				

安全提示

  1. 从安全角度看,你可以去掉输入密码的过程。在终端提示符下输入 Enter password:
  2. 还可以写入~/.my.conf文件 这样ssh mysql@example.com的时候输入第一道密码,然后进入mysql不需要输入密码
  3. 如果需要输入密码对话到建议删除.bash_history rm -rf .bash_history ln -s /dev/null .bash_history
5.2.3. ~/.mysql_history

通过~/.mysql_history文件记录DBA操作记录

插入时间点,在~/.bashrc中加入下面命令

				cat >> ~/.bashrc <<EOD
echo `date` >> ~/.mysql_history
EOD				
$ tail ~/.bashrc
echo `date` >> ~/.mysql_history				

查看实际效果

$ tail ~/.mysql_history
EXPLAIN SELECT * FROM stuff where id=3 \G
EXPLAIN SELECT * FROM stuff where id='3' \G
EXPLAIN SELECT * FROM stuff where id='2' \G
Mon Feb 27 09:15:18 CST 2012
EXPLAIN SELECT * FROM stuff where id='2' and created = '2012-02-01' \G
EXPLAIN SELECT * FROM stuff where id='1' and created = '2012-02-01' \G
EXPLAIN SELECT * FROM stuff where id='3' and created = '2012-02-01' \G
EXPLAIN SELECT * FROM stuff where id='2' and created = '2012-02-01' \G
EXPLAIN SELECT * FROM stuff where id='2' or created = '2012-02-01' \G
EXPLAIN SELECT * FROM stuff where id='2' and created = '2012-02-01' \G
Mon Feb 27 11:48:37 CST 2012				

5.3. mysqldump 安全

5.3.1. 数据备份
				MySQL Client
             ┌───┤ MySQL Adminstrator ├───┐
             │ Menu                       │
             │                            │
             │       1 MySQL Manager      │
             │       2 MySQL Backup       │
             │       2 MySQL Restore      │
             │                            │
             │                            │
             │    <Ok>        <Cancel>    │
             │                            │
             └────────────────────────────┘				
				MySQL Client
        ┌────────┤ MySQL Adminstrator ├────────┐
        │ Database Host                        │
        │                                      │
        │        127.0.0.1  localhost          │
        │        172.16.0.1 mysql master       │
        │        172.16.0.2 mysql slave        │
        │                                      │
        │       <Ok>           <Cancel>        │
        │                                      │
        └──────────────────────────────────────┘				
        ┌────────┤ MySQL Adminstrator ├────────┐
        │ User                                 │
        │                                      │
        │ root________________________________ │
        │                                      │
        │       <Ok>           <Cancel>        │
        │                                      │
        └──────────────────────────────────────┘

        ┌────────┤ MySQL Adminstrator ├────────┐
        │ Password                             │
        │                                      │
        │ ****________________________________ │
        │                                      │
        │       <Ok>           <Cancel>        │
        │                                      │
        └──────────────────────────────────────┘				
        ┌────────┤ MySQL Adminstrator ├────────┐
        │ Backup File Name                     │
        │                                      │
        │ 2010-12-12.01:00:00_________________ │
        │                                      │
        │       <Ok>           <Cancel>        │
        │                                      │
        └──────────────────────────────────────┘				
        ┌────────┤ MySQL Adminstrator ├────────┐
        │                                      │
        │ Backup?                              │
        │                                      │
        │                                      │
        │        <Yes>           <No>          │
        │                                      │
        └──────────────────────────────────────┘				
5.3.2. 数据恢复
				MySQL Client
             ┌───┤ MySQL Adminstrator ├───┐
             │ Menu                       │
             │                            │
             │       1 MySQL Manager      │
             │       2 MySQL Backup       │
             │       2 MySQL Restore      │
             │                            │
             │                            │
             │    <Ok>        <Cancel>    │
             │                            │
             └────────────────────────────┘				
        ┌────────┤ MySQL Adminstrator ├────────┐
        │ Database Host                        │
        │                                      │
        │        127.0.0.1  localhost          │
        │        172.16.0.1 mysql master       │
        │        172.16.0.2 mysql slave        │
        │                                      │
        │       <Ok>           <Cancel>        │
        │                                      │
        └──────────────────────────────────────┘				
        ┌────────┤ MySQL Adminstrator ├────────┐
        │ Backup History                       │
        │                                      │
        │        1  2010-12-03 03:00:00        │
        │        2  2012-01-01 02:00:00        │
        │        3  2012-02-01 02:00:00        │
        │                                      │
        │       <Ok>           <Cancel>        │
        │                                      │
        └──────────────────────────────────────┘				
        ┌────────┤ MySQL Adminstrator ├────────┐
        │ User                                 │
        │                                      │
        │ root________________________________ │
        │                                      │
        │       <Ok>           <Cancel>        │
        │                                      │
        └──────────────────────────────────────┘

        ┌────────┤ MySQL Adminstrator ├────────┐
        │ Password                             │
        │                                      │
        │ ****________________________________ │
        │                                      │
        │       <Ok>           <Cancel>        │
        │                                      │
        └──────────────────────────────────────┘				
        ┌────────┤ MySQL Adminstrator ├────────┐
        │                                      │
        │ Restore?                             │
        │                                      │
        │                                      │
        │        <Yes>           <No>          │
        │                                      │
        └──────────────────────────────────────┘				

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-04-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Netkiller 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Linux 系统与数据库安全
  • 1. 帐号安全
    • 1.1. Shell 安全
      • 提示
        • 1.2. .history 文件
        • 2. 临时文件安全
        • 3. 其他安全问题
        • 4. 防火墙配置
        • 5. 数据库安全
          • 5.1. 数据库程序安全
            • 5.2. 数据库客户端安全
              • 5.2.1. bind-address
              • 5.2.2. mysql 管理
            • 安全提示
              • 5.2.3. ~/.mysql_history
            • 5.3. mysqldump 安全
              • 5.3.1. 数据备份
              • 5.3.2. 数据恢复
          相关产品与服务
          数据库
          云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档