123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- #include "statistics_db.h"
- #include <iostream>
- #include <sstream>
- /**
- * Creates a new statistics_db object. Opens an existing database located at database_path. If not existing, creates
- * a new database at database_path.
- * @param database_path The file path of the database.
- */
- statistics_db::statistics_db(std::string database_path) {
- // Append file extension if not present
- if (database_path.find(".sqlite3") == database_path.npos) {
- database_path += ".sqlite3";
- }
- // 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));
- }
- /**
- * Writes the IP statistics into the database.
- * @param ipStatistics The IP statistics from class statistics.
- */
- void statistics_db::writeStatisticsIP(std::unordered_map<std::string, entry_ipStat> ipStatistics) {
- try {
- db->exec("DROP TABLE IF EXISTS ip_statistics");
- SQLite::Transaction transaction(*db);
- const char *createTable = "CREATE TABLE ip_statistics ( "
- "ipAddress TEXT, "
- "pktsReceived INTEGtimestampER, "
- "pktsSent INTEGER, "
- "kbytesReceived REAL, "
- "kbytesSent REAL, "
- "maxPktRate REAL,"
- "minPktRate REAL,"
- "ipClass TEXT, "
- "srcAnomalyScore REAL, "
- "dstAnomalyScore REAL, "
- "PRIMARY KEY(ipAddress));";
- db->exec(createTable);
- SQLite::Statement query(*db, "INSERT INTO ip_statistics VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
- for (auto it = ipStatistics.begin(); it != ipStatistics.end(); ++it) {
- entry_ipStat e = it->second;
- query.bind(1, it->first);
- query.bind(2, (int) e.pkts_received);
- query.bind(3, (int) e.pkts_sent);
- query.bind(4, e.kbytes_received);
- query.bind(5, e.kbytes_sent);
- // Aidmar
- query.bind(6, e.max_pkt_rate);
- query.bind(7, e.min_pkt_rate);
- query.bind(8, e.ip_class);
- query.bind(9, e.sourceAnomalyScore);
- query.bind(10, e.destinationAnomalyScore);
- query.exec();
- query.reset();
- }
- transaction.commit();
- }
- catch (std::exception &e) {
- std::cout << "Exception in statistics_db: " << e.what() << std::endl;
- }
- }
- /**
- * Writes the TTL distribution into the database.
- * @param ttlDistribution The TTL distribution from class statistics.
- */
- void statistics_db::writeStatisticsTTL(std::unordered_map<ipAddress_ttl, int> ttlDistribution) {
- try {
- db->exec("DROP TABLE IF EXISTS ip_ttl");
- SQLite::Transaction transaction(*db);
- const char *createTable = "CREATE TABLE ip_ttl ("
- "ipAddress TEXT,"
- "ttlValue INTEGER,"
- "ttlCount INTEGER,"
- "PRIMARY KEY(ipAddress,ttlValue));";
- db->exec(createTable);
- SQLite::Statement query(*db, "INSERT INTO ip_ttl VALUES (?, ?, ?)");
- for (auto it = ttlDistribution.begin(); it != ttlDistribution.end(); ++it) {
- ipAddress_ttl e = it->first;
- query.bind(1, e.ipAddress);
- query.bind(2, e.ttlValue);
- query.bind(3, it->second);
- query.exec();
- query.reset();
- }
- transaction.commit();
- }
- catch (std::exception &e) {
- std::cout << "Exception in statistics_db: " << e.what() << std::endl;
- }
- }
- /**
- * Writes the protocol distribution into the database.
- * @param protocolDistribution The protocol distribution from class statistics.
- */
- void statistics_db::writeStatisticsProtocols(std::unordered_map<ipAddress_protocol, int> protocolDistribution) {
- try {
- db->exec("DROP TABLE IF EXISTS ip_protocols");
- SQLite::Transaction transaction(*db);
- const char *createTable = "CREATE TABLE ip_protocols ("
- "ipAddress TEXT,"
- "protocolName TEXT,"
- "protocolCount INTEGER,"
- "PRIMARY KEY(ipAddress,protocolName));";
- db->exec(createTable);
- SQLite::Statement query(*db, "INSERT INTO ip_protocols VALUES (?, ?, ?)");
- for (auto it = protocolDistribution.begin(); it != protocolDistribution.end(); ++it) {
- ipAddress_protocol e = it->first;
- query.bind(1, e.ipAddress);
- query.bind(2, e.protocol);
- query.bind(3, it->second);
- query.exec();
- query.reset();
- }
- transaction.commit();
- }
- catch (std::exception &e) {
- std::cout << "Exception in statistics_db: " << e.what() << std::endl;
- }
- }
- /**
- * Writes the port statistics into the database.
- * @param portsStatistics The ports statistics from class statistics.
- */
- void statistics_db::writeStatisticsPorts(std::unordered_map<ipAddress_inOut_port, int> portsStatistics) {
- try {
- db->exec("DROP TABLE IF EXISTS ip_ports");
- SQLite::Transaction transaction(*db);
- const char *createTable = "CREATE TABLE ip_ports ("
- "ipAddress TEXT,"
- "portDirection TEXT,"
- "portNumber INTEGER,"
- "portCount INTEGER,"
- "PRIMARY KEY(ipAddress,portDirection,portNumber));";
- db->exec(createTable);
- SQLite::Statement query(*db, "INSERT INTO ip_ports VALUES (?, ?, ?, ?)");
- for (auto it = portsStatistics.begin(); it != portsStatistics.end(); ++it) {
- ipAddress_inOut_port e = it->first;
- query.bind(1, e.ipAddress);
- query.bind(2, e.trafficDirection);
- query.bind(3, e.portNumber);
- query.bind(4, it->second);
- query.exec();
- query.reset();
- }
- transaction.commit();
- }
- catch (std::exception &e) {
- std::cout << "Exception in statistics_db: " << e.what() << std::endl;
- }
- }
- /**
- * Writes the IP address -> MAC address mapping into the database.
- * @param IpMacStatistics The IP address -> MAC address mapping from class statistics.
- */
- void statistics_db::writeStatisticsIpMac(std::unordered_map<std::string, std::string> IpMacStatistics) {
- try {
- db->exec("DROP TABLE IF EXISTS ip_mac");
- SQLite::Transaction transaction(*db);
- const char *createTable = "CREATE TABLE ip_mac ("
- "ipAddress TEXT,"
- "macAddress TEXT,"
- "PRIMARY KEY(ipAddress));";
- db->exec(createTable);
- SQLite::Statement query(*db, "INSERT INTO ip_mac VALUES (?, ?)");
- for (auto it = IpMacStatistics.begin(); it != IpMacStatistics.end(); ++it) {
- query.bind(1, it->first);
- query.bind(2, it->second);
- query.exec();
- query.reset();
- }
- transaction.commit();
- }
- catch (std::exception &e) {
- std::cout << "Exception in statistics_db: " << e.what() << std::endl;
- }
- }
- /**
- * Writes the MSS statistics into the database.
- * @param mssStatistics The MSS statistics from class statistics.
- */
- void statistics_db::writeStatisticsMss(std::unordered_map<std::string, int> mssStatistics) {
- try {
- db->exec("DROP TABLE IF EXISTS tcp_mss");
- SQLite::Transaction transaction(*db);
- const char *createTable = "CREATE TABLE tcp_mss ("
- "ipAddress TEXT,"
- "mss INTEGER);";
- db->exec(createTable);
- SQLite::Statement query(*db, "INSERT INTO tcp_mss VALUES (?, ?)");
- for (auto it = mssStatistics.begin(); it != mssStatistics.end(); ++it) {
- query.bind(1, it->first);
- query.bind(2, it->second);
- query.exec();
- query.reset();
- }
- transaction.commit();
- }
- catch (std::exception &e) {
- std::cout << "Exception in statistics_db: " << e.what() << std::endl;
- }
- }
- /**
- * Writes general file statistics into the database.
- * @param packetCount The number of packets in the PCAP file.
- * @param captureDuration The duration of the capture (format: SS.mmmmmm).
- * @param timestampFirstPkt The timestamp of the first packet in the PCAP file.
- * @param timestampLastPkt The timestamp of the last packet in the PCAP file.
- * @param avgPacketRate The average packet rate (#packets / capture duration).
- * @param avgPacketSize The average packet size.
- * @param avgPacketsSentPerHost The average packets sent per host.
- * @param avgBandwidthIn The average incoming bandwidth.
- * @param avgBandwidthOut The average outgoing bandwidth.
- */
- void statistics_db::writeStatisticsFile(int packetCount, float captureDuration, std::string timestampFirstPkt,
- std::string timestampLastPkt, float avgPacketRate, float avgPacketSize,
- float avgPacketsSentPerHost, float avgBandwidthIn, float avgBandwidthOut) {
- try {
- db->exec("DROP TABLE IF EXISTS file_statistics");
- SQLite::Transaction transaction(*db);
- const char *createTable = "CREATE TABLE file_statistics ("
- "packetCount INTEGER,"
- "captureDuration TEXT,"
- "timestampFirstPacket TEXT,"
- "timestampLastPacket TEXT,"
- "avgPacketRate REAL,"
- "avgPacketSize REAL,"
- "avgPacketsSentPerHost REAL,"
- "avgBandwidthIn REAL,"
- "avgBandwidthOut REAL);";
- db->exec(createTable);
- SQLite::Statement query(*db, "INSERT INTO file_statistics VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
- query.bind(1, packetCount);
- query.bind(2, captureDuration);
- query.bind(3, timestampFirstPkt);
- query.bind(4, timestampLastPkt);
- query.bind(5, avgPacketRate);
- query.bind(6, avgPacketSize);
- query.bind(7, avgPacketsSentPerHost);
- query.bind(8, avgBandwidthIn);
- query.bind(9, avgBandwidthOut);
- query.exec();
- transaction.commit();
- }
- catch (std::exception &e) {
- std::cout << "Exception in statistics_db: " << e.what() << std::endl;
- }
- }
- // Aidamr
- /**
- * Writes the MSS distribution into the database.
- * @param mssDistribution The MSS distribution from class statistics.
- */
- void statistics_db::writeStatisticsMss_dist(std::unordered_map<ipAddress_mss, int> mssDistribution) {
- try {
- db->exec("DROP TABLE IF EXISTS tcp_mss_dist");
- SQLite::Transaction transaction(*db);
- const char *createTable = "CREATE TABLE tcp_mss_dist ("
- "ipAddress TEXT,"
- "mssValue INTEGER,"
- "mssCount INTEGER,"
- "PRIMARY KEY(ipAddress,mssValue));";
- db->exec(createTable);
- SQLite::Statement query(*db, "INSERT INTO tcp_mss_dist VALUES (?, ?, ?)");
- for (auto it = mssDistribution.begin(); it != mssDistribution.end(); ++it) {
- ipAddress_mss e = it->first;
- query.bind(1, e.ipAddress);
- query.bind(2, e.mssValue);
- query.bind(3, it->second);
- query.exec();
- query.reset();
- }
- transaction.commit();
- }
- catch (std::exception &e) {
- std::cout << "Exception in statistics_db: " << e.what() << std::endl;
- }
- }
- // Aidamr
- /**
- * Writes the window size distribution into the database.
- * @param winDistribution The window size distribution from class statistics.
- */
- void statistics_db::writeStatisticsWin(std::unordered_map<ipAddress_win, int> winDistribution) {
- try {
- db->exec("DROP TABLE IF EXISTS tcp_syn_win");
- SQLite::Transaction transaction(*db);
- const char *createTable = "CREATE TABLE tcp_syn_win ("
- "ipAddress TEXT,"
- "winSize INTEGER,"
- "winCount INTEGER,"
- "PRIMARY KEY(ipAddress,winSize));";
- db->exec(createTable);
- SQLite::Statement query(*db, "INSERT INTO tcp_syn_win VALUES (?, ?, ?)");
- for (auto it = winDistribution.begin(); it != winDistribution.end(); ++it) {
- ipAddress_win e = it->first;
- query.bind(1, e.ipAddress);
- query.bind(2, e.winSize);
- query.bind(3, it->second);
- query.exec();
- query.reset();
- }
- transaction.commit();
- }
- catch (std::exception &e) {
- std::cout << "Exception in statistics_db: " << e.what() << std::endl;
- }
- }
- // Aidamr
- /**
- * Writes the conversation statistics into the database.
- * @param convStatistics The conversation from class statistics.
- */
- void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat> convStatistics){
- try {
- db->exec("DROP TABLE IF EXISTS conv_statistics");
- SQLite::Transaction transaction(*db);
- const char *createTable = "CREATE TABLE conv_statistics ("
- "ipAddressA TEXT,"
- "portA INTEGER,"
- "ipAddressB TEXT,"
- "portB INTEGER,"
- "pkts_A_B INTEGER,"
- "pkts_B_A INTEGER,"
- "avgDelay INTEGER,"
- //"medianDelay TEXT,"
- "PRIMARY KEY(ipAddressA,portA,ipAddressB,portB));";
- db->exec(createTable);
- SQLite::Statement query(*db, "INSERT INTO conv_statistics VALUES (?, ?, ?, ?, ?, ?, ?)");
- for (auto it = convStatistics.begin(); it != convStatistics.end(); ++it) {
- conv f = it->first;
- entry_convStat e = it->second;
-
- // Compute the median delay
- //e.median_delay = e.pkts_delay[e.pkts_delay.size()/2];
- int sumDelay = 0;
- for(int i=0; (unsigned)i<e.pkts_delay.size();i++){
- sumDelay += e.pkts_delay[i].count();
- }
- if(e.pkts_delay.size()>0)
- e.avg_delay = (std::chrono::microseconds)sumDelay/e.pkts_delay.size(); // average
- else e.avg_delay = (std::chrono::microseconds)0;
- query.bind(1, f.ipAddressA);
- query.bind(2, f.portA);
- query.bind(3, f.ipAddressB);
- query.bind(4, f.portB);
- query.bind(5, (int) e.pkts_A_B);
- query.bind(6, (int) e.pkts_B_A);
- query.bind(7, (int) e.avg_delay.count());
- //query.bind(7, std::to_string(e.median_delay.count()));
- query.exec();
- query.reset();
- }
- transaction.commit();
- }
- catch (std::exception &e) {
- std::cout << "Exception in statistics_db: " << e.what() << std::endl;
- }
- }
- // Aidamr
- /**
- * Writes the interval statistics into the database.
- * @param intervalStatistics The interval entries from class statistics.
- */
- void statistics_db::writeStatisticsInterval(std::unordered_map<std::string, entry_intervalStat> intervalStatistics){
- try {
- db->exec("DROP TABLE IF EXISTS interval_statistics");
- SQLite::Transaction transaction(*db);
- const char *createTable = "CREATE TABLE interval_statistics ("
- "lastPktTimestamp TEXT,"
- "pktsCount INTEGER,"
- "ipSrcEntropy REAL,"
- "ipDstEntropy REAL,"
- "ipSrcCumEntropy REAL,"
- "ipDstCumEntropy REAL,"
- "PRIMARY KEY(lastPktTimestamp));";
- db->exec(createTable);
- SQLite::Statement query(*db, "INSERT INTO interval_statistics VALUES (?, ?, ?, ?, ?, ?)");
- for (auto it = intervalStatistics.begin(); it != intervalStatistics.end(); ++it) {
- std::string t = it->first;
- entry_intervalStat e = it->second;
-
- query.bind(1, t);
- query.bind(2, (int)e.pkts_count);
- query.bind(3, e.ip_src_entropy);
- query.bind(4, e.ip_dst_entropy);
- query.bind(5, e.ip_src_cum_entropy);
- query.bind(6, e.ip_dst_cum_entropy);
- query.exec();
- query.reset();
- }
- transaction.commit();
- }
- catch (std::exception &e) {
- std::cout << "Exception in statistics_db: " << e.what() << std::endl;
- }
- }
|