EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
EVEnids.cpp File Reference
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "nids.h"
#include <string>
#include <queue>
#include <map>
#include "../common/packet_dump.h"
#include "../common/packet_functions.h"
#include "../common/packet_types.h"
#include "../common/PyRep.h"
#include "../common/EVEUnmarshal.h"
#include "../common/PyPacket.h"
#include "../common/PyVisitor.h"
#include "../common/logsys.h"
#include "../common/StreamPacketizer.h"
#include "../common/CachedObjectMgr.h"
#include "../common/PyXMLGenerator.h"
#include "../common/PyDumpVisitor.h"
#include "../common/PyLookupDump.h"
#include "EVECollectDisp.h"
#include "../packets/General.h"
Include dependency graph for EVEnids.cpp:

Go to the source code of this file.

Macros

#define int_ntoa(x)   inet_ntoa(*((struct in_addr *)&x))
 

Functions

char * adres (struct tuple4 addr)
 
void ProcessCallRequest (PyPacket *packet)
 
void tcp_callback (struct tcp_stream *a_tcp, void **this_time_not_needed)
 
int EVE_NIDS_main (EVECollectDispatcher *disp, int argc, char *argv[])
 

Variables

static EVECollectDispatcherCollectDispatcher = NULL
 
StreamPacketizer clientPacketizer
 
StreamPacketizer serverPacketizer
 

Macro Definition Documentation

#define int_ntoa (   x)    inet_ntoa(*((struct in_addr *)&x))

Definition at line 61 of file EVEnids.cpp.

Referenced by adres().

Function Documentation

char* adres ( struct tuple4  addr)

Definition at line 67 of file EVEnids.cpp.

References int_ntoa, and sprintf().

Referenced by tcp_callback().

68 {
69  static char buf[256];
70  strcpy (buf, int_ntoa (addr.saddr));
71  sprintf (buf + strlen (buf), ",%i,", addr.source);
72  strcat (buf, int_ntoa (addr.daddr));
73  sprintf (buf + strlen (buf), ",%i", addr.dest);
74  return buf;
75 }
std::string sprintf(const char *fmt,...)
sprintf for std::string.
Definition: eve-compat.cpp:106
#define int_ntoa(x)
Definition: EVEnids.cpp:61

Here is the call graph for this function:

Here is the caller graph for this function:

int EVE_NIDS_main ( EVECollectDispatcher disp,
int  argc,
char *  argv[] 
)

Definition at line 196 of file EVEnids.cpp.

References strdup, and tcp_callback().

Referenced by main().

196  {
197  if(disp == NULL) {
198  fprintf(stderr,"NULL dispatcher provided to NIDS, not running\n",nids_errbuf);
199  return(1);
200  }
201  CollectDispatcher = disp;
202 
203  // here we can alter libnids params, for instance:
204  // nids_params.n_hosts=256;
205  if(argc == 2)
206  nids_params.filename = strdup(argv[1]);
207  if(argc == 3) //hack
208  nids_params.device = strdup(argv[2]);
209  if (!nids_init ()) {
210  fprintf(stderr,"%s\n",nids_errbuf);
211  return(1);
212  }
213 
214  nids_register_tcp ((void *) tcp_callback);
215  printf("Starting NIDS loop...\n");
216  nids_run ();
217  return(0);
218 }
static EVECollectDispatcher * CollectDispatcher
Definition: EVEnids.cpp:82
#define strdup
Definition: eve-compat.h:258
void tcp_callback(struct tcp_stream *a_tcp, void **this_time_not_needed)
Definition: EVEnids.cpp:88

Here is the call graph for this function:

Here is the caller graph for this function:

void ProcessCallRequest ( PyPacket packet)

Definition at line 77 of file EVEnids.cpp.

77  {
78 
79 }
void tcp_callback ( struct tcp_stream *  a_tcp,
void **  this_time_not_needed 
)

Definition at line 88 of file EVEnids.cpp.

References _hex, _log, adres(), clientPacketizer, PyPacket::Decode(), EVEPacketDispatcher::DispatchPacket(), PyPacket::Dump(), is_log_enabled, EVECollectDispatcher::lookResolver, PyPacket::payload, StreamPacketizer::PopPacket(), serverPacketizer, PyRep::visit(), and PyTuple::visit().

Referenced by EVE_NIDS_main().

88  {
89  char buf[1024];
90  strcpy (buf, adres (a_tcp->addr)); // we put conn params into buf
91 
92  if (a_tcp->nids_state == NIDS_JUST_EST) {
93 
94  //see if this is a stream we care about...
95  if(a_tcp->addr.source != 26000 && a_tcp->addr.dest != 26000 &&
96  a_tcp->addr.source != 26001 && a_tcp->addr.dest != 26001)
97  return;
98 
99  a_tcp->client.collect++; // we want data received by a client
100  a_tcp->server.collect++; // and by a server, too
101  _log(COLLECT__TCP, "%s established", buf);
102  return;
103  }
104  if (a_tcp->nids_state == NIDS_CLOSE) {
105  // connection has been closed normally
106  _log(COLLECT__TCP, "%s closing", buf);
107  return;
108  }
109  if (a_tcp->nids_state == NIDS_RESET) {
110  // connection has been closed by RST
111  _log(COLLECT__TCP, "%s reset", buf);
112  return;
113  }
114 
115  if (a_tcp->nids_state == NIDS_DATA) {
116  // new data has arrived; gotta determine in what direction
117  // and if it's urgent or not
118 
119  struct half_stream *hlf;
120  StreamPacketizer *sp;
121 
122  if (a_tcp->client.count_new) {
123  // new data for client
124  hlf = &a_tcp->client; // from now on, we will deal with hlf var,
125  // which will point to client side of conn
126  sp = &clientPacketizer;
127  strcat (buf, "(<-)"); // symbolic direction of data
128  } else {
129  sp = &serverPacketizer;
130  hlf = &a_tcp->server; // analogical
131  strcat (buf, "(->)");
132  }
133 
134  _log(COLLECT__TCP, "Data %s (len %d)", buf, hlf->count_new); // we print the connection parameters
135  // (saddr, daddr, sport, dport) accompanied
136  // by data flow direction (-> or <-)
137 
138  sp->InputBytes((const byte *) hlf->data, hlf->count_new);
139 
140  StreamPacketizer::Packet *p;
141  while((p = sp->PopPacket()) != NULL) {
142  //const PacketHeader *head = (const PacketHeader *) p->data;
143 
144  uint32 body_len = p->length;
145  const byte *body = p->data;
146 
147  _log(COLLECT__RAW_HEX, "Raw Hex Dump of len %d:", body_len);
148  _hex(COLLECT__RAW_HEX, body, body_len);
149 
150  PyRep *rep = InflateAndUnmarshal(body, body_len);
151  if(rep == NULL) {
152  printf("Failed to inflate or unmarshal!");
153  delete p;
154  continue;
155  }
156 
157  if(is_log_enabled(COLLECT__PYREP_DUMP)) {
158  //decode substreams to facilitate dumping better:
159  SubStreamDecoder v;
160  rep->visit(&v);
161  //TODO: make dump use logsys.
162  _log(COLLECT__PYREP_DUMP, "Unmarshaled PyRep:");
163  PyLookupDump dumper(&CollectDispatcher->lookResolver, COLLECT__PYREP_DUMP);
164  rep->visit(&dumper);
165  }
166 
167  PyPacket *packet = new PyPacket;
168  if(!packet->Decode(rep)) {
169  _log(COLLECT__ERROR, "Failed to decode packet rep");
170  } else {
171  if(is_log_enabled(COLLECT__PACKET_DUMP)) {
172  //decode substreams to facilitate dumping better:
173  SubStreamDecoder v;
174  packet->payload->visit(&v);
175 
176  //TODO: make dump use logsys.
177  _log(COLLECT__PACKET_DUMP, "Decoded message:");
178  PyLookupDump dumper(&CollectDispatcher->lookResolver, COLLECT__PACKET_DUMP);
179  packet->Dump(COLLECT__PACKET_DUMP, &dumper);
180 
181 
182  printf("\n\n");
183  }
184  fflush(stdout);
185 
187  }
188  delete packet;
189 
190  delete p;
191  } //end "while pop packet"
192  }
193  return ;
194 }
Base Python wire object.
Definition: PyRep.h:66
char * adres(struct tuple4 addr)
Definition: EVEnids.cpp:67
StreamPacketizer serverPacketizer
Definition: EVEnids.cpp:87
#define _log(type, fmt,...)
Definition: logsys.h:124
static EVECollectDispatcher * CollectDispatcher
Definition: EVEnids.cpp:82
PyTuple * payload
Definition: PyPacket.h:119
#define is_log_enabled(type)
Definition: logsys.h:78
void Dump(LogType type, PyVisitor &dumper)
Definition: PyPacket.cpp:95
#define _hex(type, data, len)
Definition: logsys.h:133
unsigned __int32 uint32
Definition: eve-compat.h:50
StreamPacketizer clientPacketizer
Definition: EVEnids.cpp:86
bool Decode(PyRep **packet)
Definition: PyPacket.cpp:115
PyLookupResolver lookResolver
bool visit(PyVisitor &v) const
Visits object.
Definition: PyRep.cpp:553
bool DispatchPacket(PyPacket *packet)
virtual bool visit(PyVisitor &v) const =0
Visits object.

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

StreamPacketizer clientPacketizer

Definition at line 86 of file EVEnids.cpp.

Referenced by tcp_callback().

EVECollectDispatcher* CollectDispatcher = NULL
static

Definition at line 82 of file EVEnids.cpp.

StreamPacketizer serverPacketizer

Definition at line 87 of file EVEnids.cpp.

Referenced by tcp_callback().