statistics.h 17 KB

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