예제 #1
0
    def __management_frame_handler(self, wiwo_frame_buffer):
        """
        Method that process management frame from a WiWo Worker.
        """
        ethernet_frame = Ethernet(wiwo_frame_buffer)
        ether_dst_addr = ethernet_frame.get_ether_dhost().tostring()
        ether_src_addr = ethernet_frame.get_ether_shost().tostring()

        ether_type = ethernet_frame.get_ether_type()

        if ether_type != frames.WiwoFrame.ethertype:
            return

        wiwo_frame_buffer = wiwo_frame_buffer[ethernet_frame.get_header_size():]
        wiwo_frame = frames.WiwoFrame(wiwo_frame_buffer)
        ethernet_frame.contains(wiwo_frame)

        if not (ether_src_addr in self.__worker_dict.keys()):
            if wiwo_frame.get_type() == frames.WiwoAckFrame.frametype:  # We assume it's a ACK from an Announce
                worker = Worker(ether_src_addr)
                self.__worker_dict[ether_src_addr] = worker
                wiwo_req_info_frame_buffer = self.__create_wiwo_request_info_frame(ether_src_addr, ether_dst_addr)
                worker.send(wiwo_req_info_frame_buffer, self.__iface_name)
                self.__event_handler(WiwoEvent(ether_src_addr, WiwoEvent.WorkerAdded))
        else:
            if wiwo_frame.get_type() == frames.WiwoErrorFrame.frametype:
                worker = self.__worker_dict[ether_src_addr]
                worker.update_after_error()
                wiwo_error_frame_buffer = wiwo_frame_buffer[wiwo_frame.get_header_size():]
                we = frames.WiwoErrorFrame(wiwo_error_frame_buffer)
                self.__event_handler(WiwoEvent(ether_src_addr, WiwoEvent.Error, we.get_msg_as_string()))
            else:
                worker = self.__worker_dict[ether_src_addr]
                type_from_last_frame_sent = worker.get_type_from_last_frame_sent()
                worker.process_ctrl_and_mgnt_frame_received(ethernet_frame, self.__iface_name)

                if type_from_last_frame_sent == -1:
                    return
                elif type_from_last_frame_sent == frames.WiwoDataInjectFrame.frametype:
                    self.__event_handler(WiwoEvent(ether_src_addr, WiwoEvent.DataInjected))
                else:
                    self.__event_handler(WiwoEvent(ether_src_addr, WiwoEvent.WorkerUpdated))
