https://www.supinfo.com/articles/single/4342-how-to-secure-your-linux-server-from-ddos-attack
DDoS Deflate
Fail2Ban
Apache mod_evasive module
インストール
$ sudo apt-get install apache2 apache2-utils -y
$ sudo apt-get install libapache2-mod-evasive -y
Apacheモジュールとして有効になっているかどうか確認
$ sudo apachectl -M | grep evasive
evasive20_module (shared)
mod_evasiveの設定
$ sudo nano /etc/apache2/mods-enabled/evasive.conf
<IfModule mod_evasive20.c>
DOSHashTableSize 3097
DOSPageCount 2
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 100
#DOSEmailNotify root@ubuntu1804
DOSSystemCommand "su - someuser -c '/usr/local/bin/a2_evasive_ban_ip.sh %s'"
DOSLogDir "/var/log/mod_evasive"
</IfModule>
DOSSystemCommand:ブラックリストIPを検知した際のシェルスクリプトを指定します。
$ sudo nano a2_evasive_ban_ip.sh
#!/bin/sh
# Offending IP as detected by mod_evasive
IP=$1
# Path to iptables binary executed by user www-data through sudo
IPTABLES="/sbin/iptables"
# mod_evasive lock directory
MOD_EVASIVE_LOGDIR=/var/log/mod_evasive
# Add the following firewall rule (block IP)
$IPTABLES -I INPUT -s $IP -j DROP
# Unblock offending IP after 2 hours through the 'at' command; see 'man at' for further details
echo "$IPTABLES -D INPUT -s $IP -j DROP" | at now + 1 minute
# Remove lock file for future checks
rm -f "$MOD_EVASIVE_LOGDIR"/dos-"$IP"
DOSLogDirの作成
$ sudo mkdir /var/log/mod_evasive
$ sudo chown -R www-data:www-data /var/log/mod_evasive
Apacheの再起動
$sudo service apache2 restart
動作テスト
$ sudo perl /usr/share/doc/libapache2-mod-evasive/examples/test.pl
HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
....