def __thread_service(local_ip_address, local_port): """ Service initialization and rx/tx loop """ socket = udp_socket.UdpSocket() socket.bind(local_ip_address, local_port) print( f"Service UDP Echo: Socket created, bound to {local_ip_address}, port {local_port}" ) while True: packet_rx = socket.receive_from() print( f"Service UDP Echo: Received message from {packet_rx.remote_ip_address}, port {packet_rx.remote_port} -", packet_rx.raw_data) packet_tx = UdpMetadata( local_ip_address=packet_rx.local_ip_address, local_port=packet_rx.local_port, remote_ip_address=packet_rx.remote_ip_address, remote_port=packet_rx.remote_port, raw_data=packet_rx.raw_data, tracker=Tracker("TX", echo_tracker=packet_rx.tracker), ) socket.send_to(packet_tx) print( f"Service UDP Echo: Echo'ed message back to {packet_tx.remote_ip_address}, port {packet_tx.remote_port} -", packet_tx.raw_data)
def __send_dhcp_packet(dhcp_packet_tx): socket.send_to( UdpMetadata( local_ip_address=IPv4Address("0.0.0.0"), local_port=68, remote_ip_address=IPv4Address("255.255.255.255"), remote_port=67, raw_data=dhcp_packet_tx.get_raw_packet(), ))
def __thread_service(local_ip_address, local_port): """ Service initialization and rx/tx loop """ socket = udp_socket.UdpSocket() socket.bind(local_ip_address, local_port) print( f"Service UDP Echo: Socket created, bound to {local_ip_address}, port {local_port}" ) while True: packet_rx = socket.receive_from() message = packet_rx.raw_data print( f"Service UDP Echo: Received {len(message)} bytes from {packet_rx.remote_ip_address}, port {packet_rx.remote_port}" ) if "malpka" in str(message, "utf-8").strip().lower(): message = bytes(malpka, "utf-8") elif "malpa" in str(message, "utf-8").strip().lower(): message = bytes(malpa, "utf-8") elif "malpi" in str(message, "utf-8").strip().lower(): message = b"" for malpka_line, malpa_line in zip(malpka.split("\n"), malpa.split("\n")): message += bytes(malpka_line + malpa_line + "\n", "utf-8") packet_tx = UdpMetadata( local_ip_address=packet_rx.local_ip_address, local_port=packet_rx.local_port, remote_ip_address=packet_rx.remote_ip_address, remote_port=packet_rx.remote_port, raw_data=message, tracker=Tracker("TX", echo_tracker=packet_rx.tracker), ) socket.send_to(packet_tx) print( f"Service UDP Echo: Echo'ed {len(message)} bytes back to {packet_tx.remote_ip_address}, port {packet_tx.remote_port}" )
def __thread_service(local_ip_address, local_port): """ Service initialization and rx/tx loop """ socket = udp_socket.UdpSocket() socket.bind(local_ip_address, local_port) print( f"Service UDP Daytime: Socket created, bound to {local_ip_address}, port {local_port}" ) while True: packet_rx = socket.receive_from() packet_tx = UdpMetadata( local_ip_address=packet_rx.local_ip_address, local_port=packet_rx.local_port, remote_ip_address=packet_rx.remote_ip_address, remote_port=packet_rx.remote_port, raw_data=bytes(str(datetime.now()), "utf-8"), tracker=Tracker("TX", echo_tracker=packet_rx.tracker), ) socket.send_to(packet_tx) print( f"Service UDP Daytime: Sent daytime message to {packet_tx.remote_ip_address}, port {packet_tx.remote_port} -", packet_tx.raw_data)
def phrx_udp(self, ip_packet_rx, udp_packet_rx): """ Handle inbound UDP packets """ # Validate UDP packet sanity if udp_packet_rx.sanity_check_failed: return self.logger.opt(ansi=True).info(f"<green>{udp_packet_rx.tracker}</green> - {udp_packet_rx}") # Set universal names for src and dst IP addresses whether packet was delivered by IPv6 or IPv4 protocol ip_packet_rx.ip_dst = ip_packet_rx.ip6_dst if ip_packet_rx.protocol == "IPv6" else ip_packet_rx.ip4_dst ip_packet_rx.ip_src = ip_packet_rx.ip6_src if ip_packet_rx.protocol == "IPv6" else ip_packet_rx.ip4_src # Create UdpMetadata object and try to find matching UDP socket packet = UdpMetadata( local_ip_address=ip_packet_rx.ip_dst, local_port=udp_packet_rx.udp_dport, remote_ip_address=ip_packet_rx.ip_src, remote_port=udp_packet_rx.udp_sport, raw_data=udp_packet_rx.raw_data, tracker=udp_packet_rx.tracker, ) for socket_id in packet.socket_id_patterns: socket = stack.udp_sockets.get(socket_id, None) if socket: loguru.logger.bind(object_name="socket.").debug(f"{packet.tracker} - Found matching listening socket {socket_id}") socket.process_packet(packet) return # Silently drop packet if it has all zero source IP address if ip_packet_rx.ip_src in {IPv4Address("0.0.0.0"), IPv6Address("::")}: self.logger.debug( f"Received UDP packet from {ip_packet_rx.ip_src}, port {udp_packet_rx.udp_sport} to {ip_packet_rx.ip_dst}, port {udp_packet_rx.udp_dport}, droping" ) return # Respond with ICMPv4 Port Unreachable message if no matching socket has been found self.logger.debug(f"Received UDP packet from {ip_packet_rx.ip_src} to closed port {udp_packet_rx.udp_dport}, sending ICMPv4 Port Unreachable") if ip_packet_rx.protocol == "IPv6": self.phtx_icmp6( ip6_src=ip_packet_rx.ip6_dst, ip6_dst=ip_packet_rx.ip6_src, icmp6_type=ps_icmp6.ICMP6_UNREACHABLE, icmp6_code=ps_icmp6.ICMP6_UNREACHABLE__PORT, icmp6_un_raw_data=ip_packet_rx.get_raw_packet(), echo_tracker=udp_packet_rx.tracker, ) if ip_packet_rx.protocol == "IPv4": self.phtx_icmp4( ip4_src=ip_packet_rx.ip_dst, ip4_dst=ip_packet_rx.ip_src, icmp4_type=ps_icmp4.ICMP4_UNREACHABLE, icmp4_code=ps_icmp4.ICMP4_UNREACHABLE__PORT, icmp4_un_raw_data=ip_packet_rx.get_raw_packet(), echo_tracker=udp_packet_rx.tracker, ) return