def test_ipv6_compare(self): """Test the underlying __compare__ functionality of the packet. Two packets constructed from the same bytes should be equal and two that are not should not be equal.""" file = PcapConnector("loopping6.out") packet = file.read() ip1 = ipv6(packet[file.dloff:len(packet)]) ip2 = ipv6(packet[file.dloff:len(packet)]) assert (ip1 != None) assert (ip2 != None) self.assertEqual(ip1, ip2, "packets should be equal but are not") ip1.dst = inet_pton(AF_INET6, "2001:ffff::1") self.assertNotEqual(ip1, ip2, "packets compare equal but should not")
def test_ipv6_compare(self): """Test the underlying __compare__ functionality of the packet. Two packets constructed from the same bytes should be equal and two that are not should not be equal.""" file = PcapConnector("loopping6.out") packet = file.read() ip1 = ipv6(packet[file.dloff:len(packet)]) ip2 = ipv6(packet[file.dloff:len(packet)]) assert (ip1 != None) assert (ip2 != None) self.assertEqual(ip1, ip2, "packets should be equal but are not") ip1.dst = inet_pton(AF_INET6, "2001:ffff::1"); self.assertNotEqual(ip1, ip2, "packets compare equal but should not")
def test_icmpv6_compare(self): """Test the underlying __compare__ functionality of the packet. Two packets constructed from the same bytes should be equal and two that are not should not be equal.""" file = PcapConnector("loopping6.out") packet = file.read() ip1 = ipv6(packet[file.dloff:len(packet)]) ip2 = ipv6(packet[file.dloff:len(packet)]) assert (ip1 != None) assert (ip2 != None) icmp1 = ip1.data icmp2 = ip2.data self.assertEqual(icmp1, icmp2, "packets should be equal but are not") icmp1.code = 32 self.assertNotEqual(icmp1, icmp2, "packets compare equal but should not")
def test_ipv6(self): # create one packet, copy its bytes, then compare their fields ip = ipv6() assert (ip != None) ip.traffic_class = 1 ip.flow = 0 ip.length = 64 ip.next_header = 6 ip.hop = 64 ip.src = inet_pton(AF_INET6, "::1") ip.dst = inet_pton(AF_INET6, "::1") # Create a packet to compare against ipnew = ipv6() ipnew.decode(ip.bytes) self.assertEqual(ip.bytes, ipnew.bytes, "bytes not equal") for field in ip._fieldnames: self.assertEqual(getattr(ip, field), getattr(ipnew, field), ("%s not equal" % field))
def test_icmpv6_ping(self): import os uname = os.uname()[0] if uname == "FreeBSD": devname = "edsc0" elif uname == "Linux": devname = "lo" elif uname == "Darwin": devname = "en0" else: print "unknown host os %s" % uname return e = ethernet() e.type = 0x0800 e.src = "\x00\x00\x00\x00\x00\x00" e.dst = "\xff\xff\xff\xff\xff\xff" e.type = 0x0800 ip = ipv6() ip.traffic_class = 1 ip.flow = 0 ip.length = 64 ip.next_header = IPV6_ICMP ip.hop = 64 ip.src = inet_pton(AF_INET6, "::1") ip.dst = inet_pton(AF_INET6, "::1") icmp = icmpv6() icmp.type = 128 icmp.code = 0 icmp.cksum = 0 ip.len = len(ip.bytes) + len(icmp.bytes) packet = Chain([e, ip, icmp]) packet.calc_checksums() packet.encode() input = PcapConnector(devname) input.setfilter("icmp6") output = PcapConnector(devname) assert (ip != None) # XXX The use of IP triggers a bpf header format bug if used # with loopback device on FreeBSD, so we use edsc(4) there. n_out = output.write(packet.bytes, 42) assert (n_out == 42) packet_in = input.read() assert (n_out == len(packet_in))
def executeTrigger(self, pkt): """ Execute the trigger action the pkt which satisfies the trigger's action conditions. @param pkt: The packet against which a trigger is actioned @return: A trigger Chain for transmission, None if action is internal """ retChain = None # Action based on trigger type if self.action == Trigger.Trigger.STOP: # Test if pkt is really a HTTP packet (p, i) = pkt.chain().find_first_of(pcs.packets.http.httpResponse) if not p: self.logger.debug("HTTP trigger.STOP:\n%s" % pkt.chain()) raise PcapReplayError(Trigger.ERR_PCAPR_TRIGGER_INVALID, 'executeTrigger : Invalid trigger/pkt ' + 'pair execution') # Create a terminating response chain for xmit by the caller ether = pkt e = ethernet() e.dst = ether.src e.src = ether.dst e.type = ether.type ip = pkt.data if type(ip) == pcs.packets.ipv4.ipv4: i = ipv4() i.tos = ip.tos i.protocol = ip.protocol i.id = 0 i.flags = 0 elif type(ip) == pcs.packets.ipv6.ipv6: i = ipv6() i.traffic_class = ip.traffic_class i.next_header = ip.next_header i.src = ip.dst i.dst = ip.src tcp = pkt.data.data t = pcs.packets.tcp.tcp() t.sport = tcp.dport t.dport = tcp.sport t.reset = 1 t.ack = 1 t.ack_number = tcp.ack_number t.sequence = tcp.sequence t.data = None retChain = pcs.Chain([e, i, t]) retChain.fixup() return retChain
def ICMPv6_pack_message(iface, pkt_type): #procedure to get iface address #iface = 'wlan2' #interface addrs = netifaces.ifaddresses(iface) ll_src_addr = addrs[netifaces.AF_INET6][1]['addr'] ll_src_addr = ll_src_addr[:ll_src_addr.find('%')] gl_src_addr = addrs[netifaces.AF_INET6][0]['addr'] #gl_src_addr = gl_src_addr[:gl_src_addr.find('%')] #print gl_src_addr #print ll_src_addr # building ethernet header e = ethernet() mac = netifaces.ifaddresses(iface)[netifaces.AF_LINK][0]['addr'] #geting mac addr e.src = ether_atob(mac) e.dst = ether_atob('ff:ff:ff:ff:ff:ff') e.type = 0x86dd #ETHERTYPE_IPV6 #building ipv6 packet basic info ip6 = ipv6() ip6.version = 6 #cs = icmp6.cksum(ip6) & 0xffff # building ipv6 header ip6.traffic_class = 0x0a ip6.flow = 0 ip6.length = 50 ip6.next_header = 58#IPPROTO_ICMPV6 ip6.hop = 255 ip6.src = pcs.inet_pton(AF_INET6, ll_src_addr) ip6.dst = pcs.inet_pton(AF_INET6, 'ff02::1') # building icmpv6 icmp6 = icmpv6(ICMP6_ECHO_REQUEST) #ICMP6_ECHO_REPLY icmp6.code = 0 icmp6.seq = 0 icmp6.id = 0x03e8 icmp6.checksum = 0x0 #kt.calc_checksums() data = payload(payload = gl_src_addr) #message data ip6.length = len(icmp6.getbytes()) + len(data) #recalculation of packet length if(pkt_type == 0): icmp6.cheksum = 0xf220 ip6.traffic_class = 0x0a pkt = pcs.Chain([e, ip6, icmp6, data]) #appendin packet else: icmp6.checksum = 0xe91b ip6.traffic_class = 0x0f pkt = pcs.Chain([e, ip6, icmp6]) #appendin packet #icmp6.checksum = pkt.calc_checksums() pkt.encode() #print pkt return pkt
def test_icmpv6_read(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) icmp = ip.data self.assertEqual(icmp.type, ICMP6_ECHO_REQUEST, "type not equal to %d" % icmp.type) self.assertEqual(icmp.code, 0, "code not equal to 0")
def test_ipv6_println(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) test_string = "<IPv6: version: 6, traffic_class: 0, flow: 0, length: 16, next_header: 58, hop: 64, src: \'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\', dst: \'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\'>" string = ip.println() self.assertEqual(string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def test_ipv6_print(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) test_string = "version 6\ntraffic_class 0\nflow 0\nlength 16\nnext_header 58\nhop 64\nsrc ::1\ndst ::1\n" string = ip.__str__() self.assertEqual(string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def createIpv6Pkt(self, ipproto=6, layer1=None, layer2=None): e = ethernet() e.dst = self.dmac e.src = self.smac e.type = 0x86dd i = ipv6() i.next_header = ipproto i.src = self.src6 i.dst = self.dst6 e.data = i i.data = layer1 layer1.data = layer2 if layer2 == None: retChain = pcs.Chain([e, i, layer1]).packets[0] else: retChain = pcs.Chain([e, i, layer1, layer2]).packets[0] return retChain
def test_ipv6_println(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) test_string = "<IPv6: version: 6, traffic_class: 0, flow: 0, length: 16, next_header: 58, hop: 64, src: \'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\', dst: \'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\'>" string = ip.println() self.assertEqual( string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def test_ipv6_print(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) test_string = "version 6\ntraffic_class 0\nflow 0\nlength 16\nnext_header 58\nhop 64\nsrc ::1\ndst ::1\n" string = ip.__str__() self.assertEqual( string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def test_icmpv6_print(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) icmp = ip.data #test_string = "ICMPv6\ntype 8\ncode 0\nchecksum 60550\n" test_string = "ICMPv6 Echo Request\ntype 128\ncode 0\nchecksum 60454\n" string = icmp.__str__() self.assertEqual(string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def test_ipv6_read(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) self.assertEqual(ip.version, 6, "version not equal %d" % ip.version) self.assertEqual(ip.traffic_class, 0, "traffic_class not equal %d" % ip.traffic_class) self.assertEqual(ip.flow, 0, "flow not equal %d" % ip.flow) self.assertEqual(ip.length, 16, "length not equal %d" % ip.length) self.assertEqual(ip.next_header, 58, "next_header not equal %d" % ip.next_header) self.assertEqual(ip.hop, 64, "hop not equal %d" % ip.hop) self.assertEqual(ip.src, inet_pton(AF_INET6, "::1"), "src not equal %s" % ip.src) self.assertEqual(ip.dst, inet_pton(AF_INET6, "::1"), "dst not equal %s" % ip.dst)
def test_icmpv6_print(self): """This test reads from a pre-stored pcap file generated with tcpdump and ping on the loopback interface and tests the __str__ method to make sure the correct values are printed.""" file = PcapConnector("loopping6.out") packet = file.read() ip = ipv6(packet[file.dloff:len(packet)]) assert (ip != None) icmp = ip.data #test_string = "ICMPv6\ntype 8\ncode 0\nchecksum 60550\n" test_string = "ICMPv6 Echo Request\ntype 128\ncode 0\nchecksum 60454\n" string = icmp.__str__() self.assertEqual( string, test_string, "strings are not equal \nexpected %s \ngot %s " % (test_string, string))
def main(): from optparse import OptionParser parser = OptionParser() parser.add_option("-c", "--count", dest="count", default=1, help="Stop after sending (and recieving) count ECHO_RESPONSE packets..") parser.add_option("-D", "--dont_fragment", dest="df", default=False, help="Set the Don't Fragment bit.") parser.add_option("-s", "--ip_source", dest="ip_source", default=None, help="The IP source address.") parser.add_option("-d", "--ip_dest", dest="ip_dest", default=None, help="The IP destination address.") parser.add_option("-I", "--ether_iface", dest="ether_iface", default=None, help="The name of the source interface.") parser.add_option("-e", "--ether_source", dest="ether_source", default=None, help="The host Ethernet source address.") parser.add_option("-g", "--ether_dest", dest="ether_dest", default=None, help="The gateway Ethernet destination address.") parser.add_option("-r", "--rt_hop", dest="hop", default="::", help="The intermediate router address.") (options, args) = parser.parse_args() rtcount = 1 # plen is 8 + rtcount * 16 for rt hdr, 8 for ICMP, 6 for foobar plen = 8 + rtcount * 16 + 8 + 6 c = ethernet(src=ether_atob(options.ether_source), \ dst=ether_atob(options.ether_dest)) / \ ipv6(hop=64, next_header = 43, length = plen, \ src=inet_pton(AF_INET6, options.ip_source), \ dst=inet_pton(AF_INET6, options.ip_dest)) / \ ipv6ext.rt_ext(next_header = 58, \ addr1 = inet_pton(AF_INET6, options.hop)) / \ icmpv6(type=ICMP6_ECHO_REQUEST, id=12345) / \ payload(payload="foobar") c.calc_lengths() # # Increment ICMP echo sequence number with each iteration. # output = PcapConnector(options.ether_iface) ip = c.packets[1] icmpecho = c.packets[3] count = int(options.count) while (count > 0): # c.calc_checksums() # icmpecho.cksum = icmpv6.cksum(icmpecho, ip) # icmpecho.calc_checksum() c.encode() out = output.write(c.bytes, len(c.bytes)) # packet = input.read() # print packet sleep(1) count -= 1 icmpecho.sequence += 1