suricata
app-layer-template.c
Go to the documentation of this file.
1 /* Copyright (C) 2015 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /*
19  * TODO: Update \author in this file and app-layer-template.h.
20  * TODO: Implement your app-layer logic with unit tests.
21  * TODO: Remove SCLogNotice statements or convert to debug.
22  */
23 
24 /**
25  * \file
26  *
27  * \author FirstName LastName <yourname@domain>
28  *
29  * Template application layer detector and parser for learning and
30  * template pruposes.
31  *
32  * This template implements a simple application layer for something
33  * like the echo protocol running on port 7.
34  */
35 
36 #include "suricata-common.h"
37 #include "stream.h"
38 #include "conf.h"
39 #include "app-layer-detect-proto.h"
40 #include "app-layer-parser.h"
41 #include "app-layer-template.h"
42 
43 #include "util-unittest.h"
44 
45 
46 /* The default port to probe for echo traffic if not provided in the
47  * configuration file. */
48 #define TEMPLATE_DEFAULT_PORT "7"
49 
50 /* The minimum size for a message. For some protocols this might
51  * be the size of a header. */
52 #define TEMPLATE_MIN_FRAME_LEN 1
53 
54 /* Enum of app-layer events for the protocol. Normally you might
55  * have events for errors in parsing data, like unexpected data being
56  * received. For template we'll make something up, and log an app-layer
57  * level alert if an empty message is received.
58  *
59  * Example rule:
60  *
61  * alert template any any -> any any (msg:"SURICATA Template empty message"; \
62  * app-layer-event:template.empty_message; sid:X; rev:Y;)
63  */
64 enum {
66 };
67 
69  {"EMPTY_MESSAGE", TEMPLATE_DECODER_EVENT_EMPTY_MESSAGE},
70 
71  // event table must be NULL-terminated
72  { NULL, -1 },
73 };
74 
75 static TemplateTransaction *TemplateTxAlloc(TemplateState *state)
76 {
78  if (unlikely(tx == NULL)) {
79  return NULL;
80  }
81 
82  /* Increment the transaction ID on the state each time one is
83  * allocated. */
84  tx->tx_id = state->transaction_max++;
85 
86  TAILQ_INSERT_TAIL(&state->tx_list, tx, next);
87 
88  return tx;
89 }
90 
91 static void TemplateTxFree(void *txv)
92 {
93  TemplateTransaction *tx = txv;
94 
95  if (tx->request_buffer != NULL) {
97  }
98 
99  if (tx->response_buffer != NULL) {
100  SCFree(tx->response_buffer);
101  }
102 
104 
105  SCFree(tx);
106 }
107 
108 static void *TemplateStateAlloc(void)
109 {
110  SCLogNotice("Allocating template state.");
111  TemplateState *state = SCCalloc(1, sizeof(TemplateState));
112  if (unlikely(state == NULL)) {
113  return NULL;
114  }
115  TAILQ_INIT(&state->tx_list);
116  return state;
117 }
118 
119 static void TemplateStateFree(void *state)
120 {
121  TemplateState *template_state = state;
123  SCLogNotice("Freeing template state.");
124  while ((tx = TAILQ_FIRST(&template_state->tx_list)) != NULL) {
125  TAILQ_REMOVE(&template_state->tx_list, tx, next);
126  TemplateTxFree(tx);
127  }
128  SCFree(template_state);
129 }
130 
131 /**
132  * \brief Callback from the application layer to have a transaction freed.
133  *
134  * \param state a void pointer to the TemplateState object.
135  * \param tx_id the transaction ID to free.
136  */
137 static void TemplateStateTxFree(void *statev, uint64_t tx_id)
138 {
139  TemplateState *state = statev;
140  TemplateTransaction *tx = NULL, *ttx;
141 
142  SCLogNotice("Freeing transaction %"PRIu64, tx_id);
143 
144  TAILQ_FOREACH_SAFE(tx, &state->tx_list, next, ttx) {
145 
146  /* Continue if this is not the transaction we are looking
147  * for. */
148  if (tx->tx_id != tx_id) {
149  continue;
150  }
151 
152  /* Remove and free the transaction. */
153  TAILQ_REMOVE(&state->tx_list, tx, next);
154  TemplateTxFree(tx);
155  return;
156  }
157 
158  SCLogNotice("Transaction %"PRIu64" not found.", tx_id);
159 }
160 
161 static int TemplateStateGetEventInfo(const char *event_name, int *event_id,
163 {
164  *event_id = SCMapEnumNameToValue(event_name, template_decoder_event_table);
165  if (*event_id == -1) {
166  SCLogError(SC_ERR_INVALID_ENUM_MAP, "event \"%s\" not present in "
167  "template enum map table.", event_name);
168  /* This should be treated as fatal. */
169  return -1;
170  }
171 
172  *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
173 
174  return 0;
175 }
176 
177 static AppLayerDecoderEvents *TemplateGetEvents(void *statev, uint64_t tx_id)
178 {
179  TemplateState *state = statev;
181 
182  TAILQ_FOREACH(tx, &state->tx_list, next) {
183  if (tx->tx_id == tx_id) {
184  return tx->decoder_events;
185  }
186  }
187 
188  return NULL;
189 }
190 
191 /**
192  * \brief Probe the input to see if it looks like template.
193  *
194  * \retval ALPROTO_TEMPLATE if it looks like template, otherwise
195  * ALPROTO_UNKNOWN.
196  */
197 static AppProto TemplateProbingParser(Flow *f, uint8_t direction,
198  uint8_t *input, uint32_t input_len, uint8_t *rdir)
199 {
200  /* Very simple test - if there is input, this is template. */
201  if (input_len >= TEMPLATE_MIN_FRAME_LEN) {
202  SCLogNotice("Detected as ALPROTO_TEMPLATE.");
203  return ALPROTO_TEMPLATE;
204  }
205 
206  SCLogNotice("Protocol not detected as ALPROTO_TEMPLATE.");
207  return ALPROTO_UNKNOWN;
208 }
209 
210 static int TemplateParseRequest(Flow *f, void *statev,
211  AppLayerParserState *pstate, uint8_t *input, uint32_t input_len,
212  void *local_data, const uint8_t flags)
213 {
214  TemplateState *state = statev;
215 
216  SCLogNotice("Parsing template request: len=%"PRIu32, input_len);
217 
218  /* Likely connection closed, we can just return here. */
219  if ((input == NULL || input_len == 0) &&
221  return 0;
222  }
223 
224  /* Probably don't want to create a transaction in this case
225  * either. */
226  if (input == NULL || input_len == 0) {
227  return 0;
228  }
229 
230  /* Normally you would parse out data here and store it in the
231  * transaction object, but as this is echo, we'll just record the
232  * request data. */
233 
234  /* Also, if this protocol may have a "protocol data unit" span
235  * multiple chunks of data, which is always a possibility with
236  * TCP, you may need to do some buffering here.
237  *
238  * For the sake of simplicity, buffering is left out here, but
239  * even for an echo protocol we may want to buffer until a new
240  * line is seen, assuming its text based.
241  */
242 
243  /* Allocate a transaction.
244  *
245  * But note that if a "protocol data unit" is not received in one
246  * chunk of data, and the buffering is done on the transaction, we
247  * may need to look for the transaction that this newly recieved
248  * data belongs to.
249  */
250  TemplateTransaction *tx = TemplateTxAlloc(state);
251  if (unlikely(tx == NULL)) {
252  SCLogNotice("Failed to allocate new Template tx.");
253  goto end;
254  }
255  SCLogNotice("Allocated Template tx %"PRIu64".", tx->tx_id);
256 
257  /* Make a copy of the request. */
258  tx->request_buffer = SCCalloc(1, input_len);
259  if (unlikely(tx->request_buffer == NULL)) {
260  goto end;
261  }
262  memcpy(tx->request_buffer, input, input_len);
263  tx->request_buffer_len = input_len;
264 
265  /* Here we check for an empty message and create an app-layer
266  * event. */
267  if ((input_len == 1 && tx->request_buffer[0] == '\n') ||
268  (input_len == 2 && tx->request_buffer[0] == '\r')) {
269  SCLogNotice("Creating event for empty message.");
272  }
273 
274 end:
275  return 0;
276 }
277 
278 static int TemplateParseResponse(Flow *f, void *statev, AppLayerParserState *pstate,
279  uint8_t *input, uint32_t input_len, void *local_data,
280  const uint8_t flags)
281 {
282  TemplateState *state = statev;
283  TemplateTransaction *tx = NULL, *ttx;
284 
285  SCLogNotice("Parsing Template response.");
286 
287  /* Likely connection closed, we can just return here. */
288  if ((input == NULL || input_len == 0) &&
290  return 0;
291  }
292 
293  /* Probably don't want to create a transaction in this case
294  * either. */
295  if (input == NULL || input_len == 0) {
296  return 0;
297  }
298 
299  /* Look up the existing transaction for this response. In the case
300  * of echo, it will be the most recent transaction on the
301  * TemplateState object. */
302 
303  /* We should just grab the last transaction, but this is to
304  * illustrate how you might traverse the transaction list to find
305  * the transaction associated with this response. */
306  TAILQ_FOREACH(ttx, &state->tx_list, next) {
307  tx = ttx;
308  }
309 
310  if (tx == NULL) {
311  SCLogNotice("Failed to find transaction for response on state %p.",
312  state);
313  goto end;
314  }
315 
316  SCLogNotice("Found transaction %"PRIu64" for response on state %p.",
317  tx->tx_id, state);
318 
319  /* If the protocol requires multiple chunks of data to complete, you may
320  * run into the case where you have existing response data.
321  *
322  * In this case, we just log that there is existing data and free it. But
323  * you might want to realloc the buffer and append the data.
324  */
325  if (tx->response_buffer != NULL) {
326  SCLogNotice("WARNING: Transaction already has response data, "
327  "existing data will be overwritten.");
328  SCFree(tx->response_buffer);
329  }
330 
331  /* Make a copy of the response. */
332  tx->response_buffer = SCCalloc(1, input_len);
333  if (unlikely(tx->response_buffer == NULL)) {
334  goto end;
335  }
336  memcpy(tx->response_buffer, input, input_len);
337  tx->response_buffer_len = input_len;
338 
339  /* Set the response_done flag for transaction state checking in
340  * TemplateGetStateProgress(). */
341  tx->response_done = 1;
342 
343 end:
344  return 0;
345 }
346 
347 static uint64_t TemplateGetTxCnt(void *statev)
348 {
349  const TemplateState *state = statev;
350  SCLogNotice("Current tx count is %"PRIu64".", state->transaction_max);
351  return state->transaction_max;
352 }
353 
354 static void *TemplateGetTx(void *statev, uint64_t tx_id)
355 {
356  TemplateState *state = statev;
358 
359  SCLogNotice("Requested tx ID %"PRIu64".", tx_id);
360 
361  TAILQ_FOREACH(tx, &state->tx_list, next) {
362  if (tx->tx_id == tx_id) {
363  SCLogNotice("Transaction %"PRIu64" found, returning tx object %p.",
364  tx_id, tx);
365  return tx;
366  }
367  }
368 
369  SCLogNotice("Transaction ID %"PRIu64" not found.", tx_id);
370  return NULL;
371 }
372 
373 static void TemplateSetTxLogged(void *state, void *vtx, LoggerId logged)
374 {
376  tx->logged = logged;
377 }
378 
379 static LoggerId TemplateGetTxLogged(void *state, void *vtx)
380 {
381  const TemplateTransaction *tx = (TemplateTransaction *)vtx;
382  return tx->logged;
383 }
384 
385 /**
386  * \brief Called by the application layer.
387  *
388  * In most cases 1 can be returned here.
389  */
390 static int TemplateGetAlstateProgressCompletionStatus(uint8_t direction) {
391  return 1;
392 }
393 
394 /**
395  * \brief Return the state of a transaction in a given direction.
396  *
397  * In the case of the echo protocol, the existence of a transaction
398  * means that the request is done. However, some protocols that may
399  * need multiple chunks of data to complete the request may need more
400  * than just the existence of a transaction for the request to be
401  * considered complete.
402  *
403  * For the response to be considered done, the response for a request
404  * needs to be seen. The response_done flag is set on response for
405  * checking here.
406  */
407 static int TemplateGetStateProgress(void *txv, uint8_t direction)
408 {
409  TemplateTransaction *tx = txv;
410 
411  SCLogNotice("Transaction progress requested for tx ID %"PRIu64
412  ", direction=0x%02x", tx->tx_id, direction);
413 
414  if (direction & STREAM_TOCLIENT && tx->response_done) {
415  return 1;
416  }
417  else if (direction & STREAM_TOSERVER) {
418  /* For the template, just the existence of the transaction means the
419  * request is done. */
420  return 1;
421  }
422 
423  return 0;
424 }
425 
426 /**
427  * \brief retrieve the detection engine per tx state
428  */
429 static DetectEngineState *TemplateGetTxDetectState(void *vtx)
430 {
431  TemplateTransaction *tx = vtx;
432  return tx->de_state;
433 }
434 
435 /**
436  * \brief get the detection engine per tx state
437  */
438 static int TemplateSetTxDetectState(void *vtx,
440 {
441  TemplateTransaction *tx = vtx;
442  tx->de_state = s;
443  return 0;
444 }
445 
447 {
448  const char *proto_name = "template";
449 
450  /* TEMPLATE_START_REMOVE */
451  if (ConfGetNode("app-layer.protocols.template") == NULL) {
452  return;
453  }
454  /* TEMPLATE_END_REMOVE */
455  /* Check if Template TCP detection is enabled. If it does not exist in
456  * the configuration file then it will be enabled by default. */
457  if (AppLayerProtoDetectConfProtoDetectionEnabled("tcp", proto_name)) {
458 
459  SCLogNotice("Template TCP protocol detection enabled.");
460 
462 
463  if (RunmodeIsUnittests()) {
464 
465  SCLogNotice("Unittest mode, registeringd default configuration.");
468  TemplateProbingParser, NULL);
469 
470  }
471  else {
472 
473  if (!AppLayerProtoDetectPPParseConfPorts("tcp", IPPROTO_TCP,
475  TemplateProbingParser, NULL)) {
476  SCLogNotice("No template app-layer configuration, enabling echo"
477  " detection TCP detection on port %s.",
479  AppLayerProtoDetectPPRegister(IPPROTO_TCP,
482  TemplateProbingParser, NULL);
483  }
484 
485  }
486 
487  }
488 
489  else {
490  SCLogNotice("Protocol detecter and parser disabled for Template.");
491  return;
492  }
493 
494  if (AppLayerParserConfParserEnabled("tcp", proto_name)) {
495 
496  SCLogNotice("Registering Template protocol parser.");
497 
498  /* Register functions for state allocation and freeing. A
499  * state is allocated for every new Template flow. */
501  TemplateStateAlloc, TemplateStateFree);
502 
503  /* Register request parser for parsing frame from server to client. */
505  STREAM_TOSERVER, TemplateParseRequest);
506 
507  /* Register response parser for parsing frames from server to client. */
509  STREAM_TOCLIENT, TemplateParseResponse);
510 
511  /* Register a function to be called by the application layer
512  * when a transaction is to be freed. */
514  TemplateStateTxFree);
515 
517  TemplateGetTxLogged, TemplateSetTxLogged);
518 
519  /* Register a function to return the current transaction count. */
521  TemplateGetTxCnt);
522 
523  /* Transaction handling. */
525  TemplateGetAlstateProgressCompletionStatus);
527  ALPROTO_TEMPLATE, TemplateGetStateProgress);
529  TemplateGetTx);
530 
531  /* What is this being registered for? */
533  TemplateGetTxDetectState, TemplateSetTxDetectState);
534 
536  TemplateStateGetEventInfo);
538  TemplateGetEvents);
539  }
540  else {
541  SCLogNotice("Template protocol parsing disabled.");
542  }
543 
544 #ifdef UNITTESTS
547 #endif
548 }
549 
550 #ifdef UNITTESTS
551 #endif
552 
554 {
555 #ifdef UNITTESTS
556 #endif
557 }
enum AppLayerEventType_ AppLayerEventType
uint16_t flags
#define TAILQ_FIRST(head)
Definition: queue.h:339
void AppLayerProtoDetectPPRegister(uint8_t ipproto, const char *portstr, AppProto alproto, uint16_t min_depth, uint16_t max_depth, uint8_t direction, ProbingParserFPtr ProbingParser1, ProbingParserFPtr ProbingParser2)
register parser at a port
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
struct HtpBodyChunk_ * next
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
uint32_t event_type
LoggerId
int logged
#define unlikely(expr)
Definition: util-optimize.h:35
int AppLayerProtoDetectConfProtoDetectionEnabled(const char *ipproto, const char *alproto)
Given a protocol name, checks if proto detection is enabled in the conf file.
int AppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint8_t flag)
void AppLayerParserRegisterDetectStateFuncs(uint8_t ipproto, AppProto alproto, DetectEngineState *(*GetTxDetectState)(void *tx), int(*SetTxDetectState)(void *tx, DetectEngineState *))
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:356
void AppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event)
Set an app layer decoder event.
int SCMapEnumNameToValue(const char *enum_name, SCEnumCharMap *table)
Maps a string name to an enum value from the supplied table. Please specify the last element of any m...
Definition: util-enum.c:41
void AppLayerParserRegisterLoggerFuncs(uint8_t ipproto, AppProto alproto, LoggerId(*StateGetTxLogged)(void *, void *), void(*StateSetTxLogged)(void *, void *, LoggerId))
uint16_t AppProto
void RegisterTemplateParsers(void)
SCEnumCharMap template_decoder_event_table[]
int AppLayerParserConfParserEnabled(const char *ipproto, const char *alproto_name)
check if a parser is enabled in the config Returns enabled always if: were running unittests and when...
#define SCCalloc(nm, a)
Definition: util-mem.h:197
Data structure to store app layer decoder events.
int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto, uint8_t direction, AppLayerParserFPtr Parser)
Register app layer parser for the protocol.
#define TEMPLATE_MIN_FRAME_LEN
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
#define TAILQ_INIT(head)
Definition: queue.h:370
int AppLayerProtoDetectPPParseConfPorts(const char *ipproto_name, uint8_t ipproto, const char *alproto_name, AppProto alproto, uint16_t min_depth, uint16_t max_depth, ProbingParserFPtr ProbingParserTs, ProbingParserFPtr ProbingParserTc)
void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfo)(const char *event_name, int *event_id, AppLayerEventType *event_type))
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:412
#define STREAM_TOCLIENT
Definition: stream.h:32
void AppLayerParserRegisterGetStateProgressCompletionStatus(AppProto alproto, int(*StateGetProgressCompletionStatus)(uint8_t direction))
int RunmodeIsUnittests(void)
Definition: suricata.c:259
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:385
void AppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
void TemplateParserRegisterTests(void)
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
#define SCFree(a)
Definition: util-mem.h:228
#define TEMPLATE_DEFAULT_PORT
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:269
uint16_t tx_id
void AppLayerParserRegisterGetEventsFunc(uint8_t ipproto, AppProto alproto, AppLayerDecoderEvents *(*StateGetEvents)(void *, uint64_t))
void AppLayerParserRegisterProtocolUnittests(uint8_t ipproto, AppProto alproto, void(*RegisterUnittests)(void))
#define STREAM_TOSERVER
Definition: stream.h:31
uint64_t transaction_max
#define APP_LAYER_PARSER_EOF
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:176
void AppLayerParserRegisterGetTxCnt(uint8_t ipproto, AppProto alproto, uint64_t(*StateGetTxCnt)(void *alstate))
void AppLayerParserRegisterStateFuncs(uint8_t ipproto, AppProto alproto, void *(*StateAlloc)(void), void(*StateFree)(void *))
DetectEngineState * de_state
Flow data structure.
Definition: flow.h:324
AppLayerDecoderEvents * decoder_events
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))