Linux 系统与数据库安全

http://netkiller.github.io/journal/security.db.html

Mr. Neo Chen (陈景峯), netkiller, BG7NYT


中国广东省深圳市龙华新区民治街道溪山美地
518131
+86 13113668890


版权声明

转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。

文档出处:
http://netkiller.github.io
http://netkiller.sourceforge.net

微信扫描二维码进入 Netkiller 微信订阅号

QQ群:128659835 请注明“读者”

摘要

Linux 系统安全问题


目录

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>          │
        │                                      │
        └──────────────────────────────────────┘
				
				

5.4. crontab 定时备份脚本于安全

网上备份脚本很多,但考虑都不周全。

这里增加了 umask 0077 保证创建备份文件只能是创建者跟root可以访问,其他用户没有权限,保证了备份档案的安全。

find $BACKUP_DIR -type f -mtime +$COPIES -delete 是负责备份的份数管理, 过期数据定时删除

创建专用的备份帐号

grant select, lock tables on *.* to 'backup'@'192.168.1.200' identified by "123456";
			

crontab 备份脚本

			
# cat /srv/bin/backup

#!/bin/bash
###################################
# $Id: security.db.xml 650 2013-07-24 10:04:58Z netkiller $
# Author: netkiller@msn.com
# Home: http://netkiller.github.com
###################################
BACKUP_HOST="localhost"
BACKUP_USER="backup"
BACKUP_PASS=""
BACKUP_DIR=/opt/backup
BACKUP_DBNAME="test neo"
#Number of copies
COPIES=7
####################################
MYSQLDUMP="mysqldump"
#TIMEPOINT=$(date -u +%Y-%m-%d)
TIMEPOINT=$(date -u +%Y-%m-%d.%H:%M:%S)
MYSQLDUMP_OPTS="-h $BACKUP_HOST -u$BACKUP_USER -p$BACKUP_PASS"
####################################
umask 0077
test ! -d "$BACKUP_DIR" && mkdir -p "$BACKUP_DIR"
test ! -w $BACKUP_DIR && echo "Error: $BACKUP_DIR is un-writeable." && exit 0

for dbname in $BACKUP_DBNAME
do
    test ! -d "$BACKUP_DIR/$dbname" && mkdir -p "$BACKUP_DIR/$dbname"

    $MYSQLDUMP $MYSQLDUMP_OPTS $dbname | gzip > $BACKUP_DIR/$dbname/$dbname.$TIMEPOINT.sql.gz
done
find $BACKUP_DIR -type f -mtime +$COPIES -delete
			
			

/srv/bin/backup 安全也至关重要,否则会泄漏备份用户的密码

# chown mysql:mysql /srv/bin/backup
# chmod 500 /srv/bin/backup
			

mysqldump 的安全

# chown 700 /usr/bin/mysqldump
			

5.5. 数据库归档文件

一般数据库服务器上可以保留一周的备份数据,历史数据需要保存到服务器以外的带库或者阵列柜中,怎么样保证这些数据的安全呢? 我们采用下面方式

  1. 制作PGP/GPG密钥,密钥放置在数据库服务器上,证书做好备份,否则一旦丢失,将无法在将备份文件恢复

  2. 数据库备份后,首先进行压缩处理

  3. 然后使用公钥证书进行GPG/PGP数据加密

  4. 这时可以放心的将备份数据库搬出数据库服务器到带库或磁盘阵列柜中

恢复数据,将数据库备份文件复制到该数据库服务器,然后用私钥解密备份文件,再恢复到数据库到中

5.6. 开发与测试环境的数据库安全问题

有时候需要将生产环境的数据复制到开发环境上,例如,测试的时候,重现bug需要真实数据,开发环境的模拟数据无法满足要求,这时需要将生产环境的数据拉到测试或开发环境。如果保证数据的安全非常重要。

最有效的手段是数据混淆,将重要的数据进行混淆扰乱顺序等等

扰乱手段有

  1. 颠倒顺序
  2. 曾加干扰词
  3. 重置或替换数据,例如密码可以全部改为test (update user set passwd='test')
  4. 拼装数据 如 (131,137,135,133,139,138,168)后面加8位随机数

5.7. 与数据库有关的服务器安全问题

其他服务器不能安装mysql客户端与mysqldump备份工具

例如:web服务器只能通过php/jdbc/odbc等链接mysql数据库, web服务器卸载 mysql,mysqldump工具,防止用户登录查询以及将数据库远程备份,然后通过web下载数据库

			
# adduser www
# passwd www
# chmod 500 -R /usr/local/webserver/mysql/bin/*
# chown root:root -R /usr/local/webserver/mysql/bin/*