JoomlaRegPrivExploit.py 11 KB

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