PacketGenerator.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from Attack.MembersMgmtCommAttack import MessageType
  2. import ID2TLib.PayloadGenerator as PayloadGenerator
  3. from scapy.layers.inet import IP, Ether, UDP, TCP
  4. from scapy.packet import Raw
  5. class PacketGenerator():
  6. """
  7. Creates packets, based on the set protocol
  8. """
  9. def __init__(self, protocol="udp"):
  10. """
  11. Creates a new Packet_Generator Object
  12. :param protocol: the protocol of the packets to be created, udp or tcp
  13. """
  14. super(PacketGenerator, self).__init__()
  15. self.protocol = protocol
  16. 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",
  17. mac_dst: str="F4:2B:95:B3:0E:1A", port_src: int=1337, port_dst: int=6442, ttl: int=64,
  18. tcpflags: str="S", payload: str=""):
  19. """
  20. Creates a Packet with the specified Values for the current protocol
  21. :param ip_src: the source IP address of the IP header
  22. :param ip_dst the destination IP address of the IP header
  23. :param mac_src: the source MAC address of the MAC header
  24. :param mac_dst: the destination MAC address of the MAC header
  25. :param port_src: the source port of the header
  26. :param port_dst: the destination port of the header
  27. :param ttl: the ttl Value of the packet
  28. :param tcpflags: the TCP flags of the TCP header
  29. :param payload: the payload of the packet
  30. :return: the corresponding packet
  31. """
  32. if(self.protocol == "udp"):
  33. packet = generate_udp_packet(ip_src=ip_src, ip_dst=ip_dst, mac_src=mac_src, mac_dst=mac_dst, ttl=ttl,
  34. port_src=port_src, port_dst=port_dst, payload=payload)
  35. elif(self.protocol == "tcp"):
  36. packet = generate_tcp_packet(ip_src=ip_src, ip_dst=ip_dst, mac_src=mac_src, mac_dst=mac_dst, ttl=ttl,
  37. port_src=port_src, port_dst=port_dst, tcpflags=tcpflags, payload=payload)
  38. return packet
  39. 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",
  40. mac_dst: str="F4:2B:95:B3:0E:1A", port_src: int=1337, port_dst: int=6442, tcpflags: str="S", ttl: int=64,
  41. message_type: MessageType=MessageType.SALITY_HELLO, neighborlist_entries: int=1):
  42. """
  43. Creates a Packet for Members-Management-Communication with the specified Values and the current protocol
  44. :param ip_src: the source IP address of the IP header
  45. :param ip_dst the destination IP address of the IP header
  46. :param mac_src: the source MAC address of the MAC header
  47. :param mac_dst: the destination MAC address of the MAC header
  48. :param port_src: the source port of the header
  49. :param port_dst: the destination port of the header
  50. :param tcpflags: the TCP flags of the TCP header, if tcp is selected as protocol
  51. :param ttl: the ttl Value of the packet
  52. :param message_type: affects the size of the payload
  53. :param neighborlist_entries: number of entries of a Neighbourlist-reply, affects the size of the payload
  54. :return: the corresponding packet
  55. """
  56. #Determine length of the payload that has to be generated
  57. if(message_type == MessageType.SALITY_HELLO):
  58. payload_len = 0
  59. elif(message_type == MessageType.SALITY_HELLO_REPLY):
  60. payload_len = 22
  61. elif(message_type == MessageType.SALITY_NL_REQUEST):
  62. payload_len = 28
  63. elif(message_type == MessageType.SALITY_NL_REPLY):
  64. payload_len = 24 + 6 * neighborlist_entries
  65. else:
  66. payload_len = 0
  67. payload = PayloadGenerator.generate_payload(payload_len)
  68. if(self.protocol == "udp"):
  69. packet = generate_udp_packet(ip_src=ip_src, ip_dst=ip_dst, mac_src=mac_src, mac_dst=mac_dst, ttl=ttl,
  70. port_src=port_src, port_dst=port_dst, payload=payload)
  71. elif(self.protocol == "tcp"):
  72. packet = generate_tcp_packet(ip_src=ip_src, ip_dst=ip_dst, mac_src=mac_src, mac_dst=mac_dst, ttl=ttl,
  73. port_src=port_src, port_dst=port_dst, tcpflags=tcpflags, payload=payload)
  74. else:
  75. print("Error: unsupported protocol for generating Packets")
  76. return packet
  77. 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,
  78. mac_dst:str="F4:2B:95:B3:0E:1A", port_src:int=1337, port_dst:int=6442, tcpflags:str="S", payload:str=""):
  79. """
  80. Builds a TCP packet with the values specified by the caller.
  81. :param ip_src: the source IP address of the IP header
  82. :param ip_dst the destination IP address of the IP header
  83. :param mac_src: the source MAC address of the MAC header
  84. :param ttl: the ttl value of the packet
  85. :param mac_dst: the destination MAC address of the MAC header
  86. :param port_src: the source port of the TCP header
  87. :param port_dst: the destination port of the TCP header
  88. :param tcpflags: the TCP flags of the TCP header
  89. :param payload: the payload of the packet
  90. :return: the corresponding TCP packet
  91. """
  92. ether = Ether(src=mac_src, dst=mac_dst)
  93. ip = IP(src=ip_src, dst=ip_dst, ttl=ttl)
  94. tcp = TCP(sport=port_src, dport=port_dst, flags=tcpflags)
  95. packet = ether / ip / tcp / Raw(load=payload)
  96. return packet
  97. 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,
  98. mac_dst:str="F4:2B:95:B3:0E:1A", port_src:int=1337, port_dst:int=6442, payload:str=""):
  99. """
  100. Builds an UDP packet with the values specified by the caller.
  101. :param ip_src: the source IP address of the IP header
  102. :param ip_dst the destination IP address of the IP header
  103. :param mac_src: the source MAC address of the MAC header
  104. :param ttl: the ttl value of the packet
  105. :param mac_dst: the destination MAC address of the MAC header
  106. :param port_src: the source port of the UDP header
  107. :param port_dst: the destination port of the UDP header
  108. :param payload: the payload of the packet
  109. :return: the corresponding UDP packet
  110. """
  111. ether = Ether(src=mac_src, dst=mac_dst)
  112. ip = IP(src=ip_src, dst=ip_dst, ttl=ttl)
  113. udp = UDP(sport=port_src, dport=port_dst)
  114. packet = ether / ip / udp / Raw(load=payload)
  115. return packet