DNS反向正向查询

import sys, socket

def getipaddrs(hostname):
    """Get a list of IP addresses from a given hostname.  This is a standard
    (forward) lookup."""
    result = socket.getaddrinfo(hostname, None, 0, socket.SOCK_STREAM)
    return [x[4][0] for x in result]

def gethostname(ipaddr):
    """Get the hostname from a given IP address.  This is a reverse
    lookup."""
    return socket.gethostbyaddr(ipaddr)[0]

try:
    # First, do the reverse lookup and get the hostname.
    hostname = gethostname(sys.argv[1]) # could raise socket.herror

    # Now, do a forward lookup on the result from the earlier reverse
    # lookup.
    ipaddrs = getipaddrs(hostname)      # could raise socket.gaierror
except socket.herror, e:
    print "No host names available for %s; this may be normal." % sys.argv[1]
    sys.exit(0)
except socket.gaierror, e:
    print "Got hostname %s, but it could not be forward-resolved: %s" % \
          (hostname, str(e))
    sys.exit(1)

# If the forward lookup did not yield the original IP address anywhere,
# someone is playing tricks.  Explain the situation and exit.
if not sys.argv[1] in ipaddrs:
    print "Got hostname %s, but on forward lookup," % hostname
    print "original IP %s did not appear in IP address list." % sys.argv[1]
    sys.exit(1)

# Otherwise, show the validated hostname.
print "Validated hostname:", hostname

攻击者在反向查找记录中插入伪造数据(通过ip得知域名)

因为正向查询和反向查询的原理不一样,反向查询是基于ip的。


为此 你可以先进行反向查询 通过ip得到一个域名,而后正向查询,如果正常,那么这个ip是ok的。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值