Skip to content

Instantly share code, notes, and snippets.

@mashihua
Created July 5, 2012 03:50
Show Gist options
  • Save mashihua/3051183 to your computer and use it in GitHub Desktop.
Save mashihua/3051183 to your computer and use it in GitHub Desktop.
Simply block DDOS attack
#!/bin/bash
##
## Simply block DDOS attack
##
ARGV1=$1
if [ $ARGV1 ]; then
DEFINE=$ARGV1
else
DEFINE="100"
fi
netstat -an | grep :80 | grep -v 127.0.0.1 | awk '{ print $5 }' | sort | awk -F: '{ print $1, $4 }' | uniq -c | awk ' $1 > 100 { print $1, $2 }' > /tmp/blackname.txt
for i in `awk '{ print $2 }' /tmp/blackname.txt`
do
COUNT=`grep $i /tmp/blackname.txt | awk '{ print $1 }'`
ZERO="0"
if [ $COUNT -gt $DEFINE ]; then
grep -F $i /opt/whitename.txt > /dev/null
if [ $? -gt $ZERO ]; then
echo "Drop $i"
iptables -I INPUT -p tcp -s $i -j DROP
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment