Shell编程一 Shell防范ARP攻击
#!/bin/bash
declare gw=`route -n | grep -e '^0.0.0.0'`
declare gwname=`echo $gw | grep -oe '\w*$'`
declare gwip=`echo $gw | grep -oe '[0-9]\{2,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'`
declare gwmac=`arp -n | grep -e $gwip | grep -oe '[0-9A-F]\{2\}:[0-9A-F]\{2\}:[0-9A-F]\{2\}:[0-9A-
F]\{2\}:[0-9A-F]\{2\}:[0-9A-F]\{2\}'`
echo "switch $gwname arp: $gwip - $gwmac to static"
arp -s $gwip $gwmac
echo "done, off arp reuqest .."
ifconfig $gwname -arp
echo "all done."
Shell编程二 Shell防范DDos攻击
#!/bin/bash
FilePath="access.log"
awk '{print $1}' $FilePath | sort -rn | uniq -c >ip_count.log
cat ip_count.log | while read text ####读取文件内容,以行为单位
do
echo $text
count=`echo $text | awk '{print $1}' `
ip=`echo $text | awk '{print $2}'`
if [ $count -gt 20 ]
then
if iptables -L | grep $ip ###判断是否已经在iptables 中
then
echo "ip地址存在iptables中,不添加 "
else
echo "添加ip地址到iptables"
iptables -A INPUT -s $ip -j DROP && echo $ip >>ip_drop.log
/etc/init.d/iptables save &> /dev/null ###使iptables 生效
/etc/init.d/iptables restart &> /dev/null
fi
else
echo "未到达标准,不添加到iptables"
fi
done
Shell编程三 Shell防范CC洪水攻击
#!/bin/bash
#取得参数$1为并发阈值,若留空则默认允许单IP最大100并发(实际测试发现,2M带宽,十来个并发服务器就已经无法访问了!)
if [[ -z $1 ]];then
num=50
else
num=$1
fi
#巧妙的进入到脚本工作目录
cd $(cd $(dirname $BASH_SOURCE) && pwd)
#请求检查、判断及拉黑主功能函数
function check(){
iplist=`netstat -an |grep ^tcp.*:80|egrep -v 'LISTEN|127.0.0.1'|awk -F"[ ]+|[:]" '{print $6}'|sort|uniq -c|sort -rn|awk -v str=$num '{if ($1>str){print $2}}'`
if [[ ! -z $iplist ]];
then
>./iplist/black_ip.txt
for black_ip in $iplist
do
#白名单过滤中已取消IP段的判断功能,可根据需要自行修改以下代码(请参考前天写的脚本)
#exclude_ip=`echo $black_ip | awk -F"." '{print $1"."$2"."$3}'`
#grep -q $exclude_ip ./white_ip.txt
grep -q $black_ip ./white_ip.txt
if [[ $? -eq 0 ]];then
echo "$black_ip (white_ip)" >>./black_ip.txt
else
echo $black_ip >>./black_ip.txt
iptables -nL | grep $black_ip ||(iptables -I INPUT -s $black_ip -j DROP & echo "$black_ip `date +%Y-%m-%H:%M:%S`">>./iplist/denylog.txt & echo 1 >./sendmail)
fi
done
#存在并发超过阈值的单IP就发送邮件
if [[ `cat ./sendmail` == 1 ]];then sendmsg;fi
fi
}
#发邮件函数
function sendmsg(){
netstat -nutlp | grep "sendmail" >/dev/null 2>&1 || /etc/init.d/sendmail start >/dev/null 2>&1
echo -e "From: 发邮件地址@qq.com\nTo:收邮件地址@qq.com\nSubject:Someone Attacking your system!!\nIts Ip is" >./message
cat ./black_ip.txt >>./message
/usr/sbin/sendmail -f 发邮件地址@qq.com -t 收邮件地址@qq.com -i <./message
>./sendmail
}
#间隔10s无限循环检查函数
while true
do
check
#每隔10s检查一次,时间可根据需要自定义
sleep 10
done
Shell编程四 Shell编程监控目录修改内容
#! /bin/bash
webroot="/home/www/"
cp /dev/null rsync_file
if [ ! -f file.md5 ];then
find $webroot -type f -exec md5sum {} \; >>file.md5
else
for file in $(md5sum -c file.md5|awk -F':' '/FAILED/{print $1}')
do
if [ -f $file ];then
filename_z=$(echo $file|sed 's#/#\\/#g')
sed -i "/ $filename_z/"d file.md5
md5sum $file >> file.md5
echo $file >> rsync_file
else
echo $file >>rsync_rm
fi
done
for newfile in $(find $webroot -type f)
do
grep $newfile file.md5 >/dev/null 2>&1
if [ $? -gt 0 ];then
md5sum $newfile >> file.md5
echo "$newfile" >> rsync_file
fi
done
for rfile in $(cat rsync_file)
do
rsync -avzp $rfile /home/www3/
done
1,监控 /home/www/ 目录改动,并将改动的文件rsync到/home/www3目录
2,监控方法为监控文件的md5值,如果md5值与上次不同,即发生改变