statistics_db.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. #include "statistics_db.h"
  2. #include <math.h>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <fstream>
  6. #include <numeric>
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <pybind11/pybind11.h>
  10. namespace py = pybind11;
  11. using namespace Tins;
  12. /**
  13. * Creates a new statistics_db object. Opens an existing database located at database_path. If not existing, creates
  14. * a new database at database_path.
  15. * @param database_path The file path of the database.
  16. */
  17. statistics_db::statistics_db(std::string database_path, std::string resourcePath) {
  18. // Append file extension if not present
  19. if (database_path.find(".sqlite3") == database_path.npos) {
  20. database_path += ".sqlite3";
  21. }
  22. // creates the DB if not existing, opens the DB for read+write access
  23. db.reset(new SQLite::Database(database_path, SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE));
  24. this->resourcePath = resourcePath;
  25. // Read ports and services into portServices vector
  26. readPortServicesFromNmap();
  27. }
  28. void statistics_db::getNoneExtraTestsInveralStats(std::vector<double>& intervals){
  29. try {
  30. //SQLite::Statement query(*db, "SELECT name FROM sqlite_master WHERE type='table' AND name='interval_tables';");
  31. std::vector<std::string> tables;
  32. try {
  33. SQLite::Statement query(*db, "SELECT name FROM interval_tables WHERE extra_tests=1;");
  34. while (query.executeStep()) {
  35. tables.push_back(query.getColumn(0));
  36. }
  37. } catch (std::exception &e) {
  38. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  39. }
  40. if (tables.size() != 0) {
  41. std::string table_name;
  42. double interval;
  43. for (auto table = tables.begin(); table != tables.end(); table++) {
  44. table_name = table->substr(std::string("interval_statistics_").length());
  45. interval = static_cast<double>(::atof(table_name.c_str()))/1000000;
  46. auto found = std::find(intervals.begin(), intervals.end(), interval);
  47. if (found != intervals.end()) {
  48. intervals.erase(found, found);
  49. }
  50. }
  51. }
  52. } catch (std::exception &e) {
  53. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  54. }
  55. }
  56. /**
  57. * Writes the IP statistics into the database.
  58. * @param ipStatistics The IP statistics from class statistics.
  59. */
  60. void statistics_db::writeStatisticsIP(const std::unordered_map<std::string, entry_ipStat> &ipStatistics) {
  61. try {
  62. db->exec("DROP TABLE IF EXISTS ip_statistics");
  63. SQLite::Transaction transaction(*db);
  64. const char *createTable = "CREATE TABLE ip_statistics ( "
  65. "ipAddress TEXT, "
  66. "pktsReceived INTEGER, "
  67. "pktsSent INTEGER, "
  68. "kbytesReceived REAL, "
  69. "kbytesSent REAL, "
  70. "maxPktRate REAL,"
  71. "minPktRate REAL,"
  72. "ipClass TEXT COLLATE NOCASE, "
  73. "PRIMARY KEY(ipAddress));";
  74. db->exec(createTable);
  75. SQLite::Statement query(*db, "INSERT INTO ip_statistics VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
  76. for (auto it = ipStatistics.begin(); it != ipStatistics.end(); ++it) {
  77. const entry_ipStat &e = it->second;
  78. query.bindNoCopy(1, it->first);
  79. query.bind(2, (int) e.pkts_received);
  80. query.bind(3, (int) e.pkts_sent);
  81. query.bind(4, e.kbytes_received);
  82. query.bind(5, e.kbytes_sent);
  83. query.bind(6, e.max_interval_pkt_rate);
  84. query.bind(7, e.min_interval_pkt_rate);
  85. query.bindNoCopy(8, e.ip_class);
  86. query.exec();
  87. query.reset();
  88. if (PyErr_CheckSignals()) throw py::error_already_set();
  89. }
  90. transaction.commit();
  91. }
  92. catch (std::exception &e) {
  93. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  94. }
  95. }
  96. /**
  97. * Writes the IP Degrees into the database.
  98. * @param ipStatistics The IP statistics from class statistics. Degree Statistics are supposed to be integrated into the ip_statistics table later on,
  99. * therefore they use the same parameter. But for now they are inserted into their own table.
  100. */
  101. void statistics_db::writeStatisticsDegree(const std::unordered_map<std::string, entry_ipStat> &ipStatistics){
  102. try {
  103. db->exec("DROP TABLE IF EXISTS ip_degrees");
  104. SQLite::Transaction transaction(*db);
  105. const char *createTable = "CREATE TABLE ip_degrees ( "
  106. "ipAddress TEXT, "
  107. "inDegree INTEGER, "
  108. "outDegree INTEGER, "
  109. "overallDegree INTEGER, "
  110. "PRIMARY KEY(ipAddress));";
  111. db->exec(createTable);
  112. SQLite::Statement query(*db, "INSERT INTO ip_degrees VALUES (?, ?, ?, ?)");
  113. for (auto it = ipStatistics.begin(); it != ipStatistics.end(); ++it) {
  114. const entry_ipStat &e = it->second;
  115. query.bindNoCopy(1, it->first);
  116. query.bind(2, e.in_degree);
  117. query.bind(3, e.out_degree);
  118. query.bind(4, e.overall_degree);
  119. query.exec();
  120. query.reset();
  121. if (PyErr_CheckSignals()) throw py::error_already_set();
  122. }
  123. transaction.commit();
  124. }
  125. catch (std::exception &e) {
  126. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  127. }
  128. }
  129. /**
  130. * Writes the TTL distribution into the database.
  131. * @param ttlDistribution The TTL distribution from class statistics.
  132. */
  133. void statistics_db::writeStatisticsTTL(const std::unordered_map<ipAddress_ttl, int> &ttlDistribution) {
  134. try {
  135. db->exec("DROP TABLE IF EXISTS ip_ttl");
  136. SQLite::Transaction transaction(*db);
  137. const char *createTable = "CREATE TABLE ip_ttl ("
  138. "ipAddress TEXT,"
  139. "ttlValue INTEGER,"
  140. "ttlCount INTEGER,"
  141. "PRIMARY KEY(ipAddress,ttlValue));"
  142. "CREATE INDEX ipAddressTTL ON ip_ttl(ipAddress);";
  143. db->exec(createTable);
  144. SQLite::Statement query(*db, "INSERT INTO ip_ttl VALUES (?, ?, ?)");
  145. for (auto it = ttlDistribution.begin(); it != ttlDistribution.end(); ++it) {
  146. const ipAddress_ttl &e = it->first;
  147. query.bindNoCopy(1, e.ipAddress);
  148. query.bind(2, e.ttlValue);
  149. query.bind(3, it->second);
  150. query.exec();
  151. query.reset();
  152. if (PyErr_CheckSignals()) throw py::error_already_set();
  153. }
  154. transaction.commit();
  155. }
  156. catch (std::exception &e) {
  157. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  158. }
  159. }
  160. /**
  161. * Writes the MSS distribution into the database.
  162. * @param mssDistribution The MSS distribution from class statistics.
  163. */
  164. void statistics_db::writeStatisticsMSS(const std::unordered_map<ipAddress_mss, int> &mssDistribution) {
  165. try {
  166. db->exec("DROP TABLE IF EXISTS tcp_mss");
  167. SQLite::Transaction transaction(*db);
  168. const char *createTable = "CREATE TABLE tcp_mss ("
  169. "ipAddress TEXT,"
  170. "mssValue INTEGER,"
  171. "mssCount INTEGER,"
  172. "PRIMARY KEY(ipAddress,mssValue));"
  173. "CREATE INDEX ipAddressMSS ON tcp_mss(ipAddress);";
  174. db->exec(createTable);
  175. SQLite::Statement query(*db, "INSERT INTO tcp_mss VALUES (?, ?, ?)");
  176. for (auto it = mssDistribution.begin(); it != mssDistribution.end(); ++it) {
  177. const ipAddress_mss &e = it->first;
  178. query.bindNoCopy(1, e.ipAddress);
  179. query.bind(2, e.mssValue);
  180. query.bind(3, it->second);
  181. query.exec();
  182. query.reset();
  183. if (PyErr_CheckSignals()) throw py::error_already_set();
  184. }
  185. transaction.commit();
  186. }
  187. catch (std::exception &e) {
  188. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  189. }
  190. }
  191. /**
  192. * Writes the ToS distribution into the database.
  193. * @param tosDistribution The ToS distribution from class statistics.
  194. */
  195. void statistics_db::writeStatisticsToS(const std::unordered_map<ipAddress_tos, int> &tosDistribution) {
  196. try {
  197. db->exec("DROP TABLE IF EXISTS ip_tos");
  198. SQLite::Transaction transaction(*db);
  199. const char *createTable = "CREATE TABLE ip_tos ("
  200. "ipAddress TEXT,"
  201. "tosValue INTEGER,"
  202. "tosCount INTEGER,"
  203. "PRIMARY KEY(ipAddress,tosValue));";
  204. db->exec(createTable);
  205. SQLite::Statement query(*db, "INSERT INTO ip_tos VALUES (?, ?, ?)");
  206. for (auto it = tosDistribution.begin(); it != tosDistribution.end(); ++it) {
  207. const ipAddress_tos &e = it->first;
  208. query.bindNoCopy(1, e.ipAddress);
  209. query.bind(2, e.tosValue);
  210. query.bind(3, it->second);
  211. query.exec();
  212. query.reset();
  213. if (PyErr_CheckSignals()) throw py::error_already_set();
  214. }
  215. transaction.commit();
  216. }
  217. catch (std::exception &e) {
  218. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  219. }
  220. }
  221. /**
  222. * Writes the window size distribution into the database.
  223. * @param winDistribution The window size distribution from class statistics.
  224. */
  225. void statistics_db::writeStatisticsWin(const std::unordered_map<ipAddress_win, int> &winDistribution) {
  226. try {
  227. db->exec("DROP TABLE IF EXISTS tcp_win");
  228. SQLite::Transaction transaction(*db);
  229. const char *createTable = "CREATE TABLE tcp_win ("
  230. "ipAddress TEXT,"
  231. "winSize INTEGER,"
  232. "winCount INTEGER,"
  233. "PRIMARY KEY(ipAddress,winSize));"
  234. "CREATE INDEX ipAddressWIN ON tcp_win(ipAddress);";
  235. db->exec(createTable);
  236. SQLite::Statement query(*db, "INSERT INTO tcp_win VALUES (?, ?, ?)");
  237. for (auto it = winDistribution.begin(); it != winDistribution.end(); ++it) {
  238. const ipAddress_win &e = it->first;
  239. query.bindNoCopy(1, e.ipAddress);
  240. query.bind(2, e.winSize);
  241. query.bind(3, it->second);
  242. query.exec();
  243. query.reset();
  244. if (PyErr_CheckSignals()) throw py::error_already_set();
  245. }
  246. transaction.commit();
  247. }
  248. catch (std::exception &e) {
  249. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  250. }
  251. }
  252. /**
  253. * Writes the protocol distribution into the database.
  254. * @param protocolDistribution The protocol distribution from class statistics.
  255. */
  256. void statistics_db::writeStatisticsProtocols(const std::unordered_map<ipAddress_protocol, entry_protocolStat> &protocolDistribution) {
  257. try {
  258. db->exec("DROP TABLE IF EXISTS ip_protocols");
  259. SQLite::Transaction transaction(*db);
  260. const char *createTable = "CREATE TABLE ip_protocols ("
  261. "ipAddress TEXT,"
  262. "protocolName TEXT COLLATE NOCASE,"
  263. "protocolCount INTEGER,"
  264. "byteCount REAL,"
  265. "PRIMARY KEY(ipAddress,protocolName));";
  266. db->exec(createTable);
  267. SQLite::Statement query(*db, "INSERT INTO ip_protocols VALUES (?, ?, ?, ?)");
  268. for (auto it = protocolDistribution.begin(); it != protocolDistribution.end(); ++it) {
  269. const ipAddress_protocol &e = it->first;
  270. query.bindNoCopy(1, e.ipAddress);
  271. query.bindNoCopy(2, e.protocol);
  272. query.bind(3, it->second.count);
  273. query.bind(4, it->second.byteCount);
  274. query.exec();
  275. query.reset();
  276. if (PyErr_CheckSignals()) throw py::error_already_set();
  277. }
  278. transaction.commit();
  279. }
  280. catch (std::exception &e) {
  281. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  282. }
  283. }
  284. /**
  285. * Writes the port statistics into the database.
  286. * @param portsStatistics The ports statistics from class statistics.
  287. */
  288. void statistics_db::writeStatisticsPorts(const std::unordered_map<ipAddress_inOut_port, entry_portStat> &portsStatistics) {
  289. try {
  290. db->exec("DROP TABLE IF EXISTS ip_ports");
  291. SQLite::Transaction transaction(*db);
  292. const char *createTable = "CREATE TABLE ip_ports ("
  293. "ipAddress TEXT,"
  294. "portDirection TEXT COLLATE NOCASE,"
  295. "portNumber INTEGER,"
  296. "portCount INTEGER,"
  297. "byteCount REAL,"
  298. "portProtocol TEXT COLLATE NOCASE,"
  299. "portService TEXT COLLATE NOCASE,"
  300. "PRIMARY KEY(ipAddress,portDirection,portNumber,portProtocol));";
  301. db->exec(createTable);
  302. SQLite::Statement query(*db, "INSERT INTO ip_ports VALUES (?, ?, ?, ?, ?, ?, ?)");
  303. for (auto it = portsStatistics.begin(); it != portsStatistics.end(); ++it) {
  304. const ipAddress_inOut_port &e = it->first;
  305. std::string portService = portServices[e.portNumber];
  306. if(portService.empty()) {
  307. if(portServices[{0}] == "unavailable") {portService = "unavailable";}
  308. else {portService = "unknown";}
  309. }
  310. query.bindNoCopy(1, e.ipAddress);
  311. query.bindNoCopy(2, e.trafficDirection);
  312. query.bind(3, e.portNumber);
  313. query.bind(4, it->second.count);
  314. query.bind(5, it->second.byteCount);
  315. query.bindNoCopy(6, e.protocol);
  316. query.bindNoCopy(7, portService);
  317. query.exec();
  318. query.reset();
  319. if (PyErr_CheckSignals()) throw py::error_already_set();
  320. }
  321. transaction.commit();
  322. }
  323. catch (std::exception &e) {
  324. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  325. }
  326. }
  327. /**
  328. * Writes the IP address -> MAC address mapping into the database.
  329. * @param IpMacStatistics The IP address -> MAC address mapping from class statistics.
  330. */
  331. void statistics_db::writeStatisticsIpMac(const std::unordered_map<std::string, std::string> &IpMacStatistics) {
  332. try {
  333. db->exec("DROP TABLE IF EXISTS ip_mac");
  334. SQLite::Transaction transaction(*db);
  335. const char *createTable = "CREATE TABLE ip_mac ("
  336. "ipAddress TEXT,"
  337. "macAddress TEXT COLLATE NOCASE,"
  338. "PRIMARY KEY(ipAddress));";
  339. db->exec(createTable);
  340. SQLite::Statement query(*db, "INSERT INTO ip_mac VALUES (?, ?)");
  341. for (auto it = IpMacStatistics.begin(); it != IpMacStatistics.end(); ++it) {
  342. query.bindNoCopy(1, it->first);
  343. query.bindNoCopy(2, it->second);
  344. query.exec();
  345. query.reset();
  346. if (PyErr_CheckSignals()) throw py::error_already_set();
  347. }
  348. transaction.commit();
  349. }
  350. catch (std::exception &e) {
  351. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  352. }
  353. }
  354. /**
  355. * Writes general file statistics into the database.
  356. * @param packetCount The number of packets in the PCAP file.
  357. * @param captureDuration The duration of the capture (format: SS.mmmmmm).
  358. * @param timestampFirstPkt The timestamp of the first packet in the PCAP file.
  359. * @param timestampLastPkt The timestamp of the last packet in the PCAP file.
  360. * @param avgPacketRate The average packet rate (#packets / capture duration).
  361. * @param avgPacketSize The average packet size.
  362. * @param avgPacketsSentPerHost The average packets sent per host.
  363. * @param avgBandwidthIn The average incoming bandwidth.
  364. * @param avgBandwidthOut The average outgoing bandwidth.
  365. */
  366. void statistics_db::writeStatisticsFile(int packetCount, float captureDuration, std::string timestampFirstPkt,
  367. std::string timestampLastPkt, float avgPacketRate, float avgPacketSize,
  368. float avgPacketsSentPerHost, float avgBandwidthIn, float avgBandwidthOut,
  369. bool doExtraTests) {
  370. try {
  371. db->exec("DROP TABLE IF EXISTS file_statistics");
  372. SQLite::Transaction transaction(*db);
  373. const char *createTable = "CREATE TABLE file_statistics ("
  374. "packetCount INTEGER,"
  375. "captureDuration TEXT,"
  376. "timestampFirstPacket TEXT,"
  377. "timestampLastPacket TEXT,"
  378. "avgPacketRate REAL,"
  379. "avgPacketSize REAL,"
  380. "avgPacketsSentPerHost REAL,"
  381. "avgBandwidthIn REAL,"
  382. "avgBandwidthOut REAL,"
  383. "doExtraTests INTEGER);";
  384. db->exec(createTable);
  385. SQLite::Statement query(*db, "INSERT INTO file_statistics VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  386. query.bind(1, packetCount);
  387. query.bind(2, captureDuration);
  388. query.bind(3, timestampFirstPkt);
  389. query.bind(4, timestampLastPkt);
  390. query.bind(5, avgPacketRate);
  391. query.bind(6, avgPacketSize);
  392. query.bind(7, avgPacketsSentPerHost);
  393. query.bind(8, avgBandwidthIn);
  394. query.bind(9, avgBandwidthOut);
  395. query.bind(10, doExtraTests);
  396. query.exec();
  397. transaction.commit();
  398. }
  399. catch (std::exception &e) {
  400. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  401. }
  402. }
  403. /**
  404. * Writes the conversation statistics into the database.
  405. * @param convStatistics The conversation from class statistics.
  406. */
  407. void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat> &convStatistics){
  408. try {
  409. db->exec("DROP TABLE IF EXISTS conv_statistics");
  410. SQLite::Transaction transaction(*db);
  411. const char *createTable = "CREATE TABLE conv_statistics ("
  412. "ipAddressA TEXT,"
  413. "portA INTEGER,"
  414. "ipAddressB TEXT,"
  415. "portB INTEGER,"
  416. "pktsCount INTEGER,"
  417. "avgPktRate REAL,"
  418. "avgDelay INTEGER,"
  419. "minDelay INTEGER,"
  420. "maxDelay INTEGER,"
  421. "roundTripTime INTEGER,"
  422. "PRIMARY KEY(ipAddressA,portA,ipAddressB,portB));";
  423. db->exec(createTable);
  424. SQLite::Statement query(*db, "INSERT INTO conv_statistics VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  425. // Calculate average of inter-arrival times and average packet rate
  426. for (auto it = convStatistics.begin(); it != convStatistics.end(); ++it) {
  427. const conv &f = it->first;
  428. entry_convStat &e = it->second;
  429. if (e.pkts_count > 1){
  430. int sumDelay = 0;
  431. int minDelay = -1;
  432. int maxDelay = -1;
  433. for (int i = 0; (unsigned) i < e.interarrival_time.size(); i++) {
  434. sumDelay += e.interarrival_time[i].count();
  435. if (maxDelay < e.interarrival_time[i].count())
  436. maxDelay = e.interarrival_time[i].count();
  437. if (minDelay > e.interarrival_time[i].count() || minDelay == -1)
  438. minDelay = e.interarrival_time[i].count();
  439. }
  440. if (e.interarrival_time.size() > 0)
  441. e.avg_interarrival_time = (std::chrono::microseconds) sumDelay / e.interarrival_time.size(); // average
  442. else e.avg_interarrival_time = (std::chrono::microseconds) 0;
  443. std::vector<std::chrono::microseconds>::const_iterator i1;
  444. std::vector<small_uint<12>>::const_iterator i2;
  445. std::chrono::microseconds roundTripTime = std::chrono::microseconds(0);
  446. std::vector<std::chrono::microseconds> roundTripTimes;
  447. bool flag = false;
  448. if (e.pkts_timestamp.size() != e.tcp_types.size()) {
  449. std::cout << "shit..."<< std::endl;
  450. }
  451. for(i1 = e.pkts_timestamp.begin(), i2 = e.tcp_types.begin();
  452. i1 < e.pkts_timestamp.end(), i2 < e.tcp_types.end();
  453. ++i1, ++i2) {
  454. if (*i2 == TCP::SYN && !flag) {
  455. roundTripTime = *i1;
  456. flag = true;
  457. } else if (*i2 == TCP::ACK && flag) {
  458. roundTripTime = *i1 - roundTripTime;
  459. flag = false;
  460. roundTripTimes.push_back(roundTripTime);
  461. roundTripTime = std::chrono::microseconds(0);
  462. }
  463. }
  464. if (roundTripTimes.size() != 0) {
  465. std::vector<std::chrono::microseconds>::const_iterator it;
  466. for(it = roundTripTimes.begin(); it < roundTripTimes.end(); ++it) {
  467. roundTripTime += *it;
  468. }
  469. roundTripTime = roundTripTime / roundTripTimes.size();
  470. } else {
  471. roundTripTime = std::chrono::microseconds(-1);
  472. }
  473. std::chrono::microseconds start_timesttamp = e.pkts_timestamp[0];
  474. std::chrono::microseconds end_timesttamp = e.pkts_timestamp.back();
  475. std::chrono::microseconds conn_duration = end_timesttamp - start_timesttamp;
  476. e.avg_pkt_rate = (float) e.pkts_count * 1000000 / conn_duration.count(); // pkt per sec
  477. query.bindNoCopy(1, f.ipAddressA);
  478. query.bind(2, f.portA);
  479. query.bindNoCopy(3, f.ipAddressB);
  480. query.bind(4, f.portB);
  481. query.bind(5, (int) e.pkts_count);
  482. query.bind(6, (float) e.avg_pkt_rate);
  483. query.bind(7, (int) e.avg_interarrival_time.count());
  484. query.bind(8, minDelay);
  485. query.bind(9, maxDelay);
  486. query.bind(10, static_cast<int>(roundTripTime.count()));
  487. query.exec();
  488. query.reset();
  489. if (PyErr_CheckSignals()) throw py::error_already_set();
  490. }
  491. }
  492. transaction.commit();
  493. }
  494. catch (std::exception &e) {
  495. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  496. }
  497. }
  498. /**
  499. * Writes the extended statistics for every conversation into the database.
  500. * @param conv_statistics_extended The extended conversation statistics from class statistics.
  501. */
  502. void statistics_db::writeStatisticsConvExt(std::unordered_map<convWithProt, entry_convStatExt> &conv_statistics_extended){
  503. try {
  504. db->exec("DROP TABLE IF EXISTS conv_statistics_extended");
  505. SQLite::Transaction transaction(*db);
  506. const char *createTable = "CREATE TABLE conv_statistics_extended ("
  507. "ipAddressA TEXT,"
  508. "portA INTEGER,"
  509. "ipAddressB TEXT,"
  510. "portB INTEGER,"
  511. "protocol TEXT COLLATE NOCASE,"
  512. "pktsCount INTEGER,"
  513. "avgPktRate REAL,"
  514. "avgDelay INTEGER,"
  515. "minDelay INTEGER,"
  516. "maxDelay INTEGER,"
  517. "avgIntervalPktCount REAL,"
  518. "avgTimeBetweenIntervals REAL,"
  519. "avgIntervalTime REAL,"
  520. "totalConversationDuration REAL,"
  521. "PRIMARY KEY(ipAddressA,portA,ipAddressB,portB,protocol));";
  522. db->exec(createTable);
  523. SQLite::Statement query(*db, "INSERT INTO conv_statistics_extended VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  524. // iterate over every conversation and interval aggregation pair and store the respective values in the database
  525. for (auto it = conv_statistics_extended.begin(); it != conv_statistics_extended.end(); ++it) {
  526. const convWithProt &f = it->first;
  527. entry_convStatExt &e = it->second;
  528. int sumDelay = 0;
  529. int minDelay = -1;
  530. int maxDelay = -1;
  531. if (e.pkts_count > 1 && f.protocol == "TCP"){
  532. for (int i = 0; (unsigned) i < e.interarrival_time.size(); i++) {
  533. sumDelay += e.interarrival_time[i].count();
  534. if (maxDelay < e.interarrival_time[i].count())
  535. maxDelay = e.interarrival_time[i].count();
  536. if (minDelay > e.interarrival_time[i].count() || minDelay == -1)
  537. minDelay = e.interarrival_time[i].count();
  538. }
  539. if (e.interarrival_time.size() > 0)
  540. e.avg_interarrival_time = static_cast<std::chrono::microseconds>(sumDelay) / e.interarrival_time.size(); // average
  541. else
  542. e.avg_interarrival_time = static_cast<std::chrono::microseconds>(0);
  543. }
  544. if (e.total_comm_duration == 0)
  545. e.avg_pkt_rate = e.pkts_count; // pkt per sec
  546. else
  547. e.avg_pkt_rate = e.pkts_count / e.total_comm_duration;
  548. if (e.avg_int_pkts_count > 0){
  549. query.bindNoCopy(1, f.ipAddressA);
  550. query.bind(2, f.portA);
  551. query.bindNoCopy(3, f.ipAddressB);
  552. query.bind(4, f.portB);
  553. query.bindNoCopy(5, f.protocol);
  554. query.bind(6, static_cast<int>(e.pkts_count));
  555. query.bind(7, static_cast<float>(e.avg_pkt_rate));
  556. if ((f.protocol == "UDP" || f.protocol == "TCP") && e.pkts_count < 2)
  557. query.bind(8);
  558. else
  559. query.bind(8, static_cast<int>(e.avg_interarrival_time.count()));
  560. if (minDelay == -1)
  561. query.bind(9);
  562. else
  563. query.bind(9, minDelay);
  564. if (maxDelay == -1)
  565. query.bind(10);
  566. else
  567. query.bind(10, maxDelay);
  568. query.bind(11, e.avg_int_pkts_count);
  569. query.bind(12, e.avg_time_between_ints);
  570. query.bind(13, e.avg_interval_time);
  571. query.bind(14, e.total_comm_duration);
  572. query.exec();
  573. query.reset();
  574. if (PyErr_CheckSignals()) throw py::error_already_set();
  575. }
  576. }
  577. transaction.commit();
  578. }
  579. catch (std::exception &e) {
  580. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  581. }
  582. }
  583. /**
  584. * Writes the interval statistics into the database.
  585. * @param intervalStatistics The interval entries from class statistics.
  586. */
  587. void statistics_db::writeStatisticsInterval(const std::unordered_map<std::string, entry_intervalStat> &intervalStatistics, std::vector<std::chrono::duration<int, std::micro>> timeIntervals, bool del, int defaultInterval, bool extraTests){
  588. try {
  589. // remove old tables produced by prior database versions
  590. db->exec("DROP TABLE IF EXISTS interval_statistics");
  591. // delete all former interval statistics, if requested
  592. if (del) {
  593. SQLite::Statement query(*db, "SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'interval_statistics_%';");
  594. std::vector<std::string> previous_tables;
  595. while (query.executeStep()) {
  596. previous_tables.push_back(query.getColumn(0));
  597. }
  598. for (std::string table: previous_tables) {
  599. db->exec("DROP TABLE IF EXISTS " + table);
  600. }
  601. db->exec("DROP TABLE IF EXISTS interval_tables");
  602. }
  603. // create interval table index
  604. db->exec("CREATE TABLE IF NOT EXISTS interval_tables (name TEXT, is_default INTEGER, extra_tests INTEGER);");
  605. std::string default_table_name = "";
  606. // get name for default table
  607. try {
  608. SQLite::Statement query(*db, "SELECT name FROM interval_tables WHERE is_default=1;");
  609. query.executeStep();
  610. default_table_name = query.getColumn(0).getString();
  611. } catch (std::exception &e) {
  612. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  613. }
  614. // handle default interval only runs
  615. std::string is_default = "0";
  616. std::chrono::duration<int, std::micro> defaultTimeInterval(defaultInterval);
  617. if (defaultInterval != 0.0) {
  618. is_default = "1";
  619. if (timeIntervals.empty() || timeIntervals[0].count() == 0) {
  620. timeIntervals.clear();
  621. timeIntervals.push_back(defaultTimeInterval);
  622. }
  623. }
  624. // extra tests handling
  625. std::string extra = "0";
  626. if (extraTests) {
  627. extra = "1";
  628. }
  629. for (auto timeInterval: timeIntervals) {
  630. // get interval statistics table name
  631. std::ostringstream strs;
  632. strs << timeInterval.count();
  633. std::string table_name = "interval_statistics_" + strs.str();
  634. // check for recalculation of default table
  635. if (table_name == default_table_name || timeInterval == defaultTimeInterval) {
  636. is_default = "1";
  637. } else {
  638. is_default = "0";
  639. }
  640. // add interval_tables entry
  641. db->exec("DELETE FROM interval_tables WHERE name = '" + table_name + "';");
  642. db->exec("INSERT INTO interval_tables VALUES ('" + table_name + "', '" + is_default + "', '" + extra + "');");
  643. // new interval statistics implementation
  644. db->exec("DROP TABLE IF EXISTS " + table_name);
  645. SQLite::Transaction transaction(*db);
  646. db->exec("CREATE TABLE " + table_name + " ("
  647. "last_pkt_timestamp TEXT,"
  648. "first_pkt_timestamp TEXT,"
  649. "pkts_count INTEGER,"
  650. "pkt_rate REAL,"
  651. "kBytes REAL,"
  652. "kByte_rate REAL,"
  653. "ip_src_entropy REAL,"
  654. "ip_dst_entropy REAL,"
  655. "ip_src_cum_entropy REAL,"
  656. "ip_dst_cum_entropy REAL,"
  657. "payload_count INTEGER,"
  658. "incorrect_tcp_checksum_count INTEGER,"
  659. "correct_tcp_checksum_count INTEGER,"
  660. "ip_src_novel_Count INTEGER,"
  661. "ip_dst_novel_Count INTEGER,"
  662. "port_novel_count INTEGER,"
  663. "ttl_novel_count INTEGER,"
  664. "win_size_novel_count INTEGER,"
  665. "tos_novel_count INTEGER,"
  666. "mss_novel_count INTEGER,"
  667. "port_entropy REAL,"
  668. "ttl_entropy REAL,"
  669. "win_size_entropy REAL,"
  670. "tos_entropy REAL,"
  671. "mss_entropy REAL,"
  672. "port_novel_entropy REAL,"
  673. "ttl_novel_entropy REAL,"
  674. "win_size_novel_entropy REAL,"
  675. "tos_novel_entropy REAL,"
  676. "mss_novel_entropy REAL,"
  677. "port_entropy_normalized REAL,"
  678. "ttl_entropy_normalized REAL,"
  679. "win_size_entropy_normalized REAL,"
  680. "tos_entropy_normalized REAL,"
  681. "mss_entropy_normalized REAL,"
  682. "port_novel_entropy_normalized REAL,"
  683. "ttl_novel_entropy_normalized REAL,"
  684. "win_size_novel_entropy_normalized REAL,"
  685. "tos_novel_entropy_normalized REAL,"
  686. "mss_novel_entropy_normalized REAL,"
  687. "ip_src_entropy_normalized REAL,"
  688. "ip_dst_entropy_normalized REAL,"
  689. "ip_src_cum_entropy_normalized REAL,"
  690. "ip_dst_cum_entropy_normalized REAL,"
  691. "ip_src_novel_entropy REAL,"
  692. "ip_dst_novel_entropy REAL,"
  693. "ip_src_novel_entropy_normalized REAL,"
  694. "ip_dst_novel_entropy_normalized REAL,"
  695. "PRIMARY KEY(last_pkt_timestamp));");
  696. SQLite::Statement query(*db, "INSERT INTO " + table_name + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  697. for (auto it = intervalStatistics.begin(); it != intervalStatistics.end(); ++it) {
  698. const entry_intervalStat &e = it->second;
  699. query.bindNoCopy(1, it->first);
  700. query.bind(2, e.start);
  701. query.bind(3, (int)e.pkts_count);
  702. query.bind(4, e.pkt_rate);
  703. query.bind(5, e.kbytes);
  704. query.bind(6, e.kbyte_rate);
  705. query.bind(7, e.ip_entropies[0]);
  706. query.bind(8, e.ip_entropies[1]);
  707. query.bind(9, e.ip_cum_entropies[0]);
  708. query.bind(10, e.ip_cum_entropies[1]);
  709. query.bind(11, e.payload_count);
  710. query.bind(12, e.incorrect_tcp_checksum_count);
  711. query.bind(13, e.correct_tcp_checksum_count);
  712. query.bind(14, static_cast<long long>(e.novel_ip_src_count));
  713. query.bind(15, static_cast<long long>(e.novel_ip_dst_count));
  714. query.bind(16, e.novel_port_count);
  715. query.bind(17, e.novel_ttl_count);
  716. query.bind(18, e.novel_win_size_count);
  717. query.bind(19, e.novel_tos_count);
  718. query.bind(20, e.novel_mss_count);
  719. query.bind(21, e.port_entropies[0]);
  720. query.bind(22, e.ttl_entropies[0]);
  721. query.bind(23, e.win_size_entropies[0]);
  722. query.bind(24, e.tos_entropies[0]);
  723. query.bind(25, e.mss_entropies[0]);
  724. query.bind(26, e.port_entropies[1]);
  725. query.bind(27, e.ttl_entropies[1]);
  726. query.bind(28, e.win_size_entropies[1]);
  727. query.bind(29, e.tos_entropies[1]);
  728. query.bind(30, e.mss_entropies[1]);
  729. query.bind(31, e.port_entropies[2]);
  730. query.bind(32, e.ttl_entropies[2]);
  731. query.bind(33, e.win_size_entropies[2]);
  732. query.bind(34, e.tos_entropies[2]);
  733. query.bind(35, e.mss_entropies[2]);
  734. query.bind(36, e.port_entropies[3]);
  735. query.bind(37, e.ttl_entropies[3]);
  736. query.bind(38, e.win_size_entropies[3]);
  737. query.bind(39, e.tos_entropies[3]);
  738. query.bind(40, e.mss_entropies[3]);
  739. query.bind(41, e.ip_entropies[4]);
  740. query.bind(42, e.ip_entropies[5]);
  741. query.bind(43, e.ip_cum_entropies[2]);
  742. query.bind(44, e.ip_cum_entropies[3]);
  743. query.bind(45, e.ip_entropies[2]);
  744. query.bind(46, e.ip_entropies[3]);
  745. query.bind(47, e.ip_entropies[6]);
  746. query.bind(48, e.ip_entropies[7]);
  747. query.exec();
  748. query.reset();
  749. if (PyErr_CheckSignals()) throw py::error_already_set();
  750. }
  751. transaction.commit();
  752. }
  753. }
  754. catch (std::exception &e) {
  755. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  756. }
  757. }
  758. void statistics_db::writeDbVersion(){
  759. try {
  760. SQLite::Transaction transaction(*db);
  761. SQLite::Statement query(*db, std::string("PRAGMA user_version = ") + std::to_string(DB_VERSION) + ";");
  762. query.exec();
  763. transaction.commit();
  764. }
  765. catch (std::exception &e) {
  766. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  767. }
  768. }
  769. /**
  770. * Reads all ports and their corresponding services from nmap-services-tcp.csv and stores them into portServices vector.
  771. */
  772. void statistics_db::readPortServicesFromNmap()
  773. {
  774. std::string portnumber;
  775. std::string service;
  776. std::string dump;
  777. std::string nmapPath = resourcePath + "nmap-services-tcp.csv";
  778. std::ifstream reader;
  779. reader.open(nmapPath, std::ios::in);
  780. if(reader.is_open())
  781. {
  782. getline(reader, dump);
  783. while(!reader.eof())
  784. {
  785. getline(reader, portnumber, ',');
  786. getline(reader, service, ',');
  787. getline(reader, dump);
  788. if(!service.empty() && !portnumber.empty())
  789. {
  790. portServices.insert({std::stoi(portnumber), service});
  791. }
  792. }
  793. reader.close();
  794. }
  795. else
  796. {
  797. std::cerr << "WARNING: " << nmapPath << " could not be opened! PortServices can't be read!" << std::endl;
  798. portServices.insert({0, "unavailable"});
  799. }
  800. }
  801. /**
  802. * Writes the unrecognized PDUs into the database.
  803. * @param unrecognized_PDUs The unrecognized PDUs from class statistics.
  804. */
  805. void statistics_db::writeStatisticsUnrecognizedPDUs(const std::unordered_map<unrecognized_PDU, unrecognized_PDU_stat>
  806. &unrecognized_PDUs) {
  807. try {
  808. db->exec("DROP TABLE IF EXISTS unrecognized_pdus");
  809. SQLite::Transaction transaction(*db);
  810. const char *createTable = "CREATE TABLE unrecognized_pdus ("
  811. "srcMac TEXT COLLATE NOCASE,"
  812. "dstMac TEXT COLLATE NOCASE,"
  813. "etherType INTEGER,"
  814. "pktCount INTEGER,"
  815. "timestampLastOccurrence TEXT,"
  816. "PRIMARY KEY(srcMac,dstMac,etherType));";
  817. db->exec(createTable);
  818. SQLite::Statement query(*db, "INSERT INTO unrecognized_pdus VALUES (?, ?, ?, ?, ?)");
  819. for (auto it = unrecognized_PDUs.begin(); it != unrecognized_PDUs.end(); ++it) {
  820. const unrecognized_PDU &e = it->first;
  821. query.bindNoCopy(1, e.srcMacAddress);
  822. query.bindNoCopy(2, e.dstMacAddress);
  823. query.bind(3, e.typeNumber);
  824. query.bind(4, it->second.count);
  825. query.bindNoCopy(5, it->second.timestamp_last_occurrence);
  826. query.exec();
  827. query.reset();
  828. if (PyErr_CheckSignals()) throw py::error_already_set();
  829. }
  830. transaction.commit();
  831. }
  832. catch (std::exception &e) {
  833. std::cerr << "Exception in statistics_db::" << __func__ << ": " << e.what() << std::endl;
  834. }
  835. }