The Poor Man's (or Woman's) Intrusion Detection System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

115 lines
4.0 KiB

  1. //////////////////////////////////////////////////////////////////////
  2. // IP traffic analyzer - data objects
  3. // Written by Jonathan A. Foster <ChipMaster@YeOlPiShack.net>
  4. // Started April 23rd, 2021
  5. // Copyright JF Possibilities, Inc. All rights reserved.
  6. //
  7. // This is useful for breaking a text file line into fields.
  8. //
  9. // 2021-05-14 <ChipMaster@YeOlPiShack.net>
  10. // Restructure: broke out of monolithic iptraffic.cpp and made its
  11. // own module.
  12. //////////////////////////////////////////////////////////////////////
  13. #ifndef __JFP_IPTRAFFIC_DATA_H__
  14. #define __JFP_IPTRAFFIC_DATA_H__
  15. #include <string>
  16. #include <istream>
  17. #include <ostream>
  18. #include <vector>
  19. #include "strutil.h"
  20. //////////////////////////////////////////////////////////////////////
  21. // Reduce IPv6 address string to optimal format.
  22. //
  23. // The resultant string is produced by standard routines in libC. Its
  24. // as optimal as it makes it. *DON'T* run filter strings through this
  25. // or you'll lose partial match capability.
  26. //////////////////////////////////////////////////////////////////////
  27. std::string ipv6opt(const std::string &addr);
  28. //////////////////////////////////////////////////////////////////////
  29. // Wild compare of two address strings
  30. //
  31. // returns: 0=match, -1 = <, 1 = >
  32. //////////////////////////////////////////////////////////////////////
  33. int addr_wild_comp(const std::string &str1, const std::string &str2);
  34. //////////////////////////////////////////////////////////////////////
  35. // Network connection between "us" and "them"
  36. //////////////////////////////////////////////////////////////////////
  37. typedef unsigned short word;
  38. struct Conn {
  39. std::string us; // address on our side
  40. word us_port; // the port on our side
  41. std::string them; // address on their side
  42. word them_port; // the port on their side
  43. std::string name; // name of the address
  44. std::string protocol; // protocol used to communicate
  45. bool in; // whether this was an inward bound connection.
  46. Conn(): us_port(0), them_port(0), in(false) {}
  47. // clear data
  48. void clear();
  49. // Compact IPv6 address strings
  50. void compact();
  51. // swap polarity of record
  52. void swap();
  53. // scan & copy data from log record in
  54. Conn &operator=(const Splits &sp);
  55. // compare to another Conn
  56. int cmp(const Conn &gtr) const;
  57. inline bool operator<(const Conn &gtr) const { return cmp(gtr) <0; }
  58. inline bool operator<=(const Conn &gtr) const { return cmp(gtr)<=0; }
  59. inline bool operator>(const Conn &gtr) const { return cmp(gtr) >0; }
  60. inline bool operator>=(const Conn &gtr) const { return cmp(gtr)>=0; }
  61. inline bool operator==(const Conn &gtr) const { return cmp(gtr)==0; }
  62. inline bool operator!=(const Conn &gtr) const { return cmp(gtr)!=0; }
  63. };
  64. // A text output of Conn
  65. std::ostream &operator<<(std::ostream &out, const Conn &c);
  66. // Copy data from Splits into Conn
  67. const Splits &operator>>(const Splits &tsv, Conn &conn);
  68. //////////////////////////////////////////////////////////////////////
  69. // List of connections
  70. //////////////////////////////////////////////////////////////////////
  71. struct ConnList: public std::vector<Conn> {
  72. int find(Conn &needle);
  73. };
  74. //////////////////////////////////////////////////////////////////////
  75. // Log Analyzer
  76. //////////////////////////////////////////////////////////////////////
  77. struct LogAnalyzer {
  78. StringList *us;
  79. StringList dns_ignore, // DNS response prefixes to ignore
  80. dns_del; // DNS response prefixes to /delete/ (ignore)
  81. NameVal rdns; // Reverse DNS lookup cache
  82. Conn conn; // Last connection worked on
  83. Splits ln; // Work buffer for line processing
  84. std::string alias; // The name requiring CNAME resolution
  85. std::string cname; // The cname alias was pointing to.
  86. LogAnalyzer();
  87. // Process a log line. Returns "true" if it were a netfilter entry.
  88. bool line(const std::string &in);
  89. };
  90. #endif