# Simple python example to check for possible exploitation of the
# gethostbyname bug as described on http://blog.cyberwar.nl/
# It expects the hostname as input, and outputs the ipv6 address -if found-
# and the first 4 bytes as returned by the faulty function
# The DNS module used can be found on http://www.dnspython.org/
# Edwin van Andel - @yafsec

import dns.resolver
import sys

def F6(qu):
    try:
        answers = dns.resolver.query(qu, 'AAAA')
        for rdata in answers:
            print 'IPv6 address : ' + rdata.address
            a = rdata.address.replace(':',"")[:8]
            i = 0
            addr = ''
            while i < 8:
                j=i+2
                addr = addr + str((int(a[i:j],16)))
                if i < 6:
                    addr = addr + '.'
                i=j
            print 'IPv4 target  : ' + addr
    except:
        print 'No IPv6 record found'
        return

def main():
    if len(sys.argv) == 2:
        F6(sys.argv[1])
    else:
        print 'Usage: '+ str(sys.argv[0]) + ' hostname'
        exit(0)
if __name__ == '__main__':
    main()
