如何让Apache访问Ubuntu上的IP表

我已经在新的Ubuntu 14.04.5 x64盒子上运行下面的脚本。 它工作的地步,阿帕奇阻止我的IP地址太多请求的预期。 但是,我的IP地址没有添加到iptables 。 我只是得到了一个标准的403,但是我想要丢包而没有错误。

我认为这个问题是一个权限,其中apache不能够运行ip-tables 。 但我无法弄清楚,因为我已经给了apache用户www-datasudoers文件中运行iptables的能力。

任何帮助表示赞赏,我一直在这一天饱受折磨!

 #!/bin/sh apt-get update apt-get -y install apache2 # Install mod evasive apt-get -y install libapache2-mod-evasive # Enable it a2enmod evasive a2enconf evasive # Create log directory mkdir /var/log/mod_evasive chown -R www-data:www-data /var/log/mod_evasive # Config for mod evasive cat <<'EOF' > /etc/apache2/mods-available/evasive.conf <IfModule mod_evasive20.c> DOSHashTableSize 3097 # Allow 5 requests for the same resource request per second. Per-IP DOSPageCount 5 DOSPageInterval 1 # Allow up to 50 requests across the domain per second. Per-IP DOSSiteCount 50 DOSSiteInterval 1 # Block user by IP for 60 minutes DOSBlockingPeriod 60 DOSSystemCommand "sudo /usr/local/bin/ban_ip.sh %s" DOSLogDir /var/log/mod_evasive DOSWhitelist 188.88.90.71 DOSWhitelist 188.88.90.72 DOSWhitelist 188.88.90.73 </IfModule> EOF # Config for banning users cat <<'EOF' > /usr/local/bin/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) sudo $IPTABLES -I INPUT -s $IP -j DROP # Unblock offending IP after 2 hours through the 'at' command; see 'man at' for further details echo "sudo $IPTABLES -D INPUT -s $IP -j DROP" | sudo at now + 1 minute # Remove lock file for future checks sudo rm -f "$MOD_EVASIVE_LOGDIR"/dos-"$IP" EOF echo 'www-data ALL=NOPASSWD: /sbin/iptables *, /usr/bin/at *' | EDITOR='tee -a' visudo 

我从来没有使用过mod,但是在浏览你的脚本之后,看起来你忘记了/usr/local/bin/ban_ip.sh可执行文件 – 如果我正确理解你的设置。

另外,在iptables命令中,我看不到任何额外sudo的任何原因,因为在evasive.conf中,整个usr/local/bin/ban_ip.sh脚本被configuration为由sudo启动。 如果运行,它将以root权限运行。

你可能会添加一些像

 echo here we are at 1 >>/tmp/evasive.testlog 

在#!/ bin / sh之后不久,你的脚本就会被执行。