statistics.cpp 28 KB

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