歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux系統與數據庫安全

Linux系統與數據庫安全

日期:2017/3/1 14:51:09   编辑:關於Linux
Linux系統與數據庫安全 1. 帳號安全 帳號權限安全 1.1. Shell 安全 需求:限制用戶權限,僅提供一些linux常用命令,用戶監控linux系統於網絡運行情況,不允許用戶ssh登錄後隨意運行linux命令 用戶不能進入到Shell環境 例如普通用戶一旦登錄web服務器可以看到web程序中的數據庫配置 用戶可以了解OS工作狀態如內存,cpu,網絡等等 例如:ping, tracepath, top, free, netstat 可以查看系統部分日志 例如: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 [email protected] ┌───────────────────┤ Client ├───────────────────┐ │ Administrator Tools │ │ │ │ ping ping │ │ tracepath tracepath │ │ top top │ │ free free │ │ ps ps │ │ netstat netstat │ │ lsof lsof │ │ iftop iftop │ │ log log │ │ │ │ │ │ <Ok> <Cancel> │ │ │ └────────────────────────────────────────────────┘ 提示 這裡采用的方式是給用戶提供一個界面的方式,另外還有更好的方案,你可以些一個Shell的外殼,你需要實現 與Shell相同的提示符 提供TAB補齊 上下光標鍵翻看歷史命令,左右光標改變位置,Home/End 鍵到行首與行尾 Ctrl+R 搜索, Ctrl+D 退出 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等服務器 DBA 沒有系統SSH帳號,只有數據庫帳號 系統管理員只能有SSH系統帳號,沒有數據庫帳號 DBA 可備份數據庫,還原數據庫指定的備份文件,但是接觸不到備份文件 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> 安全提示 從安全角度看,你可以去掉輸入密碼的過程。在終端提示符下輸入 Enter password: 還可以寫入~/.my.conf文件 這樣ssh [email protected]的時候輸入第一道密碼,然後進入mysql不需要輸入密碼 如果需要輸入密碼對話到建議刪除.bash_history rm -rf .bash_history ln -s /dev/null .bash_history 5.2.3. ~/.mysql_history 通過~/.mysql_history文件記錄DBA操作記錄 插入時間點,在~/.bashrc中加入下面命令 cat >> ~/.bashrc <<EODdd echo `date` >> ~/.mysql_dhistory 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.xml 500 2012-12-04 09:01:55Z netkiller $ # Author: [email protected] # 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. 數據庫歸檔文件 一般數據庫服務器上可以保留一周的備份數據,歷史數據需要保存到服務器以外的帶庫或者陣列櫃中,怎麼樣保證這些數據的安全呢? 我們采用下面方式 制作PGP/GPG密鑰,密鑰放置在數據庫服務器上,證書做好備份,否則一旦丟失,將無法在將備份文件恢復 數據庫備份後,首先進行壓縮處理 然後使用公鑰證書進行GPG/PGP數據加密 這時可以放心的將備份數據庫搬出數據庫服務器到帶庫或磁盤陣列櫃中 恢復數據,將數據庫備份文件復制到該數據庫服務器,然後用私鑰解密備份文件,再恢復到數據庫到中 5.6. 開發與測試環境的數據庫安全問題 有時候需要將生產環境的數據復制到開發環境上,例如,測試的時候,重現bug需要真實數據,開發環境的模擬數據無法滿足要求,這時需要將生產環境的數據拉到測試或開發環境。如果保證數據的安全非常重要。 最有效的手段是數據混淆,將重要的數據進行混淆擾亂順序等等 擾亂手段有 顛倒順序 曾加干擾詞 重置或替換數據,例如密碼可以全部改為test (update user set passwd='test') 拼裝數據 如 (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/*
Copyright © Linux教程網 All Rights Reserved