statistics.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. // Aidmar
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <math.h>
  6. #include <sstream>
  7. #include <SQLiteCpp/SQLiteCpp.h>
  8. #include "statistics_db.h"
  9. #include "statistics.h"
  10. // Aidmar
  11. /**
  12. * Calculates entropy of source and destination IPs for last time interval.
  13. * @param intervalStartTimestamp The timstamp where the interval starts.
  14. */
  15. std::vector<float> statistics::calculateLastIntervalIPsEntropy(std::chrono::microseconds intervalStartTimestamp){
  16. std::vector <int> IPsSrcPktsCounts;
  17. std::vector <int> IPsDstPktsCounts;
  18. std::vector <float> IPsSrcProb;
  19. std::vector <float> IPsDstProb;
  20. int pktsSent = 0, pktsReceived = 0;
  21. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  22. int indexStartSent = getClosestIndex(i->second.pktsSentTimestamp, intervalStartTimestamp);
  23. int IPsSrcPktsCount = i->second.pktsSentTimestamp.size() - indexStartSent;
  24. IPsSrcPktsCounts.push_back(IPsSrcPktsCount);
  25. pktsSent += IPsSrcPktsCount;
  26. int indexStartReceived = getClosestIndex(i->second.pktsReceivedTimestamp, intervalStartTimestamp);
  27. int IPsDstPktsCount = i->second.pktsReceivedTimestamp.size() - indexStartReceived;
  28. IPsDstPktsCounts.push_back(IPsDstPktsCount);
  29. pktsReceived += IPsDstPktsCount;
  30. }
  31. for (auto i = IPsSrcPktsCounts.begin(); i != IPsSrcPktsCounts.end(); i++) {
  32. IPsSrcProb.push_back((float)*i/pktsSent);
  33. }
  34. for (auto i = IPsDstPktsCounts.begin(); i != IPsDstPktsCounts.end(); i++) {
  35. IPsDstProb.push_back((float)*i/pktsReceived);
  36. }
  37. // Calculate IP source entropy
  38. float IPsSrcEntropy = 0;
  39. for(unsigned i=0; i < IPsSrcProb.size();i++){
  40. if (IPsSrcProb[i] > 0)
  41. IPsSrcEntropy += - IPsSrcProb[i]*log2(IPsSrcProb[i]);
  42. }
  43. // Calculate IP destination entropy
  44. float IPsDstEntropy = 0;
  45. for(unsigned i=0; i < IPsDstProb.size();i++){
  46. if (IPsDstProb[i] > 0)
  47. IPsDstEntropy += - IPsDstProb[i]*log2(IPsDstProb[i]);
  48. }
  49. std::vector<float> entropies = {IPsSrcEntropy, IPsDstEntropy};
  50. return entropies;
  51. }
  52. // Aidmar
  53. /**
  54. * Calculates cumulative entropy of source and destination IPs, i.e., the entropy for packets from the beginning of the pcap file.
  55. */
  56. std::vector<float> statistics::calculateIPsCumEntropy(){
  57. std::vector <std::string> IPs;
  58. std::vector <float> IPsSrcProb;
  59. std::vector <float> IPsDstProb;
  60. //std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  61. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  62. IPs.push_back(i->first);
  63. IPsSrcProb.push_back((float)i->second.pkts_sent/packetCount);
  64. IPsDstProb.push_back((float)i->second.pkts_received/packetCount);
  65. }
  66. //std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  67. //auto duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count()*1e-6;
  68. //std::cout<< "CumEntCalc -> ip_statistics loop: " << duration << " sec" << std::endl;
  69. // Calculate IP source entropy
  70. float IPsSrcEntropy = 0;
  71. for(unsigned i=0; i < IPsSrcProb.size();i++){
  72. if (IPsSrcProb[i] > 0)
  73. IPsSrcEntropy += - IPsSrcProb[i]*log2(IPsSrcProb[i]);
  74. }
  75. //std::cout << packetCount << ": SrcEnt: " << IPsSrcEntropy << "\n";
  76. // Calculate IP destination entropy
  77. float IPsDstEntropy = 0;
  78. for(unsigned i=0; i < IPsDstProb.size();i++){
  79. if (IPsDstProb[i] > 0)
  80. IPsDstEntropy += - IPsDstProb[i]*log2(IPsDstProb[i]);
  81. }
  82. //std::cout << packetCount << ": DstEnt: " << IPsDstEntropy << "\n";
  83. std::vector<float> entropies = {IPsSrcEntropy, IPsDstEntropy};
  84. return entropies;
  85. }
  86. // Aidmar
  87. /**
  88. * Calculates sending packet rate for each IP in last time interval. Finds min and max packet rate and adds them to ip_statistics map.
  89. * @param intervalStartTimestamp The timstamp where the interval starts.
  90. */
  91. void statistics::calculateIPIntervalPacketRate(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp){
  92. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  93. int indexStartSent = getClosestIndex(i->second.pktsSentTimestamp, intervalStartTimestamp);
  94. int IPsSrcPktsCount = i->second.pktsSentTimestamp.size() - indexStartSent;
  95. float interval_pkt_rate = (float) IPsSrcPktsCount * 1000000 / interval.count(); // used 10^6 because interval in microseconds
  96. i->second.interval_pkt_rate.push_back(interval_pkt_rate);
  97. if(interval_pkt_rate > i->second.max_pkt_rate || i->second.max_pkt_rate == 0)
  98. i->second.max_pkt_rate = interval_pkt_rate;
  99. if(interval_pkt_rate < i->second.min_pkt_rate || i->second.min_pkt_rate == 0)
  100. i->second.min_pkt_rate = interval_pkt_rate;
  101. }
  102. }
  103. // Aidmar
  104. /**
  105. * Registers statistical data for last time interval. Calculates packet rate. Calculates IPs entropy. Calculates IPs cumulative entropy.
  106. * @param intervalStartTimestamp The timstamp where the interval starts.
  107. * @param intervalEndTimestamp The timstamp where the interval ends.
  108. * @param previousPacketCount The total number of packets in last interval.
  109. */
  110. void statistics::addIntervalStat(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp, std::chrono::microseconds intervalEndTimestamp, int previousPacketCount, float previousSumPacketSize){
  111. // Add packet rate for each IP to ip_statistics map
  112. calculateIPIntervalPacketRate(interval, intervalStartTimestamp);
  113. std::vector<float> ipEntopies = calculateLastIntervalIPsEntropy(intervalStartTimestamp);
  114. std::vector<float> ipCumEntopies = calculateIPsCumEntropy();
  115. std::string lastPktTimestamp_s = std::to_string(intervalEndTimestamp.count());
  116. interval_statistics[lastPktTimestamp_s].pkts_count = packetCount - previousPacketCount;
  117. interval_statistics[lastPktTimestamp_s].kbytes = (float(sumPacketSize - previousSumPacketSize) / 1024);
  118. if(ipEntopies.size()>1){
  119. interval_statistics[lastPktTimestamp_s].ip_src_entropy = ipEntopies[0];
  120. interval_statistics[lastPktTimestamp_s].ip_dst_entropy = ipEntopies[1];
  121. }
  122. if(ipCumEntopies.size()>1){
  123. interval_statistics[lastPktTimestamp_s].ip_src_cum_entropy = ipCumEntopies[0];
  124. interval_statistics[lastPktTimestamp_s].ip_dst_cum_entropy = ipCumEntopies[1];
  125. }
  126. }
  127. // Aidmar
  128. /**
  129. * Registers statistical data for a sent packet in a given conversation (two IPs, two ports).
  130. * Increments the counter packets_A_B or packets_B_A.
  131. * Adds the timestamp of the packet in pkts_A_B_timestamp or pkts_B_A_timestamp.
  132. * @param ipAddressSender The sender IP address.
  133. * @param sport The source port.
  134. * @param ipAddressReceiver The receiver IP address.
  135. * @param dport The destination port.
  136. * @param timestamp The timestamp of the packet.
  137. */
  138. void statistics::addConvStat(std::string ipAddressSender,int sport,std::string ipAddressReceiver,int dport, std::chrono::microseconds timestamp){
  139. conv f1 = {ipAddressReceiver, dport, ipAddressSender, sport};
  140. conv f2 = {ipAddressSender, sport, ipAddressReceiver, dport};
  141. // if already exist A(ipAddressReceiver, dport), B(ipAddressSender, sport)
  142. if (conv_statistics.count(f1)>0){
  143. conv_statistics[f1].pkts_B_A++; // increment packets number from B to A
  144. conv_statistics[f1].pkts_B_A_timestamp.push_back(timestamp);
  145. // Calculate reply delay considering only delay of first two reply packets (TCP handshake)
  146. //if(conv_statistics[f1].pkts_A_B_timestamp.size()>0 && conv_statistics[f1].pkts_A_B_timestamp.size()<=2){
  147. conv_statistics[f1].pkts_delay.push_back(std::chrono::duration_cast<std::chrono::microseconds> (timestamp - conv_statistics[f1].pkts_A_B_timestamp.back()));
  148. //}
  149. }
  150. else{
  151. conv_statistics[f2].pkts_A_B++; // increment packets number from A to B
  152. conv_statistics[f2].pkts_A_B_timestamp.push_back(timestamp);
  153. }
  154. }
  155. // Aidmar
  156. /**
  157. * Increments the packet counter for the given IP address and MSS value.
  158. * @param ipAddress The IP address whose MSS packet counter should be incremented.
  159. * @param mssValue The MSS value of the packet.
  160. */
  161. void statistics::incrementMSScount(std::string ipAddress, int mssValue) {
  162. mss_distribution[{ipAddress, mssValue}]++;
  163. }
  164. // Aidmar
  165. /**
  166. * Increments the packet counter for the given IP address and window size.
  167. * @param ipAddress The IP address whose window size packet counter should be incremented.
  168. * @param winSize The window size of the packet.
  169. */
  170. void statistics::incrementWinCount(std::string ipAddress, int winSize) {
  171. win_distribution[{ipAddress, winSize}]++;
  172. }
  173. /**
  174. * Increments the packet counter for the given IP address and TTL value.
  175. * @param ipAddress The IP address whose TTL packet counter should be incremented.
  176. * @param ttlValue The TTL value of the packet.
  177. */
  178. void statistics::incrementTTLcount(std::string ipAddress, int ttlValue) {
  179. ttl_distribution[{ipAddress, ttlValue}]++;
  180. }
  181. /**
  182. * Increments the protocol counter for the given IP address and protocol.
  183. * @param ipAddress The IP address whose protocol packet counter should be incremented.
  184. * @param protocol The protocol of the packet.
  185. */
  186. void statistics::incrementProtocolCount(std::string ipAddress, std::string protocol) {
  187. protocol_distribution[{ipAddress, protocol}]++;
  188. }
  189. /**
  190. * Returns the number of packets seen for the given IP address and protocol.
  191. * @param ipAddress The IP address whose packet count is wanted.
  192. * @param protocol The protocol whose packet count is wanted.
  193. * @return an integer: the number of packets
  194. */
  195. int statistics::getProtocolCount(std::string ipAddress, std::string protocol) {
  196. return protocol_distribution[{ipAddress, protocol}];
  197. }
  198. /**
  199. * Increments the packet counter for
  200. * - the given sender IP address with outgoing port and
  201. * - the given receiver IP address with incoming port.
  202. * @param ipAddressSender The IP address of the packet sender.
  203. * @param outgoingPort The port used by the sender.
  204. * @param ipAddressReceiver The IP address of the packet receiver.
  205. * @param incomingPort The port used by the receiver.
  206. */
  207. void statistics::incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
  208. int incomingPort) {
  209. ip_ports[{ipAddressSender, "out", outgoingPort}]++;
  210. ip_ports[{ipAddressReceiver, "in", incomingPort}]++;
  211. }
  212. /**
  213. * Creates a new statistics object.
  214. */
  215. statistics::statistics(void) {
  216. }
  217. /**
  218. * Stores the assignment IP address -> MAC address.
  219. * @param ipAddress The IP address belonging to the given MAC address.
  220. * @param macAddress The MAC address belonging to the given IP address.
  221. */
  222. void statistics::assignMacAddress(std::string ipAddress, std::string macAddress) {
  223. ip_mac_mapping[ipAddress] = macAddress;
  224. }
  225. /**
  226. * Registers statistical data for a sent packet. Increments the counter packets_sent for the sender and
  227. * packets_received for the receiver. Adds the bytes as kbytes_sent (sender) and kybtes_received (receiver).
  228. * @param ipAddressSender The IP address of the packet sender.
  229. * @param ipAddressReceiver The IP address of the packet receiver.
  230. * @param bytesSent The packet's size.
  231. */
  232. void statistics::addIpStat_packetSent(std::string filePath, std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp) {
  233. // Aidmar - Adding IP as a sender for first time
  234. if(ip_statistics[ipAddressSender].pkts_sent==0){
  235. // Add the IP class
  236. ip_statistics[ipAddressSender].ip_class = getIPv4Class(ipAddressSender);
  237. // Initialize packet rates
  238. /*ip_statistics[ipAddressSender].max_pkt_rate = 0;
  239. ip_statistics[ipAddressSender].min_pkt_rate = 0;
  240. // Caculate Mahoney anomaly score for ip.src
  241. float ipSrc_Mahoney_score = 0;
  242. // s_r: The number of IP sources (the different values)
  243. // n: The number of the total instances
  244. // s_t: The "time" since last anomalous (novel) IP was appeared
  245. int s_t = 0, n = 0, s_r = 0;
  246. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  247. if (i->second.pkts_sent > 0)
  248. s_r++;
  249. }
  250. if(s_r > 0){
  251. // The number of the total instances
  252. n = packetCount;
  253. // The packet count when the last novel IP was added as a sender
  254. int pktCntNvlSndr = 0;
  255. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  256. if (pktCntNvlSndr < i->second.firstAppearAsSenderPktCount)
  257. pktCntNvlSndr = i->second.firstAppearAsSenderPktCount;
  258. }
  259. // The "time" since last anomalous (novel) IP was appeared
  260. s_t = packetCount - pktCntNvlSndr + 1;
  261. ipSrc_Mahoney_score = (float)s_t*n/s_r;
  262. }
  263. ip_statistics[ipAddressSender].firstAppearAsSenderPktCount = packetCount;
  264. ip_statistics[ipAddressSender].sourceAnomalyScore = ipSrc_Mahoney_score;
  265. */
  266. }
  267. // Aidmar - Adding IP as a receiver for first time
  268. if(ip_statistics[ipAddressReceiver].pkts_received==0){
  269. // Add the IP class
  270. ip_statistics[ipAddressReceiver].ip_class = getIPv4Class(ipAddressReceiver);
  271. // Caculate Mahoney anomaly score for ip.dst
  272. /*float ipDst_Mahoney_score = 0;
  273. // s_r: The number of IP sources (the different values)
  274. // n: The number of the total instances
  275. // s_t: The "time" since last anomalous (novel) IP was appeared
  276. int s_t = 0, n = 0, s_r = 0;
  277. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  278. if (i->second.pkts_received > 0)
  279. s_r++;
  280. }
  281. if(s_r > 0){
  282. // The number of the total instances
  283. n = packetCount;
  284. // The packet count when the last novel IP was added as a sender
  285. int pktCntNvlRcvr = 0;
  286. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  287. if (pktCntNvlRcvr < i->second.firstAppearAsReceiverPktCount)
  288. pktCntNvlRcvr = i->second.firstAppearAsReceiverPktCount;
  289. }
  290. // The "time" since last anomalous (novel) IP was appeared
  291. s_t = packetCount - pktCntNvlRcvr + 1;
  292. ipDst_Mahoney_score = (float)s_t*n/s_r;
  293. }
  294. ip_statistics[ipAddressReceiver].firstAppearAsReceiverPktCount = packetCount;
  295. ip_statistics[ipAddressReceiver].destinationAnomalyScore = ipDst_Mahoney_score;
  296. */
  297. }
  298. // Update stats for packet sender
  299. ip_statistics[ipAddressSender].kbytes_sent += (float(bytesSent) / 1024);
  300. ip_statistics[ipAddressSender].pkts_sent++;
  301. // Aidmar
  302. ip_statistics[ipAddressSender].pktsSentTimestamp.push_back(timestamp);
  303. //// Aidmar - calculate packet rate (assumption: max_pkt_rate=1/smallest time between two consecutive pkts)
  304. // resulting in very big rates, therefore it could be better to calculate pkt rate on time intervals
  305. /*if(ip_statistics[ipAddressSender].pktsSentTimestamp.size() > 0){
  306. std::chrono::microseconds temp_pkt_consecutive_time = timestamp - ip_statistics[ipAddressSender].pktsSentTimestamp.back();
  307. float temp_pkt_rate = (float) 1000000/temp_pkt_consecutive_time.count(); // pkt per sec = 10**6/micro sec
  308. if(temp_pkt_rate > ip_statistics[ipAddressSender].max_pkt_rate || ip_statistics[ipAddressSender].max_pkt_rate == 0)
  309. ip_statistics[ipAddressSender].max_pkt_rate = temp_pkt_rate;
  310. if(temp_pkt_rate < ip_statistics[ipAddressSender].min_pkt_rate || ip_statistics[ipAddressSender].min_pkt_rate == 0)
  311. ip_statistics[ipAddressSender].min_pkt_rate = temp_pkt_rate;
  312. }*/
  313. // Update stats for packet receiver
  314. ip_statistics[ipAddressReceiver].kbytes_received += (float(bytesSent) / 1024);
  315. ip_statistics[ipAddressReceiver].pkts_received++;
  316. // Aidmar
  317. ip_statistics[ipAddressReceiver].pktsReceivedTimestamp.push_back(timestamp);
  318. }
  319. /**
  320. * Registers a value of the TCP option Maximum Segment Size (MSS).
  321. * @param ipAddress The IP address which sent the TCP packet.
  322. * @param MSSvalue The MSS value found.
  323. */
  324. void statistics::addMSS(std::string ipAddress, int MSSvalue) {
  325. ip_sumMss[ipAddress] += MSSvalue;
  326. }
  327. /**
  328. * Setter for the timestamp_firstPacket field.
  329. * @param ts The timestamp of the first packet in the PCAP file.
  330. */
  331. void statistics::setTimestampFirstPacket(Tins::Timestamp ts) {
  332. timestamp_firstPacket = ts;
  333. }
  334. /**
  335. * Setter for the timestamp_lastPacket field.
  336. * @param ts The timestamp of the last packet in the PCAP file.
  337. */
  338. void statistics::setTimestampLastPacket(Tins::Timestamp ts) {
  339. timestamp_lastPacket = ts;
  340. }
  341. // Aidmar
  342. /**
  343. * Getter for the timestamp_firstPacket field.
  344. */
  345. Tins::Timestamp statistics::getTimestampFirstPacket() {
  346. return timestamp_firstPacket;
  347. }
  348. /**
  349. * Getter for the timestamp_lastPacket field.
  350. */
  351. Tins::Timestamp statistics::getTimestampLastPacket() {
  352. return timestamp_lastPacket;
  353. }
  354. /**
  355. * Getter for the packetCount field.
  356. */
  357. int statistics::getPacketCount() {
  358. return packetCount;
  359. }
  360. /**
  361. * Getter for the sumPacketSize field.
  362. */
  363. int statistics::getSumPacketSize() {
  364. return sumPacketSize;
  365. }
  366. /**
  367. * Calculates the capture duration.
  368. * @return a formatted string HH:MM:SS.mmmmmm with
  369. * HH: hour, MM: minute, SS: second, mmmmmm: microseconds
  370. */
  371. std::string statistics::getCaptureDurationTimestamp() const {
  372. // Calculate duration
  373. time_t t = (timestamp_lastPacket.seconds() - timestamp_firstPacket.seconds());
  374. time_t ms = (timestamp_lastPacket.microseconds() - timestamp_firstPacket.microseconds());
  375. long int hour = t / 3600;
  376. long int remainder = (t - hour * 3600);
  377. long int minute = remainder / 60;
  378. long int second = (remainder - minute * 60) % 60;
  379. long int microseconds = ms;
  380. // Build desired output format: YYYY-mm-dd hh:mm:ss
  381. char out[64];
  382. sprintf(out, "%02ld:%02ld:%02ld.%06ld ", hour, minute, second, microseconds);
  383. return std::string(out);
  384. }
  385. /**
  386. * Calculates the capture duration.
  387. * @return a formatted string SS.mmmmmm with
  388. * S: seconds (UNIX time), mmmmmm: microseconds
  389. */
  390. float statistics::getCaptureDurationSeconds() const {
  391. timeval d;
  392. d.tv_sec = timestamp_lastPacket.seconds() - timestamp_firstPacket.seconds();
  393. d.tv_usec = timestamp_lastPacket.microseconds() - timestamp_firstPacket.microseconds();
  394. char tmbuf[64], buf[64];
  395. auto nowtm = localtime(&(d.tv_sec));
  396. strftime(tmbuf, sizeof(tmbuf), "%S", nowtm);
  397. snprintf(buf, sizeof(buf), "%s.%06u", tmbuf, (uint) d.tv_usec);
  398. return std::stof(std::string(buf));
  399. }
  400. /**
  401. * Creates a timestamp based on a time_t seconds (UNIX time format) and microseconds.
  402. * @param seconds
  403. * @param microseconds
  404. * @return a formatted string Y-m-d H:M:S.m with
  405. * Y: year, m: month, d: day, H: hour, M: minute, S: second, m: microseconds
  406. */
  407. std::string statistics::getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const {
  408. timeval tv;
  409. tv.tv_sec = seconds;
  410. tv.tv_usec = microseconds;
  411. char tmbuf[64], buf[64];
  412. auto nowtm = localtime(&(tv.tv_sec));
  413. strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
  414. snprintf(buf, sizeof(buf), "%s.%06u", tmbuf, (uint) tv.tv_usec);
  415. return std::string(buf);
  416. }
  417. /**
  418. * Calculates the statistics for a given IP address.
  419. * @param ipAddress The IP address whose statistics should be calculated.
  420. * @return a ip_stats struct containing statistical data derived by the statistical data collected.
  421. */
  422. ip_stats statistics::getStatsForIP(std::string ipAddress) {
  423. float duration = getCaptureDurationSeconds();
  424. entry_ipStat ipStatEntry = ip_statistics[ipAddress];
  425. ip_stats s;
  426. s.bandwidthKBitsIn = (ipStatEntry.kbytes_received / duration) * 8;
  427. s.bandwidthKBitsOut = (ipStatEntry.kbytes_sent / duration) * 8;
  428. s.packetPerSecondIn = (ipStatEntry.pkts_received / duration);
  429. s.packetPerSecondOut = (ipStatEntry.pkts_sent / duration);
  430. s.AvgPacketSizeSent = (ipStatEntry.kbytes_sent / ipStatEntry.pkts_sent);
  431. s.AvgPacketSizeRecv = (ipStatEntry.kbytes_received / ipStatEntry.pkts_received);
  432. int sumMSS = ip_sumMss[ipAddress];
  433. int tcpPacketsSent = getProtocolCount(ipAddress, "TCP");
  434. s.AvgMaxSegmentSizeTCP = ((sumMSS > 0 && tcpPacketsSent > 0) ? (sumMSS / tcpPacketsSent) : 0);
  435. return s;
  436. }
  437. /**
  438. * Increments the packet counter.
  439. */
  440. void statistics::incrementPacketCount() {
  441. packetCount++;
  442. }
  443. /**
  444. * Prints the statistics of the PCAP and IP specific statistics for the given IP address.
  445. * @param ipAddress The IP address whose statistics should be printed. Can be empty "" to print only general file statistics.
  446. */
  447. void statistics::printStats(std::string ipAddress) {
  448. std::stringstream ss;
  449. ss << std::endl;
  450. ss << "Capture duration: " << getCaptureDurationSeconds() << " seconds" << std::endl;
  451. ss << "Capture duration (HH:MM:SS.mmmmmm): " << getCaptureDurationTimestamp() << std::endl;
  452. ss << "#Packets: " << packetCount << std::endl;
  453. ss << std::endl;
  454. // Print IP address specific statistics only if IP address was given
  455. if (ipAddress != "") {
  456. entry_ipStat e = ip_statistics[ipAddress];
  457. ss << "\n----- STATS FOR IP ADDRESS [" << ipAddress << "] -------" << std::endl;
  458. ss << std::endl << "KBytes sent: " << e.kbytes_sent << std::endl;
  459. ss << "KBytes received: " << e.kbytes_received << std::endl;
  460. ss << "Packets sent: " << e.pkts_sent << std::endl;
  461. ss << "Packets received: " << e.pkts_received << "\n\n";
  462. ip_stats is = getStatsForIP(ipAddress);
  463. ss << "Bandwidth IN: " << is.bandwidthKBitsIn << " kbit/s" << std::endl;
  464. ss << "Bandwidth OUT: " << is.bandwidthKBitsOut << " kbit/s" << std::endl;
  465. ss << "Packets per second IN: " << is.packetPerSecondIn << std::endl;
  466. ss << "Packets per second OUT: " << is.packetPerSecondOut << std::endl;
  467. ss << "Avg Packet Size Sent: " << is.AvgPacketSizeSent << " kbytes" << std::endl;
  468. ss << "Avg Packet Size Received: " << is.AvgPacketSizeRecv << " kbytes" << std::endl;
  469. ss << "Avg MSS: " << is.AvgMaxSegmentSizeTCP << " bytes" << std::endl;
  470. }
  471. std::cout << ss.str();
  472. }
  473. /**
  474. * Derives general PCAP file statistics from the collected statistical data and
  475. * writes all data into a SQLite database, located at database_path.
  476. * @param database_path The path of the SQLite database file ending with .sqlite3.
  477. */
  478. void statistics::writeToDatabase(std::string database_path) {
  479. // Generate general file statistics
  480. float duration = getCaptureDurationSeconds();
  481. long sumPacketsSent = 0, senderCountIP = 0;
  482. float sumBandwidthIn = 0.0, sumBandwidthOut = 0.0;
  483. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  484. sumPacketsSent += i->second.pkts_sent;
  485. // Consumed bandwith (bytes) for sending packets
  486. sumBandwidthIn += (i->second.kbytes_received / duration);
  487. sumBandwidthOut += (i->second.kbytes_sent / duration);
  488. senderCountIP++;
  489. }
  490. float avgPacketRate = (packetCount / duration);
  491. long avgPacketSize = getAvgPacketSize();
  492. long avgPacketsSentPerHost = (sumPacketsSent / senderCountIP);
  493. float avgBandwidthInKBits = (sumBandwidthIn / senderCountIP) * 8;
  494. float avgBandwidthOutInKBits = (sumBandwidthOut / senderCountIP) * 8;
  495. // Create database and write information
  496. statistics_db db(database_path);
  497. db.writeStatisticsFile(packetCount, getCaptureDurationSeconds(),
  498. getFormattedTimestamp(timestamp_firstPacket.seconds(), timestamp_firstPacket.microseconds()),
  499. getFormattedTimestamp(timestamp_lastPacket.seconds(), timestamp_lastPacket.microseconds()),
  500. avgPacketRate, avgPacketSize, avgPacketsSentPerHost, avgBandwidthInKBits,
  501. avgBandwidthOutInKBits);
  502. db.writeStatisticsIP(ip_statistics);
  503. db.writeStatisticsTTL(ttl_distribution);
  504. db.writeStatisticsIpMac(ip_mac_mapping);
  505. db.writeStatisticsMss(ip_sumMss);
  506. db.writeStatisticsPorts(ip_ports);
  507. db.writeStatisticsProtocols(protocol_distribution);
  508. // Aidmar
  509. db.writeStatisticsMss_dist(mss_distribution);
  510. db.writeStatisticsWin(win_distribution);
  511. db.writeStatisticsConv(conv_statistics);
  512. db.writeStatisticsInterval(interval_statistics);
  513. }
  514. /**
  515. * Returns the average packet size.
  516. * @return a float indicating the average packet size in kbytes.
  517. */
  518. float statistics::getAvgPacketSize() const {
  519. // AvgPktSize = (Sum of all packet sizes / #Packets)
  520. return (sumPacketSize / packetCount) / 1024;
  521. }
  522. /**
  523. * Adds the size of a packet (to be used to calculate the avg. packet size).
  524. * @param packetSize The size of the current packet in bytes.
  525. */
  526. void statistics::addPacketSize(uint32_t packetSize) {
  527. sumPacketSize += ((float) packetSize);
  528. }