Prechádzať zdrojové kódy

refactor imports in Attack/SalityBotnet.py

refactor whitspaces in Attack/SalityBotnet.py
deep refactoring Attack/SalityBotnet.py
Jens Keim 6 rokov pred
rodič
commit
534b994f74
1 zmenil súbory, kde vykonal 34 pridanie a 34 odobranie
  1. 34 34
      code/Attack/SalityBotnet.py

+ 34 - 34
code/Attack/SalityBotnet.py

@@ -1,15 +1,15 @@
 import logging
 import logging
+import random as rnd
 
 
-from random import randint, choice
-from scapy.utils import RawPcapReader
-from scapy.layers.inet import Ether
+import scapy.layers.inet as inet
+import scapy.utils
 
 
-from Attack import BaseAttack
-from Attack.AttackParameters import Parameter as Param
-from Attack.AttackParameters import ParameterTypes
+import Attack.AttackParameters as atkParam
+import Attack.BaseAttack as BaseAttack
 import ID2TLib.Utility as Util
 import ID2TLib.Utility as Util
 
 
 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
+
 # noinspection PyPep8
 # noinspection PyPep8
 
 
 
 
@@ -19,19 +19,21 @@ class SalityBotnet(BaseAttack.BaseAttack):
     def __init__(self):
     def __init__(self):
         """
         """
         Creates a new instance of the Sality botnet.
         Creates a new instance of the Sality botnet.
-
         """
         """
         # Initialize attack
         # Initialize attack
         super(SalityBotnet, self).__init__("Sality Botnet", "Injects an Sality botnet'",
         super(SalityBotnet, self).__init__("Sality Botnet", "Injects an Sality botnet'",
-                                        "Botnet")
+                                           "Botnet")
+
+        self.pkt_num = 0
+        self.path_attack_pcap = None
 
 
         # Define allowed parameters and their type
         # Define allowed parameters and their type
         self.supported_params.update({
         self.supported_params.update({
-            Param.MAC_SOURCE: ParameterTypes.TYPE_MAC_ADDRESS,
-            Param.IP_SOURCE: ParameterTypes.TYPE_IP_ADDRESS,
-            Param.INJECT_AT_TIMESTAMP: ParameterTypes.TYPE_FLOAT,
-            Param.INJECT_AFTER_PACKET: ParameterTypes.TYPE_PACKET_POSITION,
-            Param.PACKETS_PER_SECOND: ParameterTypes.TYPE_FLOAT
+            atkParam.Parameter.MAC_SOURCE: atkParam.ParameterTypes.TYPE_MAC_ADDRESS,
+            atkParam.Parameter.IP_SOURCE: atkParam.ParameterTypes.TYPE_IP_ADDRESS,
+            atkParam.Parameter.INJECT_AT_TIMESTAMP: atkParam.ParameterTypes.TYPE_FLOAT,
+            atkParam.Parameter.INJECT_AFTER_PACKET: atkParam.ParameterTypes.TYPE_PACKET_POSITION,
+            atkParam.Parameter.PACKETS_PER_SECOND: atkParam.ParameterTypes.TYPE_FLOAT
         })
         })
 
 
     def init_params(self):
     def init_params(self):
@@ -39,39 +41,39 @@ class SalityBotnet(BaseAttack.BaseAttack):
         Initialize the parameters of this attack using the user supplied command line parameters.
         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
         Use the provided statistics to calculate default parameters and to process user
         supplied queries.
         supplied queries.
-
-        :param statistics: Reference to a statistics object.
         """
         """
         # PARAMETERS: initialize with default utilsvalues
         # PARAMETERS: initialize with default utilsvalues
         # (values are overwritten if user specifies them)
         # (values are overwritten if user specifies them)
         most_used_ip_address = self.statistics.get_most_used_ip_address()
         most_used_ip_address = self.statistics.get_most_used_ip_address()
 
 
