Linux系統(tǒng)安全基礎(chǔ)應(yīng)用
一、基本安全
1、賬號安全
將非登錄用戶的Shell設(shè)為/sbin/nologin,
鎖定長期不使用的賬號,
刪除無用的賬號
鎖定賬號文件passwd、shadow
鎖定文件并查看狀態(tài)
[root@localhost ~]# chattr +i /etc/passwd /etc/shadow
[root@localhost ~]# lsattr /etc/passwd /etc/shadow
—-i——– /etc/passwd
—-i——– /etc/shadow
解鎖文件并查看狀態(tài)
[root@localhost ~]# chattr -i /etc/passwd /etc/shadow
[root@localhost ~]# lsattr /etc/passwd /etc/shadow
————- /etc/passwd
————- /etc/shadow
2、密碼安全
設(shè)置密碼有效期
要求用戶下次登錄時修改密碼
適用于新建用戶:
[root@localhost ~]# vi /etc/login.defs
……
PASS_MAX_DAYS 30
適用于已有用戶:
[root@localhost ~]# chage -M 30 lisi
強(qiáng)制在下次登錄時更改密碼
[root@localhost ~]# chage -d 0 zhangsan
3、命令歷史、自動注銷
命令歷史限制
減少記錄的命令條數(shù)
注銷時自動清空命令歷史
[root@localhost ~]# vi /etc/profile
……
HISTSIZE=200
[root@localhost ~]# vi ~/.bash_logout
……
history -c
clear
終端自動注銷:
閑置600秒后自動注銷
[root@localhost ~]# vi ~/.bash_profile
……
export TMOUT=600
4、su命令的控制
用途及用法
用途:Substitute User,切換用戶
格式:su - 目標(biāo)用戶
密碼驗證
root 到à 任意用戶, 不驗證密碼
普通用戶 à
其他用戶,驗證目標(biāo)用戶的密碼
限制使用su命令的用戶
啟用pam_wheel認(rèn)證模塊
將允許使用su命令的用戶加入wheel組
[root@localhost ~]# vi /etc/pam.d/su
#%PAM-1.0
auth sufficient pam_rootok.so
auth required pam_wheel.so use_uid
……
[root@localhost ~]# gpasswd -a tsengyia wheel
tsengyia
正在將用戶“tsengyia”加入到“wheel”組中
查看su操作記錄
安全日志文件:/var/log/secure
[root@localhost ~]# tail /var/log/secure
……
May 13 18:05:51 mail su: pam_unix(su-l:session): session opened for user root by tsengyia(uid=1006)
May 13 18:07:34 mail su: pam_unix(su-l:session): session opened for user jerry by tsengyia(uid=1006)
5、使用sudo機(jī)制提升權(quán)限
用途及用法
用途:以其他用戶身份(如root)執(zhí)行授權(quán)的命令
用法:sudo 授權(quán)命令
密碼驗證
初次執(zhí)行sudo命令時,驗證當(dāng)前用戶的密碼
不需驗證目標(biāo)用戶的密碼
[root@localhost ~]# sudo -u jerry /bin/touch /tmp/test.file
[root@localhost ~]# ls -l /tmp/test.file
-rw-r–r– 1 jerry jerry 0 07-12 05:39 /tmp/test.file
使用-u 選項指定目標(biāo)用戶,缺省則為root
配置sudo授權(quán)
visudo 或者 vi /etc/sudoers
記錄格式:用戶 主機(jī)名列表=命令程序列表
[root@localhost ~]# visudo
……
%wheel ALL=NOPASSWD: ALL
jerry localhost=/sbin/ifconfig
syrianer localhost=/sbin/*,!/sbin/ifconfig,!/sbin/route
Cmnd_Alias PKGTOOLS=/bin/rpm,/usr/bin/yum
mike localhost=PKGTOOLS
查看sudo操作記錄
需啟用 Defaults logfile 配置
默認(rèn)日志文件:/var/log/sudo
[root@localhost ~]# visudo
……
Defaults logfile = ”/var/log/sudo”
查詢授權(quán)的sudo操作
sudo -l
二、開關(guān)機(jī)安全控制
1、BIOS安全
將第一引導(dǎo)設(shè)備設(shè)為當(dāng)前系統(tǒng)所在硬盤
禁止從其他設(shè)備(光盤、U盤、網(wǎng)絡(luò))引導(dǎo)系統(tǒng)
將安全級別設(shè)為setup,并設(shè)置管理員密碼
2、禁止Ctrl+Alt+delete重新啟動機(jī)器命令
修改/etc/inittab文件,將“ca::ctrlaltdel:/sbin/shutdown -t3 -r now”一行注釋掉。然后重新設(shè)置/etc/rc.d/init.d/目錄下所有文件的許可權(quán)限,運行如下命令:
# chmod -R 700 /etc/rc.d/init.d/*
這樣便僅有root可以讀、寫或執(zhí)行上述所有腳本文件。
3、GRUB菜單安全
未經(jīng)授權(quán)禁止修改啟動參數(shù),未經(jīng)授權(quán)禁止進(jìn)入指定系統(tǒng)。
密碼設(shè)置方式(grub.conf):password 明文密碼串
password –md5 加密密碼串
密碼記錄的位置:全局部分(第一個“title”之前)
系統(tǒng)引導(dǎo)部分(每個“title”部分之后)
使用grub-md5-crypt獲得加密字串:
[root@localhost ~]# grub-md5-crypt
Password:
Retype password:
$1$Kndw50$wRW2w1v/jbZ8n5q2fON4y/
修改grub.conf文件,添加密碼記錄:
[root@localhost ~]# vi /boot/grub/grub.conf
……
password –md5 $1$Kndw50$wRW2w1v/jbZ8n5q2fON4y/
title Red Hat Enterprise Linux Server (2.6.18-194.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-194.el5 ro root=/dev/VolGroup00/LogVol00
initrd /initrd-2.6.18-194.el5.img
三、終端登陸安全
1、登陸信息
系統(tǒng)登陸信息保存在/etc/issue、/etc/issue.net 文件夾中,包括操作系統(tǒng)類型、內(nèi)核版本等。
一種思路注釋掉相關(guān)文件中的信息,如下:
#Thiswilloverwrite/etc/issueateveryboot.So,makeanychangesyou
#wanttomaketo/etc/issuehereoryouwilllosethemwhenyoureboot.
#echo\”\”>/etc/issue
#echo\”$R\”>>/etc/issue
#echo\”Kernel$(uname-r)on$a$(uname-m)\”>>/etc/issue
#cp-f/etc/issue/etc/issue.net
#echo>>/etc/issue
然后,進(jìn)行如下操作:
#rm-f/etc/issue
#rm-f/etc/issue.net
#touch/etc/issue
#touch/etc/issue.net
另一只思路修改登陸信息來欺騙攻擊者:
本機(jī)登錄提示:/etc/issue
網(wǎng)絡(luò)登錄提示:/etc/issue.net
[root@localhost ~]# vi /etc/issue
Microsoft Windows Server 2008 R2
2、登錄終端設(shè)置
減少tty終端開放的個數(shù):
通過修改初始化配置文件/etc/inittab,可以減少開放的tty終端個數(shù)。
只允許4.、5、6可以登陸
[root@localhost ~]# vi /etc/inittab
……
# Run gettys in standard runlevels
#1:2345:respawn:/sbin/mingetty tty1
#2:2345:respawn:/sbin/mingetty tty2
#3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
[root@localhost ~]# init q //重新加載配置文件
Root用戶登陸:
還有一種思路/etc/securetty文件指定了允許root登錄的tty設(shè)備,由/bin/login程序讀取, 其格式是一個被允許的名字列表,您可以編輯/etc/securetty且注釋掉如下的行
#tty1
#tty2
#tty3
#tty4
#tty5
#tty6
這時,root僅可在tty1終端登錄。
普通用戶登陸:
建立/etc/nologin文件禁止普通用戶登錄
刪除nologin文件或重啟后即恢復(fù)正常
[root@localhost ~]# touch /etc/nologin
[root@localhost ~]# rm -rf /etc/nologin
四、防止攻擊
1、阻止ping如果沒人能ping通您的系統(tǒng),安全性自然增加了。
為此,可以在/etc/rc.d/rc.local
文件中增加如下一行:
echo1>/proc/sys/net/ipv4/icmp_echo_ignore_all
2、防止IP欺騙
編輯host.conf文件并增加如下幾行來防止IP欺騙攻擊。
orderbind,hosts
multioff
nospoofon
3.防止DoS攻擊
對系統(tǒng)所有的用戶設(shè)置資源限制可以防止DoS類型攻擊。如最大進(jìn)程數(shù)和內(nèi)存使用數(shù)量等。例如,可以在/etc/security/limits.conf中添加如下幾行:
*hardcore0
*hardrss5000
*hardnproc20
然后必須編輯/etc/pam.d/login文件檢查下面一行是否存在。
sessionrequired/lib/security/pam_limits.so
上面的命令禁止調(diào)試文件,限制進(jìn)程數(shù)為50并且限制內(nèi)存使用為5MB。
經(jīng)過以上的設(shè)置,你的Linux服務(wù)器已經(jīng)可以對絕大多數(shù)已知的安全問題和網(wǎng)絡(luò)攻擊具有免疫能力,但一名優(yōu)秀的系統(tǒng)管理員仍然要時刻注意網(wǎng)絡(luò)安全動態(tài),隨時對已經(jīng)暴露出的和潛在安全漏洞進(jìn)行修補。