Linux 系統(tǒng)的安全加固
共 9089字,需瀏覽 19分鐘
·
2024-09-06 07:33
鏈接:https://www.cnblogs.com/LyShark/p/11407373.html
Linux是一套免費(fèi)使用和自由傳播的類Unix操作系統(tǒng),作為一個開放源代碼的操作系統(tǒng),Linux服務(wù)器以其安全、高效和穩(wěn)定的顯著優(yōu)勢而得以廣泛應(yīng)用,但如果不做好權(quán)限的合理分配,Linux系統(tǒng)的安全性還是會得不到更好的保障,下面我們將主要使用RHEL7系統(tǒng),分別從賬戶安全、登錄控制,SeLinux配置等,優(yōu)化Linux系統(tǒng)的安全性。
早在1985年,美國國防部就已經(jīng)提出了可信計算機(jī)系統(tǒng)評測標(biāo)準(zhǔn)TCSEC,TCSEC將系統(tǒng)分成ABCD四類7個安全級別。D級是安全級別最低的級別,C類為自主保護(hù)級別;B類為強(qiáng)制保護(hù)級別;A類為驗證保護(hù)類。
D級,最低安全性
C1級,主存取控制
C2級,較完善的自主存取控制(DAC)、審計
B1級,強(qiáng)制存取控制(MAC)
B2級,良好的結(jié)構(gòu)化設(shè)計、形式化安全模型
B3級,全面的訪問控制、可信恢復(fù)
A1級,形式化認(rèn)證
當(dāng)前主流的操作系統(tǒng)安全性遠(yuǎn)遠(yuǎn)不夠,如Windows NT都只能達(dá)到C2級,安全性均有待提高,不過經(jīng)過安全加固后的Linux系統(tǒng)可達(dá)到B1的安全級別。
控制系統(tǒng)賬戶: 系統(tǒng)賬戶默認(rèn)存放在cat /etc/passwd中,你可以手動查詢用戶信息,我們直接除了Root賬戶需要登錄以外,其他的賬戶全部設(shè)置為禁止登錄。
使用 passwd -l 用戶名 鎖定用戶登錄,如下我們寫BASH腳本批量的完成這個過程。
for temp in `cut -d ":" -f 1 /etc/passwd | grep -v "root"`dopasswd -l $tempdone
修改口令生存期: 口令生存期,即用戶密碼的過期時間,默認(rèn)在cat /etc/login.defs | grep "PASS" 中存儲著,我們需要把這個時間改小,如下配置即可。
[root@localhost ~]# vim /etc/login.defs# Password aging controls:## PASS_MAX_DAYS Maximum number of days a password may be used.# PASS_MIN_DAYS Minimum number of days allowed between password changes.# PASS_MIN_LEN Minimum acceptable password length.# PASS_WARN_AGE Number of days warning given before a password expires.#PASS_MAX_DAYS 90 # 新建用戶密碼最長使用天數(shù)PASS_MIN_DAYS 0 # 新建用戶密碼最短使用天數(shù)PASS_MIN_LEN 7 # 新建用戶密碼到期提示天數(shù)PASS_WARN_AGE 10 # 最小密碼長度
設(shè)置口令復(fù)雜度: 設(shè)置新建用戶時輸入的口令復(fù)雜程度,該配置默認(rèn)在cat /etc/pam.d/system-auth 文件中存放。
[root@localhost ~]# vim /etc/pam.d/system-auth#%PAM-1.0# This file is auto-generated.# User changes will be destroyed the next time authconfig is run.password required pam_cracklib.so try_first_pass retry=3 dcredit=-1 lcredit=-1 ucredit=-1 ocredit=-1 minlen=10
在上方文件中添加如下一行配置,其含義是至少包含一個數(shù)字、一個小寫字母、一個大寫字母、一個特殊字符、且密碼長度>=10
限制登錄超時: 限制用戶登陸成功后的等待時間,當(dāng)用戶終端無操作時則默認(rèn)斷開連接。
[root@localhost ~]# vim /etc/profileTMOUT=300export TMOUT
限制TTY嘗試次數(shù): 該配置可以有效的防止,爆破登錄情況的發(fā)生,其配置文件在cat /etc/pam.d/login中添加如下配置,這個方法只是限制用戶從TTY終端登錄,而沒有限制遠(yuǎn)程登錄。
[]auth required pam_tally2.so deny=3 lock_time=300 even_deny_root root_unlock_time=10[]
修改SSH遠(yuǎn)程端口: 修改SSH登錄端口,這里可以修改為65534等高位端口,因為Nmap掃描器默認(rèn)也就探測0-1024端口,這樣能夠有效的規(guī)避掃描。
[root@localhost ~]# vim /etc/ssh/sshd_config# If you want to change the port on a SELinux system, you have to tell# SELinux about this change.# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER#Port 65534 # 登錄端口改為65534MaxAuthTries=3 # 密碼最大嘗試次數(shù)3[root@localhost ~]# systemctl restart sshd[C:\Users]$ ssh root@192.168.1.30 6553
禁止Root用戶登錄: 首先創(chuàng)建一個普通用戶 lyshark ,然后配置好Sudo授權(quán),需要時使用Sudo授權(quán)執(zhí)行命令,禁止Root用戶登錄主機(jī)。
# --------------------------------------------------------------------------------------------# 創(chuàng)建普通用戶 lyshark[root@localhost ~]# useradd lyshark[root@localhost ~]# passwd lyshark# --------------------------------------------------------------------------------------------# 給普通用戶添加Sudo授權(quán)[root@localhost ~]# vim /etc/sudoers## The COMMANDS section may have other options added to it.#### Allow root to run any commands anywhereroot ALL=(ALL) ALLlyshark ALL=(ALL) ALL# --------------------------------------------------------------------------------------------# 修改ROOT用戶禁止登錄系統(tǒng)[root@localhost ~]# vim /etc/ssh/sshd_configPermitRootLogin no[root@localhost ~]# systemctl restart sshd
除此之外,你可以通過指定那些被允許用來使用SSH的用戶名,從而使得SSH服務(wù)更為安全。
[root@localhost ~]# vim /etc/ssh/sshd_configAllowUsers lyshark admin # 指定允許登錄的用戶AllowGroup lyshark admin # 指定允許登錄的用戶組
登錄警告提示: 通過修改 /etc/motd和/etc/issue.net來實現(xiàn)彈出警告提示框,當(dāng)用戶遠(yuǎn)程登陸以后就會提示以下的兩行文字。
[root@localhost ~]# vim /etc/motd[root@localhost ~]# vim /etc/issue.net-----------------------------------------------------------------------------------------------Warning! If unauthorized, illegal login system, please exit immediately!!Your system fingerprint has been recorded!!-----------------------------------------------------------------------------------------------
限制Umask值: umask 值用于設(shè)置文件的默認(rèn)屬性,系統(tǒng)默認(rèn)的Umask 值是0022,也就是U權(quán)限不動,G權(quán)限減去2,O權(quán)限減2,這里為了防止上傳一句話木馬,我們將系統(tǒng)的Umask值改為0777,也就是說,當(dāng)用戶新建任何文件的時候,其都不會具有(讀寫執(zhí)行)權(quán)限,就算上傳成功也不具有任何權(quán)限。
[root@localhost ~]# echo "umask 0777" >> /etc/bashrc
[root@localhost ~]# touch test1
[root@localhost ~]# mkdir test2
[root@localhost ~]#
[root@localhost ~]# ls -lh
total 0
----------. 1 root root 0 Aug 25 05:46 test1
d---------. 2 root root 6 Aug 25 05:46 test2
鎖定系統(tǒng)文件: 鎖定文件是Linux系統(tǒng)中最為強(qiáng)大的安全特性,任何用戶(即使是root),都無法對不可修改文件進(jìn)行寫入、刪除、等操作,我們將一些二進(jìn)制文件設(shè)置為只讀模式,能夠更好的防止系統(tǒng)被非法篡改或注入惡意代碼,一般情況下/sbin 和/usr/lib兩個目錄內(nèi)容能被設(shè)置為不可改變。
[][][][][][][]
限制GCC編譯器: 如果系統(tǒng)已經(jīng)被黑客入侵,那么黑客的下一個目標(biāo)應(yīng)該是編譯一些POC文件,用來提權(quán),從而在幾秒鐘之內(nèi)就成為了root用戶,那么我們需要對系統(tǒng)中的編譯器進(jìn)行一定的限制。
首先,你需要檢查單數(shù)據(jù)包以確定其包含有哪些二進(jìn)制文件。然后將這些文件全部設(shè)置為000無權(quán)限。
[][][][][][][]
然后,單獨(dú)創(chuàng)建一個可以訪問二進(jìn)制文件的編譯器的組,賦予他這個組相應(yīng)的權(quán)限。
[][][]
至此,任何試圖使用gcc的用戶將會看到權(quán)限被拒絕的信息。
[lyshark ~]$ gcc -c test.c-bash: /usr/bin/gcc: Permission denied
限制日志文件: 接著我們需要對日志文件,進(jìn)行一定的限制,因為一般情況如果系統(tǒng)被入侵了,日志文件將對我們?nèi)∽C有所幫助,而一旦被入侵以后,黑客首先會想辦法清除這些痕跡,所以我們需要設(shè)置日志文件只能增加不能刪除屬性,防止其將日志刪除掉。
[root@localhost ~]# cd /var/log/[root@localhost log]# chattr +a dmesg cron lastlog messages secure wtmp[root@localhost log]# lsattr secure-----a---------- secure[root@localhost log]# rm -fr securerm: cannot remove ‘secure’: Operation not permitted
最小化防火墻規(guī)則: 配置防火墻,拒絕所有端口,只放行SSH,HTTP這兩個必要的端口。
[][][][][][][]
開啟SELinux: 由于系統(tǒng)管理員都會關(guān)閉,所以這里要手動開啟。
[root@localhost ~]# vim /etc/selinux/config# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.SELINUX=enforcing[root@localhost ~]# setenforce 1
開啟SeLinux后,會發(fā)現(xiàn)sshd服務(wù)無法正常啟動了,這是因為SELinux策略生效了,下面我們需要修改配置。
SELinux放行SSH端口: 通過 Semanage 管理工具放行6553這個端口。
[root@localhost ~]# yum install -y policycoreutils-python-2.5-29.el7.x86_64[root@localhost ~]# semanage port -l | grep sshssh_port_t tcp 22[root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 6553[root@localhost ~]# semanage port -l | grep sshssh_port_t tcp 6553, 22
設(shè)置Web目錄權(quán)限: 通過 semanage 命令設(shè)置,web目錄權(quán)限。
[root@localhost html]# semanage fcontext -a -t httpd_sys_content_t /var/www/html/index.html[root@localhost html]# ls -Z-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
END
秋招已經(jīng)開始啦,大家如果不做好充足準(zhǔn)備的話,秋招很難找到好工作。
送大家一份就業(yè)大禮包,大家可以突擊一下春招,找個好工作!
