通過Apache配置限制訪問頻率,可以使用mod_evasive
模塊或mod_security
模塊。以下是使用這兩個模塊的方法:
安裝mod_evasive
在Debian/Ubuntu系統上:
sudo apt-get install libapache2-mod-evasive
在CentOS/RHEL系統上:
sudo yum install mod_evasive
啟用mod_evasive
在Debian/Ubuntu系統上:
sudo a2enmod evasive
在CentOS/RHEL系統上:
sudo systemctl enable mod_evasive
配置mod_evasive
編輯Apache配置文件(通常是/etc/apache2/apache2.conf
或/etc/httpd/conf/httpd.conf
),添加以下內容:
<IfModule mod_evasive20.c>
DOSHashTableSize 3097
DOSPageCount 2
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 10
</IfModule>
解釋:
DOSHashTableSize
: 哈希表的大小,用于存儲IP地址和請求信息。DOSPageCount
: 在指定時間間隔內允許的最大頁面請求次數。DOSSiteCount
: 在指定時間間隔內允許的最大站點請求次數。DOSPageInterval
: 頁面請求的時間間隔(秒)。DOSSiteInterval
: 站點請求的時間間隔(秒)。DOSBlockingPeriod
: 被阻止的IP地址將被阻止的時間(秒)。重啟Apache
sudo systemctl restart apache2
安裝mod_security
在Debian/Ubuntu系統上:
sudo apt-get install libapache2-mod-security2
在CentOS/RHEL系統上:
sudo yum install mod_security
啟用mod_security
在Debian/Ubuntu系統上:
sudo a2enmod security2
在CentOS/RHEL系統上:
sudo systemctl enable mod_security
配置mod_security
編輯Apache配置文件(通常是/etc/apache2/apache2.conf
或/etc/httpd/conf/httpd.conf
),添加以下內容:
<IfModule security2_module>
SecAction "id:500001,\
phase:2,\
nolog,\
pass,\
initcol:ip=%{REMOTE_ADDR},\
setvar:ip.request_count=+1,\
expirevar:ip.request_count=60"
SecRule IP:REQUEST_COUNT "@gt 10" \
"id:500002,\
phase:2,\
pass,\
log,\
msg:'Too many requests',\
status:429,\
tag:'application-multi',\
tag:'language-multi',\
tag:'platform-multi',\
tag:'attack-reconnaissance',\
tag:'OWASP_CRS/WEB_ATTACK/REQUEST_RATE_EXCESSIVE',\
tag:'WASCTC/WASC-21',\
tag:'OWASP_TOP_10/A7',\
tag:'OWASP_AppSensor/CIE1',\
tag:'PCI/6.5.10',\
setvar:ip.request_count=0"
</IfModule>
解釋:
SecAction
: 初始化IP地址的請求計數器,并設置過期時間為60秒。SecRule
: 如果IP地址的請求計數超過10次,則返回429狀態碼(Too Many Requests),并重置計數器。重啟Apache
sudo systemctl restart apache2
通過以上步驟,你可以使用mod_evasive
或mod_security
模塊來限制Apache服務器的訪問頻率。選擇哪個模塊取決于你的具體需求和偏好。