예제 #2
0
파일: mysniff.py 프로젝트: cthtuf/smscrs
	def packetHandler(self, hdr, data):
		e = Ethernet(data)
		eoff = e.get_header_size()
		print "==============================================================="
		print "Eth:\n\thdrsize:%s\n\tsourceaddr:%s\n\tdestaddr:%s\n\tethtype:%s" % (eoff, num_from_barray(e.get_ether_shost()), num_from_barray(e.get_ether_dhost()), e.get_ether_type())
		if e.get_ether_type() == IP.ethertype:
			ipdata = data[eoff:]
			i = IP(ipdata)
			ioff = i.get_header_size()
			print "\tproto:IP\n\t\tipversion:%s\n\t\thdrsize:%s\n\t\ttos:%s\n\t\tipsize:%s\n\t\tid:%s\n\t\tdf:%s\n\t\tmf:%s\n\t\toffset:%s\n\t\tttl:%s\n\t\tproto:%s\n\t\tsum:%s\n\t\tsrc:%s\n\t\tdst:%s" % (i.get_ip_v(), i.get_header_size(), i.get_ip_tos(), i.get_ip_len(), i.get_ip_id(), i.get_ip_df(), i.get_ip_mf(), i.get_ip_off(), i.get_ip_ttl(), i.get_ip_p(), i.get_ip_sum(), num_from_barray(i.get_ip_src().split('.')), num_from_barray(i.get_ip_dst().split('.')))
			if i.get_ip_p() == UDP.protocol:
				udpdata = ipdata[ioff:]
				u = UDP(udpdata)
				print "\t\tproto:UDP\n\t\t\tsrcport:%s\n\t\t\tdstport:%s\n\t\t\tsize:%s\n\t\t\tcksum:%s" % (u.get_uh_sport(), u.get_uh_dport(), u.get_uh_ulen(), u.get_uh_sum())
			elif i.get_ip_p() == TCP.protocol:
				tcpdata = ipdata[ioff:]
				t = TCP(tcpdata)
				print "\t\tproto:TCP\n\t\t\tsrcport:%s\n\t\t\tdstport:%s\n\t\t\tseq:%s\n\t\t\tack:%s\n\t\t\tflags:%s\n\t\t\twinsize:%s\n\t\t\tcksum:%s\n\t\t\turg:%s\n\t\t\topts:%s" % (t.get_th_sport(), t.get_th_dport(), t.get_th_seq(), t.get_th_ack(), t.get_th_flags(), t.get_th_win(), t.get_th_sum(), t.get_URG(), '0')#t.get_options()
			elif i.get_ip_p() == ICMP.protocol:
				icmpdata = ipdata[ioff:]
				ic = ICMP(icmpdata)
				print "\t\tproto:ICMP\n\t\t\ttype:%s\n\t\t\tcode:%s\n\t\t\tcksum:%s\n\t\t\tid:%s\n\t\t\tseq:%s\n\t\t\tgwaddr:%s\n\t\t\tmask:%s" % (ic.get_icmp_type(), ic.get_icmp_code(), ic.get_icmp_cksum(), ic.get_icmp_id(), ic.get_icmp_seq(), ic.get_icmp_gwaddr(), ic.get_icmp_mask())
			else:
				print "\t\tunknown child protocol"
		elif e.get_ether_type() == ARP.ethertype:
			adata = data[eoff:]
			a = ARP(adata)
			print "\tproto:ARP\n\t\thrd:%s\n\t\tpro:%s\n\t\thlen:%s\n\t\tplen:%s\n\t\top:%s\n\t\tsha:%s\n\t\tspa:%s\n\t\ttha:%s\n\t\ttpa:%s" % (a.get_ar_hrd(), a.get_ar_pro(), a.get_ar_hln(), a.get_ar_pln(), a.get_ar_op(), num_from_barray(a.get_ar_sha()), num_from_barray(a.get_ar_spa()), num_from_barray(a.get_ar_tha()), num_from_barray(a.get_ar_tpa()))
		else:
			print "\tunknown child protocol"		
		print "==============================================================="
