flood-detector.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/local/bin/bash
  2. # Author:: Benjamin Sonntag (<benjamin _at_ sonntag _dot_ fr>)
  3. # Author:: Sebastien Badia (<seb _at_ sebian _dot_ fr>)
  4. # Author:: Philippe Le Brouster (<plb _at_ nebkha _dot_ net>)
  5. # detect flood by first counting the number of packets per second on an interface,
  6. # if the PPS is too high, search for a victim and add it to blackhole
  7. if [[ "x${DEBUG}" == "x1" ]]; then
  8. set -e
  9. set -x
  10. fi
  11. hostname=$(hostname -s)
  12. case $hostname in
  13. "zoulou")
  14. IFACE="em1.109"
  15. ;;
  16. "yankee")
  17. IFACE="em0.179"
  18. ;;
  19. "x-ray")
  20. IFACE="em0.3012"
  21. ;;
  22. "grimoire")
  23. IFACE="eth0"
  24. ;;
  25. "whiskey")
  26. IFACE="lagg0.3012"
  27. ;;
  28. *)
  29. echo "Unknown router"
  30. exit 1
  31. esac
  32. # Above this rate, trigger the flood removal process
  33. MAXPPS=50000
  34. # Max PPS for the victim itself :
  35. MAXPPSVICTIM=5000
  36. # The mail recipient to notify :
  37. MAILRECIPIENT="root@gitoyen.net"
  38. show_usage(){
  39. cat <<EOHELP
  40. Usage: $0 [OPTION]
  41. Flood detector and blackhole
  42. -e Nmap expression.
  43. -i Interface to catch (default $IFACE)
  44. -n PPs that trigger tcpdump (default $MAXPPS)
  45. -t PPs that trigger the blackhole (default $MAXPPSVICTIM)
  46. -m Recipient (default $MAILRECIPIENT)
  47. -h Show this help.
  48. ./flood-detector.sh -e 'dst net 80.67.160.0/24 or dst net 80.67.174.0/24'
  49. EOHELP
  50. }
  51. while getopts "i:e:n:t:m:h" opt; do
  52. case $opt in
  53. e)
  54. # Filter the tcpdump on that expression then
  55. FILTER="$OPTARG"
  56. ;;
  57. n)
  58. MAXPPS="$OPTARG"
  59. ;;
  60. t)
  61. MAXPPSVICTIM="$OPTARG"
  62. ;;
  63. i)
  64. IFACE="$OPTARG"
  65. ;;
  66. m)
  67. MAILRECIPIENT="$OPTARG"
  68. ;;
  69. *)
  70. show_usage
  71. exit 1
  72. ;;
  73. esac
  74. done
  75. if [ -z "$FILTER" ]; then
  76. show_usage
  77. exit 1
  78. fi
  79. echo "Auto Blackholing"
  80. echo "IFACE $IFACE"
  81. echo "FILTER $FILTER"
  82. echo "MAXPPS $MAXPPS"
  83. echo "MAXPPSVICTIM $MAXPPSVICTIM"
  84. while true
  85. do
  86. INPPS=`netstat -w 1 -I "$IFACE" -q 3 | tail -1 | awk '{print $1}' `
  87. if [ "$INPPS" -gt "$MAXPPS" ]
  88. then
  89. echo "`date` $INPPS > $MAXPPS, searching for a victim"
  90. # Ok, we are flooded, turn the blackhole ON for the victim
  91. #tcpdump -i "$IFACE" -p -n -w /tmp/flood.pcap "$FILTER" &
  92. tcpdump -i "$IFACE" -p -n -w - "$FILTER" | gzip - > /tmp/flood.pcap.gz &
  93. ME="$!"
  94. sleep 2
  95. kill -TERM "$ME" >/dev/null 2>/dev/null
  96. sleep 1
  97. kill -KILL "$ME" >/dev/null 2>/dev/null
  98. # Now find the victim and add it to the blackhole :
  99. #BLACK="`tcpdump -r /tmp/flood.pcap -n 2>/dev/null |awk '{print $5}' |awk -F "." '{print $1 "." $2 "." $3 "." $4}' |sort | uniq -c | sort -gr | head -1`"
  100. BLACK="` zcat /tmp/flood.pcap.gz 2>/dev/null | tcpdump -r - -n 2>/dev/null |awk '{print $5}' |awk -F "." '{print $1 "." $2 "." $3 "." $4}' |sort | uniq -c | sort -gr | head -1`"
  101. BLACKCOUNT="`echo $BLACK | awk '{print $1}'`"
  102. BLACKME="`echo $BLACK | awk '{print $2}'|cut -d':' -f1`"
  103. if [ -n "$BLACKME" ] && [ -n "$BLACKCOUNT" ] && [ "$BLACKCOUNT" -gt "$MAXPPSVICTIM" ];
  104. then
  105. echo "`date` found $BLACKME that received $BLACKCOUNT pps, blackholing..."
  106. # we got him, add him to the blackhole :
  107. bash /tmp/blackhole.sh -a add -i ${BLACKME}/32
  108. # And we tell the admin ;)
  109. ( echo "Automatic blackhole triggered at `date`" ; echo "PPS was $INPPS, and $BLACKCOUNT was sent to $BLACKME" ) | mail -s "Automatic Blackhole triggered on `hostname` for $BLACKME" $MAILRECIPIENT
  110. else
  111. echo "`date` Nobody found for the filtered expression, will try again later"
  112. fi
  113. fi
  114. sleep 5
  115. done