Java - WebリクエストがGoogleクローラからのものかどうかを確認する

Java – WebリクエストがGoogleクローラーからのものかどうかを確認します

google-Bot

WebリクエストがGoogleクローラーまたはGoogleボットから送信されている場合、リクエストされた「ユーザーエージェント」は次のようになります。

Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
or
(rarely used): Googlebot/2.1 (+http://www.google.com/bot.html)

ソース:Google crawlers

1. Javaの例

Javaでは、HttpServletRequestから「ユーザーエージェント」を取得できます。

例:abcdefg.comでホストされるサービス

    @Autowired
    private HttpServletRequest request;

    //...
    String userAgent =  request.getHeader("user-agent");

    System.out.println("User Agent : " + userAgent);

    if(!StringUtils.isEmpty(userAgent)){
        if(userAgent.toLowerCase().contains("googlebot")){
            System.out.println("This is Google bot");
        }else{
            System.out.println("Not from Google");
        }

    }

Note
上記の解決策はうまく機能しますが、偽のまたはなりすましのユーザーエージェントを検出できませんでした。

2. 偽のユーザーエージェント

偽/なりすましのユーザーエージェントリクエストを作成するのは簡単です。 例えば ​​:

例:偽のユーザーエージェントリクエストをabcdefg.comに送信する

package com.example.web;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

public class test {

    public static void main(String[] args) throws Exception {

        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet("abcdefg.com");
        request.setHeader("user-agent", "fake googlebot");
        HttpResponse response = client.execute(request);

    }

}

abcdefg.comでの出力。

User Agent : fake googlebot

This is Google bot

3. Googlebotの検証

実際のGooglebotを確認するには、次のように「逆DNSルックアップ」を手動で使用できます。

> host 66.249.66.1
1.66.249.66.in-addr.arpa domain name pointer
crawl-66-249-66-1.googlebot.com.

> host crawl-66-249-66-1.googlebot.com
crawl-66-249-66-1.googlebot.com has address 66.249.66.1

ソース:Verifying Googlebot

4. Googlebotの検証-Javaの例

上記の理論に基づいて、「逆DNSルックアップ」の最初の部分をシミュレートできます。 hostコマンドを使用して、要求されたIPポイントがどこにあるかを判別します。

リクエストがGooglebotからのものである場合、次のパターンが表示されます:xx *.googlebot.com.

P.S host command is available at *nix system only.

例:偽のユーザーエージェントを検出する

    @Autowired
    private HttpServletRequest request;

    //...
    String requestIp = getRequestIp();
    String userAgent = request.getHeader("user-agent");

    System.out.println("User Agent : " + userAgent);

    if(!StringUtils.isEmpty(userAgent)){

        if(userAgent.toLowerCase().contains("googlebot")){

            //check fake user agent
            String output = executeCommand("host " + requestIp);
            System.out.println("Output : " + output);

            if(output.toLowerCase().contains("googlebot.com")){
                System.out.println("This is Google bot");
            }else{
                System.out.println("This is fake user agent");
            }

        }else{
            System.out.println("Not from Google");
        }
    }

    //get requested IP
    private String getRequestIp() {
        String ipAddress = request.getHeader("X-FORWARDED-FOR");
        if (ipAddress == null) {
            ipAddress = request.getRemoteAddr();
        }
        return ipAddress;
    }

    // execute external command
    private String executeCommand(String command) {

        StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader =
                new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();

    }

「ステップ2」の偽のユーザーエージェントの例をもう一度試してください。 これで、次の出力が得られます。

Output : Host 142.1.168.192.in-addr.arpa. not found: 3(NXDOMAIN) //this output may vary.

User Agent : fake googlebot
This is fake user agent

Note
この単純なソリューションでは、偽の/なりすましユーザーエージェントを100%阻止できない場合がありますが、この追加のセキュリティレイヤーは、基本的なユーザーエージェントのなりすまし攻撃のほとんどを阻止できるはずです。

より良い解決策がある場合は、以下で共有してください、ありがとう。