from Attack.MembersMgmtCommAttack import MessageType import ID2TLib.PayloadGenerator as PayloadGenerator from scapy.layers.inet import IP, Ether, UDP, TCP from scapy.packet import Raw class PacketGenerator(): """ Creates packets, based on the set protocol """ def __init__(self, protocol="udp"): """ Creates a new Packet_Generator Object :param protocol: the protocol of the packets to be created, udp or tcp """ super(PacketGenerator, self).__init__() self.protocol = protocol def generate_packet(self, ip_src: str="192.168.64.32", ip_dst: str="192.168.64.48", mac_src: str="56:6D:D9:BC:70:1C", mac_dst: str="F4:2B:95:B3:0E:1A", port_src: int=1337, port_dst: int=6442, ttl: int=64, tcpflags: str="S", payload: str=""): """ Creates a Packet with the specified Values for the current protocol :param ip_src: the source IP address of the IP header :param ip_dst the destination IP address of the IP header :param mac_src: the source MAC address of the MAC header :param mac_dst: the destination MAC address of the MAC header :param port_src: the source port of the header :param port_dst: the destination port of the header :param ttl: the ttl Value of the packet :param tcpflags: the TCP flags of the TCP header :param payload: the payload of the packet :return: the corresponding packet """ if(self.protocol == "udp"): packet = generate_udp_packet(ip_src=ip_src, ip_dst=ip_dst, mac_src=mac_src, mac_dst=mac_dst, ttl=ttl, port_src=port_src, port_dst=port_dst, payload=payload) elif(self.protocol == "tcp"): packet = generate_tcp_packet(ip_src=ip_src, ip_dst=ip_dst, mac_src=mac_src, mac_dst=mac_dst, ttl=ttl, port_src=port_src, port_dst=port_dst, tcpflags=tcpflags, payload=payload) return packet def generate_mmcom_packet(self, ip_src: str="192.168.64.32", ip_dst: str="192.168.64.48", mac_src: str="56:6D:D9:BC:70:1C", mac_dst: str="F4:2B:95:B3:0E:1A", port_src: int=1337, port_dst: int=6442, tcpflags: str="S", ttl: int=64, message_type: MessageType=MessageType.SALITY_HELLO, neighborlist_entries: int=1): """ Creates a Packet for Members-Management-Communication with the specified Values and the current protocol :param ip_src: the source IP address of the IP header :param ip_dst the destination IP address of the IP header :param mac_src: the source MAC address of the MAC header :param mac_dst: the destination MAC address of the MAC header :param port_src: the source port of the header :param port_dst: the destination port of the header :param tcpflags: the TCP flags of the TCP header, if tcp is selected as protocol :param ttl: the ttl Value of the packet :param message_type: affects the size of the payload :param neighborlist_entries: number of entries of a Neighbourlist-reply, affects the size of the payload :return: the corresponding packet """ #Determine length of the payload that has to be generated if(message_type == MessageType.SALITY_HELLO): payload_len = 0 elif(message_type == MessageType.SALITY_HELLO_REPLY): payload_len = 22 elif(message_type == MessageType.SALITY_NL_REQUEST): payload_len = 28 elif(message_type == MessageType.SALITY_NL_REPLY): payload_len = 24 + 6 * neighborlist_entries else: payload_len = 0 payload = PayloadGenerator.generate_payload(payload_len) if(self.protocol == "udp"): packet = generate_udp_packet(ip_src=ip_src, ip_dst=ip_dst, mac_src=mac_src, mac_dst=mac_dst, ttl=ttl, port_src=port_src, port_dst=port_dst, payload=payload) elif(self.protocol == "tcp"): packet = generate_tcp_packet(ip_src=ip_src, ip_dst=ip_dst, mac_src=mac_src, mac_dst=mac_dst, ttl=ttl, port_src=port_src, port_dst=port_dst, tcpflags=tcpflags, payload=payload) else: print("Error: unsupported protocol for generating Packets") return packet def generate_tcp_packet(ip_src:str="192.168.64.32", ip_dst:str="192.168.64.48", mac_src:str="56:6D:D9:BC:70:1C", ttl: int=64, mac_dst:str="F4:2B:95:B3:0E:1A", port_src:int=1337, port_dst:int=6442, tcpflags:str="S", payload:str=""): """ Builds a TCP packet with the values specified by the caller. :param ip_src: the source IP address of the IP header :param ip_dst the destination IP address of the IP header :param mac_src: the source MAC address of the MAC header :param ttl: the ttl value of the packet :param mac_dst: the destination MAC address of the MAC header :param port_src: the source port of the TCP header :param port_dst: the destination port of the TCP header :param tcpflags: the TCP flags of the TCP header :param payload: the payload of the packet :return: the corresponding TCP packet """ ether = Ether(src=mac_src, dst=mac_dst) ip = IP(src=ip_src, dst=ip_dst, ttl=ttl) tcp = TCP(sport=port_src, dport=port_dst, flags=tcpflags) packet = ether / ip / tcp / Raw(load=payload) return packet def generate_udp_packet(ip_src:str="192.168.64.32", ip_dst:str="192.168.64.48", mac_src:str="56:6D:D9:BC:70:1C", ttl: int=64, mac_dst:str="F4:2B:95:B3:0E:1A", port_src:int=1337, port_dst:int=6442, payload:str=""): """ Builds an UDP packet with the values specified by the caller. :param ip_src: the source IP address of the IP header :param ip_dst the destination IP address of the IP header :param mac_src: the source MAC address of the MAC header :param ttl: the ttl value of the packet :param mac_dst: the destination MAC address of the MAC header :param port_src: the source port of the UDP header :param port_dst: the destination port of the UDP header :param payload: the payload of the packet :return: the corresponding UDP packet """ ether = Ether(src=mac_src, dst=mac_dst) ip = IP(src=ip_src, dst=ip_dst, ttl=ttl) udp = UDP(sport=port_src, dport=port_dst) packet = ether / ip / udp / Raw(load=payload) return packet