1. 程式人生 > 實用技巧 >Linux主機加固

Linux主機加固

​前 言

Linux系統被應用於大部分企業的伺服器上,因此在等保測評中主機加固也是必須要完成的一項環節。

由於在之後專案開始要進行主機加固,因此對linux的加固流程進行總結學習。

Linux的主機加固主要分為:賬號安全、認證授權、協議安全、審計安全。簡而言之,就是4A(統一安全管理平臺解決方案)。

這邊就使用我自己kali的虛擬機器進行試驗學習。

一、賬戶安全

1、口令生存期

gedit /etc/login.defs

在此處對密碼的長度、時間、過期警告進行修改

PASS_MAX_DAYS 90 #密碼最長過期天數

PASS_MIN_DAYS 10 #密碼最小過期天數

PASS_WARN_AGE 7 #密碼過期警告天數

如果修改設定有最小長度也需要修改

PASS_MIN_LEN 8 #密碼最小長度

2、口令複雜度(很重要)

password requisite pam_cracklib.so

在檔案中找到

password requisite pam_cracklib.so

將其修改為:

password requisite pam_cracklib.so try_first_pass retry=3 dcredit=-1 lcredit=-1 ucredit=-1 ocredit=-1 minlen=8

備註:至少包含一個數字、一個小寫字母、一個大寫字母、一個特殊字元、且密碼長度>=8

3、版本資訊

cat /proc/version

4、限制xx使用者登入

/etc/hosts.deny

新增內容:

sshd : 192.168.1.1

禁止192.168.1.1對伺服器進行ssh的登陸

5、檢查是否有其他uid=0的使用者

awk -F “:” '($3==0) {print $1} ' /etc/passwd

6、登陸超時限制

cp -p /etc/profile /etc/profile_bak(備份)

gedit /etc/profile

增加

TMOUT=300

export TMOUT

或者

echo 'export TMOUT=300'>>/etc/profile

echo 'readonly TMOUT' >>/etc/profile

source /etc/profile

7、檢查是否使用PAM認證模組禁止wheel組之外的使用者su為root

gedit /etc/pam.d/su

新增

auth  sufficient pam_rootok.so

auth  required pam_wheel.so use_uid

備註:auth與sufficient之間由兩個tab建隔開,sufficient與動態庫路徑之間使用一個tab建隔開

8、禁用無用賬戶

cat /etc/passwd #檢視口令檔案,確認不必要的賬號。

passwd -l user #鎖定不必要的賬號

9、賬戶鎖定

gedit /etc/pam.d/system-auth

在檔案中修改或者新增

auth required pam_tally.so onerr=fail deny=3 unlock_time=7200

鎖定賬戶舉例:

passwd -l bin

passwd -l sys

passwd -l adm

10、檢查系統弱口令

john /etc/shadow --single

john /etc/shadow --wordlist=pass.dic

我這邊有報錯 就不展示了

使用passwd 使用者 命令為使用者設定複雜的密碼

二、協議安全

1、openssh升級(按需做)

yum update openssh

2、定時任務(防止病毒感染)

定時任務檢查:

crontab -l

一次性任務檢查:

at -l

3、限制ssh登入(看是否需要)

首先cat /etc/ssh/sshd_config

檢視PermitRootLogin是否為no

gedit /etc/ssh/sshd_config

PermitRootLogin no不允許root登陸

Protocol 2 修改ssh使用的協議版本

MaxAuthTries 3 修改允許密碼錯誤次數

或echo "tty1" > /etc/securetty

hmod 700 /root

4、限制su為root使用者

gedit /etc/pam.d/su

在頭部新增auth required /lib/security/pam_wheel.so group=wheel

5、禁止root使用者登入ftp

因為我的kali下沒有這個檔案,因此借鑑一下網上的

cat /etc/pam.d/vsftpd

Auth required pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed

#其中file=/etc/vsftpd/ftpusers即為當前系統上的ftpusers檔案.

echo “root” >> /etc/vsftpd/ftpusersv

6、防止flood攻擊

gedit /etc/sysctl.conf

增加net.ipv4.tcp_syncookies = 1

然後sysctl -p

7、禁ping

echo 0 > /proc/sys/net/ipv4/icmp_echo_igore_all

8、檢查異常程序

ps aux|sort -rn -k +3|head

#檢查cpu佔用前10

ps aux|sort -rn -k +4|head

#檢查記憶體佔用前10

9、關閉無效的服務及埠

比如郵箱

service postfix status

chkconfig --del postfix

chkconfig postfix off

比如cpus

service cups status

chkconfig --del cups

chkconfig cups off

10、設定防火牆策略

或者用防火牆策略:

service iptables status

echo '請根據使用者實際業務端口占用等情況進行設定!'

例如:

gedit /etc/sysconfig/iptables新增如下策略

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

-A INPUT -m state --state NEW -m udp -p udp --dport 8080 -j ACCEPT

以下舉例:

iptables -I INPUT -s 22.48.11.11 -j DROP

# 22.48.11.11的包全部遮蔽

iptables -I INPUT -s 22.48.11.0/24 -j DROP

#22.48.11.1到22.48.11.255的訪問全部遮蔽

iptables -I INPUT -s 192.168.1.1 -p tcp --dport 80 -j DROP

# 192.168.1.1的80埠的訪問全部遮蔽

iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 80 -j DROP

#192.168.1.1到192.168.1.255的80埠的訪問全部遮蔽

service iptabels restart

#重啟防火牆

11、設定歷史記錄數量

cp /etc/profile /etc/profile_xu_bak(備份)

sed -i s/'HISTSIZE=1000'/'HISTSIZE=5000'/g /etc/profile(修改)

cat /etc/profile |grep HISTSIZE|grep -v export(檢查)

三、認證許可權

1、配置使用者最小許可權

chmod 644 /etc/passwd

chmod 400 /etc/shadow

chmod 644 /etc/group

2、檔案與目錄預設許可權控制

cp /etc/profile /etc/profile.bak(備份)

gedit /etc/profile

增加

umask 027

source /etc/profile

四、日誌審計

1、啟用遠端日誌功能

gedit /etc/rsyslog.conf

*.* @Syslog日誌伺服器IP

###注意:*和@之間存在的是tab鍵,非空格。

2、檢查是否記錄安全事件日誌

gedit /etc/syslog.conf或者/etc/rsyslog.conf

在檔案中加入如下內容:

*.err;kern.debug;daemon.notice /var/log/messages

chmod 640 /var/log/messages

service rsyslog restart

3、日誌保留半年以上

cp/etc/logrotate.conf /etc/logrotate.conf_xu_bak(備份)

sed -i s/'rotate 4'/'rotate 12'/g /etc/logrotate.conf(修改)

service syslog restart(重啟)

cat /etc/logrotate.conf |grep -v '#' |grep rotate(檢查)

轉 FreeBuf.COM