Linux系統加固paper

1.BIOS
你應該總是在系統啓動的時候設置一個BIOS密碼和禁用從CD-ROM和軟盤引導。
這將防止一些人未經允許訪問你的系統和更改BIOS設置

2.SSH安全

SSH是一個協議,利用它可以登錄到一個遠程系統或遠程執行系統命令,
默認允許root登錄,並且sshv1存在缺陷,我們應該在
sshd_config禁止root訪問和使用sshv2來讓ssh更加安全。

方法:
vi /etc/ssh/sshd_config

把協議改爲2
PermitRootLogin = no

重啓
sshd /etc/rc.d/init.d/sshd restart


3.禁用telnet

早期的Linux默認開啓telnet服務,telnet,ftp,rlogin都是明文傳輸的協議
是容易被嗅探到的,這就是爲什麼推薦使用安全的版本(sftp,scp,ssh)的原因
如果你必須要使用telnet,那麼至少應該隱藏banner信息

方法:
修改
/etc/xinetd.d/telnet
disable=yes


4.禁用代碼編譯

你可以禁用代碼編譯並且只把編譯的權限分配給一個用戶組
方法:
添加編譯用戶組
/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


5.ProFTP

你可以通過修改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


8.root通知

當一個具有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]

9.history安全

這是一個避免刪除.bash_history或重定向到/dev/null的好主意
因此他不能清除或刪除他最後執行的命令
方法:
chattr +a .bash_history
chattr +i .bash_history

獲取用戶的人會知道他的歷史命令鎖定並且要同意纔可以使用服務

10.使用歡迎信息

你必須提供一些信息讓攻擊者知道該系統不對公衆開放。
在國外有類似案件,攻擊者入侵一個系統並且系統沒有這些信息,
這種情況下法院不能做任何裁決,因爲系統說welcome

方法:
刪除/etc/redhat-release
編輯/etc/issue /etc/motd並顯示警告信息

11.禁用所有特殊賬戶

你應該從系統中刪除所有默認用戶和組
例如news,lp,sync,shutdown,uucp,games,halt 等
方法:
刪除賬戶userdel name
刪除組 groupdel name
鎖定特定賬戶:
/usr/sbin/usermod -L -s /bin/false user


12.chmod危險文件

這可能是限制不具有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


13.指定允許root登陸的TTY設備

/etc/securetty文件允許你指定root可以從哪個TTY設備登錄
方法:
vi /etc/securetty

只留2個連接
tty1
tty2


14.選擇一個安全的密碼

在/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]


16.安裝補丁

你要經常檢查更新以修復某些缺陷或系統穩定性的改進
否則你存在漏洞的系統將會不時的遭受新的攻擊
方法:
列出可用更新:up2date -l
安裝未排除的更新:up2date -u
安裝包括排除的更新up2date -uf


17.隱藏Apache信息

你應該隱藏Apache的banner信息使攻擊者不知道Apache的版本,從而使他們難以利用漏洞
方法:
修改/etc/httpd/conf/httpd.conf
改變服務器簽名:
ServerSignature Off

重啓Apache /sbin/service httpd restart

18.隱藏php信息

你應該隱藏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

22.刪除不用的rpm包

首先應該清楚你的系統的作用,它是web,mail,file服務器或其他
然後覺得哪些包是必要的,之後刪除不需要的軟件包
方法:
首先列出安裝列表rpm -qa
更詳細的信息rpm -qi rpmname
還可以檢測刪除包可能出現的衝突rpm -e ?test rpmname

23.禁用危險的php函數

你應該禁用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”


24.安裝配置防火牆

高級策略防火牆(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
# Egress filtering [0 = Disabled / 1 = Enabled]
EGF=”1″
# Common egress (outbound) TCP ports
EG_TCP_CPORTS=”21,25,80,443,43,2089″
#
# Common egress (outbound) UDP ports
EG_UDP_CPORTS=”20,21,53″

之後啓動防火牆 /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
[email protected]

你應把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

26.內核加固(sysctl.conf)

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
net.ipv4.conf.default.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
# 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
14
net.ipv4.conf.default.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
# Disables the magic-sysrq key
kernel.sysrq = 0
# 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
net.ipv4.tcp_syncookies = 1
# 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
net.ipv4.conf.all.log_martians = 1
# 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


27.更改SSH端口

更改SSH默認端口號在一定程度上可以提高安全性
方法:
vi /etc/ssh/sshd_config
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僞裝及隱藏。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章