from MessageType import MessageType import PayloadGenerator from scapy.layers.inet import IP, Ether, UDP, TCP from scapy.packet import Raw class Packet_Generator(): """docstring for Packet_Generator""" def __init__(self, protocol="udp"): super(Packet_Generator, 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, 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 tcpflags: the TCP flags of the TCP header :param payload: the payload of the packet :return: the corresponding packet """ #directly generate packet with the current protocol if(self.protocol == "udp"): packet = generate_udp_packet(ip_src=ip_src, ip_dst=ip_dst, mac_src=mac_src, mac_dst=mac_dst, 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, port_src=port_src, port_dst=port_dst, tcpflags=tcpflags, payload=payload) #else raise exception? 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", message_type: MessageType=MessageType.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 :param message_type: affects the size of the payload :param neighborlist_entries: Number of entries for the payload :return: the corresponding packet """ #Determine length of the payload that has to be generated if(message_type == MessageType.HELLO): payload_len = 0 elif(message_type == MessageType.HELLO_REPLY): payload_len = 22 elif(message_type == MessageType.NEIGHBORLIST_REQUEST): payload_len = 28 elif(message_type == MessageType.NEIGHBORLIST_REPLY): payload_len = 24 + 6 * neighborlist_entries else: payload_len = 0 #generate payload payload = PayloadGenerator.generate_Payload(payload_len) #generate the 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, 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, port_src=port_src, port_dst=port_dst, tcpflags=tcpflags, payload=payload) #else raise exception? 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", mac_dst:str="F4:2B:95:B3:0E:1A", port_src:int=1337, port_dst:int=6442, tcpflags:str="S", payload:str=""): """ Builds an TCP packet with the values specified by the caller. If a parameter was not specified by the caller, it is set to a default value. :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 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 IP packet """ ether = Ether(src=mac_src, dst=mac_dst) ip = IP(src=ip_src, dst=ip_dst) 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", 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. If a parameter was not specified by the caller, it is set to a default value. :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 UDP header :param port_dst: the destination port of the UDP header :param udp_len: length of the UDP packet, 8Byte header + payload :param udp_chksum: checksum of the UDP packet :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) udp = UDP(sport=port_src, dport=port_dst) packet = ether / ip / udp / Raw(load=payload) return packet