|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Tech and Me, ©2016 - www.techandme.se |
| 4 | + |
| 5 | +# Based on: http://www.techrepublic.com/blog/smb-technologist/secure-your-apache-server-from-ddos-slowloris-and-dns-injection-attacks/ |
| 6 | + |
| 7 | +SPAMHAUS=/etc/spamhaus.wl |
| 8 | +ENVASIVE=/etc/apache2/mods-available/mod-evasive.load |
| 9 | +APACHE2=/etc/apache2/apache2.conf |
| 10 | + |
| 11 | +set -e |
| 12 | + |
| 13 | +# Protect against DDOS |
| 14 | +apt-get -y install libapache2-mod-evasive |
| 15 | +mkdir -p /var/log/apache2/evasive |
| 16 | +chown -R www-data:root /var/log/apache2/evasive |
| 17 | +if [ -f $ENVASIVE ]; |
| 18 | +then |
| 19 | + echo "Envasive mod exists" |
| 20 | +else |
| 21 | + touch $ENVASIVE |
| 22 | + cat << ENVASIVE > "$ENVASIVE" |
| 23 | +DOSHashTableSize 2048 |
| 24 | +DOSPageCount 20 # maximum number of requests for the same page |
| 25 | +DOSSiteCount 300 # total number of requests for any object by the same client IP on the same listener |
| 26 | +DOSPageInterval 1.0 # interval for the page count threshold |
| 27 | +DOSSiteInterval 1.0 # interval for the site count threshold |
| 28 | +DOSBlockingPeriod 10.0 # time that a client IP will be blocked for |
| 29 | +DOSLogDir |
| 30 | +ENVASIVE |
| 31 | +fi |
| 32 | + |
| 33 | +# Protect against Slowloris |
| 34 | +apt-get -y install libapache2-mod-qos |
| 35 | + |
| 36 | +# Protect against DNS Injection |
| 37 | +apt-get -y install libapache2-mod-spamhaus |
| 38 | +if [ -f $SPAMHAUS ]; |
| 39 | +then |
| 40 | + echo "Spamhaus mod exists" |
| 41 | +else |
| 42 | + touch $SPAMHAUS |
| 43 | + cat << SPAMHAUS >> "$APACHE2" |
| 44 | +
|
| 45 | +# Spamhaus module |
| 46 | +<IfModule mod_spamhaus.c> |
| 47 | + MS_METHODS POST,PUT,OPTIONS,CONNECT |
| 48 | + MS_WhiteList /etc/spamhaus.wl |
| 49 | + MS_CacheSize 256 |
| 50 | +</IfModule> |
| 51 | +SPAMHAUS |
| 52 | +fi |
| 53 | + |
| 54 | +service apache2 restart |
| 55 | +if [[ $? > 0 ]] |
| 56 | +then |
| 57 | + echo "Something went wrong..." |
| 58 | + sleep 5 |
| 59 | + exit 1 |
| 60 | +else |
| 61 | + echo "Security added!" |
| 62 | + exit 0 |
| 63 | +fi |
0 commit comments