- 论坛徽章:
- 0
|
服务器不断遭受暴力攻击
经过仔细研究,参考了网上很多资料,最后找到一片较正规的解决方案,供大家参考:
方法一:
規則:只要是透過ssh想要用root直接登入的ip都登錄拒絕名單
方法:
1.利用sshd[18362]: Failed password for root from 66.238.200.230 port 50060 ssh2這則訊息,取得IP
2.將取得的IP檢查拒絕名單是否有同樣的IP
3.如拒絕名單沒有此IP,將此IP登錄並新增iptables 規則
4.可以使用root 登入的ip只要加一行註解填入ip就可了
#!/bin/bash
#/usr/local/virus/iptables
dropfilepath="/usr/local/virus/iptables/dropip.txt"
denyfilepath="/usr/local/virus/iptables/iptables.deny"
networkcard="eth1"
#cut messager to get IP address
#sshd[18362]: Failed password for root from 66.238.200.230 port 50060 ssh2
cat /var/log/messages | grep ssh | grep Failed | grep root | cut -d ":" -f 4 | cut -d " " -f 7 | uniq | sort >; $dropfilepath
target1=`cat $dropfilepath`
if [ "$target1" = "" ]; then
echo "No IP Address Drop"
exit 0
fi
for i in $target1; do
if [ -f "$denyfilepath" ]; then
target2=`cat $denyfilepath | grep $i`
else
target2=$i
fi
if [ "$target2" = "" ]; then
if [ -f "$denyfilepath" ]; then
echo "/sbin/iptables -A INPUT -i "'$EXTIF'" -s $i -j DROP # From $0 "`date` >;>; $denyfilepath
else
echo "/sbin/iptables -A INPUT -i "'$EXTIF'" -s $i -j DROP # From $0 "`date` >; $denyfilepath
fi
/sbin/iptables -A INPUT -i $networkcard -s $i -j DROP
echo "IP: $i Add to $denyfilepath"
else
echo "IP: $i to use as drop"
fi
done
方法二:
前言
因為網路上很多小白會try我主機 自己手動block ip太麻煩 所以 自己寫了一個script來達到自動化阻擋ip 因為是第一次寫script 所以 有問 題的地方 大家一起來討論
PS:目前已修正過一次
1.需先使用鳥哥所寫的iptables script 並設定好 <鳥哥都有教 我跳過>;
http://linux.vbird.org/download/linux_security/iptables.rule
http://linux.vbird.org/download/linux_security/iptables.deny
2. TCP_Wrappers 設定 <也是鳥哥的方法>;
vi /etc/hosts.deny
貼上
代碼:
#
# hosts.deny This file describes the names of the hosts which are
# *not* allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
in.proftpd : ALL : spawn (/bin/echo Security notice from host `/bin/hostname`; \
/bin/echo; /usr/sbin/safe_finger @%h ) | \
/bin/mail -s "%d -%h security" root@localhost & \
: twist ( /bin/echo -e "\n\nWARNING connectin not allowed. Your attempt has been logged. \n\n\n警告您尚未允許登入,您的連線將 會被紀錄,並且作為以後的參考\n\n ". )
sshd : ALL : spawn (/bin/echo Security notice from host `/bin/hostname`; \
/bin/echo; /usr/sbin/safe_finger @%h ) | \
/bin/mail -s "%d -%h security" root@localhost & \
: twist ( /bin/echo -e "\n\nWARNING connectin not allowed. Your attempt has been logged. \n\n\n警告您尚未允許登入,您的連線將 會被紀錄,並且作為以後的參考\n\n ". )
3.在/usr/local/virus/iptables 下,建立 wip.txt <此檔案為排除阻擋的ip列表>;
vi wip.txt
代碼:
填上你不想被iptables阻擋的ip
4.在/usr/local/virus/iptables 下,建立自動檢查/var/log/messages 符合惡意登入ssh的script,將要阻擋的ip自動寫入iptables.deny中
vi sshd
貼上
代碼:
#!/bin/sh
# 2005/01/15 ver4 byBoolean@NTHU Email:boolean@boolean.ath.cx
#/usr/local/virus/iptables
nodenyip="/usr/local/virus/iptables/wip.txt"
workpath="/usr/local/virus/iptables/sshd_block_ip_now"
denyfile="/usr/local/virus/iptables/iptables.deny"
declare -i lognumber=2
grep sshd /var/log/messages | grep twist | grep WARNING | cut -d ":" -f 7 | cut -d " " -f 1 | sort | uniq >; $workpath
for (( m=1 ; m<=$lognumber ; m=m+1 )); do
if [ -f "/var/log/messages.$m" ]; then
grep sshd /var/log/messages.$m | grep twist | grep WARNING | cut -d ":" -f 7 | cut -d " " -f 1 | sort | uniq >;>; $workpath
else
echo "檔案 /var/log/messages.$m 並不存在 !!!!"
fi
done
if [ -f "$denyfile" ];then
cat $denyfile | cut -d " " -f 7 >;>; $workpath
rm -rf $denyfile
else
echo "不存在 $denyfile ,Script會依需要自動建立"
fi
cat $workpath | sort | uniq >; $workpath
target=`cat $workpath`
if [ "$target" = "" ]; then
echo "目前沒有添�[需要被阻擋IP的需要"
else
for i in $target; do
target1=`cat $nodenyip | grep $i`
if [ "$target1" = "" ]; then
if [ -f "$denyfile" ]; then
echo "/sbin/iptables -A INPUT -i "'$EXTIF'" -s $i -j DROP #auto" >;>; $denyfile
else
echo "/sbin/iptables -A INPUT -i "'$EXTIF'" -s $i -j DROP #auto" >; $denyfile
fi
echo "<<添�[>;>;IP: $i Add to $denyfile"
else
echo "<<去除>;>;According to the $nodenyip ,drop the IP: $i"
fi
done
fi
cat $denyfile | sort | uniq >; $denyfile
./iptables.rule
5.在/usr/local/virus/iptables 下,建立自動檢查/var/log/messages 符合惡意登入proftpd的script,將要阻擋的ip自動寫入iptables.deny 中<我proftpd是採用inetd啟動的方式>;
vi proftpd
貼上
代碼:
#!/bin/sh
# 2005/01/15 ver4 byBoolean@NTHU Email: boolean@boolean.ath.cx
#/usr/local/virus/iptables
nodenyip="/usr/local/virus/iptables/wip.txt"
workpath="/usr/local/virus/iptables/proftpd_block_ip_now"
denyfile="/usr/local/virus/iptables/iptables.deny"
declare -i lognumber=2
grep xinetd /var/log/messages | grep twist | grep WARNING | cut -d ":" -f 4 | cut -d " " -f 3 | sort | uniq >; $workpath
for (( m=1 ; m<=$lognumber ; m=m+1 )); do
if [ -f "/var/log/messages.$m" ]; then
grep xinetd /var/log/messages.$m | grep twist | grep WARNING | cut -d ":" -f 4 | cut -d " " -f 3 | sort | uniq >;>; $workpath
else
echo "The File /var/log/messages.$m does not exist !!!!"
fi
done
if [ -f "$denyfile" ];then
cat $denyfile | cut -d " " -f 7 >;>; $workpath
rm -rf $denyfile
else
echo "不存在 $denyfile ,Script會依需要自動建立"
fi
cat $workpath | sort | uniq >; $workpath
target=`cat $workpath`
if [ "$target" = "" ]; then
echo "目前沒有添�[需要被阻擋IP的需要"
else
for i in $target; do
target1=`cat $nodenyip | grep $i`
if [ "$target1" = "" ]; then
if [ -f "$denyfile" ]; then
echo "/sbin/iptables -A INPUT -i "'$EXTIF'" -s $i -j DROP #auto" >;>; $denyfile
else
echo "/sbin/iptables -A INPUT -i "'$EXTIF'" -s $i -j DROP #auto" >; $denyfile
fi
echo "<<添�[>;>;IP: $i Add to $denyfile"
else
echo "<<去除>;>;According to the $nodenyip ,drop the IP: $i"
fi
done
fi
cat $denyfile | sort | uniq >; $denyfile
./iptables.rule
6.更改檔案權限並設定為自動執行(每五分鐘)
chmod 755 sshd
chmod 755 proftpd
vi /etc/crontab
代碼:
*/5 * * * * root /usr/local/virus/iptables/sshd >; /dev/null 2>;&1
*/5 * * * * root /usr/local/virus/iptables/proftpd >; /dev/null 2>;&1 |
评分
-
查看全部评分
|