系統(tǒng)之家 - 系統(tǒng)光盤下載網(wǎng)站!

當(dāng)前位置:系統(tǒng)之家 > 系統(tǒng)教程 > Linux利用shell腳本抵御小量的ddos攻擊

Linux如何利用shell腳本抵御小量的ddos攻擊

時(shí)間:2015-06-05 16:30:45 作者:qipeng 來源:系統(tǒng)之家 1. 掃描二維碼隨時(shí)看資訊 2. 請(qǐng)使用手機(jī)瀏覽器訪問: https://m.xitongzhijia.net/xtjc/20150605/50211.html 手機(jī)查看 評(píng)論

  Linux系統(tǒng)shell腳本的作用強(qiáng)大,用于執(zhí)行各種命令,網(wǎng)站被小量ddos攻擊可通過shell腳本進(jìn)行抵御,下面小編就給大家講解下Linux shell腳本分析Nginx日志如何抵抗ddos攻擊。

Linux如何利用shell腳本抵御小量的ddos攻擊

  實(shí)現(xiàn)方式:

  1. 攻擊特征,不同ip不斷POST網(wǎng)站首頁,造成資源消耗過度

  2. 分析nginx訪問日志,判斷POST特征取得客戶端訪問ip

  3. 將連接數(shù)大于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系統(tǒng)shell腳本通過分析Nginx日志,能夠?qū)ξ⒘康膁dos攻擊起到防御作用,你也可使用iptables進(jìn)行防止。

標(biāo)簽 腳本 Shell

發(fā)表評(píng)論

0

沒有更多評(píng)論了

評(píng)論就這些咯,讓大家也知道你的獨(dú)特見解

立即評(píng)論

以上留言僅代表用戶個(gè)人觀點(diǎn),不代表系統(tǒng)之家立場(chǎng)

其他版本軟件

熱門教程

人氣教程排行

Linux系統(tǒng)推薦

掃碼關(guān)注
掃碼關(guān)注

掃碼關(guān)注 官方交流群 軟件收錄