EternalBlueExploit.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import logging
  2. from random import randint, uniform
  3. from lea import Lea
  4. from Attack import BaseAttack
  5. from Attack.AttackParameters import Parameter as Param
  6. from Attack.AttackParameters import ParameterTypes
  7. logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
  8. # noinspection PyPep8
  9. from scapy.utils import RawPcapReader
  10. from scapy.layers.inet import IP, Ether, TCP, RandShort
  11. class EternalBlueExploit(BaseAttack.BaseAttack):
  12. def __init__(self, statistics, pcap_file_path):
  13. """
  14. Creates a new instance of the EternalBlue Exploit.
  15. :param statistics: A reference to the statistics class.
  16. """
  17. # Initialize attack
  18. super(EternalBlueExploit, self).__init__(statistics, "EternalBlue Exploit", "Injects an EternalBlue exploit'",
  19. "Resource Exhaustion")
  20. # Define allowed parameters and their type
  21. self.supported_params = {
  22. Param.MAC_SOURCE: ParameterTypes.TYPE_MAC_ADDRESS,
  23. Param.IP_SOURCE: ParameterTypes.TYPE_IP_ADDRESS,
  24. Param.PORT_SOURCE: ParameterTypes.TYPE_PORT,
  25. Param.MAC_DESTINATION: ParameterTypes.TYPE_MAC_ADDRESS,
  26. Param.IP_DESTINATION: ParameterTypes.TYPE_IP_ADDRESS,
  27. Param.INJECT_AT_TIMESTAMP: ParameterTypes.TYPE_FLOAT,
  28. Param.INJECT_AFTER_PACKET: ParameterTypes.TYPE_PACKET_POSITION,
  29. Param.PACKETS_PER_SECOND: ParameterTypes.TYPE_FLOAT
  30. }
  31. # PARAMETERS: initialize with default utilsvalues
  32. # (values are overwritten if user specifies them)
  33. most_used_ip_address = self.statistics.get_most_used_ip_address()
  34. if isinstance(most_used_ip_address, list):
  35. most_used_ip_address = most_used_ip_address[0]
  36. self.add_param_value(Param.IP_SOURCE, most_used_ip_address)
  37. self.add_param_value(Param.MAC_SOURCE, self.statistics.get_mac_address(most_used_ip_address))
  38. self.add_param_value(Param.INJECT_AFTER_PACKET, randint(0, self.statistics.get_packet_count()))
  39. self.add_param_value(Param.PORT_SOURCE, str(RandShort()))
  40. self.add_param_value(Param.PACKETS_PER_SECOND, randint(1, 64))
  41. # victim configuration
  42. # TO-DO: confirm that ip.dst uses Win OS
  43. random_ip_address = self.statistics.get_random_ip_address()
  44. self.add_param_value(Param.IP_DESTINATION, random_ip_address)
  45. destination_mac = self.statistics.get_mac_address(random_ip_address)
  46. if isinstance(destination_mac, list) and len(destination_mac) == 0:
  47. destination_mac = self.generate_random_mac_address()
  48. self.add_param_value(Param.MAC_DESTINATION, destination_mac)
  49. def generate_attack_pcap(self):
  50. def update_timestamp(timestamp, pps, maxdelay):
  51. """
  52. Calculates the next timestamp to be used based on the packet per second rate (pps) and the maximum delay.
  53. :return: Timestamp to be used for the next packet.
  54. """
  55. return timestamp + uniform(0.1 / pps, maxdelay)
  56. # Timestamp
  57. timestamp_next_pkt = self.get_param_value(Param.INJECT_AT_TIMESTAMP)
  58. # TO-DO: find better pkt rate
  59. pps = self.get_param_value(Param.PACKETS_PER_SECOND)
  60. randomdelay = Lea.fromValFreqsDict({1 / pps: 70, 2 / pps: 30, 5 / pps: 15, 10 / pps: 3})
  61. # Initialize parameters
  62. packets = []
  63. mac_source = self.get_param_value(Param.MAC_SOURCE)
  64. ip_source = self.get_param_value(Param.IP_SOURCE)
  65. port_source = self.get_param_value(Param.PORT_SOURCE)
  66. mac_destination = self.get_param_value(Param.MAC_DESTINATION)
  67. ip_destination = self.get_param_value(Param.IP_DESTINATION)
  68. path_attack_pcap = None
  69. # Scan (MS17) for EternalBlue
  70. # Read Win7_eternalblue_scan_vulnerable pcap file
  71. orig_ip_dst = None
  72. exploit_raw_packets = RawPcapReader("Win7_eternalblue_scan.pcap")
  73. for pkt_num, pkt in enumerate(exploit_raw_packets):
  74. eth_frame = Ether(pkt[0])
  75. ip_pkt = eth_frame.payload
  76. tcp_pkt = ip_pkt.payload
  77. smb_port = 445
  78. if pkt_num == 0:
  79. if tcp_pkt.getfieldval("dport") == smb_port:
  80. orig_ip_dst = ip_pkt.getfieldval("dst")
  81. # Request
  82. if ip_pkt.getfieldval("dst") == orig_ip_dst:
  83. # Ether
  84. eth_frame.setfieldval("src", mac_source)
  85. eth_frame.setfieldval("dst", mac_destination)
  86. # IP
  87. ip_pkt.setfieldval("src", ip_source)
  88. ip_pkt.setfieldval("dst", ip_destination)
  89. # TCP
  90. tcp_pkt.setfieldval("sport",port_source)
  91. # Reply
  92. else:
  93. # Ether
  94. eth_frame.setfieldval("src", mac_destination)
  95. eth_frame.setfieldval("dst", mac_source)
  96. # IP
  97. ip_pkt.setfieldval("src", ip_destination)
  98. ip_pkt.setfieldval("dst", ip_source)
  99. # TCP
  100. tcp_pkt.setfieldval("dport", port_source)
  101. new_pkt = (eth_frame / ip_pkt / tcp_pkt)
  102. # TO-DO: reply should have different timestamp delay
  103. new_pkt.time = timestamp_next_pkt
  104. packets.append(new_pkt)
  105. maxdelay = randomdelay.random()
  106. timestamp_next_pkt = update_timestamp(timestamp_next_pkt, pps, maxdelay)
  107. # Inject EternalBlue exploit packets
  108. # Read Win7_eternalblue_exploit pcap file
  109. exploit_raw_packets = RawPcapReader("Win7_eternalblue_exploit.pcap")
  110. for pkt_num, pkt in enumerate(exploit_raw_packets):
  111. eth_frame = Ether(pkt[0])
  112. ip_pkt = eth_frame.payload
  113. tcp_pkt = ip_pkt.payload
  114. smb_port = 445
  115. if pkt_num == 0:
  116. if tcp_pkt.getfieldval("dport") == smb_port:
  117. orig_ip_dst = ip_pkt.getfieldval("dst")
  118. # Request
  119. if ip_pkt.getfieldval("dst") == orig_ip_dst:
  120. # Ether
  121. eth_frame.setfieldval("src", mac_source)
  122. eth_frame.setfieldval("dst", mac_destination)
  123. # IP
  124. ip_pkt.setfieldval("src", ip_source)
  125. ip_pkt.setfieldval("dst", ip_destination)
  126. # TCP
  127. #tcp_pkt.setfieldval("sport", port_source)
  128. # Reply
  129. else:
  130. # Ether
  131. eth_frame.setfieldval("src", mac_destination)
  132. eth_frame.setfieldval("dst", mac_source)
  133. # IP
  134. ip_pkt.setfieldval("src", ip_destination)
  135. ip_pkt.setfieldval("dst", ip_source)
  136. # TCP
  137. #tcp_pkt.setfieldval("dport", port_source)
  138. new_pkt = (eth_frame / ip_pkt / tcp_pkt)
  139. # TO-DO: reply should have different timestamp delay
  140. new_pkt.time = timestamp_next_pkt
  141. packets.append(new_pkt)
  142. maxdelay = randomdelay.random()
  143. timestamp_next_pkt = update_timestamp(timestamp_next_pkt, pps, maxdelay)
  144. # Store timestamp of first packet (for attack label)
  145. self.attack_start_utime = packets[0].time
  146. self.attack_end_utime = packets[-1].time
  147. if len(packets) > 0:
  148. packets = sorted(packets, key=lambda pkt: pkt.time)
  149. path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
  150. # return packets sorted by packet time_sec_start
  151. # pkt_num+1: because pkt_num starts at 0
  152. return pkt_num + 1, path_attack_pcap