예제 #3
0
class TestEthernet(unittest.TestCase):
    def setUp(self):
        # Ethernet frame with a 802.1Q tag (TPID=0x8100, PCP=5, DEI=0, VID=3315)
        # and ethertype 0x0800 (IPv4)
        self.frame = b'\x54\xab\xa3\xb9\x38\x3d\xe2\xef\x8d\xc7\xa8\x5e\x81\x00\xac\xf3\x08\x00'
        self.eth = Ethernet(self.frame)

    def test_01(self):
        """Test Ethernet getters"""
        self.assertEqual(self.eth.get_packet(), self.frame)
        self.assertEqual(self.eth.get_header_size(), 18)
        self.assertEqual(self.eth.get_ether_type(), 0x0800)

        # Check source and destination MACs
        self.assertEqual(self.eth.get_ether_dhost(),
                         array('B', self.frame[0:6]))
        self.assertEqual(self.eth.get_ether_shost(),
                         array('B', self.frame[6:12]))

    def test_02(self):
        """Test Ethernet setters"""
        self.eth.set_ether_type(0x88cc)
        self.assertEqual(self.eth.get_ether_type(), 0x88cc)

        # Swap source and destination MACs
        dhost = self.eth.get_ether_dhost()
        shost = self.eth.get_ether_shost()
        self.eth.set_ether_dhost(shost)
        self.eth.set_ether_shost(dhost)
        self.assertEqual(self.eth.get_ether_dhost(),
                         array('B', self.frame[6:12]))
        self.assertEqual(self.eth.get_ether_shost(),
                         array('B', self.frame[0:6]))

    def test_03(self):
        """Test EthernetTag getters"""
        tag = self.eth.pop_tag()
        self.assertEqual(tag.get_buffer_as_string(), b'\x81\x00\xac\xf3')
        self.assertEqual(tag.get_tpid(), 0x8100)
        self.assertEqual(tag.get_pcp(), 5)
        self.assertEqual(tag.get_dei(), 0)
        self.assertEqual(tag.get_vid(), 3315)

    def test_04(self):
        """Test EthernetTag setters"""
        tag = self.eth.pop_tag()
        tag.set_tpid(0x88a8)
        tag.set_pcp(2)
        tag.set_dei(1)
        tag.set_vid(876)
        self.assertEqual(tag.get_buffer_as_string(), b'\x88\xa8\x53\x6c')

    def test_05(self):
        """Test manipulation with VLAN tags"""
        def check_tags(*tags):
            self.assertEqual(self.eth.tag_cnt, len(tags))
            self.assertEqual(self.eth.get_header_size(), 14 + 4 * len(tags))
            self.assertEqual(self.eth.get_ether_type(), 0x0800)
            for i, tag in enumerate(tags):
                self.assertEqual(
                    self.eth.get_tag(i).get_buffer_as_string(), tag)

        # Add S-tag (outer tag, closest to the Ethernet header)
        self.eth.push_tag(EthernetTag(0x88a85001))
        check_tags(b'\x88\xa8\x50\x01', b'\x81\x00\xac\xf3')

        # Set C-tag (inner tag, closest to the payload) to default
        self.eth.set_tag(1, EthernetTag())
        check_tags(b'\x88\xa8\x50\x01', b'\x81\x00\x00\x00')

        # Insert a deprecated 802.1QinQ header between S-tag and C-tag
        self.eth.push_tag(EthernetTag(0x910054d2), index=1)
        check_tags(b'\x88\xa8\x50\x01', b'\x91\x00\x54\xd2',
                   b'\x81\x00\x00\x00')

        # Test negative indices
        tags = {}
        for i in range(-3, 3):
            tags[i] = self.eth.get_tag(i).get_buffer_as_string()

        self.assertEqual(tags[-1], tags[2])
        self.assertEqual(tags[-2], tags[1])
        self.assertEqual(tags[-3], tags[0])

        # Accessing non-existent tags raises IndexError
        self.assertRaises(IndexError, self.eth.get_tag, 3)
        self.assertRaises(IndexError, self.eth.get_tag, -4)
        self.assertRaises(IndexError, self.eth.set_tag, 3, EthernetTag())
        self.assertRaises(IndexError, self.eth.set_tag, -4, EthernetTag())

        # Test Ethernet constructor
        data = self.eth.get_buffer_as_string()
        eth_copy = Ethernet(data)
        self.assertEqual(eth_copy.tag_cnt, 3)
        self.assertEqual(eth_copy.get_header_size(), 26)
        self.assertEqual(eth_copy.get_ether_type(), 0x0800)

        # Remove the deprecated 802.1QinQ header and check resulting frame
        eth_copy.pop_tag(1)
        self.assertEqual(eth_copy.tag_cnt, 2)
        self.assertEqual(eth_copy.get_packet(),
                         self.frame[:12] + tags[0] + tags[2] + self.frame[-2:])
