linux防ddos攻击脚本,Linux如何利用shell脚本抵御小量的ddos攻击

Linux系统shell脚本的作用强大,用于执行各种命令,网站被小量ddos攻击可通过shell脚本进行抵御,下面小编就给大家讲解下Linux shell脚本分析Nginx日志如何抵抗ddos攻击。

4c4565c720dd6955c5a012465999d38b.png

实现方式:

1. 攻击特征,不同ip不断POST网站首页,造成资源消耗过度

2. 分析nginx访问日志,判断POST特征取得客户端访问ip

3. 将连接数大于50的攻击ip封杀

4. 记录攻击ip到文档

5. 每次取得的攻击ip与已有攻击ip比较

查看源代码:

#!/bin/bash

WEBSITES=(

example.com

)

minute_now=`date +%M`

max_connections=50

banips=“/wwwdata/jobs/banips.txt”

for site in ${WEBSITES[*]}

do

access_log_file=“/wwwdata/logs/${site}.access.log”

if [ -f “${access_log_file}” ]

then

cat ${access_log_file} | grep POST | awk ‘{print $1}’ | sort |uniq -c| sort -nr 》 /wwwdata/jobs/ip_records.txt

lines=`wc -l /wwwdata/jobs/ip_records.txt | awk ‘{print $1}’`

echo “Lines: $lines”

i=1

while [ ${i} -le ${lines} ]

do

ip_record=`head -${i} /wwwdata/jobs/ip_records.txt | tail -1 | sed ‘s/^[ \t]*//g’`

ip_count=`echo ${ip_record} | awk ‘{print $1}’`

ip_address=`echo ${ip_record} | awk ‘{print $2}’`

echo “${ip_count} ${ip_address}”

if [ ${ip_count} -gt ${max_connections} ]

then

banned=`cat ${banips} | grep ${ip_address} | wc -l`

if [ ${banned} -lt 1 ]

then

iptables -A INPUT -s x.x.x.x -p tcp -m state --state NEW -m tcp --dport 80 -j DROP

echo ${ip_address} 》》 ${banips}

fi

fi

i=`expr ${i} + 1`

done

service iptables save

service iptables restart

if [ ${minute_now} -eq 30 ]

then

cat ${access_log_file} 》》 /wwwdata/logs/olds/${site}.access.log

cat /dev/null 》 ${access_log_file}

fi

fi

done

if [ ${minute_now} -eq 30 ]

then

service nginx restart

fi

Linux系统shell脚本通过分析Nginx日志,能够对微量的ddos攻击起到防御作用,你也可使用iptables进行防止。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值