Просмотр исходного кода

Easier implementation of output parameter that doesn't require rebuild.
Also fix small bug of default inject after packet value selection.

dustin.born 7 лет назад
Родитель
Сommit
215f96c5b2

+ 1 - 1
code/Attack/MembersMgmtCommAttack.py

@@ -114,7 +114,7 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
 
         # PARAMETERS: initialize with default values
         # (values are overwritten if user specifies them)
-        self.add_param_value(Param.INJECT_AFTER_PACKET, randint(0, int(self.statistics.get_packet_count()/5)))
+        self.add_param_value(Param.INJECT_AFTER_PACKET, randint(1, int(self.statistics.get_packet_count()/5)))
         #self.add_param_value(Param.INJECT_AFTER_PACKET, 1)
 
         self.add_param_value(Param.PACKETS_PER_SECOND, 0)

+ 13 - 10
code/ID2TLib/Controller.py

@@ -14,19 +14,19 @@ class Controller:
         :param pcap_file_path:
         """
         # Fields
-        self.in_pcap_src_path = in_pcap_file_path.strip()
+        self.pcap_src_path = in_pcap_file_path.strip()
         if out_pcap_file_path:
-            self.out_pcap_src_path = out_pcap_file_path.strip()
+            self.pcap_out_path = out_pcap_file_path.strip()
         else:
-            self.out_pcap_src_path = None
+            self.pcap_out_path = None
         self.pcap_dest_path = ''
         self.written_pcaps = []
         self.do_extra_tests = do_extra_tests
 
         # Initialize class instances
-        print("Input file: %s" % self.in_pcap_src_path)
-        self.pcap_file = PcapFile(self.in_pcap_src_path)
-        self.label_manager = LabelManager(self.in_pcap_src_path)
+        print("Input file: %s" % self.pcap_src_path)
+        self.pcap_file = PcapFile(self.pcap_src_path)
+        self.label_manager = LabelManager(self.pcap_src_path)
         self.statistics = Statistics(self.pcap_file)
         self.statistics.do_extra_tests = self.do_extra_tests
         self.statisticsDB = self.statistics.get_statistics_database()
@@ -72,10 +72,13 @@ class Controller:
         # merge single attack pcap with all attacks into base pcap
         print("Merging base pcap with single attack pcap...", end=" ")
         sys.stdout.flush()  # force python to print text immediately
-        if self.out_pcap_src_path:
-            self.pcap_dest_path = self.pcap_file.merge_attack_into(attacks_pcap_path, self.out_pcap_src_path)
-        else:
-            self.pcap_dest_path = self.pcap_file.merge_attack(attacks_pcap_path)
+        self.pcap_dest_path = self.pcap_file.merge_attack(attacks_pcap_path)
+        if self.pcap_out_path:
+            if not self.pcap_out_path.endswith(".pcap"):
+                self.pcap_out_path += ".pcap"
+            os.rename(self.pcap_dest_path, self.pcap_out_path)
+            self.pcap_dest_path = self.pcap_out_path
+
         print("done.")
 
         # delete intermediate PCAP files

+ 0 - 12
code/ID2TLib/PcapFile.py

@@ -24,18 +24,6 @@ class PcapFile(object):
         file_out_path = pcap.merge_pcaps(attack_pcap_path)
         return file_out_path
 
-    def merge_attack_into(self, attack_pcap_path: str, merged_out_path: str):
-        """
-        Merges the loaded PCAP with the PCAP at attack_pcap_path and stores the result in merged_out_path.
-
-        :param attack_pcap_path: The path to the PCAP file to merge with the PCAP at pcap_file_path
-        :param merged_out_path: The path to the output PCAP file
-        :return: The file path of the resulting PCAP file
-        """
-        pcap = pr.pcap_processor(self.pcap_file_path, "False")
-        file_out_path = pcap.merge_pcaps_into(attack_pcap_path, merged_out_path)
-        return file_out_path
-
     def get_file_hash(self):
         """
         Returns the hash for the loaded PCAP file. The hash is calculated bsaed on:

+ 0 - 65
code_boost/src/cxx/pcap_processor.cpp