예제 #4
0
class TestEthernet(unittest.TestCase):

    def setUp(self):
        # Ethernet frame with a 802.1Q tag (TPID=0x8100, PCP=5, DEI=0, VID=3315)
        # and ethertype 0x0800 (IPv4)
        self.frame = '\x54\xab\xa3\xb9\x38\x3d\xe2\xef\x8d\xc7\xa8\x5e\x81\x00\xac\xf3\x08\x00'
        self.eth = Ethernet(self.frame)

    def test_01(self):
        """Test Ethernet getters"""
        self.assertEqual(self.eth.get_packet(), self.frame)
        self.assertEqual(self.eth.get_header_size(), 18)
        self.assertEqual(self.eth.get_ether_type(), 0x0800)

        # Check source and destination MACs
        self.assertEqual(self.eth.get_ether_dhost(), array('B', self.frame[0:6]))
        self.assertEqual(self.eth.get_ether_shost(), array('B', self.frame[6:12]))

    def test_02(self):
        """Test Ethernet setters"""
        self.eth.set_ether_type(0x88cc)
        self.assertEqual(self.eth.get_ether_type(), 0x88cc)

        # Swap source and destination MACs
        dhost = self.eth.get_ether_dhost()
        shost = self.eth.get_ether_shost()
        self.eth.set_ether_dhost(shost)
        self.eth.set_ether_shost(dhost)
        self.assertEqual(self.eth.get_ether_dhost(), array('B', self.frame[6:12]))
        self.assertEqual(self.eth.get_ether_shost(), array('B', self.frame[0:6]))

    def test_03(self):
        """Test EthernetTag getters"""
        tag = self.eth.pop_tag()
        self.assertEqual(tag.get_buffer_as_string(), '\x81\x00\xac\xf3')
        self.assertEqual(tag.get_tpid(), 0x8100)
        self.assertEqual(tag.get_pcp(), 5)
        self.assertEqual(tag.get_dei(), 0)
        self.assertEqual(tag.get_vid(), 3315)

    def test_04(self):
        """Test EthernetTag setters"""
        tag = self.eth.pop_tag()
        tag.set_tpid(0x88a8)
        tag.set_pcp(2)
        tag.set_dei(1)
        tag.set_vid(876)
        self.assertEqual(tag.get_buffer_as_string(), '\x88\xa8\x53\x6c')

    def test_05(self):
        """Test manipulation with VLAN tags"""
        def check_tags(*tags):
            self.assertEqual(self.eth.tag_cnt, len(tags))
            self.assertEqual(self.eth.get_header_size(), 14 + 4*len(tags))
            self.assertEqual(self.eth.get_ether_type(), 0x0800)
            for i,tag in enumerate(tags):
                self.assertEqual(self.eth.get_tag(i).get_buffer_as_string(), tag)

        # Add S-tag (outer tag, closest to the Ethernet header)
        self.eth.push_tag(EthernetTag(0x88a85001))
        check_tags('\x88\xa8\x50\x01', '\x81\x00\xac\xf3')

        # Set C-tag (inner tag, closest to the payload) to default
        self.eth.set_tag(1, EthernetTag())
        check_tags('\x88\xa8\x50\x01', '\x81\x00\x00\x00')

        # Insert a deprecated 802.1QinQ header between S-tag and C-tag
        self.eth.push_tag(EthernetTag(0x910054d2), index=1)
        check_tags('\x88\xa8\x50\x01', '\x91\x00\x54\xd2', '\x81\x00\x00\x00')

        # Test negative indices
        tags = {}
        for i in range(-3,3):
            tags[i] = self.eth.get_tag(i).get_buffer_as_string()

        self.assertEqual(tags[-1], tags[2])
        self.assertEqual(tags[-2], tags[1])
        self.assertEqual(tags[-3], tags[0])

        # Accessing non-existent tags raises IndexError
        self.assertRaises(IndexError, self.eth.get_tag, 3)
        self.assertRaises(IndexError, self.eth.get_tag, -4)
        self.assertRaises(IndexError, self.eth.set_tag, 3, EthernetTag())
        self.assertRaises(IndexError, self.eth.set_tag, -4, EthernetTag())

        # Test Ethernet constructor
        data = self.eth.get_buffer_as_string()
        eth_copy = Ethernet(data)
        self.assertEqual(eth_copy.tag_cnt, 3)
        self.assertEqual(eth_copy.get_header_size(), 26)
        self.assertEqual(eth_copy.get_ether_type(), 0x0800)

        # Remove the deprecated 802.1QinQ header and check resulting frame
        eth_copy.pop_tag(1)
        self.assertEqual(eth_copy.tag_cnt, 2)
        self.assertEqual(eth_copy.get_packet(), self.frame[:12] + tags[0] + tags[2] + self.frame[-2:])