コード例 #1
0
def phtx_arp(self,
             ether_src,
             ether_dst,
             arp_oper,
             arp_sha,
             arp_spa,
             arp_tha,
             arp_tpa,
             echo_tracker=None):
    """ Handle outbound ARP packets """

    # Check if IPv4 protocol support is enabled, if not then silently drop the packet
    if not stack.ip4_support:
        return

    arp_packet_tx = ps_arp.ArpPacket(
        arp_oper=arp_oper,
        arp_sha=arp_sha,
        arp_spa=arp_spa,
        arp_tha=arp_tha,
        arp_tpa=arp_tpa,
        echo_tracker=echo_tracker,
    )

    self.logger.opt(ansi=True).info(
        f"<magenta>{arp_packet_tx.tracker}</magenta> - {arp_packet_tx}")

    self.phtx_ether(ether_src=ether_src,
                    ether_dst=ether_dst,
                    child_packet=arp_packet_tx)
コード例 #2
0
def phrx_ether(self, ether_packet_rx):
    """ Handle inbound Ethernet packets """

    # Validate Ethernet packet sanity
    if ether_packet_rx.sanity_check_failed:
        return

    self.logger.debug(f"{ether_packet_rx.tracker} - {ether_packet_rx}")

    # Check if received packet matches any of stack MAC addresses
    if ether_packet_rx.ether_dst not in {
            self.mac_unicast, *self.mac_multicast, self.mac_broadcast
    }:
        self.logger.opt(ansi=True).debug(
            f"{ether_packet_rx.tracker} - Ethernet packet not destined for this stack, droping"
        )
        return

    if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_ARP and config.ip4_support:
        self.phrx_arp(ether_packet_rx, ps_arp.ArpPacket(ether_packet_rx))
        return

    if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_IP4 and config.ip4_support:
        self.phrx_ip4(ps_ip4.Ip4Packet(ether_packet_rx))
        return

    if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_IP6 and config.ip6_support:
        self.phrx_ip6(ps_ip6.Ip6Packet(ether_packet_rx))
        return
コード例 #3
0
ファイル: phrx_ether.py プロジェクト: dmohnani/PyTCP
def phrx_ether(self, ether_packet_rx):
    """ Handle inbound Ethernet packets """

    self.logger.debug(f"{ether_packet_rx.tracker} - {ether_packet_rx}")

    # Check if received packet uses valid Ethernet II format
    if ether_packet_rx.ether_type < ps_ether.ETHER_TYPE_MIN:
        self.logger.opt(ansi=True).debug(
            f"{ether_packet_rx.tracker} - Packet doesn't comply with the Ethernet II standard, droping"
        )
        return

    # Check if received packet matches any of stack MAC addresses
    if ether_packet_rx.ether_dst not in {
            *self.stack_mac_unicast, *self.stack_mac_multicast,
            *self.stack_mac_broadcast
    }:
        self.logger.opt(ansi=True).debug(
            f"{ether_packet_rx.tracker} - Ethernet packet not destined for this stack, droping"
        )
        return

    if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_ARP and stack.ipv4_support:
        self.phrx_arp(ether_packet_rx, ps_arp.ArpPacket(ether_packet_rx))
        return

    if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_IP4 and stack.ipv4_support:
        self.phrx_ipv4(ps_ipv4.Ip4Packet(ether_packet_rx))
        return

    if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_IP6 and stack.ipv6_support:
        self.phrx_ipv6(ps_ipv6.Ip6Packet(ether_packet_rx))
        return
コード例 #4
0
def main():
    while True:
        raw_packet_rx = raw_socket.recv(2048)
        ether_packet_rx = ps_ether.EtherPacket(raw_packet_rx)

        if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_ARP:
            arp_packet_rx = ps_arp.ArpPacket(ether_packet_rx)
            print("-" * 160)
            print(ether_packet_rx)
            print(arp_packet_rx)
            print("-" * 160)
            continue

        if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_IP6:
            ipv6_packet_rx = ps_ipv6.Ip6Packet(ether_packet_rx)

            if ipv6_packet_rx.ipv6_next == ps_ipv6.IP6_NEXT_HEADER_ICMP6:
                icmpv6_packet_rx = ps_icmpv6.Icmp6Packet(ipv6_packet_rx)
                print("-" * 160)
                print(ether_packet_rx)
                print(ipv6_packet_rx)
                print(icmpv6_packet_rx)
                print("-" * 160)
                continue

            print("-" * 160)
            print(ether_packet_rx)
            print(ipv6_packet_rx)
            continue

        if ether_packet_rx.ether_type == ps_ether.ETHER_TYPE_IP6:
            ipv4_packet_rx = ps_ipv4.Ip4Packet(ether_packet_rx)

            if ipv4_packet_rx.ipv4_proto == ps_ipv4.IP4_PROTO_ICMP4:
                icmpv4_packet_rx = ps_icmpv4.Icmp4Packet(ipv4_packet_rx)
                print("-" * 160)
                print(ether_packet_rx)
                print(ipv4_packet_rx)
                print(icmpv4_packet_rx)
                print("-" * 160)
                continue

            if ipv4_packet_rx.ipv4_proto == ps_ipv4.IP4_PROTO_UDP:
                udp_packet_rx = ps_udp.UdpPacket(ipv4_packet_rx)
                print("-" * 160)
                print(ether_packet_rx)
                print(ipv4_packet_rx)
                print(udp_packet_rx)
                print("-" * 160)
                continue

            if ipv4_packet_rx.ipv4_proto == ps_ipv4.IP4_PROTO_TCP:
                tcp_packet_rx = ps_tcp.TcpPacket(ipv4_packet_rx)
                if 22 in {tcp_packet_rx.tcp_dport, tcp_packet_rx.tcp_sport}:
                    continue
                print("-" * 160)
                print(ether_packet_rx)
                print(ipv4_packet_rx)
                print(tcp_packet_rx)
                print("-" * 160)
                continue

            print("-" * 160)
            print(ether_packet_rx)
            print(ipv4_packet_rx)
            print("-" * 160)
            continue

        print("-" * 160)
        print(ether_packet_rx)