Server anti crawler strategy: Apache/Nginx/PHP prohibits some user agents from crawling websites

 Watson Blog September 18, 2017 08:12:00 The server comment two hundred and sixty-six Reading mode

We all know that there are a lot of crawlers on the web, some of which are beneficial to website inclusion, such as Baidu spider, and some useless crawlers that not only do not follow the robots rules, but also can not bring traffic to the website. Recently, we found that there are many records of garbage crawlers in nginx logs, So I sorted out and collected various methods to prohibit garbage spiders from crawling on the network. While setting up my own web, I also provided reference for webmasters.

 Server anti crawler strategy: Apache/Nginx/PHP prohibits some user agents from crawling websites

I Apache

① . By modifying the. htaccess file

Modify. htaccess under the website directory and add the following codes (2 codes are optional):

Available codes (1):

  1. RewriteEngine?On
  2. RewriteCond?%{HTTP_USER_AGENT}? (^$|FeedDemon|Indy? Library|Alexa?Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft?URL?Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports?Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms)? [NC]
  3. RewriteRule?^(.*)$?-? [F]

Available codes (2):

  1. SetEnvIfNoCase?^User-Agent$?.*(FeedDemon|Indy? Library|Alexa?Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft?URL?Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports?Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms)?BADBOT
  2. Order?Allow,Deny
  3. Allow?from?all
  4. Deny?from?env=BADBOT

② . By modifying the httpd.conf configuration file

Find the following similar locations, add/modify according to the following code, and then restart Apache:

  1. DocumentRoot?/home/wwwroot/xxx
  2. <Directory? "/home/wwwroot/xxx" >
  3. SetEnvIfNoCase?User-Agent? ".*(FeedDemon|Indy? Library|Alexa?Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft?URL?Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports?Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms)" ? BADBOT
  4. ???????? Order?allow,deny
  5. ???????? Allow?from?all
  6. ??????? deny?from?env=BADBOT
  7. </Directory>

II Nginx code

Enter the conf directory under the nginx installation directory and save the following code as agent_deny. conf

cd /usr/local/nginx/conf

vim agent_deny.conf

  1. #It is prohibited to grab tools such as Scrapy
  2. if ? ( $http_user_agent ?~*? (Scrapy|Curl|HttpClient))? {
  3. ????? return ? 403;
  4. }
  5. #It is forbidden to specify UA and access with empty UA
  6. if ? ( $http_user_agent ?~? "FeedDemon|Indy? Library|Alexa?Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft?URL?Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports?Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|^$" ?)? {
  7. ????? return ? 403;
  8. }
  9. #Forbid fetching in non GET | HEAD | POST mode
  10. if ? ( $request_method ?!~?^ (GET|HEAD|POST)$)? {
  11. ???? return ? 403;
  12. }

Then, insert the following code after location/{in the website related configuration:

  1. include ? agent_deny.conf;

For example, the configuration of Zhang Ge's blog:

  1. [ marsge@Mars_Server ?~]$? cat?/usr/local/nginx/conf/zhangge.conf
  2. location?/? {
  3. ???????? try_files? $uri ? $uri /?/ index.php? $args ;
  4. ????????# Add 1 line at this position:
  5. ???????? include ? agent_deny.conf;
  6. ???????? rewrite?^/sitemap_360_sp.txt$?/sitemap_360_sp.php?last;
  7. ???????? rewrite?^/sitemap_baidu_sp.xml$?/sitemap_baidu_sp.php?last;
  8. ???????? rewrite?^/sitemap_m.xml$?/sitemap_m.php?last;

After saving, execute the following command and restart nginx smoothly:

  1. /usr/local/nginx/sbin/nginx?- s?reload

III PHP code

Put the following method in the first<? After php, you can:

  1. //Get UA information
  2. $ua ?=? $_SERVER ['HTTP_USER_AGENT'];
  3. //Storing malicious USER_AGENT into the array
  4. $now_ua ?=? array ('FeedDemon?','BOT/0.1?(BOT? for ? JCE)','CrawlDaddy?','Java','Feedly','UniversalFeedParser','ApacheBench','Swiftbot','ZmEu','Indy?Library','oBot','jaunty','YandexBot','AhrefsBot','MJ12bot','WinHttp','EasouSpider','HttpClient','Microsoft?URL?Control','YYSpider','jaunty','Python-urllib','lightDeckReports?Bot');
  5. //Forbid empty USER_AGENT, The mainstream collection programs such as dedecms are empty USER_AGENT, and some sql injection tools are also empty USER_AGENT
  6. if (! $ua )? {
  7. ???? header( "Content-type:? text/html;?charset=utf-8" );
  8. ???? die ('Do not collect this station, because the collected stationmaster has no small JJ! ');
  9. } else {
  10. ???? foreach ( $now_ua ? as ? $value ?)
  11. //Determine whether the UA exists in the array
  12. ???? if ( eregi ( $value , $ua ))? {
  13. ???????? header( "Content-type:? text/html;?charset=utf-8" );
  14. ???????? die ('Do not collect this station, because the collected stationmaster has no small JJ! ');
  15. ????}
  16. }

4、 Test effect

If it is vps, it is very simple. Use curl - A to simulate the capture, such as:

Simulate the crawling of searchable spiders:

 curl -I -A 'YisouSpider' zhangge.net

Simulate grab with empty UA:

 curl -I -A '' zhangge.net

Simulate Baidu spider capture:

 curl -I -A 'Baiduspider' zhangge.net

The screenshots of the three capture results are as follows:
 Server anti crawler strategy: Apache/Nginx/PHP prohibits some user agents from crawling websites

It can be seen that Yisou Spider and the return of empty UA are 403 no access signs, while Baidu Spider successfully returns 200, indicating that it is effective!

The screenshot of the effect of viewing nginx logs the next day:

①、 The garbage collection with empty UA information is intercepted:
 Server anti crawler strategy: Apache/Nginx/PHP prohibits some user agents from crawling websites

② . The prohibited UA is intercepted:
 Server anti crawler strategy: Apache/Nginx/PHP prohibits some user agents from crawling websites

Therefore, for the collection of garbage spiders, we can analyze the website's access logs to find out the names of some spiders that we have never seen before. After the query is correct, we can add them to the prohibition list of the previous code to prevent crawling.

5、 Appendix: UA Collection

The following is a list of common garbage UA on the network, for reference only, and you are also welcome to add.

 FeedDemon              Content collection BOT/0.1 (BOT for JCE) sql injection CrawlDaddy sql injection Java                   Content collection Jullo                  Content collection Feedly                 Content collection UniversalFeedParser    Content collection Apache Bench cc Attacker Swiftbot               Useless crawler YandexBot              Useless crawler AhrefsBot              Useless crawler YisouSpider            Useless crawler (acquired by UC Shenma Search, this spider can be released!) MJ12bot                Useless crawler ZmEu phpmyadmin        Vulnerability scanning WinHttp                Collect cc attacks EasouSpider            Useless crawler HttpClient tcp attack Microsoft URL Control Scan YYSpider               Useless crawler Jaunty wordpress blasting scanner oBot                   Useless crawler Python-urllib          Content collection Indy Library           scanning FlightDeckReports Bot useless crawler Linguee Bot            Useless crawler

6、 References

Ask: http://www.uedsc.com/acquisition.html

Haohai: http://www.it300.com/article-15358.html

night sky: http://blog.slogra.com/post-135.html

PS: From Zhang Ge's blog. View the original link: https://zhangge.net/4458.html

 Watson Blog
  • This article is written by Published on September 18, 2017 08:12:00
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/605.html

Comment