1#!/usr/local/bin/python2.7 2# send Neighbor Unreachability Detection neighbor solicitation 3# expect an neighbor advertisement answer and check it 4 5import os 6from addr import * 7from scapy.all import * 8 9# link-local solicited-node multicast address 10def nsma(a): 11 n = inet_pton(socket.AF_INET6, a) 12 return inet_ntop(socket.AF_INET6, in6_getnsma(n)) 13 14# ethernet multicast address of multicast address 15def nsmac(a): 16 n = inet_pton(socket.AF_INET6, a) 17 return in6_getnsmac(n) 18 19# ethernet multicast address of solicited-node multicast address 20def nsmamac(a): 21 return nsmac(nsma(a)) 22 23# link-local address 24def lla(m): 25 return "fe80::"+in6_mactoifaceid(m) 26 27ip=IPv6(src=SRC_OUT6, dst=DST_IN6)/ICMPv6ND_NS(tgt=DST_IN6) 28eth=Ether(src=SRC_MAC, dst=DST_MAC)/ip 29 30if os.fork() == 0: 31 time.sleep(1) 32 sendp(eth, iface=SRC_IF) 33 os._exit(0) 34 35ans=sniff(iface=SRC_IF, timeout=3, filter= 36 "ip6 and src "+DST_IN6+" and dst "+SRC_OUT6+" and icmp6") 37for a in ans: 38 if a and a.type == scapy.layers.dot11.ETHER_TYPES.IPv6 and \ 39 ipv6nh[a.payload.nh] == 'ICMPv6' and \ 40 icmp6types[a.payload.payload.type] == 'Neighbor Advertisement': 41 tgt=a.payload.payload.tgt 42 print "target=%s" % (tgt) 43 if tgt == DST_IN6: 44 exit(0) 45 print "TARGET!=%s" % (DST_IN6) 46 exit(1) 47print "NO NEIGHBOR ADVERTISEMENT" 48exit(2) 49