-        self.add_param_value(Param.IP_SOURCE, most_used_ip_address)
-        self.add_param_value(Param.MAC_SOURCE, self.statistics.get_mac_address(most_used_ip_address))
+        self.add_param_value(atkParam.Parameter.IP_SOURCE, most_used_ip_address)
+        self.add_param_value(atkParam.Parameter.MAC_SOURCE, self.statistics.get_mac_address(most_used_ip_address))
 
 
         # Attack configuration
         # Attack configuration
-        self.add_param_value(Param.INJECT_AFTER_PACKET, randint(0, self.statistics.get_packet_count()))
-        self.add_param_value(Param.PACKETS_PER_SECOND,
+        self.add_param_value(atkParam.Parameter.INJECT_AFTER_PACKET, rnd.randint(0, self.statistics.get_packet_count()))
+        self.add_param_value(atkParam.Parameter.PACKETS_PER_SECOND,
                              (self.statistics.get_pps_sent(most_used_ip_address) +
                              (self.statistics.get_pps_sent(most_used_ip_address) +
                               self.statistics.get_pps_received(most_used_ip_address)) / 2)
                               self.statistics.get_pps_received(most_used_ip_address)) / 2)
 
 
     def generate_attack_packets(self):
     def generate_attack_packets(self):
 
 
         # Timestamp
         # Timestamp
-        timestamp_next_pkt = self.get_param_value(Param.INJECT_AT_TIMESTAMP)
+        timestamp_next_pkt = self.get_param_value(atkParam.Parameter.INJECT_AT_TIMESTAMP)
 
 
-        pps = self.get_param_value(Param.PACKETS_PER_SECOND)
+        pps = self.get_param_value(atkParam.Parameter.PACKETS_PER_SECOND)
 
 
         # Calculate complement packet rates of BG traffic per interval
         # Calculate complement packet rates of BG traffic per interval
         complement_interval_pps = self.statistics.calculate_complement_packet_rates(pps)
         complement_interval_pps = self.statistics.calculate_complement_packet_rates(pps)
 
 
         # Initialize parameters
         # Initialize parameters
         self.packets = []
         self.packets = []
-        mac_source = self.get_param_value(Param.MAC_SOURCE)
-        ip_source = self.get_param_value(Param.IP_SOURCE)
+        mac_source = self.get_param_value(atkParam.Parameter.MAC_SOURCE)
+        ip_source = self.get_param_value(atkParam.Parameter.IP_SOURCE)
 
 
         # Pick a DNS server from the background traffic
         # Pick a DNS server from the background traffic
-        ip_dns_server = self.statistics.process_db_query("SELECT ipAddress FROM ip_protocols WHERE protocolName='DNS' AND protocolCount=(SELECT MAX(protocolCount) FROM ip_protocols WHERE protocolName='DNS');")
+        ip_dns_server = self.statistics.process_db_query(
+            "SELECT ipAddress FROM ip_protocols WHERE protocolName='DNS' AND protocolCount=(SELECT MAX(protocolCount) "
+            "FROM ip_protocols WHERE protocolName='DNS');")
         ip_dns_server = Util.handle_most_used_outputs(ip_dns_server)
         ip_dns_server = Util.handle_most_used_outputs(ip_dns_server)
         if not ip_dns_server or ip_source == ip_dns_server:
         if not ip_dns_server or ip_source == ip_dns_server:
             ip_dns_server = self.statistics.get_random_ip_address()
             ip_dns_server = self.statistics.get_random_ip_address()
@@ -86,24 +88,22 @@ class SalityBotnet(BaseAttack.BaseAttack):
 
 
         ttl_map = {}
         ttl_map = {}
 
 
