SQLiAttack.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. # Created by Aidmar
  2. """
  3. ATutor 2.2.1 SQL Injection / Remote Code Execution
  4. This module exploits a SQL Injection vulnerability and an authentication weakness vulnerability in ATutor. This essentially
  5. means an attacker can bypass authentication and reach the administrator's interface where they can upload malicious code.
  6. more info:
  7. https://www.rapid7.com/db/modules/exploit/multi/http/atutor_sqli
  8. """
  9. import logging
  10. import math
  11. from operator import itemgetter
  12. import operator
  13. from random import randint, uniform
  14. from lea import Lea
  15. from Attack import BaseAttack
  16. from Attack.AttackParameters import Parameter as Param
  17. from Attack.AttackParameters import ParameterTypes
  18. logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
  19. # noinspection PyPep8
  20. from scapy.utils import RawPcapReader
  21. from scapy.layers.inet import IP, Ether, TCP, RandShort
  22. class SQLiAttack(BaseAttack.BaseAttack):
  23. template_attack_pcap_path = "resources/ATutorSQLi.pcap"
  24. # Metasploit default packet rate
  25. minDefaultPPS = 5
  26. # HTTP port
  27. http_port = 80
  28. # Metasploit experiments show this range of ports
  29. minDefaultPort = 30000
  30. maxDefaultPort = 50000
  31. def __init__(self, statistics, pcap_file_path):
  32. """
  33. Creates a new instance of the SQLi Attack.
  34. :param statistics: A reference to the statistics class.
  35. """
  36. # Initialize attack
  37. super(SQLiAttack, self).__init__(statistics, "SQLi Attack", "Injects a SQLi attack'",
  38. "Resource Exhaustion")
  39. # Define allowed parameters and their type
  40. self.supported_params = {
  41. Param.MAC_SOURCE: ParameterTypes.TYPE_MAC_ADDRESS,
  42. Param.IP_SOURCE: ParameterTypes.TYPE_IP_ADDRESS,
  43. Param.MAC_DESTINATION: ParameterTypes.TYPE_MAC_ADDRESS,
  44. Param.IP_DESTINATION: ParameterTypes.TYPE_IP_ADDRESS,
  45. Param.TARGET_HOST: ParameterTypes.TYPE_DOMAIN,
  46. #Param.TARGET_URI: ParameterTypes.TYPE_URI,
  47. Param.INJECT_AT_TIMESTAMP: ParameterTypes.TYPE_FLOAT,
  48. Param.INJECT_AFTER_PACKET: ParameterTypes.TYPE_PACKET_POSITION,
  49. Param.PACKETS_PER_SECOND: ParameterTypes.TYPE_FLOAT
  50. }
  51. # PARAMETERS: initialize with default utilsvalues
  52. # (values are overwritten if user specifies them)
  53. most_used_ip_address = self.statistics.get_most_used_ip_address()
  54. if isinstance(most_used_ip_address, list):
  55. most_used_ip_address = most_used_ip_address[0]
  56. self.add_param_value(Param.IP_SOURCE, most_used_ip_address)
  57. self.add_param_value(Param.MAC_SOURCE, self.statistics.get_mac_address(most_used_ip_address))
  58. #self.add_param_value(Param.TARGET_URI, "/")
  59. self.add_param_value(Param.TARGET_HOST, "www.hackme.com")
  60. self.add_param_value(Param.INJECT_AFTER_PACKET, randint(0, self.statistics.get_packet_count()))
  61. self.add_param_value(Param.PACKETS_PER_SECOND,
  62. (self.statistics.get_pps_sent(most_used_ip_address) +
  63. self.statistics.get_pps_received(most_used_ip_address)) / 2)
  64. # victim configuration
  65. # consider that the destination has port 80 opened
  66. random_ip_address = self.statistics.get_random_ip_address()
  67. self.add_param_value(Param.IP_DESTINATION, random_ip_address)
  68. destination_mac = self.statistics.get_mac_address(random_ip_address)
  69. if isinstance(destination_mac, list) and len(destination_mac) == 0:
  70. destination_mac = self.generate_random_mac_address()
  71. self.add_param_value(Param.MAC_DESTINATION, destination_mac)
  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. # Aidmar
  83. def getIntervalPPS(complement_interval_pps, timestamp):
  84. """
  85. Gets the packet rate (pps) in specific time interval.
  86. :return: the corresponding packet rate for packet rate (pps) .
  87. """
  88. for row in complement_interval_pps:
  89. if timestamp <= row[0]:
  90. return row[1]
  91. return complement_interval_pps[-1][1] # in case the timstamp > capture max timestamp
  92. # Timestamp
  93. timestamp_next_pkt = self.get_param_value(Param.INJECT_AT_TIMESTAMP)
  94. # TO-DO: find better pkt rate
  95. pps = self.get_param_value(Param.PACKETS_PER_SECOND)
  96. randomdelay = Lea.fromValFreqsDict({1 / pps: 70, 2 / pps: 30, 5 / pps: 15, 10 / pps: 3})
  97. # Aidmar - calculate complement packet rates of BG traffic per interval
  98. complement_interval_pps = self.statistics.calculate_complement_packet_rates(pps)
  99. # Initialize parameters
  100. packets = []
  101. mac_source = self.get_param_value(Param.MAC_SOURCE)
  102. ip_source = self.get_param_value(Param.IP_SOURCE)
  103. mac_destination = self.get_param_value(Param.MAC_DESTINATION)
  104. ip_destination = self.get_param_value(Param.IP_DESTINATION)
  105. target_host = self.get_param_value(Param.TARGET_HOST)
  106. target_uri = "/" #self.get_param_value(Param.TARGET_URI)
  107. # Check ip.src == ip.dst
  108. self.ip_src_dst_equal_check(ip_source, ip_destination)
  109. path_attack_pcap = None
  110. minDelay, maxDelay = self.get_reply_delay(ip_destination)
  111. # Set TTL based on TTL distribution of IP address
  112. source_ttl_dist = self.statistics.get_ttl_distribution(ip_source)
  113. if len(source_ttl_dist) > 0:
  114. source_ttl_prob_dict = Lea.fromValFreqsDict(source_ttl_dist)
  115. source_ttl_value = source_ttl_prob_dict.random()
  116. else:
  117. source_ttl_value = self.statistics.process_db_query("most_used(ttlValue)")
  118. destination_ttl_dist = self.statistics.get_ttl_distribution(ip_destination)
  119. if len(destination_ttl_dist) > 0:
  120. destination_ttl_prob_dict = Lea.fromValFreqsDict(destination_ttl_dist)
  121. destination_ttl_value = destination_ttl_prob_dict.random()
  122. else:
  123. destination_ttl_value = self.statistics.process_db_query("most_used(ttlValue)")
  124. # Inject SQLi Attack
  125. # Read SQLi Attack pcap file
  126. orig_ip_dst = None
  127. exploit_raw_packets = RawPcapReader(self.template_attack_pcap_path)
  128. port_source = randint(self.minDefaultPort,self.maxDefaultPort) # experiments show this range of ports
  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. orig_ip_dst = ip_pkt.getfieldval("dst") # victim IP
  146. if tcp_pkt.getfieldval("dport") == 80 or tcp_pkt.getfieldval("sport") == 80:
  147. # Attacker --> vicitm
  148. if ip_pkt.getfieldval("dst") == orig_ip_dst: # victim IP
  149. # There are 363 TCP connections with different source ports, for each of them we generate random port
  150. if tcp_pkt.getfieldval("sport") != prev_orig_port_source and tcp_pkt.getfieldval("dport") != 4444:
  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. if len(str_tcp_seg) > 0:
  168. # convert payload bytes to str => str = "b'..\\r\\n..'"
  169. str_tcp_seg = str_tcp_seg[2:-1]
  170. str_tcp_seg = str_tcp_seg.replace('/ATutor', target_uri)
  171. str_tcp_seg = str_tcp_seg.replace(orig_ip_dst, target_host)
  172. str_tcp_seg = self.clean_white_spaces(str_tcp_seg)
  173. # TCP Seq, Ack
  174. if tcp_pkt.getfieldval("ack") != 0:
  175. tcp_pkt.setfieldval("ack", victim_seq)
  176. tcp_pkt.setfieldval("seq", attacker_seq)
  177. if not (tcp_pkt.getfieldval("flags") == 16 and len(str_tcp_seg) == 0): # flags=A:
  178. attacker_seq += max(len(str_tcp_seg), 1)
  179. new_pkt = (eth_frame / ip_pkt/ tcp_pkt / str_tcp_seg)
  180. new_pkt.time = timestamp_next_pkt
  181. pps = max(getIntervalPPS(complement_interval_pps, timestamp_next_pkt), 10)
  182. timestamp_next_pkt = update_timestamp(timestamp_next_pkt, pps)
  183. # Victim --> attacker
  184. else:
  185. # Ether
  186. eth_frame.setfieldval("src", mac_destination)
  187. eth_frame.setfieldval("dst", mac_source)
  188. # IP
  189. ip_pkt.setfieldval("src", ip_destination)
  190. ip_pkt.setfieldval("dst", ip_source)
  191. ip_pkt.setfieldval("ttl", destination_ttl_value)
  192. # TCP
  193. tcp_pkt.setfieldval("dport", port_source)
  194. if len(str_tcp_seg) > 0:
  195. # convert payload bytes to str => str = "b'..\\r\\n..'"
  196. str_tcp_seg = str_tcp_seg[2:-1]
  197. str_tcp_seg = str_tcp_seg.replace('/ATutor', target_uri)
  198. str_tcp_seg = str_tcp_seg.replace(orig_ip_dst, target_host)
  199. str_tcp_seg = self.clean_white_spaces(str_tcp_seg)
  200. # TCP Seq, ACK
  201. tcp_pkt.setfieldval("ack", attacker_seq)
  202. tcp_pkt.setfieldval("seq", victim_seq)
  203. strLen = len(str_tcp_seg)
  204. if not (tcp_pkt.getfieldval("flags") == 16 and strLen == 0): # flags=A:
  205. victim_seq += max(strLen, 1)
  206. new_pkt = (eth_frame / ip_pkt / tcp_pkt / str_tcp_seg)
  207. timestamp_next_pkt = timestamp_next_pkt + uniform(minDelay, 2 * maxDelay)
  208. new_pkt.time = timestamp_next_pkt
  209. # The last connection
  210. else:
  211. # New connection, new random TCP sequence numbers
  212. attacker_seq = randint(1000, 50000)
  213. victim_seq = randint(1000, 50000)
  214. # First packet in a connection has ACK = 0
  215. tcp_pkt.setfieldval("ack", 0)
  216. #port_source = randint(self.minDefaultPort, self.maxDefaultPort)
  217. # Attacker --> vicitm
  218. if ip_pkt.getfieldval("dst") == orig_ip_dst: # victim IP
  219. # Ether
  220. eth_frame.setfieldval("src", mac_source)
  221. eth_frame.setfieldval("dst", mac_destination)
  222. # IP
  223. ip_pkt.setfieldval("src", ip_source)
  224. ip_pkt.setfieldval("dst", ip_destination)
  225. ip_pkt.setfieldval("ttl", source_ttl_value)
  226. # TCP
  227. #tcp_pkt.setfieldval("sport", port_source)
  228. if len(str_tcp_seg) > 0:
  229. # convert payload bytes to str => str = "b'..\\r\\n..'"
  230. str_tcp_seg = str_tcp_seg[2:-1]
  231. str_tcp_seg = str_tcp_seg.replace('/ATutor', target_uri)
  232. str_tcp_seg = str_tcp_seg.replace(orig_ip_dst, target_host)
  233. str_tcp_seg = self.clean_white_spaces(str_tcp_seg)
  234. # TCP Seq, Ack
  235. if tcp_pkt.getfieldval("ack") != 0:
  236. tcp_pkt.setfieldval("ack", victim_seq)
  237. tcp_pkt.setfieldval("seq", attacker_seq)
  238. if not (tcp_pkt.getfieldval("flags") == 16 and len(str_tcp_seg) == 0): # flags=A:
  239. attacker_seq += max(len(str_tcp_seg), 1)
  240. new_pkt = (eth_frame / ip_pkt / tcp_pkt / str_tcp_seg)
  241. new_pkt.time = timestamp_next_pkt
  242. pps = max(getIntervalPPS(complement_interval_pps, timestamp_next_pkt), 10)
  243. timestamp_next_pkt = update_timestamp(timestamp_next_pkt, pps)
  244. # Victim --> attacker
  245. else:
  246. # Ether
  247. eth_frame.setfieldval("src", mac_destination)
  248. eth_frame.setfieldval("dst", mac_source)
  249. # IP
  250. ip_pkt.setfieldval("src", ip_destination)
  251. ip_pkt.setfieldval("dst", ip_source)
  252. ip_pkt.setfieldval("ttl", destination_ttl_value)
  253. # TCP
  254. #tcp_pkt.setfieldval("dport", port_source)
  255. if len(str_tcp_seg) > 0:
  256. # convert payload bytes to str => str = "b'..\\r\\n..'"
  257. str_tcp_seg = str_tcp_seg[2:-1]
  258. str_tcp_seg = str_tcp_seg.replace('/ATutor', target_uri)
  259. str_tcp_seg = str_tcp_seg.replace(orig_ip_dst, target_host)
  260. str_tcp_seg = self.clean_white_spaces(str_tcp_seg)
  261. # TCP Seq, ACK
  262. tcp_pkt.setfieldval("ack", attacker_seq)
  263. tcp_pkt.setfieldval("seq", victim_seq)
  264. strLen = len(str_tcp_seg)
  265. if not (tcp_pkt.getfieldval("flags") == 16 and strLen == 0): # flags=A:
  266. victim_seq += max(strLen, 1)
  267. new_pkt = (eth_frame / ip_pkt / tcp_pkt / str_tcp_seg)
  268. timestamp_next_pkt = timestamp_next_pkt + uniform(minDelay, 2 * maxDelay)
  269. new_pkt.time = timestamp_next_pkt
  270. packets.append(new_pkt)
  271. # Store timestamp of first packet (for attack label)
  272. self.attack_start_utime = packets[0].time
  273. self.attack_end_utime = packets[-1].time
  274. if len(packets) > 0:
  275. packets = sorted(packets, key=lambda pkt: pkt.time)
  276. path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
  277. # return packets sorted by packet time_sec_start
  278. # pkt_num+1: because pkt_num starts at 0
  279. return pkt_num + 1, path_attack_pcap