statistics.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * Class providing containers and access methods for statistical data collection.
  3. */
  4. #ifndef CPP_PCAPREADER_STATISTICS_H
  5. #define CPP_PCAPREADER_STATISTICS_H
  6. #include <vector>
  7. #include <unordered_map>
  8. #include <list>
  9. #include <tuple>
  10. #include <tins/timestamp.h>
  11. #include <tins/ip_address.h>
  12. #include "utilities.h"
  13. using namespace Tins;
  14. /*
  15. * Definition of structs used in unordered_map fields
  16. */
  17. /*
  18. * Struct used as data structure for method get_stats_for_ip, represents:
  19. * - Incoming bandwidth in KBits
  20. * - Outgoing bandwidth in KBits
  21. * - Number of incoming packets per second
  22. * - Number of outgoing packets per second
  23. * - Average size of sent packets in kbytes
  24. * - Average size of received packets in kybtes
  25. * - Average value of TCP option Maximum Segment Size (MSS)
  26. */
  27. struct ip_stats {
  28. float bandwidthKBitsIn;
  29. float bandwidthKBitsOut;
  30. float packetPerSecondIn;
  31. float packetPerSecondOut;
  32. float AvgPacketSizeSent;
  33. float AvgPacketSizeRecv;
  34. };
  35. /*
  36. * Struct used to represent a conversation by:
  37. * - IP address A
  38. * - Port A
  39. * - IP address B
  40. * - Port B
  41. */
  42. struct conv{
  43. std::string ipAddressA;
  44. int portA;
  45. std::string ipAddressB;
  46. int portB;
  47. bool operator==(const conv &other) const {
  48. return ipAddressA == other.ipAddressA
  49. && portA == other.portA
  50. &&ipAddressB == other.ipAddressB
  51. && portB == other.portB;
  52. }
  53. };
  54. /*
  55. * Struct used to represent:
  56. * - IP address (IPv4 or IPv6)
  57. * - MSS value
  58. */
  59. struct ipAddress_mss {
  60. std::string ipAddress;
  61. int mssValue;
  62. bool operator==(const ipAddress_mss &other) const {
  63. return ipAddress == other.ipAddress
  64. && mssValue == other.mssValue;
  65. }
  66. };
  67. /*
  68. * Struct used to represent:
  69. * - IP address (IPv4 or IPv6)
  70. * - ToS value
  71. */
  72. struct ipAddress_tos {
  73. std::string ipAddress;
  74. int tosValue;
  75. bool operator==(const ipAddress_tos &other) const {
  76. return ipAddress == other.ipAddress
  77. && tosValue == other.tosValue;
  78. }
  79. };
  80. /*
  81. * Struct used to represent:
  82. * - IP address (IPv4 or IPv6)
  83. * - Window size
  84. */
  85. struct ipAddress_win {
  86. std::string ipAddress;
  87. int winSize;
  88. bool operator==(const ipAddress_win &other) const {
  89. return ipAddress == other.ipAddress
  90. && winSize == other.winSize;
  91. }
  92. };
  93. /*
  94. * Struct used to represent:
  95. * - IP address (IPv4 or IPv6)
  96. * - TTL value
  97. */
  98. struct ipAddress_ttl {
  99. std::string ipAddress;
  100. int ttlValue;
  101. bool operator==(const ipAddress_ttl &other) const {
  102. return ipAddress == other.ipAddress
  103. && ttlValue == other.ttlValue;
  104. }
  105. };
  106. /*
  107. * Struct used to represent:
  108. * - IP address (IPv4 or IPv6)
  109. * - Protocol (e.g. TCP, UDP, IPv4, IPv6)
  110. */
  111. struct ipAddress_protocol {
  112. std::string ipAddress;
  113. std::string protocol;
  114. bool operator==(const ipAddress_protocol &other) const {
  115. return ipAddress == other.ipAddress
  116. && protocol == other.protocol;
  117. }
  118. };
  119. /*
  120. * Struct used to represent:
  121. * - Number of received packets
  122. * - Number of sent packets
  123. * - Data received in kbytes
  124. * - Data sent in kbytes
  125. */
  126. struct entry_ipStat {
  127. long pkts_received;
  128. long pkts_sent;
  129. float kbytes_received;
  130. float kbytes_sent;
  131. std::string ip_class;
  132. // Collects statstics over time interval
  133. std::vector<float> interval_pkt_rate;
  134. float max_interval_pkt_rate;
  135. float min_interval_pkt_rate;
  136. std::vector<std::chrono::microseconds> pkts_sent_timestamp;
  137. std::vector<std::chrono::microseconds> pkts_received_timestamp;
  138. bool operator==(const entry_ipStat &other) const {
  139. return pkts_received == other.pkts_received
  140. && pkts_sent == other.pkts_sent
  141. && kbytes_sent == other.kbytes_sent
  142. && kbytes_received == other.kbytes_received
  143. && interval_pkt_rate == other.interval_pkt_rate
  144. && max_interval_pkt_rate == other.max_interval_pkt_rate
  145. && min_interval_pkt_rate == other.min_interval_pkt_rate
  146. && ip_class == other.ip_class
  147. && pkts_sent_timestamp == other.pkts_sent_timestamp
  148. && pkts_received_timestamp == other.pkts_received_timestamp;
  149. }
  150. };
  151. /*
  152. * Struct used to represent interval statistics:
  153. * - # packets
  154. * - # bytes
  155. * - IP source entropy
  156. * - IP destination entropy
  157. * - IP source cumulative entropy
  158. * - IP destination cumulative entropy
  159. * - # packets that have payload
  160. * - # incorrect TCP checksum
  161. * - # correct TCP checksum
  162. * - # novel IPs
  163. * - # novel TTL
  164. * - # novel Window Size
  165. * - # novel ToS
  166. * - # novel MSS
  167. */
  168. struct entry_intervalStat {
  169. int pkts_count;
  170. float kbytes;
  171. float ip_src_entropy;
  172. float ip_dst_entropy;
  173. float ip_src_cum_entropy;
  174. float ip_dst_cum_entropy;
  175. int payload_count;
  176. int incorrect_tcp_checksum_count;
  177. int correct_tcp_checksum_count;
  178. int novel_ip_count;
  179. int novel_ttl_count;
  180. int novel_win_size_count;
  181. int novel_tos_count;
  182. int novel_mss_count;
  183. int novel_port_count;
  184. bool operator==(const entry_intervalStat &other) const {
  185. return pkts_count == other.pkts_count
  186. && kbytes == other.kbytes
  187. && ip_src_entropy == other.ip_src_entropy
  188. && ip_dst_entropy == other.ip_dst_entropy
  189. && ip_src_cum_entropy == other.ip_src_cum_entropy
  190. && ip_dst_cum_entropy == other.ip_dst_cum_entropy
  191. && payload_count == other.payload_count
  192. && incorrect_tcp_checksum_count == other.incorrect_tcp_checksum_count
  193. && novel_ip_count == other.novel_ip_count
  194. && novel_ttl_count == other.novel_ttl_count
  195. && novel_win_size_count == other.novel_win_size_count
  196. && novel_tos_count == other.novel_tos_count
  197. && novel_mss_count == other.novel_mss_count
  198. && novel_port_count == other.novel_port_count;
  199. }
  200. };
  201. /*
  202. * Struct used to represent converstaion statistics:
  203. * - # packets
  204. * - Average packet rate
  205. * - Timestamps of packets
  206. * - Inter-arrival time
  207. * - Average inter-arrival time
  208. */
  209. struct entry_convStat {
  210. long pkts_count;
  211. float avg_pkt_rate;
  212. std::vector<std::chrono::microseconds> pkts_timestamp;
  213. std::vector<std::chrono::microseconds> interarrival_time;
  214. std::chrono::microseconds avg_interarrival_time;
  215. bool operator==(const entry_convStat &other) const {
  216. return pkts_count == other.pkts_count
  217. && avg_pkt_rate == avg_pkt_rate
  218. && pkts_timestamp == other.pkts_timestamp
  219. && interarrival_time == other.interarrival_time
  220. && avg_interarrival_time == other.avg_interarrival_time;
  221. }
  222. };
  223. /*
  224. * Struct used to represent:
  225. * - IP address (IPv4 or IPv6)
  226. - Traffic direction (out: outgoing connection, in: incoming connection)
  227. * - Port number
  228. */
  229. struct ipAddress_inOut_port {
  230. std::string ipAddress;
  231. std::string trafficDirection;
  232. int portNumber;
  233. bool operator==(const ipAddress_inOut_port &other) const {
  234. return ipAddress == other.ipAddress
  235. && trafficDirection == other.trafficDirection
  236. && portNumber == other.portNumber;
  237. }
  238. };
  239. /*
  240. * Definition of hash functions for structs used as key in unordered_map
  241. */
  242. namespace std {
  243. template<>
  244. struct hash<ipAddress_ttl> {
  245. std::size_t operator()(const ipAddress_ttl &k) const {
  246. using std::size_t;
  247. using std::hash;
  248. using std::string;
  249. return ((hash<string>()(k.ipAddress)
  250. ^ (hash<int>()(k.ttlValue) << 1)) >> 1);
  251. }
  252. };
  253. template<>
  254. struct hash<ipAddress_mss> {
  255. std::size_t operator()(const ipAddress_mss &k) const {
  256. using std::size_t;
  257. using std::hash;
  258. using std::string;
  259. return ((hash<string>()(k.ipAddress)
  260. ^ (hash<int>()(k.mssValue) << 1)) >> 1);
  261. }
  262. };
  263. template<>
  264. struct hash<ipAddress_tos> {
  265. std::size_t operator()(const ipAddress_tos &k) const {
  266. using std::size_t;
  267. using std::hash;
  268. using std::string;
  269. return ((hash<string>()(k.ipAddress)
  270. ^ (hash<int>()(k.tosValue) << 1)) >> 1);
  271. }
  272. };
  273. template<>
  274. struct hash<ipAddress_win> {
  275. std::size_t operator()(const ipAddress_win &k) const {
  276. using std::size_t;
  277. using std::hash;
  278. using std::string;
  279. return ((hash<string>()(k.ipAddress)
  280. ^ (hash<int>()(k.winSize) << 1)) >> 1);
  281. }
  282. };
  283. template<>
  284. struct hash<conv> {
  285. std::size_t operator()(const conv &k) const {
  286. using std::size_t;
  287. using std::hash;
  288. using std::string;
  289. return ((hash<string>()(k.ipAddressA)
  290. ^ (hash<int>()(k.portA) << 1)) >> 1)
  291. ^ ((hash<string>()(k.ipAddressB)
  292. ^ (hash<int>()(k.portB) << 1)) >> 1);
  293. }
  294. };
  295. template<>
  296. struct hash<ipAddress_protocol> {
  297. std::size_t operator()(const ipAddress_protocol &k) const {
  298. using std::size_t;
  299. using std::hash;
  300. using std::string;
  301. return ((hash<string>()(k.ipAddress)
  302. ^ (hash<string>()(k.protocol) << 1)) >> 1);
  303. }
  304. };
  305. template<>
  306. struct hash<ipAddress_inOut_port> {
  307. std::size_t operator()(const ipAddress_inOut_port &k) const {
  308. using std::size_t;
  309. using std::hash;
  310. using std::string;
  311. return ((hash<string>()(k.ipAddress)
  312. ^ (hash<string>()(k.trafficDirection) << 1)) >> 1)
  313. ^ (hash<int>()(k.portNumber) << 1);
  314. }
  315. };
  316. }
  317. class statistics {
  318. public:
  319. /*
  320. * Constructor
  321. */
  322. statistics();
  323. /*
  324. * Methods
  325. */
  326. std::string getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const;
  327. /*
  328. * Access methods for containers
  329. */
  330. void incrementPacketCount();
  331. void calculateIPIntervalPacketRate(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp);
  332. void incrementMSScount(std::string ipAddress, int mssValue);
  333. void incrementWinCount(std::string ipAddress, int winSize);
  334. void addConvStat(std::string ipAddressSender,int sport,std::string ipAddressReceiver,int dport, std::chrono::microseconds timestamp);
  335. std::vector<float> calculateIPsCumEntropy();
  336. std::vector<float> calculateLastIntervalIPsEntropy(std::chrono::microseconds intervalStartTimestamp);
  337. void addIntervalStat(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp, std::chrono::microseconds lastPktTimestamp);
  338. void checkPayload(const PDU *pdu_l4);
  339. void checkTCPChecksum(std::string ipAddressSender, std::string ipAddressReceiver, TCP tcpPkt);
  340. void checkToS(uint8_t ToS);
  341. void incrementToScount(std::string ipAddress, int tosValue);
  342. void incrementTTLcount(std::string ipAddress, int ttlValue);
  343. void incrementProtocolCount(std::string ipAddress, std::string protocol);
  344. void incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
  345. int incomingPort);
  346. int getProtocolCount(std::string ipAddress, std::string protocol);
  347. void setTimestampFirstPacket(Tins::Timestamp ts);
  348. void setTimestampLastPacket(Tins::Timestamp ts);
  349. Tins::Timestamp getTimestampFirstPacket();
  350. Tins::Timestamp getTimestampLastPacket();
  351. void assignMacAddress(std::string ipAddress, std::string macAddress);
  352. void addIpStat_packetSent(std::string filePath, std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp);
  353. int getPacketCount();
  354. int getSumPacketSize();
  355. void addMSS(std::string ipAddress, int MSSvalue);
  356. void writeToDatabase(std::string database_path);
  357. void addPacketSize(uint32_t packetSize);
  358. std::string getCaptureDurationTimestamp() const;
  359. float getCaptureDurationSeconds() const;
  360. float getAvgPacketSize() const;
  361. void printStats(std::string ipAddress);
  362. bool getDoExtraTests();
  363. void setDoExtraTests(bool var);
  364. /*
  365. * IP Address-specific statistics
  366. */
  367. ip_stats getStatsForIP(std::string ipAddress);
  368. private:
  369. /*
  370. * Data fields
  371. */
  372. Tins::Timestamp timestamp_firstPacket;
  373. Tins::Timestamp timestamp_lastPacket;
  374. float sumPacketSize = 0;
  375. int packetCount = 0;
  376. /* Extra tests includes:
  377. * - calculate IPs entropies for intervals
  378. * - calculate IPs cumulative entropies interval-wise
  379. * - check payload availability
  380. * - chech TCP checksum correctness
  381. */
  382. bool doExtraTests = false;
  383. int payloadCount = 0;
  384. int incorrectTCPChecksumCount = 0;
  385. int correctTCPChecksumCount = 0;
  386. // Variables that are used for interval-wise statistics
  387. int intervalPayloadCount = 0;
  388. int intervalIncorrectTCPChecksumCount = 0;
  389. int intervalCorrectTCPChecksumCount = 0;
  390. int intervalCumPktCount = 0;
  391. float intervalCumSumPktSize = 0;
  392. int intervalCumNovelIPCount = 0;
  393. int intervalCumNovelTTLCount = 0;
  394. int intervalCumNovelWinSizeCount = 0;
  395. int intervalCumNovelToSCount = 0;
  396. int intervalCumNovelMSSCount = 0;
  397. int intervalCumNovelPortCount = 0;
  398. /*
  399. * Data containers
  400. */
  401. // {IP Address, TTL value, count}
  402. std::unordered_map<ipAddress_ttl, int> ttl_distribution;
  403. // {IP Address, MSS value, count}
  404. std::unordered_map<ipAddress_mss, int> mss_distribution;
  405. // {IP Address, Win size, count}
  406. std::unordered_map<ipAddress_win, int> win_distribution;
  407. // {IP Address, ToS value, count}
  408. std::unordered_map<ipAddress_tos, int> tos_distribution;
  409. // {IP Address A, Port A, IP Address B, Port B, #packets, packets timestamps, inter-arrival times,
  410. // average of inter-arrival times}
  411. std::unordered_map<conv, entry_convStat> conv_statistics;
  412. // {Last timestamp in the interval, #packets, #bytes, source IP entropy, destination IP entropy,
  413. // source IP cumulative entropy, destination IP cumulative entropy, #payload, #incorrect TCP checksum,
  414. // #correct TCP checksum, #novel IP, #novel TTL, #novel Window Size, #novel ToS,#novel MSS}
  415. std::unordered_map<std::string, entry_intervalStat> interval_statistics;
  416. // {TTL value, count}
  417. std::unordered_map<int, int> ttl_values;
  418. // {Win size, count}
  419. std::unordered_map<int, int> win_values;
  420. // {ToS, count}
  421. std::unordered_map<int, int> tos_values;
  422. // {MSS, count}
  423. std::unordered_map<int, int> mss_values;
  424. // {Port, count}
  425. std::unordered_map<int, int> port_values;
  426. // {IP Address, Protocol, count}
  427. std::unordered_map<ipAddress_protocol, int> protocol_distribution;
  428. // {IP Address, #received packets, #sent packets, Data received in kbytes, Data sent in kbytes}
  429. std::unordered_map<std::string, entry_ipStat> ip_statistics;
  430. // {IP Address, in_out, Port Number, count}
  431. std::unordered_map<ipAddress_inOut_port, int> ip_ports;
  432. // {IP Address, MAC Address}
  433. std::unordered_map<std::string, std::string> ip_mac_mapping;
  434. };
  435. #endif //CPP_PCAPREADER_STATISTICS_H