
linux系統加強
目錄:
1.BIOS
2.SSH安全
3.禁用telnet
4.禁用代碼編譯
5.ProFTP
6.TCPwrappers
7.建立一個SU組
8.root通知
9.history安全
10.歡迎資訊
11.禁用所有特殊賬戶
12.chmod危險檔案
13.指定允許root登陸的TTY裝置
14.選擇一個安全的密碼
15.檢查Rootkit
16.安裝更新檔
17.隐藏Apache資訊
18.隐藏php資訊
19.關閉不使用的服務
20.檢測監聽中的端口
21.關閉打開的端口和服務
22.删除不用的rpm包
23.禁用危險的php函數
24.安裝配置防火牆
25.安裝和配置BFD
26.核心加強(sysctl.conf)
27.更改SSH端口
28./tmp /var/tmp,/dev/shm分區安全
29.PHP IDS
總結
========================================================================
介紹
這個教程将一步步的指引你,使你的Linux系統變得安全。
任何預設安裝的作業系統都是不夠安全的,本文将指引你如何建立一個
相對安全的Linux系統。
你應該總是在系統啟動的時候設定一個BIOS密碼和禁用從CD-ROM和軟碟引導。
這将防止一些人未經允許通路你的系統和更改BIOS設定
SSH是一個協定,利用它可以登入到一個遠端系統或遠端執行系統指令,
預設允許root登入,并且sshv1存在缺陷,我們應該在
sshd_config禁止root通路和使用sshv2來讓ssh更加安全。
方法:
vi /etc/ssh/sshd_config
把協定改為2
PermitRootLogin = no
重新開機
sshd /etc/rc.d/init.d/sshd restart
早期的Linux預設開啟telnet服務,telnet,ftp,rlogin都是明文傳輸的協定
是容易被嗅探到的,這就是為什麼推薦使用安全的版本(sftp,scp,ssh)的原因
如果你必須要使用telnet,那麼至少應該隐藏banner資訊
修改
/etc/xinetd.d/telnet
disable=yes
你可以禁用代碼編譯并且隻把編譯的權限配置設定給一個使用者組
添加編譯使用者組
/usr/sbin/groupadd compiler ,cd /usr/bin
把常見的編譯器所屬組賦給編譯使用者組
chgrp compiler *cc*
chgrp compiler *++*
chgrp compiler ld
chgrp compiler as
設定mysqlaccess的通路
chgrp root mysqlaccess
設定權限
chmod 750 *cc*
chmod 750 *++*
chmod 750 ld
chmod 750 as
chmod 755 mysqlaccess
把使用者添加到組裡
修改/etc/group
compiler:x:520:user1,user2
你可以通過修改proftpd.conf來禁止root登陸
修改/etc/proftpd.conf
Add RootLogin off
proftpd /sbin/service proftpd stop
/sbin/service proftpd start
6.TCP wrappers
編輯hosts.allow和hosts.deny可以限制或允許通路inet服務
限制通路inet服務
修改/etc/hosts.allow
建議格式:
#Approved IP addresses
ALL:192.168.0.1
ALL:192.168.5.2
#CSV uploader machine
proftpd:10.0.0.5
#pop3 from antwhere
ipop3:ALL
修改/etc/hosts.deny
ALL:ALL EXCEPT localhostENY
7.建立SU使用者組
因為我們在SSH禁止了root使用者通路并且禁用了telnet,所有我們應該
配置設定給一些使用者su權限來擷取root特權
vi /etc/group
添加一行 wheel:x:10:root,user1,user2
chgrp wheel /bin/su
chmod o-rwx /bin/su
當一個具有root權限的使用者登入的時候發mail
編輯/root下的.bashrc ,當有root權限的使用者登入時發生email通知
echo ‘ALERT – Root Shell Access (Server Name) on:’ `date` `who` | mail -s “Alert: Root Access from `who | cut -d”(” -f2 | cut -d”)” -f1`” [email protected]
這是一個避免删除.bash_history或重定向到/dev/null的好主意
是以他不能清除或删除他最後執行的指令
chattr +a .bash_history
chattr +i .bash_history
擷取使用者的人會知道他的曆史指令鎖定并且要同意才可以使用服務
10.使用歡迎資訊
你必須提供一些資訊讓攻擊者知道該系統不對公衆開放。
在國外有類似案件,攻擊者入侵一個系統并且系統沒有這些資訊,
這種情況下法院不能做任何裁決,因為系統說welcome
删除/etc/redhat-release
編輯/etc/issue /etc/motd并顯示警告資訊
你應該從系統中删除所有預設使用者群組
例如news,lp,sync,shutdown,uucp,games,halt 等
删除賬戶userdel name
删除組 groupdel name
鎖定特定賬戶:
/usr/sbin/usermod -L -s /bin/false user
這可能是限制不具有root權限的使用者執行下面這些指令的好主意
chmod 700 /bin/ping
chmod 700 /usr/bin/finger
chmod 700 /usr/bin/who
chmod 700 /usr/bin/w
chmod 700 /usr/bin/locate
chmod 700 /usr/bin/whereis
chmod 700 /sbin/ifconfig
chmod 700 /usr/bin/pico
chmod 700 /usr/bin/vi
chmod 700 /usr/bin/which
chmod 700 /usr/bin/gcc
chmod 700 /usr/bin/make
chmod 700 /bin/rpm
/etc/securetty檔案允許你指定root可以從哪個TTY裝置登入
vi /etc/securetty
隻留2個連接配接
tty1
tty2
在/etc/login.defs檔案中定義了shadow密碼的具體配置
預設密碼長度最短為5字元,你應該至少設定為8
vi /etc/login.defs
PASS_MIN_LEN 8
15.檢測Rootkit
用chkrootkit或rkhunter,以chkrootkit為例
wget ftp://ftp.pangeia.com.br/pub/seg/pac/chkrootkit.tar.gz
wget ftp://ftp.pangeia.com.br/pub/seg/pac/chkrootkit.md5
首先檢查md5校驗值: md5sum chkrootkit.tar.gz
然後解壓安裝
tar -zxvf chkrootkit.tar.gz
cd chkrootkit
./configure
make sense
然後運作./chkrootkit
我們可以将其添加到contrab使其每天自動掃描:
vi /etc/cron.daily/chkrootkit.sh
#!/bin/bash
# 輸入chkrootkit的安裝目錄
cd /root/chkrootkit/
# 輸入你想收到檢測報告的email
./chkrootkit | mail -s “Daily chkrootkit from Server Name” [email protected]
你要經常檢查更新以修複某些缺陷或系統穩定性的改進
否則你存在漏洞的系統将會不時的遭受新的攻擊
列出可用更新:up2date -l
安裝未排除的更新:up2date -u
安裝包括排除的更新up2date -uf
你應該隐藏Apache的banner資訊使攻擊者不知道Apache的版本,進而使他們難以利用漏洞
修改/etc/httpd/conf/httpd.conf
改變伺服器簽名:
ServerSignature Off
重新開機Apache /sbin/service httpd restart
你應該隐藏php的banner資訊,原因同上
修改php.ini
改變
expose_php=Off
重新開機Apache
19.關閉不用的服務
你應該把任何未使用的服務關閉,可以在/etc/xinetd.d檔案夾裡找到
cd /etc/xinetd.d
grep disable *
這将顯示所有服務開啟或關閉的狀态,然後根據需要來開啟或關閉服務
20.檢測監聽的端口
檢測是否有必要開放端口是非常重要的
netstat -tulp或
lsof -i -n | egrep ‘COMMAND|LISTEN|UDP’或
nmap!
21.關閉端口和服務
重點是關閉在系統啟動時打開的不需要的端口
對于正在運作的服務,可以執行chkconfig -list | grep on
禁用服務可以執行chkconfig servicename off
然後停止正在運作的服務:/etc/init.d/service stop
首先應該清楚你的系統的作用,它是web,mail,file伺服器或其他
然後覺得哪些包是必要的,之後删除不需要的軟體包
首先列出安裝清單rpm -qa
更詳細的資訊rpm -qi rpmname
還可以檢測删除包可能出現的沖突rpm -e –test rpmname
你應該禁用php的危險函數防止在網站上執行系統指令
whereis php.ini
vi /usr/local/lib/php.ini
編輯
disable_functions = “symlink,shell_exec,exec,proc_close,proc_open,popen,
system,dl,passthru,escapeshellarg, escapeshellcmd”
進階政策防火牆(APF)是一種IP表(網絡過濾),它是基于當今網際網路部署伺服器防火牆系統的基本需要和客戶部署LINUX安裝的唯一需要而設計的。 它是最好的開源防
火牆之一。
配置APF防火牆方法:
下載下傳APF:wget http://www.r-fx.ca/downloads/apf-current.tar.gz
解壓安裝:
tar -zxvf apf-current.tar.gz
cd apf-0.9.7-1
./install.sh
然後我們配置它vi /etc/apf/conf.apf
一般配置:
啟用防火牆使用DShield.org塊清單
USE_DS=”1″
然後我将列出正常的配置和CPanel配置方式,因為CPanel是應該最廣泛的虛拟主機管理軟體
1.正常配置(DNS,Mail,Web,FTP)
Common ingress (inbound)
# Common ingress (inbound) TCP ports -3000_3500 = passive port range for Pure FTPD IG_TCP_CPORTS=”21,22,25,53,80,110,143,443,995″
#
# Common ingress (inbound) UDP ports IG_UDP_CPORTS=”53″
# Egress filtering [0 = Disabled / 1 = Enabled]
EGF=”1″
# Common egress (outbound) TCP ports
EG_TCP_CPORTS=”21,25,80,443,43″
# Common egress (outbound) UDP ports
EG_UDP_CPORTS=”20,21,53″
2.CPanel配置
Common ingress (inbound) ports
# Common ingress (inbound) TCP ports -3000_3500 = passive port range for Pure FTPD IG_TCP_CPORTS=”21,22,25,53,80,110,143,443,2082,2083, 2086,2087,
2095, 2096,3000_3500″
# Common ingress (inbound) UDP ports
IG_UDP_CPORTS=”53″
Common egress (outbound) ports
EG_TCP_CPORTS=”21,25,80,443,43,2089″
之後啟動防火牆 /etc/apf/apf -s
如果運作良好我在回去修改配置檔案,使DEVM=”0″
然後我們配置APF的AntiDos: vi /etc/apf/ad/conf.antidos
找到下面的内容并替換成你的資料
# Organization name to display on outgoing alert emails
CONAME=”Your Company”
# Send out user defined attack alerts [0=off,1=on]
USR_ALERT=”0″
# User for alerts to be mailed to
你應把USR_ALERT改為1
儲存後重新開機APF:/etc/apf/apf –r
To make the firewall start with the Operating System: chkconfig –level 2345 apf on
APF開機自啟動:chkconfig –level 2345 apf on
禁止一個IP用/etc/apf/apf -d ip或vi /etc/apf/deny_hosts.rules
允許一個IP用/etc/apf/apf -a ip或vi /etc/apf/deny_hosts.rules
25.安裝配置BFD(暴力破解檢測)
BFD是一個用于分析應用日志和檢測驗證失敗的子產品化shell腳本
而且安裝配置和用法都是非常容易的。使用BFD的原因很簡單。
其實在LINUX領域幾乎沒有結合防火牆或實時裝置來監控不驗證和
暴力攻擊審計的程式。在用BFD之前你必須安裝APF防火牆。
wget http://www.r-fx.ca/downloads/bfd-current.tar.gz
tar -zxvf bfd-current.tar.gz
cd bfd-0.9
然後我們來配置它 vi /usr/local/bfd/conf.bfd
把以下内容改為你的資料
# Enable/disable user alerts [0 = off; 1 = on]
ALERT_USR=”1″
# User alert email address
EMAIL_USR=”[email protected]”
# User alert email; subject
SUBJ_USR=”Brute Force Warning for $HOSTNAME”
然後vi /usr/local/bfd/ignore.hosts
把你的IP設定成允許主機,避免意外的鎖定自己。
之後重新開機BFD /usr/local/sbin/bfd -s
sysctl.conf用來加強核心,目的是避免DOS和欺騙攻擊
到/proc/sys目錄或sysctl -a指令了解下目前配置的大概情況
然後vi /etc/sysctl.conf
添加如下内容:
# Kernel sysctl configuration file for Red Hat Linux
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
# Controls IP packet forwarding
net.ipv4.ip_forward = 0
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
#Prevent SYN attack
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
# Disables packet forwarding
net.ipv4.ip_forward=0
# Disables IP source routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.lo.accept_source_route = 0
net.ipv4.conf.eth0.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
# Enable IP spoofing protection, turn on source route verification
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.lo.rp_filter = 1
net.ipv4.conf.eth0.rp_filter = 1
# Disable ICMP Redirect Acceptance
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.lo.accept_redirects = 0
net.ipv4.conf.eth0.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
# Enable Log Spoofed Packets, Source Routed Packets, Redirect Packets
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.lo.log_martians = 1
net.ipv4.conf.eth0.log_martians = 1
14
# Disables the magic-sysrq key
# Modify system limits for Ensim WEBppliance
fs.file-max = 65000
# Decrease the time default value for tcp_fin_timeout connection
net.ipv4.tcp_fin_timeout = 15
# Decrease the time default value for tcp_keepalive_time connection
net.ipv4.tcp_keepalive_time = 1800
# Turn off the tcp_window_scaling
net.ipv4.tcp_window_scaling = 0
# Turn off the tcp_sack
net.ipv4.tcp_sack = 0
# Turn off the tcp_timestamps
net.ipv4.tcp_timestamps = 0
# Enable TCP SYN Cookie Protection
# Enable ignoring broadcasts request
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Enable bad error message Protection
net.ipv4.icmp_ignore_bogus_error_responses = 1
# Log Spoofed Packets, Source Routed Packets, Redirect Packets
# Set maximum amount of memory allocated to shm to 256MB
kernel.shmmax = 268435456
# Improve file system performance
vm.bdflush = 100 1200 128 512 15 5000 500 1884 2
# Improve virtual memory performance
vm.buffermem = 90 10 60
# Increases the size of the socket queue (effectively, q0).
net.ipv4.tcp_max_syn_backlog = 1024
# Increase the maximum total TCP buffer-space allocatable
net.ipv4.tcp_mem = 57344 57344 65536
# Increase the maximum TCP write-buffer-space allocatable
net.ipv4.tcp_wmem = 32768 65536 524288
15
# Increase the maximum TCP read-buffer space allocatable
net.ipv4.tcp_rmem = 98304 196608 1572864
# Increase the maximum and default receive socket buffer size
net.core.rmem_max = 524280
net.core.rmem_default = 524280
# Increase the maximum and default send socket buffer size
net.core.wmem_max = 524280
net.core.wmem_default = 524280
# Increase the tcp-time-wait buckets pool size
net.ipv4.tcp_max_tw_buckets = 1440000
# Allowed local port range
net.ipv4.ip_local_port_range = 16384 65536
# Increase the maximum memory used to reassemble IP fragments
net.ipv4.ipfrag_high_thresh = 512000
net.ipv4.ipfrag_low_thresh = 446464
# Increase the maximum amount of option memory buffers
net.core.optmem_max = 57344
# Increase the maximum number of skb-heads to be cached
net.core.hot_list_length = 1024
## DO NOT REMOVE THE FOLLOWING LINE!
## nsobuild:20051206
重新開機後生效
/sbin/sysctl -p
sysctl -w net.ipv4.route.flush=1
更改SSH預設端口号在一定程度上可以提高安全性
Port 22改為其他端口
當然不要忘記把更改的端口加進防火牆
然後重新開機生效/etc/init.d/ssh restart
如果安裝了APF并把端口添加之後,還要重新開機APF:/etc/init.d/apf restart
28./tmp,/var/tmp,/dev/shm分區的安全
/tmp,/var/tmp,/dev/shm目錄是不安全的,任何使用者都可以執行腳本。
最好的解決辦法是挂載ncexec和nosuid選項的參數
注意:不建議在CPanel使用
/tmp目錄:
cd /dev
建立 100M (“count”) 的存儲檔案:
dd if=/dev/zero of=tmpMnt bs=1024 count=100000
設為一個擴充的檔案系統:
/sbin/mke2fs /dev/tmpMnt (“…is not a block special device. continue?”回答yes)
備份現有臨時檔案:
cp -R /tmp/ /tmp_backup
用noexec挂載新檔案系統:
mount -o loop,rw,nosuid,noexec /dev/tmpMnt /tmp
chmod 0777 /tmp
把備份的檔案拷貝回去:
cp -R /tmp_backup/* /tmp/
删除備份:
rm -rf /tmp_backup
修改/etc/fstab 添加下面的條目使其在引導時仍然有效
/dev/tmpMnt /tmp ext2 loop,rw,nosuid,noexec 0 0
/var/tmp目錄:
mv /var/tmp /var/tmpbak
ln -s /tmp /var/tmp
cp /var/tmpbak/* /tmp/
/dev/shm目錄:
編輯/etc/fstab
把 none /dev/shm tmpfs defaults,rw 0 0
改為
none /dev/shm tmpfs defaults,nosuid,noexec,rw 0 0
賬号安全管理:
1.限制使用su的使用者并合理利用sudo:
vi /etc/pam.d/su,添加auth required /lib/security/$ISA/pam_wheel.so group=wheel行,
用指令“usermod -G 10 <使用者名>”來添加允許使用su的使用者
合理利用sudo大家可以檢視資料,也可以有人整理專題讨論。
2.禁止root使用ssh遠端登入:
vi /etc/ssh/sshd_config,找到#PermitRootLogin yes 改成–> PermitRootLogin no,重新開機ssh服務
3.給重要檔案加鎖,拒絕修改:
# chattr +i /etc/passwd
# chattr +i /etc/shadow
4.删除大部分不必要帳号,取消帳号中不必要的shell。
如下帳号可被删除:
adm,lp,sync,shutdown,halt,mail,news,uucp,operator,games,gopher,ftp,rpm,nscd,rpc,
rpcuser,nfsnobody,mailnull,smmsp,pcap,xfs,ntp
如果要使用KDE之類的圖形視窗,則有些帳号如rpc,xfs是需要的。
檔案系統權限
1) 找出系統中所有含s"位的程式,把不必要的"s"位去掉,或者把根本不用的直接删除,這樣可以防止使用者濫用及提升權限的可能性,其指令如下:
find / -type f -perm -4000 -o -perm -2000 -print | xargs ls -lg
2) 把重要檔案加上不可改變屬性(一般情況不用這麼做):
chattr +i /etc/passwd
Immutable,系統不允許對這個檔案進行任何的修改。如果目錄具有這個屬性,那麼任何的程序隻能修改目錄之下的檔案,不允許建立和删除檔案。
3) 找出系統中沒有屬主的檔案:
find / -nouser -o -nogroup
4) 找出任何都有寫權限的檔案和目錄:
find / -type f -perm -2 -o -perm -20 |xargs ls -lg
find / -type d -perm -2 -o -perm -20 |xargs ls -ldg
5) Suid及sgid檔案檢查:
執行如下指令:
find / -user root -perm -4000 -print -exec md5sum {} \;
find / -user root -perm -2000 -print -exec md5sum {} \;
将結果重定向到一個檔案,儲存起來以後備查。
Banner僞裝
1)系統banner
2)各服WEB服務軟體banner僞裝及隐藏。
補充的比較亂,可能不對!我再總結一下:
1.linux系統服務去掉和關閉不必要的服務。
2.删除一些沒有用的賬号
3.嚴格限制gcc及限制非root使用者執行指令的權限及nc這些工具的權限。
3.将系統可執行指令的md5值儲存下來友善以後查找問題。
4.機房安裝系統後arp -an > arp.mac 将arp清單mac位址儲存下來,友善遇到問題的時候查詢。
5.關于PHP,WEB方面的安全,我想kindle版主将帶我們一起進入這方面的攻防讨論,他在這方面經驗比較多。
#!/bin/sh
for a in $(chkconfig --list |grep 0:|awk '{print $1}') ; do
case "$a" in
"sshd" ) STATUS=on;;
"crond" ) STATUS=on;;
"network" ) STATUS=on;;
"syslog" ) STATUS=on;;
"iptables" ) STATUS=on;;
"irqbalance" ) STATUS=on;;
* ) STATUS=off;;
esac
/sbin/chkconfig --level 345 $a $STATUS
done
哈,我也來兩句。
第一,把伺服器設定iptables,指定IP(×××) SSH
第二,用跳轉機,建×××專用帳号,可以記錄誰登陸,還可以防止誤操作,所能的操作記錄都有備份。
這樣安全系統大大增加!
隻要防止應用程式安全就行了,比如web 伺服器,防httpd PHP。等!!!
使用 md5sum監控檔案内容是否被修改
[root@neco ~]# md5sum /etc/passwd
f0ae736aa3d4c58642f9c93170ec8af6 /etc/passwd
使用md5sum對目錄下的檔案求哈希值
find /etc/ -type f -exec md5sum {} \; > /tmp/etc/md5sum.log
修改目錄下的檔案(這裡隻是拿/etc/passwd做例子)
vim /etc/passwd
再求哈希值
find /etc/ -type f -exec md5sum {} \; > /tmp/etc/md5sum.log.1
比較
diff /tmp/etc.md5sum.log{,.1}