def generate(self, ip_dst=None, eth_dst=None): """Generates a Neighbor Solicitation (NS) packet (ICMP over IPv6). Args: ip_dst: NS ipv6 destination (Optional) eth_dst: Ethernet (layer 2) destination address (Optional) """ # Compute IP addresses target_ip6 = ip_dst if ip_dst is not None else self.dst_ipv6 ndst_ip = socket.inet_pton(socket.AF_INET6, target_ip6) nnode_mcast = scapy.in6_getnsma(ndst_ip) node_mcast = socket.inet_ntop(socket.AF_INET6, nnode_mcast) # Compute MAC addresses hw_dst = (eth_dst if eth_dst is not None else scapy.in6_getnsmac(nnode_mcast)) # Create IPv6 layer base = scapy.IPv6(dst=node_mcast, src=self.src_ipv6) neighbor_solicitation = scapy.ICMPv6ND_NS(tgt=target_ip6) src_ll_addr = scapy.ICMPv6NDOptSrcLLAddr(lladdr=self.src_mac) ip6 = base / neighbor_solicitation / src_ll_addr # Create Ethernet layer ethernet = scapy.Ether(src=self.src_mac, dst=hw_dst) self.packet = ethernet / ip6 return self.packet
def NS(srcaddr, tgtaddr, srcmac): solicited = inet_pton(AF_INET6, tgtaddr) last3bytes = tuple([ord(b) for b in solicited[-3:]]) solicited = "ff02::1:ff%02x:%02x%02x" % last3bytes packet = (scapy.IPv6(src=srcaddr, dst=solicited) / scapy.ICMPv6ND_NS(tgt=tgtaddr) / scapy.ICMPv6NDOptSrcLLAddr(lladdr=srcmac)) return ("ICMPv6 NS", packet)
def ExpectUnicastProbe(self, addr): version = 6 if ":" in addr else 4 if version == 6: expected = ( scapy.IPv6(src=self.MyLinkLocalAddress(self.netid), dst=addr) / scapy.ICMPv6ND_NS(tgt=addr) / scapy.ICMPv6NDOptSrcLLAddr( lladdr=self.MyMacAddress(self.netid))) self.ExpectPacketOn(self.netid, "Unicast probe", expected) else: raise NotImplementedError
def create_ns(dst_ip, dst_mac, src_ip=None, src_mac=None, tgt_ip=None): # Solicitation if src_ip is None: src_ip = mac2ipv6(router_mac) if src_mac is None: src_mac = router_mac if tgt_ip is None: tgt_ip = dst_ip ether_head = scapy.Ether(dst=dst_mac, src=src_mac) # With solicited node multicast ipv6_head = scapy.IPv6(src=src_ip, dst=make_sn_mc(dst_ip)) icmpv6_ns = scapy.ICMPv6ND_NS(tgt=tgt_ip) icmpv6_opt_pref = scapy.ICMPv6NDOptSrcLLAddr(lladdr=src_mac) sol = (ether_head / ipv6_head / icmpv6_ns / icmpv6_opt_pref) return sol
def ExpectProbe(self, is_unicast, addr): version = 6 if ":" in addr else 4 if version == 6: llsrc = self.MyMacAddress(self.netid) if is_unicast: src = self.MyLinkLocalAddress(self.netid) dst = addr else: solicited = inet_pton(AF_INET6, addr) last3bytes = tuple([ord(b) for b in solicited[-3:]]) dst = "ff02::1:ff%02x:%02x%02x" % last3bytes src = self.MyAddress(6, self.netid) expected = (scapy.IPv6(src=src, dst=dst) / scapy.ICMPv6ND_NS(tgt=addr) / scapy.ICMPv6NDOptSrcLLAddr(lladdr=llsrc)) msg = "%s probe" % ("Unicast" if is_unicast else "Multicast") self.ExpectPacketOn(self.netid, msg, expected) else: raise NotImplementedError