InetAddress.getCanonicalHostName() returns IP instead of Hostname

前端 未结 2 823
醉梦人生
醉梦人生 2020-12-06 16:57

I looked for how to do IP lookup in Java on Stack Overflow but the answers match what I am already doing and do not resolve my problem.

Here is my code:



        
2条回答
  •  攒了一身酷
    2020-12-06 17:25

    The problem lies in the fact that the java.net.InetAdress has a certain procedure against ip-spoofing.

    It first resolves the name into (an) ip address(es). This works fine. In your case the result are two IP adresses. InetAdress then checks back if (at least one of) these adresses resolve to the original input name.

    If they do not, it just returns the original ip adress. The following picture shows the situation after the check for baiduspider-123-125-71-75.crawl.baidu.com

    Note: The ip adresses resolved by getAllByName0 are the same as via nslookup, namely:

    nslookup baiduspider-123-125-71-75.crawl.baidu.com
    Server:     192.168.2.1
    Address:    192.168.2.1#53
    
    Non-authoritative answer:
    Name:   baiduspider-123-125-71-75.crawl.baidu.com
    Address: 62.157.140.133
    Name:   baiduspider-123-125-71-75.crawl.baidu.com
    Address: 80.156.86.78
    

    A solution would be to use the dnsjava library. It skips the spoofing check and therefore works fine.

    dnsjava example:

    String addr = Address.getHostName(InetAddress.getByName("123.125.71.75")); outputs just as expected baiduspider-123-125-71-75.crawl.baidu.com

    Disclaimer: As i am a Java developer and not a security expert, i am not totally aware of the security implications of using a spoofed ip address.

提交回复
热议问题