3
0
Prechádzať zdrojové kódy

Merge branch 'nmap_services_path_fix' of stefan.schmidt/ID2T-toolkit into master

Carlos Garcia 6 rokov pred
rodič
commit
0e838d296d

+ 1 - 1
code/Attack/BaseAttack.py

@@ -417,7 +417,7 @@ class BaseAttack(metaclass=abc.ABCMeta):
                 print('Error: Statistics-dependent attack parameter added without setting a statistics object first.')
                 exit(1)
 
-            ts = pr.pcap_processor(self.statistics.pcap_filepath, "False").get_timestamp_mu_sec(int(value))
+            ts = pr.pcap_processor(self.statistics.pcap_filepath, "False", Util.RESOURCE_DIR).get_timestamp_mu_sec(int(value))
             if 0 <= int(value) <= self.statistics.get_packet_count() and ts >= 0:
                 is_valid = True
                 param_name = atkParam.Parameter.INJECT_AT_TIMESTAMP

+ 1 - 1
code/Core/Statistics.py

@@ -59,7 +59,7 @@ class Statistics:
 
         # Recalculate statistics if database does not exist OR param -r/--recalculate is provided
         if (not self.stats_db.get_db_exists()) or flag_recalculate_stats or self.stats_db.get_db_outdated():
-            self.pcap_proc = pr.pcap_processor(self.pcap_filepath, str(self.do_extra_tests))
+            self.pcap_proc = pr.pcap_processor(self.pcap_filepath, str(self.do_extra_tests), Util.RESOURCE_DIR)
             self.pcap_proc.collect_statistics()
             self.pcap_proc.write_to_database(self.path_db)
             outstring_datasource = "by PCAP file processor."

+ 1 - 1
code/ID2TLib/PcapFile.py

