
ウェブのリクエストが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)
出典:https://support.google.com/webmasters/answer/1061943[Googleクローラー]
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");
}
}
2.偽のユーザエージェント
偽の/偽のユーザーエージェント要求を作成するのは簡単です。例えば :
例:abcdefg.comに偽のユーザーエージェントリクエストを送信する
package com.mkyong.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
ソース:https://support.google.com/webmasters/answer/80553[Googlebotの確認]
4. Googlebotの確認 – Javaの例
上記の理論に基づいて、 “逆DNSルックアップ”の第1部分をシミュレートすることができます。要求されたIPポイントがどこにあるかを判断するには `host`コマンドを使います。
リクエストがGooglebotからのものである場合、次のパターンが表示されます。
xx ** .googlebot.com.
。
__P.S `host`コマンドは、** nixシステムのみで利用可能です。
例:偽のユーザエージェントを検出する
@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
-
注意** この簡単な解決策は、偽装/偽装ユーザーエージェントを100%停止することはできませんが、この余分なセキュリティレイヤーは基本的なユーザーエージェントのなりすまし攻撃の大半を止めることができます。
あなたがより良い解決策を持っているなら、以下を共有してください、ありがとう。
参考文献
Googlebot]。
https://support.google.com/webmasters/answer/1061943
[Google
クローラー/a>]。リンク://java/how-to-execute-shell-command-from-java/[シェルを実行する
Javaからのコマンド]
リンク://タグ/googlebot/[googlebot]リンク://タグ/ユーザーエージェント/[ユーザーエージェント]