1.限制 root 不可使用ssh :
因為開放root就等於開放大門給有心人來試 root的密碼,如果猜對的話!
你的主機就變成對方的工具了!!
編輯sshd_config檔
vi/etc/ssh/sshd_config
設定限制root不能使用ssh
將PermitRootLogin “YES” 改成 PermitRootLogin “NO”
存檔離開
2.限制使用SU指令:
限制登入的使用者使用SU這指令,可以防止使用者用SU 變成root
編輯su
vi /etc/pam.d/su
#auth required /lib/security/$ISA/pam_whelle.so use_uid
將前面的”#”刪除 存檔離開
設定只有wheel群組的成員才能使用su命令
usermod -G 10 帳號
3.設定某帳號才能使用ssh:
可以做控管,開放了哪些帳號可以用ssh登入主機
編輯sshd_config
vi/etc/ssh/sshd_config
在最後加入 AllowUsers 帳號
1~3做完後,記得restart ssh
4. 登入的port改掉,不要使用22
# vim /etc/ssh/sshd_config
找到 把#拿掉,並把 port number 改成你要設定的
Port 22
5. iptable設定,只開有在使用的port
centos可以直接輸入”setup”指令進去修改防火牆的port
6. 設定帳號login in 不可失敗太多次
設計原理:
1. 抵擋 ssh 暴力攻擊
2. 如果連續攻擊3次以上則抵擋
3. 利用 TCP-Wrapper 偵測是否有登入失敗 ,有失敗才執行shell
mkdir /etc/firewall/
touch /etc/firewall/sshd.sh
chmod 755 /etc/firewall/sshd.sh
vi /etc/firewall/sshd.sh
寫入以下資料
#!/bin/bash
# 檔案存放的路徑
basedir="/etc/firewall"
# log 存放的地方
sshlog="/var/log/auth.log"
# mail for
mailfor="root"
# 登入錯誤訊息
faildMsg="sshd.*Failed password"
faildMsgInvalid="sshd.*Failed password for invalid user"
# 登入幾次失敗 就擋掉
loginCountFail=3
# 計算前 XXXX 行是否有登入失敗的狀況
failcount=`/usr/bin/tail -n 50 "$sshlog" | /bin/grep "$faildMsg" | wc -l`
#cat "$sshlog" | grep "sshd.*Failed password for" | grep -v "invalid" | cut -d " " -f11
#cat "$sshlog" | grep "sshd.*Failed password for invalid user" | cut -d " " -f13
# 如果有登入失敗的話
if [ "$failcount" -gt "0" ]; then
# 建立 登入失敗 暫存檔
#cat /var/log/auth.log | grep "sshd.*Failed password for" | grep -v "invalid"
cat "$sshlog" | grep "sshd.*Failed password for" | grep -v "invalid" > /tmp/ssh_tmp.log
#cat /var/log/auth.log | grep "sshd.*Failed password for" | grep -v "invalid"
cat "$sshlog" | grep "sshd.*Failed password for invalid user" > /tmp/ssh_tmp2.log
# 建立 登入失敗 IP 暫存檔
#cat "$sshlog" | grep "$faildMsg" | cut -d " " -f13 | sort | uniq > /tmp/ssh_ip_tmp.log
cat /tmp/ssh_tmp.log | cut -d " " -f11 > /tmp/ssh_ip_tmp.log
cat /tmp/ssh_tmp2.log | cut -d " " -f13 >> /tmp/ssh_ip_tmp.log
# 取得要封鎖的IP位址
#blockip=`cat /tmp/ssh_ip_tmp.log`
blockip=`cat /tmp/ssh_ip_tmp.log | sort | uniq`
# iptables中,找出定義的ruleexistchar中的ip。
#iptables -L INPUT -n | grep "tcp dpt:22" | grep "DROP" | awk '{print $4}' | sort | uniq
ruleexistip=`iptables -L INPUT -n | grep "tcp dpt:22" | grep "DROP" | awk '{print $4}' | sort | uniq`
blacklist=`cat "$basedir"/ssd_blacklist_d`
for ip in $blacklist
do
`iptables -D INPUT -p TCP -s $ip --dport 22 -j DROP` > /dev/null
done
rm -rf "$basedir"/ssd_blacklist_d
# 利用 for 迴圈去跑 drop IP
for ip in $blockip
do
# 計算此IP登入幾次失敗
count=`cat /tmp/ssh_ip_tmp.log | grep "$ip" | wc -l`
# 如果登入失敗次數大於 預設值 則執行 drop
if [ "$count" -gt "$loginCountFail" ] ; then
#echo $ip $count
# 如果從secure找出的ip已存在於iptables,就不需要加這條rule了。
if [ "$ip" = "$ruleexistip" ]; then
:
else
`iptables -I INPUT -p TCP -s $ip --dport 22 -j DROP`
fi
# if [ "$ip" = "$ruleexistip" ];
# 建立黑名單
echo "$ip" >> "$basedir"/ssd_blacklist_d
cat "$basedir"/ssd_blacklist_d | sort | uniq > "$basedir"/ssd_blacklist_d
fi
# if [ "$count" -gt "$loginCountFail"] ;
done
# for loop done
# 刪除暫存檔
rm -rf /tmp/ssh*.log
mail -s "ssh hacker 抵擋的IP位址" $mailfor < "$basedir"/ssd_blacklist_d
fi
# if [ "$failcount" -gt "0" ];
然後再
vi /etc/hosts.allow
加入 sshd : ALL : spawn /etc/firewall/sshd.sh
參考資料:
* http://www.andowson.com/posts/list/33.page
* http://ssorc.tw/rewrite.php/read-93.html
7. service只開啟有在使用的service,像是sendmail等等沒在使用的可以關掉
因為開放root就等於開放大門給有心人來試 root的密碼,如果猜對的話!
你的主機就變成對方的工具了!!
編輯sshd_config檔
vi/etc/ssh/sshd_config
設定限制root不能使用ssh
將PermitRootLogin “YES” 改成 PermitRootLogin “NO”
存檔離開
2.限制使用SU指令:
限制登入的使用者使用SU這指令,可以防止使用者用SU 變成root
編輯su
vi /etc/pam.d/su
#auth required /lib/security/$ISA/pam_whelle.so use_uid
將前面的”#”刪除 存檔離開
設定只有wheel群組的成員才能使用su命令
usermod -G 10 帳號
3.設定某帳號才能使用ssh:
可以做控管,開放了哪些帳號可以用ssh登入主機
編輯sshd_config
vi/etc/ssh/sshd_config
在最後加入 AllowUsers 帳號
1~3做完後,記得restart ssh
4. 登入的port改掉,不要使用22
# vim /etc/ssh/sshd_config
找到 把#拿掉,並把 port number 改成你要設定的
Port 22
5. iptable設定,只開有在使用的port
centos可以直接輸入”setup”指令進去修改防火牆的port
6. 設定帳號login in 不可失敗太多次
設計原理:
1. 抵擋 ssh 暴力攻擊
2. 如果連續攻擊3次以上則抵擋
3. 利用 TCP-Wrapper 偵測是否有登入失敗 ,有失敗才執行shell
mkdir /etc/firewall/
touch /etc/firewall/sshd.sh
chmod 755 /etc/firewall/sshd.sh
vi /etc/firewall/sshd.sh
寫入以下資料
#!/bin/bash
# 檔案存放的路徑
basedir="/etc/firewall"
# log 存放的地方
sshlog="/var/log/auth.log"
# mail for
mailfor="root"
# 登入錯誤訊息
faildMsg="sshd.*Failed password"
faildMsgInvalid="sshd.*Failed password for invalid user"
# 登入幾次失敗 就擋掉
loginCountFail=3
# 計算前 XXXX 行是否有登入失敗的狀況
failcount=`/usr/bin/tail -n 50 "$sshlog" | /bin/grep "$faildMsg" | wc -l`
#cat "$sshlog" | grep "sshd.*Failed password for" | grep -v "invalid" | cut -d " " -f11
#cat "$sshlog" | grep "sshd.*Failed password for invalid user" | cut -d " " -f13
# 如果有登入失敗的話
if [ "$failcount" -gt "0" ]; then
# 建立 登入失敗 暫存檔
#cat /var/log/auth.log | grep "sshd.*Failed password for" | grep -v "invalid"
cat "$sshlog" | grep "sshd.*Failed password for" | grep -v "invalid" > /tmp/ssh_tmp.log
#cat /var/log/auth.log | grep "sshd.*Failed password for" | grep -v "invalid"
cat "$sshlog" | grep "sshd.*Failed password for invalid user" > /tmp/ssh_tmp2.log
# 建立 登入失敗 IP 暫存檔
#cat "$sshlog" | grep "$faildMsg" | cut -d " " -f13 | sort | uniq > /tmp/ssh_ip_tmp.log
cat /tmp/ssh_tmp.log | cut -d " " -f11 > /tmp/ssh_ip_tmp.log
cat /tmp/ssh_tmp2.log | cut -d " " -f13 >> /tmp/ssh_ip_tmp.log
# 取得要封鎖的IP位址
#blockip=`cat /tmp/ssh_ip_tmp.log`
blockip=`cat /tmp/ssh_ip_tmp.log | sort | uniq`
# iptables中,找出定義的ruleexistchar中的ip。
#iptables -L INPUT -n | grep "tcp dpt:22" | grep "DROP" | awk '{print $4}' | sort | uniq
ruleexistip=`iptables -L INPUT -n | grep "tcp dpt:22" | grep "DROP" | awk '{print $4}' | sort | uniq`
blacklist=`cat "$basedir"/ssd_blacklist_d`
for ip in $blacklist
do
`iptables -D INPUT -p TCP -s $ip --dport 22 -j DROP` > /dev/null
done
rm -rf "$basedir"/ssd_blacklist_d
# 利用 for 迴圈去跑 drop IP
for ip in $blockip
do
# 計算此IP登入幾次失敗
count=`cat /tmp/ssh_ip_tmp.log | grep "$ip" | wc -l`
# 如果登入失敗次數大於 預設值 則執行 drop
if [ "$count" -gt "$loginCountFail" ] ; then
#echo $ip $count
# 如果從secure找出的ip已存在於iptables,就不需要加這條rule了。
if [ "$ip" = "$ruleexistip" ]; then
:
else
`iptables -I INPUT -p TCP -s $ip --dport 22 -j DROP`
fi
# if [ "$ip" = "$ruleexistip" ];
# 建立黑名單
echo "$ip" >> "$basedir"/ssd_blacklist_d
cat "$basedir"/ssd_blacklist_d | sort | uniq > "$basedir"/ssd_blacklist_d
fi
# if [ "$count" -gt "$loginCountFail"] ;
done
# for loop done
# 刪除暫存檔
rm -rf /tmp/ssh*.log
mail -s "ssh hacker 抵擋的IP位址" $mailfor < "$basedir"/ssd_blacklist_d
fi
# if [ "$failcount" -gt "0" ];
然後再
vi /etc/hosts.allow
加入 sshd : ALL : spawn /etc/firewall/sshd.sh
參考資料:
* http://www.andowson.com/posts/list/33.page
* http://ssorc.tw/rewrite.php/read-93.html
7. service只開啟有在使用的service,像是sendmail等等沒在使用的可以關掉
沒有留言:
張貼留言