Browse Source

add ATTACK DURATION as an input parameter to DDoS attack

aidmar.wainakh 6 years ago
parent
commit
2efcfa4ec8
3 changed files with 28 additions and 29 deletions
  1. 4 0
      code/Attack/AttackParameters.py
  2. 3 2
      code/Attack/BaseAttack.py
  3. 21 27
      code/Attack/DDoSAttack.py

+ 4 - 0
code/Attack/AttackParameters.py

@@ -20,6 +20,10 @@ class Parameter(Enum):
     # recommended type: Integer positive -------------------------
     PACKETS_LIMIT = 'packets.limit'
     NUMBER_ATTACKERS = 'attackers.count'
+
+    # Aidmar
+    ATTACK_DURATION = "attack.duration" # in seconds
+
     # recommended type: Float ------------------------------------
     PACKETS_PER_SECOND = 'packets.per-second'  # packets per second
     INJECT_AT_TIMESTAMP = 'inject.at-timestamp'  # unix epoch time (seconds.millis) where attack should be injected

+ 3 - 2
code/Attack/BaseAttack.py

@@ -358,7 +358,7 @@ class BaseAttack(metaclass=ABCMeta):
         #def generate_address():
         #    return ipaddress.IPv4Address(random.randint(0, 2 ** 32 - 1))
         def generate_address(ipClass):
-            print(ipClass)
+            #print(ipClass)
             """if "private" in ipClass:
                 ipClassesByte1 = {"A-private": 10, "B-private": 172, "C-private": 192}
                 b1 = ipClassesByte1[ipClass]
@@ -375,8 +375,9 @@ class BaseAttack(metaclass=ABCMeta):
             if ipClass == "Unknown":
                 return ipaddress.IPv4Address(random.randint(0, 2 ** 32 - 1))
             else:
+                # For DDoS attack, we do not generate private IPs
                 if "private" in ipClass:
-                    ipClass = ipClass[0]
+                    ipClass = ipClass[0] # convert A-private to A
                 ipClassesByte1 = {"A": {1,126}, "B": {128,191}, "C":{192, 223}, "D":{224, 239}, "E":{240, 254}}
                 temp = list(ipClassesByte1[ipClass])
                 minB1 = temp[0]

+ 21 - 27
code/Attack/DDoSAttack.py

@@ -15,6 +15,10 @@ from collections import deque
 
 
 class DDoSAttack(BaseAttack.BaseAttack):
+    # Metasploit DoS default PPS
+    maxDefaultPPS = 1400
+    minDefaultPPS = 400
+
     def __init__(self, statistics, pcap_file_path):
         """
         Creates a new instance of the DDoS attack.
@@ -36,8 +40,11 @@ class DDoSAttack(BaseAttack.BaseAttack):
             Param.INJECT_AT_TIMESTAMP: ParameterTypes.TYPE_FLOAT,
             Param.INJECT_AFTER_PACKET: ParameterTypes.TYPE_PACKET_POSITION,
             Param.PACKETS_PER_SECOND: ParameterTypes.TYPE_FLOAT,
-            Param.PACKETS_LIMIT: ParameterTypes.TYPE_INTEGER_POSITIVE,
-            Param.NUMBER_ATTACKERS: ParameterTypes.TYPE_INTEGER_POSITIVE
+            # Aidmar - use attack duration instead
+            #Param.PACKETS_LIMIT: ParameterTypes.TYPE_INTEGER_POSITIVE,
+            Param.NUMBER_ATTACKERS: ParameterTypes.TYPE_INTEGER_POSITIVE,
+            # Aidmar
+            Param.ATTACK_DURATION: ParameterTypes.TYPE_INTEGER_POSITIVE
         }
 
         # PARAMETERS: initialize with default values
@@ -53,25 +60,10 @@ class DDoSAttack(BaseAttack.BaseAttack):
         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, randint(1, 64))
-
-        """
-         # Aidmar - PPS = avg packet rate per host = avgPacketsSentPerHost / captureDuration
-         max_pkts_sent_per_host = self.statistics.process_db_query(
-             "SELECT MAX(pktsSent) FROM ip_statistics;")
-         print("\nmax_pkts_sent_per_host: %f" % (max_pkts_sent_per_host))
-
-         capture_duration = self.statistics.process_db_query(
-             "SELECT captureDuration FROM file_statistics;")
-         max_pkt_rate_per_host = max_pkts_sent_per_host/float(capture_duration)
-         print("\nmax_pkt_rate_per_host: %f" % (max_pkt_rate_per_host))
-         #num_attackers = self.get_param_value(Param.NUMBER_ATTACKERS)
-         # the minumum PPS is the maximum packet rate per host * attackers number
-         min_pps = math.floor(max_pkt_rate_per_host * int(num_attackers))
-         print("\nMIN PPS: %f" % (min_pps))
-
-         self.add_param_value(Param.PACKETS_PER_SECOND, randint(min_pps, 64))
-         """
+        # Aidmar
+        #self.add_param_value(Param.PACKETS_PER_SECOND, randint(1, 64))
+        self.add_param_value(Param.PACKETS_PER_SECOND, randint(self.minDefaultPPS, self.maxDefaultPPS))
+        self.add_param_value(Param.ATTACK_DURATION, randint(5,30))
 
         # victim configuration
         random_ip_address = self.statistics.get_random_ip_address()
@@ -89,8 +81,8 @@ class DDoSAttack(BaseAttack.BaseAttack):
             port_destination = str(RandShort())
         self.add_param_value(Param.PORT_DESTINATION, port_destination)
         """
-
-        self.add_param_value(Param.PACKETS_LIMIT, randint(1000, 5000))
+        # Aidmar
+        #self.add_param_value(Param.PACKETS_LIMIT, randint(1000, 5000))
 
     def generate_attack_pcap(self):
         def update_timestamp(timestamp, pps, maxdelay):
@@ -204,7 +196,11 @@ class DDoSAttack(BaseAttack.BaseAttack):
         # Aidmar
         replies = []
 
-        for pkt_num in range(self.get_param_value(Param.PACKETS_LIMIT)):
+        # Aidmar
+        #for pkt_num in range(self.get_param_value(Param.PACKETS_LIMIT)):
+        attack_duration = self.get_param_value(Param.ATTACK_DURATION)
+        pkts_num = int(pps * attack_duration)
+        for pkt_num in range(pkts_num):
             # Build reply package
             # Select one IP address and its corresponding MAC address
             (ip_source, mac_source) = get_nth_random_element(ip_source_list, mac_source_list)
@@ -271,13 +267,11 @@ class DDoSAttack(BaseAttack.BaseAttack):
                 path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)
                 packets = []
 
-
             # Requests are sent all, send all replies
-            if pkt_num == self.get_param_value(Param.PACKETS_LIMIT)-1:
+            if pkt_num == pkts_num-1:
                 for reply in replies:
                     packets.append(reply)
 
-
         if len(packets) > 0:
             packets = sorted(packets, key=lambda pkt: pkt.time)
             path_attack_pcap = self.write_attack_pcap(packets, True, path_attack_pcap)