123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- import logging
- from random import randint, uniform, choice
- from lea import Lea
- from scipy.stats import gamma
- from Attack import BaseAttack
- from Attack.AttackParameters import Parameter as Param
- from Attack.AttackParameters import ParameterTypes
- logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
- from scapy.layers.inet import IP, Ether, TCP, RandShort
- from collections import deque
- class DDoSAttack(BaseAttack.BaseAttack):
- def __init__(self):
- """
- Creates a new instance of the DDoS attack.
- """
-
- super(DDoSAttack, self).__init__("DDoS Attack", "Injects a DDoS attack'",
- "Resource Exhaustion")
-
- self.supported_params = {
- Param.IP_SOURCE: ParameterTypes.TYPE_IP_ADDRESS,
- Param.MAC_SOURCE: ParameterTypes.TYPE_MAC_ADDRESS,
- Param.PORT_SOURCE: ParameterTypes.TYPE_PORT,
- Param.IP_DESTINATION: ParameterTypes.TYPE_IP_ADDRESS,
- Param.MAC_DESTINATION: ParameterTypes.TYPE_MAC_ADDRESS,
- Param.PORT_DESTINATION: ParameterTypes.TYPE_PORT,
- Param.INJECT_AT_TIMESTAMP: ParameterTypes.TYPE_FLOAT,
- Param.INJECT_AFTER_PACKET: ParameterTypes.TYPE_PACKET_POSITION,
- Param.PACKETS_PER_SECOND: ParameterTypes.TYPE_FLOAT,
- Param.NUMBER_ATTACKERS: ParameterTypes.TYPE_INTEGER_POSITIVE,
- Param.ATTACK_DURATION: ParameterTypes.TYPE_INTEGER_POSITIVE,
- Param.VICTIM_BUFFER: ParameterTypes.TYPE_INTEGER_POSITIVE
- }
- def init_params(self):
- """
- Initialize the parameters of this attack using the user supplied command line parameters.
- Use the provided statistics to calculate default parameters and to process user
- supplied queries.
- :param statistics: Reference to a statistics object.
- """
-
-
- self.add_param_value(Param.INJECT_AFTER_PACKET, randint(0, self.statistics.get_packet_count()))
-
- num_attackers = randint(1, 16)
-
- most_used_ip_class = self.statistics.process_db_query("most_used(ipClass)")
- self.add_param_value(Param.IP_SOURCE, self.generate_random_ipv4_address(most_used_ip_class, num_attackers))
- self.add_param_value(Param.MAC_SOURCE, self.generate_random_mac_address(num_attackers))
- self.add_param_value(Param.PORT_SOURCE, str(RandShort()))
- self.add_param_value(Param.PACKETS_PER_SECOND, 0)
- self.add_param_value(Param.ATTACK_DURATION, randint(5,30))
-
- random_ip_address = self.statistics.get_random_ip_address()
- self.add_param_value(Param.IP_DESTINATION, random_ip_address)
- destination_mac = self.statistics.get_mac_address(random_ip_address)
- if isinstance(destination_mac, list) and len(destination_mac) == 0:
- destination_mac = self.generate_random_mac_address()
- self.add_param_value(Param.MAC_DESTINATION, destination_mac)
- self.add_param_value(Param.VICTIM_BUFFER, randint(1000,10000))
- def generate_attack_pcap(self):
- def update_timestamp(timestamp, pps, delay=0):
- """
- Calculates the next timestamp to be used based on the packet per second rate (pps) and the maximum delay.
- :return: Timestamp to be used for the next packet.
- """
- if delay == 0:
-
-
- randomdelay = Lea.fromValFreqsDict({1 / pps: 70, 2 / pps: 20, 5 / pps: 7, 10 / pps: 3})
- return timestamp + uniform(1 / pps, randomdelay.random())
- else:
-
- randomdelay = Lea.fromValFreqsDict({2 * delay: 70, 3 * delay: 20, 5 * delay: 7, 10 * delay: 3})
- return timestamp + uniform(1 / pps + delay, 1 / pps + randomdelay.random())
- def get_nth_random_element(*element_list):
- """
- Returns the n-th element of every list from an arbitrary number of given lists.
- For example, list1 contains IP addresses, list 2 contains MAC addresses. Use of this function ensures that
- the n-th IP address uses always the n-th MAC address.
- :param element_list: An arbitrary number of lists.
- :return: A tuple of the n-th element of every list.
- """
- range_max = min([len(x) for x in element_list])
- if range_max > 0: range_max -= 1
- n = randint(0, range_max)
- return tuple(x[n] for x in element_list)
- def index_increment(number: int, max: int):
- if number + 1 < max:
- return number + 1
- else:
- return 0
- def getIntervalPPS(complement_interval_pps, timestamp):
- """
- Gets the packet rate (pps) for a specific time interval.
- :param complement_interval_pps: an array of tuples (the last timestamp in the interval, the packet rate in the crresponding interval).
- :param timestamp: the timestamp at which the packet rate is required.
- :return: the corresponding packet rate (pps) .
- """
- for row in complement_interval_pps:
- if timestamp <= row[0]:
- return row[1]
-
- return complement_interval_pps[-1][1]
- def get_attacker_config(ipAddress: str):
- """
- Returns the attacker configuration depending on the IP address, this includes the port for the next
- attacking packet and the previously used (fixed) TTL value.
- :param ipAddress: The IP address of the attacker
- :return: A tuple consisting of (port, ttlValue)
- """
-
- port = attacker_port_mapping.get(ipAddress)
- if port is not None:
- next_port = attacker_port_mapping.get(ipAddress) + 1
- if next_port > (2 ** 16 - 1):
- next_port = 1
- else:
- next_port = RandShort()
- attacker_port_mapping[ipAddress] = next_port
-
- ttl = attacker_ttl_mapping.get(ipAddress)
- if ttl is None:
- is_invalid = True
- pos = ip_source_list.index(ipAddress)
- pos_max = len(gd)
- while is_invalid:
- ttl = int(round(gd[pos]))
- if 0 < ttl < 256:
- is_invalid = False
- else:
- pos = index_increment(pos, pos_max)
- attacker_ttl_mapping[ipAddress] = ttl
-
- return next_port, ttl
- BUFFER_SIZE = 1000
-
- num_attackers = self.get_param_value(Param.NUMBER_ATTACKERS)
- if num_attackers is not None:
-
- most_used_ip_class = self.statistics.process_db_query("most_used(ipClass)")
-
- ip_source_list = self.generate_random_ipv4_address(most_used_ip_class, num_attackers)
- mac_source_list = self.generate_random_mac_address(num_attackers)
- else:
-
-
- ip_source_list = self.get_param_value(Param.IP_SOURCE)
- mac_source_list = self.get_param_value(Param.MAC_SOURCE)
- num_attackers = len(ip_source_list)
-
- packets = deque(maxlen=BUFFER_SIZE)
- port_source_list = self.get_param_value(Param.PORT_SOURCE)
- mac_destination = self.get_param_value(Param.MAC_DESTINATION)
- ip_destination = self.get_param_value(Param.IP_DESTINATION)
- most_used_ip_address = self.statistics.get_most_used_ip_address()
- pps = self.get_param_value(Param.PACKETS_PER_SECOND)
- if pps == 0:
- result = self.statistics.process_db_query("SELECT MAX(maxPktRate) FROM ip_statistics WHERE ipAddress='"+ip_destination+"';")
- if result is not None and not 0:
- pps = num_attackers * result
- else:
- result = self.statistics.process_db_query("SELECT MAX(maxPktRate) FROM ip_statistics WHERE ipAddress='"+most_used_ip_address+"';")
- pps = num_attackers * result
-
- attacker_pps = pps / num_attackers
- complement_interval_attacker_pps = self.statistics.calculate_complement_packet_rates(attacker_pps)
-
- self.ip_src_dst_equal_check(ip_source_list, ip_destination)
- port_destination = self.get_param_value(Param.PORT_DESTINATION)
- if not port_destination:
- port_destination = self.statistics.process_db_query(
- "SELECT portNumber FROM ip_ports WHERE portDirection='in' AND ipAddress='" + ip_destination + "' ORDER BY portCount DESC LIMIT 1;")
- if not port_destination:
- port_destination = self.statistics.process_db_query(
- "SELECT portNumber FROM ip_ports WHERE portDirection='in' GROUP BY portNumber ORDER BY SUM(portCount) DESC LIMIT 1;")
- if not port_destination:
- port_destination = max(1, str(RandShort()))
- attacker_port_mapping = {}
- attacker_ttl_mapping = {}
-
- alpha, loc, beta = (2.3261710235, -0.188306914406, 44.4853123884)
- gd = gamma.rvs(alpha, loc=loc, scale=beta, size=len(ip_source_list))
- path_attack_pcap = None
- timestamp_prv_reply, timestamp_confirm = 0, 0
- minDelay, maxDelay = self.get_reply_delay(ip_destination)
- victim_buffer = self.get_param_value(Param.VICTIM_BUFFER)
- attack_duration = self.get_param_value(Param.ATTACK_DURATION)
- pkts_num = int(pps * attack_duration)
- source_win_sizes = self.statistics.process_db_query(
- "SELECT DISTINCT winSize FROM tcp_win ORDER BY RANDOM() LIMIT "+str(pkts_num)+";")
- destination_win_dist = self.statistics.get_win_distribution(ip_destination)
- if len(destination_win_dist) > 0:
- destination_win_prob_dict = Lea.fromValFreqsDict(destination_win_dist)
- destination_win_value = destination_win_prob_dict.random()
- else:
- destination_win_value = self.statistics.process_db_query("most_used(winSize)")
-
- mss_dst = self.statistics.get_most_used_mss(ip_destination)
- if mss_dst is None:
- mss_dst = self.statistics.process_db_query("most_used(mssValue)")
- replies_count = 0
- total_pkt_num = 0
-
- for attacker in range(num_attackers):
-
- timestamp_next_pkt = self.get_param_value(Param.INJECT_AT_TIMESTAMP)
- attack_ends_time = timestamp_next_pkt + attack_duration
- timestamp_next_pkt = update_timestamp(timestamp_next_pkt, attacker_pps)
- attacker_pkts_num = int(pkts_num / num_attackers) + randint(0,100)
- for pkt_num in range(attacker_pkts_num):
-
- if timestamp_next_pkt > attack_ends_time:
- break
-
-
- (ip_source, mac_source) = get_nth_random_element(ip_source_list, mac_source_list)
-
- (port_source, ttl_value) = get_attacker_config(ip_source)
- request_ether = Ether(dst=mac_destination, src=mac_source)
- request_ip = IP(src=ip_source, dst=ip_destination, ttl=ttl_value)
-
- source_win_size = choice(source_win_sizes)
- request_tcp = TCP(sport=port_source, dport=port_destination, flags='S', ack=0, window=source_win_size)
- request = (request_ether / request_ip / request_tcp)
- request.time = timestamp_next_pkt
-
- packets.append(request)
- total_pkt_num +=1
-
- if replies_count <= victim_buffer:
- reply_ether = Ether(src=mac_destination, dst=mac_source)
- reply_ip = IP(src=ip_destination, dst=ip_source, flags='DF')
- reply_tcp = TCP(sport=port_destination, dport=port_source, seq=0, ack=1, flags='SA', window=destination_win_value,options=[('MSS', mss_dst)])
- reply = (reply_ether / reply_ip / reply_tcp)
- timestamp_reply = update_timestamp(timestamp_next_pkt, attacker_pps, minDelay)
- while (timestamp_reply <= timestamp_prv_reply):
- timestamp_reply = update_timestamp(timestamp_prv_reply, attacker_pps, minDelay)
- timestamp_prv_reply = timestamp_reply
- reply.time = timestamp_reply
- packets.append(reply)
- replies_count+=1
- total_pkt_num += 1
- attacker_pps = max(getIntervalPPS(complement_interval_attacker_pps, timestamp_next_pkt), (pps/num_attackers)/2)
- timestamp_next_pkt = update_timestamp(timestamp_next_pkt, attacker_pps)
-
- if total_pkt_num <= 2 :
- self.attack_start_utime = packets[0].time
- elif pkt_num % BUFFER_SIZE == 0:
- last_packet = packets[-1]
- packets = sorted(packets, key=lambda pkt: pkt.time)
- path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
- packets = []
- if len(packets) > 0:
- packets = sorted(packets, key=lambda pkt: pkt.time)
- path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
-
- self.attack_end_utime = last_packet.time
-
-
- return total_pkt_num , path_attack_pcap
|