<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR] RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC] RewriteRule .* - [F] </IfModule>
有時候,網站會受到一種叫 cc 攻擊,CC攻擊就是黑客,經過軟件,肉雞同時去訪問一個站點,超過服務器的併發,就會致使站點宕機;經過肉雞,軟件去訪問站點,就是普通的訪問,沒有什麼特殊的,只是讓站點超過併發致使嚴重超負荷而宕機,因此沒辦法去進行控制;所謂CC攻擊都會有一個規律的特徵,就是user_agent是一致的,好比同一個IP、同一個標識、同一個地址;遇到這種規律的user_agent頻繁訪問的狀況咱們就能夠斷定他就是CC攻擊,咱們就能夠經過限制他的user_agent 減輕服務器壓力,只須要讓他從正常訪問的200,限制爲403,就能減輕服務器的壓力,由於403僅僅是一個請求,只會使用到不多的帶寬,畢竟他沒有牽扯到php 和mysqlphp
cc攻擊html
肉雞 (受黑客遠程控制的電腦)mysql
[root@hf-01 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR] //條件 RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC] //條件 RewriteRule .* - [F] </IfModule>
[root@hf-01 111.com]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@hf-01 111.com]# /usr/local/apache2.4/bin/apachectl graceful [root@hf-01 111.com]#
[root@hf-01 111.com]# curl -x127.0.0.1:80 '111.com/upload/123.php' -I HTTP/1.1 403 Forbidden Date: Tue, 26 Dec 2017 20:52:15 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1 [root@hf-01 111.com]# curl -x127.0.0.1:80 '111.com/123.php' -I HTTP/1.1 403 Forbidden Date: Tue, 26 Dec 2017 20:53:30 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1 [root@hf-01 111.com]#
[root@hf-01 logs]# tail -5 /usr/local/apache2.4/logs/123.com-access_20171227.log 192.168.74.1 - - [27/Dec/2017:04:32:09 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0" 192.168.74.1 - - [27/Dec/2017:04:32:10 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0" 192.168.74.1 - - [27/Dec/2017:04:32:17 +0800] "GET /upload/123.php HTTP/1.1" 403 223 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0" 127.0.0.1 - - [27/Dec/2017:04:52:15 +0800] "HEAD HTTP://111.com/upload/123.php HTTP/1.1" 403 - "-" "curl/7.29.0" 127.0.0.1 - - [27/Dec/2017:04:53:30 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 403 - "-" "curl/7.29.0" [root@hf-01 logs]#
模擬user_agent,去訪問會看到狀態碼爲200 能夠正常訪問 [root@hf-01 111.com]# curl -A "hanfeng hanfeng" -x127.0.0.1:80 '111.com/123.php' -I HTTP/1.1 200 OK Date: Tue, 26 Dec 2017 21:17:47 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 X-Powered-By: PHP/5.6.30 Content-Type: text/html; charset=UTF-8 [root@hf-01 111.com]# curl -A "hanfeng hanfeng" -x127.0.0.1:80 '111.com/123.php' 123.php[root@hf-01 111.com]# [root@hf-01 111.com]#
[root@hf-01 111.com]# !tail tail -5 /usr/local/apache2.4/logs/123.com-access_20171227.log 192.168.74.1 - - [27/Dec/2017:04:32:17 +0800] "GET /upload/123.php HTTP/1.1" 403 223 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0" 127.0.0.1 - - [27/Dec/2017:04:52:15 +0800] "HEAD HTTP://111.com/upload/123.php HTTP/1.1" 403 - "-" "curl/7.29.0" 127.0.0.1 - - [27/Dec/2017:04:53:30 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 403 - "-" "curl/7.29.0" 127.0.0.1 - - [27/Dec/2017:05:17:47 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - "-" "hanfeng hanfeng" 127.0.0.1 - - [27/Dec/2017:05:19:40 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 "-" "hanfeng hanfeng" [root@hf-01 111.com]#