SalityBotnet.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import logging
  2. import random as rnd
  3. import scapy.layers.inet as inet
  4. import scapy.utils
  5. import Attack.AttackParameters as atkParam
  6. import Attack.BaseAttack as BaseAttack
  7. import ID2TLib.Utility as Util
  8. logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
  9. # noinspection PyPep8
  10. class SalityBotnet(BaseAttack.BaseAttack):
  11. template_attack_pcap_path = Util.RESOURCE_DIR + "/../resources/sality_botnet.pcap"
  12. def __init__(self):
  13. """
  14. Creates a new instance of the Sality botnet.
  15. """
  16. # Initialize attack
  17. super(SalityBotnet, self).__init__("Sality Botnet", "Injects an Sality botnet'",
  18. "Botnet")
  19. self.pkt_num = 0
  20. self.path_attack_pcap = None
  21. # Define allowed parameters and their type
  22. self.supported_params.update({
  23. atkParam.Parameter.MAC_SOURCE: atkParam.ParameterTypes.TYPE_MAC_ADDRESS,
  24. atkParam.Parameter.IP_SOURCE: atkParam.ParameterTypes.TYPE_IP_ADDRESS,
  25. atkParam.Parameter.INJECT_AT_TIMESTAMP: atkParam.ParameterTypes.TYPE_FLOAT,
  26. atkParam.Parameter.INJECT_AFTER_PACKET: atkParam.ParameterTypes.TYPE_PACKET_POSITION,
  27. atkParam.Parameter.PACKETS_PER_SECOND: atkParam.ParameterTypes.TYPE_FLOAT
  28. })
  29. def init_params(self):
  30. """
  31. Initialize the parameters of this attack using the user supplied command line parameters.
  32. Use the provided statistics to calculate default parameters and to process user
  33. supplied queries.
  34. """
  35. # PARAMETERS: initialize with default utilsvalues
  36. # (values are overwritten if user specifies them)
  37. most_used_ip_address = self.statistics.get_most_used_ip_address()
  38. self.add_param_value(atkParam.Parameter.IP_SOURCE, most_used_ip_address)
  39. self.add_param_value(atkParam.Parameter.MAC_SOURCE, self.statistics.get_mac_address(most_used_ip_address))
  40. # Attack configuration
  41. self.add_param_value(atkParam.Parameter.INJECT_AFTER_PACKET, rnd.randint(0, self.statistics.get_packet_count()))
  42. self.add_param_value(atkParam.Parameter.PACKETS_PER_SECOND,
  43. (self.statistics.get_pps_sent(most_used_ip_address) +
  44. self.statistics.get_pps_received(most_used_ip_address)) / 2)
  45. def generate_attack_packets(self):
  46. # Timestamp
  47. timestamp_next_pkt = self.get_param_value(atkParam.Parameter.INJECT_AT_TIMESTAMP)
  48. pps = self.get_param_value(atkParam.Parameter.PACKETS_PER_SECOND)
  49. # Calculate complement packet rates of BG traffic per interval
  50. complement_interval_pps = self.statistics.calculate_complement_packet_rates(pps)
  51. # Initialize parameters
  52. self.packets = []
  53. mac_source = self.get_param_value(atkParam.Parameter.MAC_SOURCE)
  54. ip_source = self.get_param_value(atkParam.Parameter.IP_SOURCE)
  55. # Pick a DNS server from the background traffic
  56. ip_dns_server = self.statistics.process_db_query(
  57. "SELECT ipAddress FROM ip_protocols WHERE protocolName='DNS' AND protocolCount=(SELECT MAX(protocolCount) "
  58. "FROM ip_protocols WHERE protocolName='DNS');")
  59. ip_dns_server = Util.handle_most_used_outputs(ip_dns_server)
  60. if not ip_dns_server or ip_source == ip_dns_server:
  61. ip_dns_server = self.statistics.get_random_ip_address()
  62. mac_dns_server = self.statistics.get_mac_address(ip_dns_server)
  63. # Bot original config in the template PCAP
  64. origin_mac_src = "08:00:27:e5:d7:b0"
  65. origin_ip_src = "10.0.2.15"
  66. origin_mac_dns_server = "52:54:00:12:35:02"
  67. origin_ip_dns_server = "10.0.2.2"
  68. ttl_map = {}
  69. ip_map = {origin_ip_src: ip_source, origin_ip_dns_server: ip_dns_server}
  70. mac_map = {origin_mac_src: mac_source, origin_mac_dns_server: mac_dns_server}
  71. # Inject Sality botnet
  72. # Read sality_botnet pcap file
  73. exploit_raw_packets = scapy.utils.RawPcapReader(self.template_attack_pcap_path)
  74. for self.pkt_num, pkt in enumerate(exploit_raw_packets):
  75. eth_frame = inet.Ether(pkt[0])
  76. ip_pkt = eth_frame.payload
  77. # Ether
  78. if eth_frame.getfieldval("src") in mac_map:
  79. eth_frame.setfieldval("src", mac_map[eth_frame.getfieldval("src")])
  80. if eth_frame.getfieldval("dst") in mac_map:
  81. eth_frame.setfieldval("dst", mac_map[eth_frame.getfieldval("dst")])
  82. # IP
  83. if ip_pkt.getfieldval("src") in ip_map:
  84. ip_pkt.setfieldval("src", ip_map[ip_pkt.getfieldval("src")])
  85. if ip_pkt.getfieldval("dst") in ip_map:
  86. ip_pkt.setfieldval("dst", ip_map[ip_pkt.getfieldval("dst")])
  87. # TTL
  88. if ip_pkt.getfieldval("ttl") not in ttl_map:
  89. source_ttl = self.statistics.get_most_used_ttl(ip_pkt.getfieldval("src"))
  90. if not source_ttl:
  91. source_ttl = self.statistics.process_db_query("SELECT ttlValue FROM ip_ttl;")
  92. if isinstance(source_ttl, list):
  93. source_ttl = rnd.choice(source_ttl)
  94. ttl_map[ip_pkt.getfieldval("ttl")] = source_ttl
  95. ip_pkt.setfieldval("ttl", ttl_map[ip_pkt.getfieldval("ttl")])
  96. new_pkt = (eth_frame / ip_pkt)
  97. new_pkt.time = timestamp_next_pkt
  98. pps = max(Util.get_interval_pps(complement_interval_pps, timestamp_next_pkt), 10)
  99. timestamp_next_pkt = Util.update_timestamp(timestamp_next_pkt, pps)
  100. self.packets.append(new_pkt)
  101. exploit_raw_packets.close()
  102. def generate_attack_pcap(self):
  103. # Store timestamp of first packet (for attack label)
  104. self.attack_start_utime = self.packets[0].time
  105. self.attack_end_utime = self.packets[-1].time
  106. if len(self.packets) > 0:
  107. self.packets = sorted(self.packets, key=lambda pkt: pkt.time)
  108. self.path_attack_pcap = self.write_attack_pcap(self.packets, True, self.path_attack_pcap)
  109. # return packets sorted by packet time_sec_start
  110. # pkt_num+1: because pkt_num starts at 0
  111. return self.pkt_num + 1, self.path_attack_pcap