linux 系統(tǒng)簡單加固
賬戶配置
- 使用命令
awk -F: '($2=="")' /etc/shadow查看空口令賬號 - 使用命令
passwd <用戶名>為空口令賬號設置密碼 - 使用命令
awk -F: '($3==0)' /etc/passwd查看UID為0的賬號, 確認只有root的UID為0 - 使用命令
userdel <用戶名>刪除不必要的賬號 - 使用命令
passwd -l <用戶名>鎖定不必要的賬號 - 使用命令
passwd -u <用戶名>解鎖必要的賬號
修改ssh端口
使用命令 vi /etc/ssh/sshd_config 修改ssh服務配置文件, 將以 PORT 開頭的行改為 PORT 30022, 并取消注釋.
重啟sshd服務后配置生效, ssh端口被改為30022.
注意: 需要在各層防火墻上開放新端口.
文件權限配置
使用命令 vi /etc/profile 修改配置文件, 添加行 umask 027, 即新創(chuàng)建的文件屬主擁有讀寫執(zhí)行權限, 同組用戶擁有讀和執(zhí)行權限, 其他用戶無權限.
登錄超時配置
使用命令 vi /etc/profile 修改配置文件, 將以 TMOUT= 開頭的行注釋, 設置為TMOUT=180, 即超時時間為三分鐘.
用戶操作歷史記錄配置
通過腳本代碼實現(xiàn)記錄所有用戶的登錄操作日志, 防止出現(xiàn)安全事件后無據(jù)可查.
使用命令 vi /etc/profile 修改配置文件, 添加以下內(nèi)容:
history
USER=${whoami}
USER_IP=$(who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g')
if [ "$USER_IP" = "" ]
then
USER_IP=`hostname`
fi
if [ ! -d /var/log/history ]
then
mkdir /var/log/history
chmod 777 /var/log/history
fi
if [ ! -d /var/log/history/${LOGNAME} ]
then
mkdir /var/log/history/${LOGNAME}
chmod 300 /var/log/history/${LOGNAME}
fi
export HISTSIZE=4096
DT=$(date +"%Y%m%d_%H:%M:%S")
export HISTFILE="/var/log/history/${LOGNAME}/${USER}@${USER_IP}_$DT"
chmod 600 /var/log/history/${LOGNAME}/*history* 2>/dev/null
注意: /var/log/history 是記錄日志的存放位置, 可以自定義.
通過上述步驟, 可以在 /var/log/history 目錄下以每個用戶為名新建一個文件夾, 每次用戶退出后都會產(chǎn)生以用戶名/登錄IP/時間的日志文件, 包含此用戶本次的所有操作(root用戶除外).
防止ssh遠程登錄的暴力破解
使用fail2ban防止ssh遠程登錄的暴力破解. fail2ban是一套python腳本, 從系統(tǒng)日志中讀取登錄的IP和登錄結果, 如果一定時長(findtime)內(nèi)登錄失敗超過一定的次數(shù)(maxretry), 則將這個IP屏蔽相應的時間(bantime).
根據(jù)操作系統(tǒng)版本, 從下面選擇相應的腳本, 執(zhí)行即可安裝并配置fail2ban.
centos7
yum -y install fail2ban-hostsdeny
sed 's/^loglevel.*=.*$/loglevel = ERROR/g' -i /etc/fail2ban/fail2ban.conf
tee /etc/fail2ban/jail.local <<- 'EOF'
[DEFAULT]
ignoreip = 127.0.0.0/8
bantime = 3600
findtime = 300
maxretry = 5
backend = auto
[sshd]
enabled = true
filter = sshd
action = hostsdeny[daemon_list=sshd]
logpath = /var/log/secure
maxretry = 5
bantime = 3600
findtime = 300
EOF
systemctl enable fail2ban
systemctl restart fail2ban
centos6
yum -y install fail2ban
sed 's/^loglevel.*=.*$/loglevel = ERROR/g' -i /etc/fail2ban/fail2ban.conf
tee /etc/fail2ban/jail.local <<- 'EOF'
[DEFAULT]
ignoreip = 127.0.0.0/8
bantime = 3600
findtime = 300
maxretry = 5
backend = auto
[sshd]
enabled = true
filter = sshd
action = hostsdeny[daemon_list=sshd]
logpath = /var/log/secure
maxretry = 5
bantime = 3600
findtime = 300
EOF
chkconfig fail2ban on
service fail2ban restart
ubuntu1604
apt-get update
apt-get -y install fail2ban
sed 's/^loglevel.*=.*$/loglevel = ERROR/g' -i /etc/fail2ban/fail2ban.conf
tee /etc/fail2ban/jail.local <<- 'EOF'
[DEFAULT]
ignoreip = 127.0.0.0/8
bantime = 3600
findtime = 300
maxretry = 5
backend = auto
[sshd]
enabled = true
filter = sshd
action = hostsdeny[daemon_list=sshd]
logpath = /var/log/auth.log
maxretry = 5
bantime = 3600
findtime = 300
EOF
systemctl enable fail2ban
systemctl restart fail2ban
ubuntu1404
apt-get update
apt-get -y install fail2ban
sed 's/^loglevel.*=.*$/loglevel = 1/g' -i /etc/fail2ban/fail2ban.conf
tee /etc/fail2ban/jail.local <<- 'EOF'
[DEFAULT]
ignoreip = 127.0.0.0/8
bantime = 3600
findtime = 300
maxretry = 5
backend = auto
[ssh]
enabled = true
filter = sshd
action = hostsdeny[daemon_list=sshd]
logpath = /var/log/auth.log
maxretry = 5
bantime = 3600
findtime = 300
EOF
service fail2ban restart
- 使用命令
fail2ban-client status sshd查看當前屏蔽的IP - 使用命令
fail2ban-client set sshd banip <IP>屏蔽IP - 使用命令
fail2ban-client set sshd unbanip <IP>解鎖IP