statistics.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. bool operator==(const entry_intervalStat &other) const {
  184. return pkts_count == other.pkts_count
  185. && kbytes == other.kbytes
  186. && ip_src_entropy == other.ip_src_entropy
  187. && ip_dst_entropy == other.ip_dst_entropy
  188. && ip_src_cum_entropy == other.ip_src_cum_entropy
  189. && ip_dst_cum_entropy == other.ip_dst_cum_entropy
  190. && payload_count == other.payload_count
  191. && incorrect_tcp_checksum_count == other.incorrect_tcp_checksum_count
  192. && novel_ip_count == other.novel_ip_count
  193. && novel_ttl_count == other.novel_ttl_count
  194. && novel_win_size_count == other.novel_win_size_count
  195. && novel_tos_count == other.novel_tos_count
  196. && novel_mss_count == other.novel_mss_count;
  197. }
  198. };
  199. /*
  200. * Struct used to represent converstaion statistics:
  201. * - # packets
  202. * - Average packet rate
  203. * - Timestamps of packets
  204. * - Inter-arrival time
  205. * - Average inter-arrival time
  206. */
  207. struct entry_convStat {
  208. long pkts_count;
  209. float avg_pkt_rate;
  210. std::vector<std::chrono::microseconds> pkts_timestamp;
  211. std::vector<std::chrono::microseconds> interarrival_time;
  212. std::chrono::microseconds avg_interarrival_time;
  213. bool operator==(const entry_convStat &other) const {
  214. return pkts_count == other.pkts_count
  215. && avg_pkt_rate == avg_pkt_rate
  216. && pkts_timestamp == other.pkts_timestamp
  217. && interarrival_time == other.interarrival_time
  218. && avg_interarrival_time == other.avg_interarrival_time;
  219. }
  220. };
  221. /*
  222. * Struct used to represent:
  223. * - IP address (IPv4 or IPv6)
  224. - Traffic direction (out: outgoing connection, in: incoming connection)
  225. * - Port number
  226. */
  227. struct ipAddress_inOut_port {
  228. std::string ipAddress;
  229. std::string trafficDirection;
  230. int portNumber;
  231. bool operator==(const ipAddress_inOut_port &other) const {
  232. return ipAddress == other.ipAddress
  233. && trafficDirection == other.trafficDirection
  234. && portNumber == other.portNumber;
  235. }
  236. };
  237. /*
  238. * Definition of hash functions for structs used as key in unordered_map
  239. */
  240. namespace std {
  241. template<>
  242. struct hash<ipAddress_ttl> {
  243. std::size_t operator()(const ipAddress_ttl &k) const {
  244. using std::size_t;
  245. using std::hash;
  246. using std::string;
  247. return ((hash<string>()(k.ipAddress)
  248. ^ (hash<int>()(k.ttlValue) << 1)) >> 1);
  249. }
  250. };
  251. template<>
  252. struct hash<ipAddress_mss> {
  253. std::size_t operator()(const ipAddress_mss &k) const {
  254. using std::size_t;
  255. using std::hash;
  256. using std::string;
  257. return ((hash<string>()(k.ipAddress)
  258. ^ (hash<int>()(k.mssValue) << 1)) >> 1);
  259. }
  260. };
  261. template<>
  262. struct hash<ipAddress_tos> {
  263. std::size_t operator()(const ipAddress_tos &k) const {
  264. using std::size_t;
  265. using std::hash;
  266. using std::string;
  267. return ((hash<string>()(k.ipAddress)
  268. ^ (hash<int>()(k.tosValue) << 1)) >> 1);
  269. }
  270. };
  271. template<>
  272. struct hash<ipAddress_win> {
  273. std::size_t operator()(const ipAddress_win &k) const {
  274. using std::size_t;
  275. using std::hash;
  276. using std::string;
  277. return ((hash<string>()(k.ipAddress)
  278. ^ (hash<int>()(k.winSize) << 1)) >> 1);
  279. }
  280. };
  281. template<>
  282. struct hash<conv> {
  283. std::size_t operator()(const conv &k) const {
  284. using std::size_t;
  285. using std::hash;
  286. using std::string;
  287. return ((hash<string>()(k.ipAddressA)
  288. ^ (hash<int>()(k.portA) << 1)) >> 1)
  289. ^ ((hash<string>()(k.ipAddressB)
  290. ^ (hash<int>()(k.portB) << 1)) >> 1);
  291. }
  292. };
  293. template<>
  294. struct hash<ipAddress_protocol> {
  295. std::size_t operator()(const ipAddress_protocol &k) const {
  296. using std::size_t;
  297. using std::hash;
  298. using std::string;
  299. return ((hash<string>()(k.ipAddress)
  300. ^ (hash<string>()(k.protocol) << 1)) >> 1);
  301. }
  302. };
  303. template<>
  304. struct hash<ipAddress_inOut_port> {
  305. std::size_t operator()(const ipAddress_inOut_port &k) const {
  306. using std::size_t;
  307. using std::hash;
  308. using std::string;
  309. return ((hash<string>()(k.ipAddress)
  310. ^ (hash<string>()(k.trafficDirection) << 1)) >> 1)
  311. ^ (hash<int>()(k.portNumber) << 1);
  312. }
  313. };
  314. }
  315. class statistics {
  316. public:
  317. /*
  318. * Constructor
  319. */
  320. statistics();
  321. /*
  322. * Methods
  323. */
  324. std::string getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const;
  325. /*
  326. * Access methods for containers
  327. */
  328. void incrementPacketCount();
  329. void calculateIPIntervalPacketRate(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp);
  330. void incrementMSScount(std::string ipAddress, int mssValue);
  331. void incrementWinCount(std::string ipAddress, int winSize);
  332. void addConvStat(std::string ipAddressSender,int sport,std::string ipAddressReceiver,int dport, std::chrono::microseconds timestamp);
  333. std::vector<float> calculateIPsCumEntropy();
  334. std::vector<float> calculateLastIntervalIPsEntropy(std::chrono::microseconds intervalStartTimestamp);
  335. void addIntervalStat(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp, std::chrono::microseconds lastPktTimestamp);
  336. void checkPayload(const PDU *pdu_l4);
  337. void checkTCPChecksum(std::string ipAddressSender, std::string ipAddressReceiver, TCP tcpPkt);
  338. void checkToS(uint8_t ToS);
  339. void incrementToScount(std::string ipAddress, int tosValue);
  340. void incrementTTLcount(std::string ipAddress, int ttlValue);
  341. void incrementProtocolCount(std::string ipAddress, std::string protocol);
  342. void incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
  343. int incomingPort);
  344. int getProtocolCount(std::string ipAddress, std::string protocol);
  345. void setTimestampFirstPacket(Tins::Timestamp ts);
  346. void setTimestampLastPacket(Tins::Timestamp ts);
  347. Tins::Timestamp getTimestampFirstPacket();
  348. Tins::Timestamp getTimestampLastPacket();
  349. void assignMacAddress(std::string ipAddress, std::string macAddress);
  350. void addIpStat_packetSent(std::string filePath, std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp);
  351. int getPacketCount();
  352. int getSumPacketSize();
  353. void addMSS(std::string ipAddress, int MSSvalue);
  354. void writeToDatabase(std::string database_path);
  355. void addPacketSize(uint32_t packetSize);
  356. std::string getCaptureDurationTimestamp() const;
  357. float getCaptureDurationSeconds() const;
  358. float getAvgPacketSize() const;
  359. void printStats(std::string ipAddress);
  360. bool getDoExtraTests();
  361. void setDoExtraTests(bool var);
  362. /*
  363. * IP Address-specific statistics
  364. */
  365. ip_stats getStatsForIP(std::string ipAddress);
  366. private:
  367. /*
  368. * Data fields
  369. */
  370. Tins::Timestamp timestamp_firstPacket;
  371. Tins::Timestamp timestamp_lastPacket;
  372. float sumPacketSize = 0;
  373. int packetCount = 0;
  374. /* Extra tests includes:
  375. * - calculate IPs entropies for intervals
  376. * - calculate IPs cumulative entropies interval-wise
  377. * - check payload availability
  378. * - chech TCP checksum correctness
  379. */
  380. bool doExtraTests = false;
  381. int payloadCount = 0;
  382. int incorrectTCPChecksumCount = 0;
  383. int correctTCPChecksumCount = 0;
  384. // Variables that are used for interval-wise statistics
  385. int intervalPayloadCount = 0;
  386. int intervalIncorrectTCPChecksumCount = 0;
  387. int intervalCorrectTCPChecksumCount = 0;
  388. int intervalCumPktCount = 0;
  389. float intervalCumSumPktSize = 0;
  390. int intervalCumNewIPCount = 0;
  391. int intervalCumNewTTLCount = 0;
  392. int intervalCumNewWinSizeCount = 0;
  393. int intervalCumNewToSCount = 0;
  394. int intervalCumNewMSSCount = 0;
  395. /*
  396. * Data containers
  397. */
  398. // {IP Address, TTL value, count}
  399. std::unordered_map<ipAddress_ttl, int> ttl_distribution;
  400. // {IP Address, MSS value, count}
  401. std::unordered_map<ipAddress_mss, int> mss_distribution;
  402. // {IP Address, Win size, count}
  403. std::unordered_map<ipAddress_win, int> win_distribution;
  404. // {IP Address, ToS value, count}
  405. std::unordered_map<ipAddress_tos, int> tos_distribution;
  406. // {IP Address A, Port A, IP Address B, Port B, #packets, packets timestamps, inter-arrival times,
  407. // average of inter-arrival times}
  408. std::unordered_map<conv, entry_convStat> conv_statistics;
  409. // {Last timestamp in the interval, #packets, #bytes, source IP entropy, destination IP entropy,
  410. // source IP cumulative entropy, destination IP cumulative entropy, #payload, #incorrect TCP checksum,
  411. // #correct TCP checksum, #novel IP, #novel TTL, #novel Window Size, #novel ToS,#novel MSS}
  412. std::unordered_map<std::string, entry_intervalStat> interval_statistics;
  413. // {TTL value, count}
  414. std::unordered_map<int, int> ttl_values;
  415. // {Win size, count}
  416. std::unordered_map<int, int> win_values;
  417. // {ToS, count}
  418. std::unordered_map<int, int> tos_values;
  419. // {MSS, count}
  420. std::unordered_map<int, int> mss_values;
  421. // {IP Address, Protocol, count}
  422. std::unordered_map<ipAddress_protocol, int> protocol_distribution;
  423. // {IP Address, #received packets, #sent packets, Data received in kbytes, Data sent in kbytes}
  424. std::unordered_map<std::string, entry_ipStat> ip_statistics;
  425. // {IP Address, in_out, Port Number, count}
  426. std::unordered_map<ipAddress_inOut_port, int> ip_ports;
  427. // {IP Address, MAC Address}
  428. std::unordered_map<std::string, std::string> ip_mac_mapping;
  429. };
  430. #endif //CPP_PCAPREADER_STATISTICS_H