@@ -21,7 +21,7 @@ class PcapFile(object):
         :param attack_pcap_path: The path to the PCAP file to merge with the PCAP at pcap_file_path
         :return: The file path of the resulting PCAP file
         """
-        pcap = pr.pcap_processor(self.pcap_file_path, "False")
+        pcap = pr.pcap_processor(self.pcap_file_path, "False", Util.RESOURCE_DIR)
         file_out_path = pcap.merge_pcaps(attack_pcap_path)
         return file_out_path
 

+ 3 - 3
code_boost/src/cxx/pcap_processor.cpp

@@ -9,12 +9,12 @@ using namespace Tins;
  * Creates a new pcap_processor object.
  * @param path The path where the PCAP to get analyzed is locatated.
  */
-pcap_processor::pcap_processor(std::string path, std::string extraTests) {
+pcap_processor::pcap_processor(std::string path, std::string extraTests, std::string resourcePath) : stats(resourcePath) {
     filePath = path;
     hasUnrecognized = false;
     if(extraTests == "True")
         stats.setDoExtraTests(true);
-    else stats.setDoExtraTests(false);;
+    else stats.setDoExtraTests(false);
 }
 
 /**
@@ -419,7 +419,7 @@ bool inline pcap_processor::file_exists(const std::string &filePath) {
  */
 PYBIND11_MODULE (libpcapreader, m) {
     py::class_<pcap_processor>(m, "pcap_processor")
-            .def(py::init<std::string, std::string>())
+            .def(py::init<std::string, std::string, std::string>())
             .def("merge_pcaps", &pcap_processor::merge_pcaps)
             .def("collect_statistics", &pcap_processor::collect_statistics)
             .def("get_timestamp_mu_sec", &pcap_processor::get_timestamp_mu_sec)

+ 1 - 1
code_boost/src/cxx/pcap_processor.h

@@ -23,7 +23,7 @@ public:
     /*
     * Class constructor
     */
-    pcap_processor(std::string path, std::string extraTests);
+    pcap_processor(std::string path, std::string extraTests, std::string resource_path);
 
     /*
      * Attributes

+ 3 - 2
code_boost/src/cxx/statistics.cpp

@@ -472,7 +472,8 @@ void statistics::incrementUnrecognizedPDUCount(const std::string &srcMac, const
 /**
  * Creates a new statistics object.
  */
-statistics::statistics(void) {
+statistics::statistics(std::string resourcePath) {
+    this->resourcePath = resourcePath;
 }
 
 /**
@@ -756,7 +757,7 @@ void statistics::writeToDatabase(std::string database_path) {
         float avgBandwidthOutInKBits = (sumBandwidthOut / senderCountIP) * 8;
 
         // Create database and write information
-        statistics_db db(database_path);
+        statistics_db db(database_path, resourcePath);
         db.writeStatisticsFile(packetCount, getCaptureDurationSeconds(),
                                getFormattedTimestamp(timestamp_firstPacket.seconds(), timestamp_firstPacket.microseconds()),
                                getFormattedTimestamp(timestamp_lastPacket.seconds(), timestamp_lastPacket.microseconds()),

+ 2 - 1
code_boost/src/cxx/statistics.h

@@ -515,7 +515,7 @@ public:
     /*
      * Constructor
      */
-    statistics();
+    statistics(std::string resourcePath);
 
     /*
      * Methods
@@ -618,6 +618,7 @@ private:
     Tins::Timestamp timestamp_lastPacket;
     float sumPacketSize = 0;
     int packetCount = 0;
+    std::string resourcePath;
 
     /* Extra tests includes:
      * - calculate IPs entropies for intervals

+ 4 - 61
code_boost/src/cxx/statistics_db.cpp

@@ -13,7 +13,7 @@ namespace py = pybind11;
  * a new database at database_path.
  * @param database_path The file path of the database.
  */
-statistics_db::statistics_db(std::string database_path) {
+statistics_db::statistics_db(std::string database_path, std::string resourcePath) {
     // Append file extension if not present
     if (database_path.find(".sqlite3") == database_path.npos) {
         database_path += ".sqlite3";
@@ -21,6 +21,8 @@ statistics_db::statistics_db(std::string database_path) {
     // creates the DB if not existing, opens the DB for read+write access
     db.reset(new SQLite::Database(database_path, SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE));
 
+    this->resourcePath = resourcePath;
+
     // Read ports and services into portServices vector
     readPortServicesFromNmap();
 }
@@ -637,7 +639,7 @@ void statistics_db::readPortServicesFromNmap()
     std::string portnumber;
     std::string service;
     std::string dump;
-    std::string nmapPath = getNmapPath();
+    std::string nmapPath = resourcePath + "nmap-services-tcp.csv";
     std::ifstream reader;
 
     reader.open(nmapPath, std::ios::in);
@@ -667,65 +669,6 @@ void statistics_db::readPortServicesFromNmap()
     }
 }
 
-/**
- * Gets the path to nmap-services-tcp.csv and makes sure the file is reached from any working directory within "/code"
- * because the working directory can be different when running tests. Checks if the file/path exists and warns the user.
- */
-std::string statistics_db::getNmapPath()
-{
-    //The different working directory paths according to how the database is built:
-    //<ID2T> stands for the directory id2t.sh is located in
-    //From tests(e.g. pycharm)  /<ID2T>/code/Test
-    //From run_tests.sh         /<ID2T>/code
-    //From id2t.sh              /<ID2T>
-    std::string filename = "nmap-services-tcp.csv";
-    std::string resourcesDir = "/resources/";
-    std::string codeDir = "/code";
-    std::string testDir = "/code/Test";
-    char buff[FILENAME_MAX];
-    // Working directory
-    std::string dir(getcwd(buff, FILENAME_MAX));
-
-    // Check if working directory is id2t.sh directory(try to reach file from working directory)
-    if(pathExists(dir + resourcesDir + filename))
-    {
-        return dir + resourcesDir + filename;
-    }
-
-    // If working directory is test directory(happens if tests are called from pycharm for example)
-    else if(dir.rfind(testDir) == (dir.size()-testDir.size()))
-    {
-        // Remove test directory from path
-        dir = dir.substr(0, (dir.size()-testDir.size()));
-    }
-
-    // If working directory is code directory(happens if tests are called with testscript)
-    else if(dir.rfind(codeDir) == (dir.size()-codeDir.size()))
-    {
-        // Remove code directory from path
-        dir = dir.substr(0, (dir.size()-codeDir.size()));
-    }
-
-    dir = dir + resourcesDir + filename;
-
-    return dir;
-}
-
-bool statistics_db::pathExists(std::string path)
-{
-    std::ifstream file;
-    file.open(path, std::ios::in);
-    if(file.is_open())
-    {
-        file.close();
-        return true;
-    }
-
-    else
-    {
-        return false;
-    }
-}
 /**
  * Writes the unrecognized PDUs into the database.
  * @param unrecognized_PDUs The unrecognized PDUs from class statistics.

+ 4 - 6
code_boost/src/cxx/statistics_db.h

@@ -17,12 +17,12 @@ public:
     /*
      * Constructor: Creates new database / Opens existing database
      */
-    statistics_db(std::string database_path);
+    statistics_db(std::string database_path, std::string resourcePath);
 
     /*
      * Database version: Increment number on every change in the C++ code!
      */
-    static const int DB_VERSION = 11;
+    static const int DB_VERSION = 12;
 
     /*
      * Methods for writing values into database
@@ -60,10 +60,6 @@ public:
 
     void readPortServicesFromNmap();
 
-    std::string getNmapPath();
-
-    bool pathExists(std::string path);
-
     void writeStatisticsUnrecognizedPDUs(const std::unordered_map<unrecognized_PDU, unrecognized_PDU_stat> &unrecognized_PDUs);
 
 
@@ -74,6 +70,8 @@ private:
     // Vector which contains all ports and their corresponding services
     std::unordered_map<int, std::string> portServices;
 
+    std::string resourcePath;
+
 };