@@ -103,70 +103,6 @@ std::string pcap_processor::merge_pcaps(const std::string pcap_path) {
     return new_filepath;
 }
 
-/**
- * Merges two PCAP files, given by paths in filePath and parameter pcap_path.
- * @param pcap_path The path to the file which should be merged with the loaded PCAP file.
- * @param output_path: The path to the output file which the two PCAPs are merged into.
- * @return The string containing the file path to the merged PCAP file.
- */
-std::string pcap_processor::merge_pcaps_into(const std::string pcap_path, const std::string output_path) {
-    // If filename does not end with .pcap, add .pcap extension
-    std::string new_filepath = output_path;
-    const std::string &pcap_ext = ".pcap";
-    std::string::size_type h = new_filepath.rfind('.', new_filepath.length());
-    if (h != std::string::npos) {
-        if (new_filepath.compare(h, new_filepath.length() - h, pcap_ext) != 0) {
-            new_filepath.append(pcap_ext);
-        }
-    } else {
-        new_filepath.append(pcap_ext);
-    }
-
-    FileSniffer sniffer_base(filePath);
-    SnifferIterator iterator_base = sniffer_base.begin();
-
-    FileSniffer sniffer_attack(pcap_path);
-    SnifferIterator iterator_attack = sniffer_attack.begin();
-
-    PacketWriter writer(new_filepath, PacketWriter::ETH2);
-
-    bool all_attack_pkts_processed = false;
-    // Go through base PCAP and merge packets by timestamp
-    for (; iterator_base != sniffer_base.end();) {
-        auto tstmp_base = (iterator_base->timestamp().seconds()) + (iterator_base->timestamp().microseconds()*1e-6);
-        auto tstmp_attack = (iterator_attack->timestamp().seconds()) + (iterator_attack->timestamp().microseconds()*1e-6);
-        if (!all_attack_pkts_processed && tstmp_attack <= tstmp_base) {
-            try {
-                writer.write(*iterator_attack);
-            } catch (serialization_error) {
-                std::cout << std::setprecision(15) << "Could not serialize attack packet with timestamp " << tstmp_attack << std::endl;
-            }
-            iterator_attack++;
-            if (iterator_attack == sniffer_attack.end())
-                all_attack_pkts_processed = true;
-        } else {
-            try {
-                writer.write(*iterator_base);
-            } catch (serialization_error) {
-                    std::cout << "Could not serialize base packet with timestamp " << std::setprecision(15) << tstmp_attack << std::endl;
-            }
-            iterator_base++;
-        }
-    }    
-    
-    // This may happen if the base PCAP is smaller than the attack PCAP
-    // In this case append the remaining packets of the attack PCAP
-    for (; iterator_attack != sniffer_attack.end(); iterator_attack++) {
-        try {
-            writer.write(*iterator_attack);
-        } catch (serialization_error) {
-            auto tstmp_attack = (iterator_attack->timestamp().seconds()) + (iterator_attack->timestamp().microseconds()*1e-6);
-            std::cout << "Could not serialize attack packet with timestamp " << std::setprecision(15) << tstmp_attack << std::endl;
-        }
-    }
-    return new_filepath;
-}
-
 /**
  * Collect statistics of the loaded PCAP file. Calls for each packet the method process_packets.
  */
@@ -401,7 +337,6 @@ using namespace boost::python;
 BOOST_PYTHON_MODULE (libpcapreader) {
     class_<pcap_processor>("pcap_processor", init<std::string, std::string>())
             .def("merge_pcaps", &pcap_processor::merge_pcaps)
-            .def("merge_pcaps_into", &pcap_processor::merge_pcaps_into)
             .def("collect_statistics", &pcap_processor::collect_statistics)
             .def("get_timestamp_mu_sec", &pcap_processor::get_timestamp_mu_sec)
             .def("write_to_database", &pcap_processor::write_to_database);

+ 0 - 2
code_boost/src/cxx/pcap_processor.h

@@ -41,8 +41,6 @@ public:
 
     std::string merge_pcaps(const std::string pcap_path);
 
-    std::string merge_pcaps_into(const std::string pcap_path, const std::string output_path);
-
     void collect_statistics();
 
     void write_to_database(std::string database_path);