-        ip_map = {origin_ip_src : ip_source, origin_ip_dns_server: ip_dns_server}
-        mac_map = {origin_mac_src : mac_source, origin_mac_dns_server: mac_dns_server}
-
-        self.path_attack_pcap = None
+        ip_map = {origin_ip_src: ip_source, origin_ip_dns_server: ip_dns_server}
+        mac_map = {origin_mac_src: mac_source, origin_mac_dns_server: mac_dns_server}
 
 
         # Inject Sality botnet
         # Inject Sality botnet
         # Read sality_botnet pcap file
         # Read sality_botnet pcap file
-        exploit_raw_packets = RawPcapReader(self.template_attack_pcap_path)
+        exploit_raw_packets = scapy.utils.RawPcapReader(self.template_attack_pcap_path)
 
 
         for self.pkt_num, pkt in enumerate(exploit_raw_packets):
         for self.pkt_num, pkt in enumerate(exploit_raw_packets):
-            eth_frame = Ether(pkt[0])
+            eth_frame = inet.Ether(pkt[0])
             ip_pkt = eth_frame.payload
             ip_pkt = eth_frame.payload
 
 
             # Ether
             # Ether
             if eth_frame.getfieldval("src") in mac_map:
             if eth_frame.getfieldval("src") in mac_map:
                 eth_frame.setfieldval("src", mac_map[eth_frame.getfieldval("src")])
                 eth_frame.setfieldval("src", mac_map[eth_frame.getfieldval("src")])
             if eth_frame.getfieldval("dst") in mac_map:
             if eth_frame.getfieldval("dst") in mac_map:
-                    eth_frame.setfieldval("dst", mac_map[eth_frame.getfieldval("dst")])
+                eth_frame.setfieldval("dst", mac_map[eth_frame.getfieldval("dst")])
 
 
             # IP
             # IP
             if ip_pkt.getfieldval("src") in ip_map:
             if ip_pkt.getfieldval("src") in ip_map:
@@ -111,13 +111,13 @@ class SalityBotnet(BaseAttack.BaseAttack):
             if ip_pkt.getfieldval("dst") in ip_map:
             if ip_pkt.getfieldval("dst") in ip_map:
                 ip_pkt.setfieldval("dst", ip_map[ip_pkt.getfieldval("dst")])
                 ip_pkt.setfieldval("dst", ip_map[ip_pkt.getfieldval("dst")])
 
 
-            ## TTL
+            # TTL
             if ip_pkt.getfieldval("ttl") not in ttl_map:
             if ip_pkt.getfieldval("ttl") not in ttl_map:
                 source_ttl = self.statistics.get_most_used_ttl(ip_pkt.getfieldval("src"))
                 source_ttl = self.statistics.get_most_used_ttl(ip_pkt.getfieldval("src"))
                 if not source_ttl:
                 if not source_ttl:
                     source_ttl = self.statistics.process_db_query("SELECT ttlValue FROM ip_ttl;")
                     source_ttl = self.statistics.process_db_query("SELECT ttlValue FROM ip_ttl;")
                     if isinstance(source_ttl, list):
                     if isinstance(source_ttl, list):
-                        source_ttl = choice(source_ttl)
+                        source_ttl = rnd.choice(source_ttl)
                 ttl_map[ip_pkt.getfieldval("ttl")] = source_ttl
                 ttl_map[ip_pkt.getfieldval("ttl")] = source_ttl
             ip_pkt.setfieldval("ttl", ttl_map[ip_pkt.getfieldval("ttl")])
             ip_pkt.setfieldval("ttl", ttl_map[ip_pkt.getfieldval("ttl")])
 
 
@@ -142,4 +142,4 @@ class SalityBotnet(BaseAttack.BaseAttack):
 
 
         # return packets sorted by packet time_sec_start
         # return packets sorted by packet time_sec_start
         # pkt_num+1: because pkt_num starts at 0
         # pkt_num+1: because pkt_num starts at 0
-        return self.pkt_num + 1, self.path_attack_pcap
+        return self.pkt_num + 1, self.path_attack_pcap