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.
 
 
 
 

145 lines
3.3 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. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdexcept>
  10. #include "data.h"
  11. //////////////////////////////////////////////////////////////////////
  12. // Conn
  13. //////////////////////////////////////////////////////////////////////
  14. void Conn::clear() {
  15. us = them = name = protocol = "";
  16. in=false;
  17. us_port = them_port = 0;
  18. }
  19. void Conn::swap() {
  20. std::string s;
  21. int x;
  22. s = us;
  23. us = them;
  24. them =s;
  25. x = us_port;
  26. us_port = them_port;
  27. them_port = x;
  28. in=!in;
  29. }
  30. Conn &Conn::operator=(const Splits &sp) {
  31. int x;
  32. clear();
  33. for(x=0; x<sp.count; x++) {
  34. if(!strncmp(sp.fields[x], "SRC=", 4)) {
  35. us = sp.fields[x]+4;
  36. continue;
  37. }
  38. if(!strncmp(sp.fields[x], "DST=", 4)) {
  39. them = sp.fields[x]+4;
  40. continue;
  41. }
  42. if(!strncmp(sp.fields[x], "SPT=", 4)) {
  43. us_port = atoi(sp.fields[x]+4);
  44. continue;
  45. }
  46. if(!strncmp(sp.fields[x], "DPT=", 4)) {
  47. them_port = atoi(sp.fields[x]+4);
  48. continue;
  49. }
  50. if(!strncmp(sp.fields[x], "TYPE=", 5) && protocol=="ICMP") {
  51. us_port = them_port = atoi(sp.fields[x]+5);
  52. continue;
  53. }
  54. if(!strncmp(sp.fields[x], "PROTO=", 6))
  55. protocol = sp.fields[x]+6;
  56. }
  57. }
  58. // TODO: does < > have any actual meaning in this context?
  59. int Conn::cmp(const Conn &gtr) const {
  60. if(us!="*" && gtr.us!="*") {
  61. if(us<gtr.us) return -1;
  62. if(us>gtr.us) return 1;
  63. }
  64. // TODO: auto-wildcard port based on in?
  65. if(us_port && gtr.us_port) { // 0 = no comparison wildcard
  66. if(us_port<gtr.us_port) return -1;
  67. if(us_port>gtr.us_port) return 1;
  68. }
  69. if(them!="*" && gtr.them!="*") {
  70. if(them<gtr.them) return -1;
  71. if(them>gtr.them) return 1;
  72. }
  73. if(them_port && gtr.them_port) { // 0 = no comparison wildcard
  74. if(them_port<gtr.them_port) return -1;
  75. if(them_port>gtr.them_port) return 1;
  76. }
  77. // TODO: do we want to consider the name?
  78. if(name!="*" && gtr.name!="*") {
  79. if(name<gtr.name) return -1;
  80. if(name>gtr.name) return 1;
  81. }
  82. if(protocol<gtr.protocol) return -1;
  83. if(protocol>gtr.protocol) return 1;
  84. if(in<gtr.in) return -1;
  85. if(in>gtr.in) return 1;
  86. return 0;
  87. }
  88. std::ostream &operator<<(std::ostream &out, const Conn &c) {
  89. out << c.us
  90. << ( c.in ? " <- " : " -> " )
  91. << c.them
  92. << " " << c.protocol
  93. << "[" << ( c.in ? c.us_port : c.them_port ) << "] "
  94. << c.name;
  95. return out;
  96. }
  97. const Splits &operator>>(const Splits &tsv, Conn &conn) {
  98. if(tsv.count<7) throw std::runtime_error("Conn=TSV: too few columns");
  99. conn.clear();
  100. conn.us = tsv[0];
  101. conn.us_port = atoi(tsv.fields[1]);
  102. conn.them = tsv[2];
  103. conn.them_port = atoi(tsv.fields[3]);
  104. conn.name = tsv[4];
  105. conn.protocol = tsv[5];
  106. conn.in = tsv[6]=="1";
  107. return tsv;
  108. }
  109. //////////////////////////////////////////////////////////////////////
  110. // ConnList
  111. //////////////////////////////////////////////////////////////////////
  112. int ConnList::find(Conn &needle) {
  113. int r;
  114. for(r=0; r<size(); r++) if((*this)[r]==needle) return r;
  115. return -1;
  116. }