PacketGenerator.py 5.8 KB

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