Пример #1
0
def phrx_ip4(self, ip4_packet_rx):
    """ Handle inbound IP packets """

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

    # Check if received packet has been sent to us directly or by unicast/broadcast, allow any destination if no unicast address is configured (for DHCP client)
    if self.stack_ip4_unicast and ip4_packet_rx.ip4_dst not in {*self.stack_ip4_unicast, *self.stack_ip4_multicast, *self.stack_ip4_broadcast}:
        self.logger.debug(f"{ip4_packet_rx.tracker} - IP packet not destined for this stack, droping")
        return

    # Validate IP header checksum
    if not ip4_packet_rx.validate_cksum():
        self.logger.debug(f"{ip4_packet_rx.tracker} - IP packet has invalid checksum, droping")
        return

    # Check if packet is a fragment, and if so process it accrdingly
    ip4_packet_rx = handle_ip4_fragmentation(ip4_packet_rx)
    if not ip4_packet_rx:
        return

    if ip4_packet_rx.ip4_proto == ps_ip4.IP4_PROTO_ICMP4 and ps_icmp4.preliminary_sanity_check(ip4_packet_rx.raw_data, ip4_packet_rx.tracker, self.logger):
        self.phrx_icmp4(ip4_packet_rx, ps_icmp4.Icmp4Packet(ip4_packet_rx))
        return

    if ip4_packet_rx.ip4_proto == ps_ip4.IP4_PROTO_UDP and ps_udp.preliminary_sanity_check(ip4_packet_rx.raw_data, ip4_packet_rx.tracker, self.logger):
        self.phrx_udp(ip4_packet_rx, ps_udp.UdpPacket(ip4_packet_rx))
        return

    if ip4_packet_rx.ip4_proto == ps_ip4.IP4_PROTO_TCP and ps_tcp.preliminary_sanity_check(ip4_packet_rx.raw_data, ip4_packet_rx.tracker, self.logger):
        self.phrx_tcp(ip4_packet_rx, ps_tcp.TcpPacket(ip4_packet_rx))
        return
Пример #2
0
def phrx_ip6(self, ip6_packet_rx):
    """ Handle inbound IP packets """

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

    # Check if received packet has been sent to us directly or by unicast or multicast
    if ip6_packet_rx.ip6_dst not in {
            *self.stack_ip6_unicast, *self.stack_ip6_multicast
    }:
        self.logger.debug(
            f"{ip6_packet_rx.tracker} - IP packet not destined for this stack, droping"
        )
        return

    if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_ICMP6 and ps_icmp6.preliminary_sanity_check(
            ip6_packet_rx.raw_data, ip6_packet_rx.tracker, self.logger):
        self.phrx_icmp6(ip6_packet_rx, ps_icmp6.Icmp6Packet(ip6_packet_rx))
        return

    if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_UDP and ps_udp.preliminary_sanity_check(
            ip6_packet_rx.raw_data, ip6_packet_rx.tracker, self.logger):
        self.phrx_udp(ip6_packet_rx, ps_udp.UdpPacket(ip6_packet_rx))
        return

    if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_TCP and ps_tcp.preliminary_sanity_check(
            ip6_packet_rx.raw_data, ip6_packet_rx.tracker, self.logger):
        self.phrx_tcp(ip6_packet_rx, ps_tcp.TcpPacket(ip6_packet_rx))
        return
Пример #3
0
def phrx_ip6(self, ip6_packet_rx):
    """ Handle inbound IP packets """

    # Validate IPv6 packet sanity
    if ip6_packet_rx.sanity_check_failed:
        return

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

    # Check if received packet has been sent to us directly or by unicast or multicast
    if ip6_packet_rx.ip6_dst not in {*self.ip6_unicast, *self.ip6_multicast}:
        self.logger.debug(
            f"{ip6_packet_rx.tracker} - IP packet not destined for this stack, droping"
        )
        return

    if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_ICMP6:
        self.phrx_icmp6(ip6_packet_rx, ps_icmp6.Icmp6Packet(ip6_packet_rx))
        return

    if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_UDP:
        self.phrx_udp(ip6_packet_rx, ps_udp.UdpPacket(ip6_packet_rx))
        return

    if ip6_packet_rx.ip6_next == ps_ip6.IP6_NEXT_HEADER_TCP:
        self.phrx_tcp(ip6_packet_rx, ps_tcp.TcpPacket(ip6_packet_rx))
        return
Пример #4
0
def phtx_udp(self, ip_src, ip_dst, udp_sport, udp_dport, raw_data=b"", echo_tracker=None):
    """ Handle outbound UDP packets """

    # Check if IPv4 protocol support is enabled, if not then silently drop the IPv4 packet
    if not stack.ipv4_support and ip_dst.version == 4:
        return

    # Check if IPv6 protocol support is enabled, if not then silently drop the IPv6 packet
    if not stack.ipv6_support and ip_dst.version == 6:
        return

    udp_packet_tx = ps_udp.UdpPacket(udp_sport=udp_sport, udp_dport=udp_dport, raw_data=raw_data, echo_tracker=echo_tracker)

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

    assert type(ip_src) in {IPv4Address, IPv6Address}
    assert type(ip_dst) in {IPv4Address, IPv6Address}

    if ip_src.version == 6 and ip_dst.version == 6:
        self.phtx_ipv6(ipv6_src=ip_src, ipv6_dst=ip_dst, child_packet=udp_packet_tx)

    if ip_src.version == 4 and ip_dst.version == 4:
        self.phtx_ipv4(ipv4_src=ip_src, ipv4_dst=ip_dst, child_packet=udp_packet_tx)
Пример #5
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)