Linux 系统的安全加固
文章目錄
?
- 系列文章目錄
- 前言
- 一、pandas是什么?
- 二、使用步驟
- 1.引入庫
- 2.讀入數(shù)據(jù)
- 總結(jié)
前言
一、Linux為什么需要加固?
Linux操作系統(tǒng)是一個通俗且被混淆的說法,它應(yīng)該是GNU/Linux。我們不是咬文嚼字,因為這和它的穩(wěn)定及安全性有重大關(guān)系。
二、開源軟件與Linux Kernel
GNU/Linux 是GNU(通用公共許可證(GPL))軟件與Linux Kernel 的統(tǒng)稱。由于 Linux 實際是一個操作系統(tǒng)的核心( Kernel )而不是一個完整的操作系統(tǒng),它缺少正常使用中許多必要的軟件,而 Linux 發(fā)行版則是將 Linux kernel 、GNU 軟件包、工具集和文檔、桌面環(huán)境等打包在一起的一個完整操作系統(tǒng)。(如下Linux與GNU的組合圖標)。
GNU software 開源社區(qū)的一個很明顯的問題是,很多項目都活不長,開發(fā)者在丟出最初幾個版本后就棄坑的項目比比皆是。造成這種情況的原因是多種多樣的,總的來說分享源碼是開發(fā)者的權(quán)利,不在提供后續(xù)支持也是某個人的權(quán)利。畢竟社區(qū)不是公司,沒有強制約束力,也不對社區(qū)軟件的后續(xù)維護、版本更新付法律責任(僅有版權(quán)約束)。
目前 Linux 發(fā)行版可以分為由公司維護的版本如 Red Hat (Red Hat), openSUSE (SUSE)和 Ubuntu (Canonical Ltd. ),以及由社區(qū)維護的版本如 Debian 、Arch Linux 、Mandriva 。中國的第一個 Linux 發(fā)行版紅旗 Linux 于1999年8月發(fā)行。
GNU/Linux系統(tǒng)經(jīng)過幾十年的發(fā)展已經(jīng)穩(wěn)定安全了許多,但是這種安全穩(wěn)定是建立在操作人員的專業(yè)素質(zhì)上。
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、控制系統(tǒng)賬戶:
示例:pandas 是基于NumPy 的一種工具,該工具是為了解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的。
二、使用步驟
1.引入庫
代碼如下(示例):
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import warnings warnings.filterwarnings('ignore') import ssl ssl._create_default_https_context = ssl._create_unverified_context2.讀入數(shù)據(jù)
代碼如下(示例):
data = pd.read_csv( 'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv') print(data.head())該處使用的url網(wǎng)絡(luò)請求的數(shù)據(jù)。
總結(jié)
提示:這里對文章進行總結(jié):
例如:以上就是今天要講的內(nèi)容,本文僅僅簡單介紹了pandas的使用,而pandas提供了大量能使我們快速便捷地處理數(shù)據(jù)的函數(shù)和方法。
?
控制系統(tǒng)賬戶: 系統(tǒng)賬戶默認存放在cat /etc/passwd中,你可以手動查詢用戶信息,我們直接除了Root賬戶需要登錄以外,其他的賬戶全部設(shè)置為禁止登錄。
使用 passwd -l 用戶名 鎖定用戶登錄,如下我們寫B(tài)ASH腳本批量的完成這個過程。
#!/bin/bashfor temp in `cut -d ":" -f 1 /etc/passwd | grep -v "root"` dopasswd -l $temp done修改口令生存期: 口令生存期,即用戶密碼的過期時間,默認在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ù)雜程度,該配置默認在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
限制登錄超時: 限制用戶登陸成功后的等待時間,當用戶終端無操作時則默認斷開連接。
[root@localhost ~]# vim /etc/profileTMOUT=300 export TMOUT限制TTY嘗試次數(shù): 該配置可以有效的防止,爆破登錄情況的發(fā)生,其配置文件在cat /etc/pam.d/login中添加如下配置,這個方法只是限制用戶從TTY終端登錄,而沒有限制遠程登錄。
[root@localhost ~]# vim /etc/pam.d/login#%PAM-1.0 auth required pam_tally2.so deny=3 lock_time=300 even_deny_root root_unlock_time=10[root@localhost ~]# pam_tally2 --user lyshark 查詢遠程登錄次數(shù)修改SSH遠程端口: 修改SSH登錄端口,這里可以修改為65534等高位端口,因為Nmap掃描器默認也就探測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 # 登錄端口改為65534 MaxAuthTries=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用戶登錄主機。
# -------------------------------------------------------------------------------------------- # 創(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 anywhere root ALL=(ALL) ALL lyshark ALL=(ALL) ALL # -------------------------------------------------------------------------------------------- # 修改ROOT用戶禁止登錄系統(tǒng) [root@localhost ~]# vim /etc/ssh/sshd_config PermitRootLogin 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)彈出警告提示框,當用戶遠程登陸以后就會提示以下的兩行文字。
[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è)置文件的默認屬性,系統(tǒng)默認的Umask 值是0022,也就是U權(quán)限不動,G權(quán)限減去2,O權(quán)限減2,這里為了防止上傳一句話木馬,我們將系統(tǒng)的Umask值改為0777,也就是說,當用戶新建任何文件的時候,其都不會具有(讀寫執(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)中最為強大的安全特性,任何用戶(即使是root),都無法對不可修改文件進行寫入、刪除、等操作,我們將一些二進制文件設(shè)置為只讀模式,能夠更好的防止系統(tǒng)被非法篡改或注入惡意代碼,一般情況下/sbin 和/usr/lib兩個目錄內(nèi)容能被設(shè)置為不可改變。
[root@localhost sbin]# chattr +i /sbin/ [root@localhost sbin]# chattr +i /usr/sbin/ [root@localhost sbin]# chattr +i /bin/ [root@localhost sbin]# chattr +i /sbin/ [root@localhost sbin]# chattr +i /usr/lib [root@localhost sbin]# chattr +i /usr/lib64 [root@localhost sbin]# chattr +i /usr/libexec限制GCC編譯器: 如果系統(tǒng)已經(jīng)被黑客入侵,那么黑客的下一個目標應(yīng)該是編譯一些POC文件,用來提權(quán),從而在幾秒鐘之內(nèi)就成為了root用戶,那么我們需要對系統(tǒng)中的編譯器進行一定的限制。
首先,你需要檢查單數(shù)據(jù)包以確定其包含有哪些二進制文件。然后將這些文件全部設(shè)置為000無權(quán)限。
[root@localhost ~]# rpm -q --filesbypkg gcc | grep "bin"[root@localhost ~]# chmod 000 /usr/bin/c89 [root@localhost ~]# chmod 000 /usr/bin/c99 [root@localhost ~]# chmod 000 /usr/bin/cc [root@localhost ~]# chmod 000 /usr/bin/gcc [root@localhost ~]# chmod 000 /usr/bin/gcc-* [root@localhost ~]# chmod 000 /usr/bin/gcc-*然后,單獨創(chuàng)建一個可以訪問二進制文件的編譯器的組,賦予他這個組相應(yīng)的權(quán)限。
[root@localhost ~]# groupadd compilerGroup [root@localhost ~]# chown root:compilerGroup /usr/bin/gcc [root@localhost ~]# chmod 0750 /usr/bin/gcc至此,任何試圖使用gcc的用戶將會看到權(quán)限被拒絕的信息。
[lyshark@localhost ~]$ gcc -c test.c -bash: /usr/bin/gcc: Permission denied限制日志文件: 接著我們需要對日志文件,進行一定的限制,因為一般情況如果系統(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 secure rm: cannot remove ‘secure’: Operation not permitted最小化防火墻規(guī)則: 配置防火墻,拒絕所有端口,只放行SSH,HTTP這兩個必要的端口。
[root@localhost ~]# iptables -F [root@localhost ~]# iptables -p INPUT DROP[root@localhost ~]# iptables -I INPUT -p tcp --dport 6553 -j ACCEPT [root@localhost ~]# iptables -I OUTPUT -p tcp --dport 6553 -j ACCEPT [root@localhost ~]# iptables -A INPUT -p tcp --doprt 80 -j ACCEPT [root@localhost ~]# iptables -A INPUT -p tcp --dport 443 -j ACCEPT [root@localhost ~]# iptables-save開啟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 ssh ssh_port_t tcp 22[root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 6553[root@localhost ~]# semanage port -l | grep ssh ssh_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總結(jié)
以上是生活随笔為你收集整理的Linux 系统的安全加固的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux服务器基本安全加固
- 下一篇: Linux安全加固之账号密码安全策略