JoomlaRegPrivExploit.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 JoomlaRegPrivExploit(BaseAttack.BaseAttack):
  12. template_attack_pcap_path = "resources/joomla_registration_privesc.pcap"
  13. # HTTP port
  14. http_port = 80
  15. # Metasploit experiments show this range of ports
  16. minDefaultPort = 30000
  17. maxDefaultPort = 50000
  18. def __init__(self, statistics, pcap_file_path):
  19. """
  20. Creates a new instance of the Joomla Registeration Privileges Escalation Exploit.
  21. :param statistics: A reference to the statistics class.
  22. """
  23. # Initialize attack
  24. super(JoomlaRegPrivExploit, self).__init__(statistics, "JoomlaRegPrivesc Exploit", "Injects an JoomlaRegPrivesc exploit'",
  25. "Resource Exhaustion")
  26. # Define allowed parameters and their type
  27. self.supported_params = {
  28. Param.MAC_SOURCE: ParameterTypes.TYPE_MAC_ADDRESS,
  29. Param.IP_SOURCE: ParameterTypes.TYPE_IP_ADDRESS,
  30. Param.MAC_DESTINATION: ParameterTypes.TYPE_MAC_ADDRESS,
  31. Param.IP_DESTINATION: ParameterTypes.TYPE_IP_ADDRESS,
  32. Param.TARGET_HOST: ParameterTypes.TYPE_DOMAIN,
  33. #Param.TARGET_URI: ParameterTypes.TYPE_URI,
  34. Param.INJECT_AT_TIMESTAMP: ParameterTypes.TYPE_FLOAT,
  35. Param.INJECT_AFTER_PACKET: ParameterTypes.TYPE_PACKET_POSITION,
  36. Param.PACKETS_PER_SECOND: ParameterTypes.TYPE_FLOAT
  37. }
  38. # PARAMETERS: initialize with default utilsvalues
  39. # (values are overwritten if user specifies them)
  40. most_used_ip_address = self.statistics.get_most_used_ip_address()
  41. if isinstance(most_used_ip_address, list):
  42. most_used_ip_address = most_used_ip_address[0]
  43. self.add_param_value(Param.IP_SOURCE, most_used_ip_address)
  44. self.add_param_value(Param.MAC_SOURCE, self.statistics.get_mac_address(most_used_ip_address))
  45. #self.add_param_value(Param.TARGET_URI, '/')
  46. self.add_param_value(Param.TARGET_HOST, "www.hackme.com")
  47. self.add_param_value(Param.INJECT_AFTER_PACKET, randint(0, self.statistics.get_packet_count()))
  48. self.add_param_value(Param.PACKETS_PER_SECOND,
  49. (self.statistics.get_pps_sent(most_used_ip_address) +
  50. self.statistics.get_pps_received(most_used_ip_address)) / 2)
  51. # victim configuration
  52. # consider that the destination has port 80 opened
  53. random_ip_address = self.statistics.get_random_ip_address()
  54. self.add_param_value(Param.IP_DESTINATION, random_ip_address)
  55. destination_mac = self.statistics.get_mac_address(random_ip_address)
  56. if isinstance(destination_mac, list) and len(destination_mac) == 0:
  57. destination_mac = self.generate_random_mac_address()
  58. self.add_param_value(Param.MAC_DESTINATION, destination_mac)
  59. def generate_attack_pcap(self):
  60. def update_timestamp(timestamp, pps):
  61. """
  62. Calculates the next timestamp to be used based on the packet per second rate (pps) and the maximum delay.
  63. :return: Timestamp to be used for the next packet.
  64. """
  65. # Calculate the request timestamp
  66. # A distribution to imitate the bursty behavior of traffic
  67. randomdelay = Lea.fromValFreqsDict({1 / pps: 70, 2 / pps: 20, 5 / pps: 7, 10 / pps: 3})
  68. return timestamp + uniform(1 / pps, randomdelay.random())
  69. # Aidmar
  70. def getIntervalPPS(complement_interval_pps, timestamp):
  71. """
  72. Gets the packet rate (pps) in specific time interval.
  73. :return: the corresponding packet rate for packet rate (pps) .
  74. """
  75. for row in complement_interval_pps:
  76. if timestamp <= row[0]:
  77. return row[1]
  78. return complement_interval_pps[-1][1] # in case the timstamp > capture max timestamp
  79. # Timestamp
  80. timestamp_next_pkt = self.get_param_value(Param.INJECT_AT_TIMESTAMP)
  81. pps = self.get_param_value(Param.PACKETS_PER_SECOND)
  82. # Calculate complement packet rates of BG traffic per interval
  83. complement_interval_pps = self.statistics.calculate_complement_packet_rates(pps)
  84. # Initialize parameters
  85. packets = []
  86. mac_source = self.get_param_value(Param.MAC_SOURCE)
  87. ip_source = self.get_param_value(Param.IP_SOURCE)
  88. mac_destination = self.get_param_value(Param.MAC_DESTINATION)
  89. ip_destination = self.get_param_value(Param.IP_DESTINATION)
  90. target_host = self.get_param_value(Param.TARGET_HOST)
  91. target_uri = "/" #self.get_param_value(Param.TARGET_URI)
  92. # Check ip.src == ip.dst
  93. self.ip_src_dst_equal_check(ip_source, ip_destination)
  94. path_attack_pcap = None
  95. # Set TTL based on TTL distribution of IP address
  96. source_ttl_dist = self.statistics.get_ttl_distribution(ip_source)
  97. if len(source_ttl_dist) > 0:
  98. source_ttl_prob_dict = Lea.fromValFreqsDict(source_ttl_dist)
  99. source_ttl_value = source_ttl_prob_dict.random()
  100. else:
  101. source_ttl_value = self.statistics.process_db_query("most_used(ttlValue)")
  102. destination_ttl_dist = self.statistics.get_ttl_distribution(ip_destination)
  103. if len(destination_ttl_dist) > 0:
  104. destination_ttl_prob_dict = Lea.fromValFreqsDict(destination_ttl_dist)
  105. destination_ttl_value = destination_ttl_prob_dict.random()
  106. else:
  107. destination_ttl_value = self.statistics.process_db_query("most_used(ttlValue)")
  108. # Inject Joomla_registration_privesc
  109. # Read joomla_registration_privesc pcap file
  110. orig_ip_dst = None
  111. exploit_raw_packets = RawPcapReader(self.template_attack_pcap_path)
  112. inter_arrival_time_dist = self.get_inter_arrival_time_dist(exploit_raw_packets)
  113. timeSteps = Lea.fromValFreqsDict(inter_arrival_time_dist)
  114. exploit_raw_packets = RawPcapReader(self.template_attack_pcap_path)
  115. port_source = randint(self.minDefaultPort,self.maxDefaultPort) # experiments show this range of ports
  116. # Random TCP sequence numbers
  117. global attacker_seq
  118. attacker_seq = randint(1000,50000)
  119. global victim_seq
  120. victim_seq = randint(1000,50000)
  121. for pkt_num, pkt in enumerate(exploit_raw_packets):
  122. eth_frame = Ether(pkt[0])
  123. ip_pkt = eth_frame.payload
  124. tcp_pkt = ip_pkt.payload
  125. str_tcp_seg = str(tcp_pkt.payload)
  126. # Clean payloads
  127. eth_frame.payload = b''
  128. ip_pkt.payload = b''
  129. tcp_pkt.payload = b''
  130. if pkt_num == 0:
  131. prev_orig_port_source = tcp_pkt.getfieldval("sport")
  132. if tcp_pkt.getfieldval("dport") == self.http_port:
  133. orig_ip_dst = ip_pkt.getfieldval("dst") # victim IP
  134. # Request: Attacker --> vicitm
  135. if ip_pkt.getfieldval("dst") == orig_ip_dst: # victim IP
  136. # There are 7 TCP connections with different source ports, for each of them we generate random port
  137. if tcp_pkt.getfieldval("sport") != prev_orig_port_source:
  138. port_source = randint(self.minDefaultPort, self.maxDefaultPort)
  139. prev_orig_port_source = tcp_pkt.getfieldval("sport")
  140. # New connection, new random TCP sequence numbers
  141. attacker_seq = randint(1000, 50000)
  142. victim_seq = randint(1000, 50000)
  143. # First packet in a connection has ACK = 0
  144. tcp_pkt.setfieldval("ack", 0)
  145. # Ether
  146. eth_frame.setfieldval("src", mac_source)
  147. eth_frame.setfieldval("dst", mac_destination)
  148. # IP
  149. ip_pkt.setfieldval("src", ip_source)
  150. ip_pkt.setfieldval("dst", ip_destination)
  151. ip_pkt.setfieldval("ttl", source_ttl_value)
  152. # TCP
  153. tcp_pkt.setfieldval("sport",port_source)
  154. if len(str_tcp_seg) > 0:
  155. # convert payload bytes to string => str = "b'..\\r\\n..'" additional characters are added in the string,
  156. # mainly backslashes to escape single quotes and whitespaces
  157. str_tcp_seg = str_tcp_seg[2:-1]
  158. str_tcp_seg = str_tcp_seg.replace('/joomla360', target_uri)
  159. str_tcp_seg = str_tcp_seg.replace(orig_ip_dst, target_host)
  160. str_tcp_seg = self.clean_white_spaces(str_tcp_seg)
  161. # TCP Seq, Ack
  162. if tcp_pkt.getfieldval("ack") != 0:
  163. tcp_pkt.setfieldval("ack", victim_seq)
  164. tcp_pkt.setfieldval("seq", attacker_seq)
  165. if not(tcp_pkt.getfieldval("flags") == 16 and len(str_tcp_seg) == 0): # flags=A:
  166. attacker_seq += max(len(str_tcp_seg),1)
  167. new_pkt = (eth_frame / ip_pkt/ tcp_pkt / str_tcp_seg)
  168. new_pkt.time = timestamp_next_pkt
  169. pps = max(getIntervalPPS(complement_interval_pps, timestamp_next_pkt), 10)
  170. timestamp_next_pkt = update_timestamp(timestamp_next_pkt, pps) + float(timeSteps.random())
  171. # Reply: Victim --> attacker
  172. else:
  173. # Ether
  174. eth_frame.setfieldval("src", mac_destination)
  175. eth_frame.setfieldval("dst", mac_source)
  176. # IP
  177. ip_pkt.setfieldval("src", ip_destination)
  178. ip_pkt.setfieldval("dst", ip_source)
  179. ip_pkt.setfieldval("ttl", destination_ttl_value)
  180. # TCP
  181. tcp_pkt.setfieldval("dport", port_source)
  182. if len(str_tcp_seg) > 0:
  183. # convert payload bytes to string => str = "b'..\\r\\n..'" additional characters are added in the string,
  184. # mainly backslashes to escape single quotes and whitespaces
  185. str_tcp_seg = str_tcp_seg[2:-1]
  186. str_tcp_seg = str_tcp_seg.replace('/joomla360', target_uri)
  187. str_tcp_seg = str_tcp_seg.replace(orig_ip_dst, target_host)
  188. str_tcp_seg = self.clean_white_spaces(str_tcp_seg)
  189. # TCP Seq, ACK
  190. tcp_pkt.setfieldval("ack", attacker_seq)
  191. tcp_pkt.setfieldval("seq", victim_seq)
  192. strLen = len(str_tcp_seg)
  193. if not(tcp_pkt.getfieldval("flags") == 16 and strLen == 0): # flags=A:
  194. victim_seq += max(strLen, 1)
  195. new_pkt = (eth_frame / ip_pkt / tcp_pkt / str_tcp_seg)
  196. pps = max(getIntervalPPS(complement_interval_pps, timestamp_next_pkt), 10)
  197. timestamp_next_pkt = update_timestamp(timestamp_next_pkt, pps) + float(timeSteps.random())
  198. new_pkt.time = timestamp_next_pkt
  199. packets.append(new_pkt)
  200. # Store timestamp of first packet (for attack label)
  201. self.attack_start_utime = packets[0].time
  202. self.attack_end_utime = packets[-1].time
  203. if len(packets) > 0:
  204. packets = sorted(packets, key=lambda pkt: pkt.time)
  205. path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
  206. # return packets sorted by packet time_sec_start
  207. # pkt_num+1: because pkt_num starts at 0
  208. return pkt_num + 1, path_attack_pcap