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 *input, uint32_t input_len)
198 {
199  /* Very simple test - if there is input, this is template. */
200  if (input_len >= TEMPLATE_MIN_FRAME_LEN) {
201  SCLogNotice("Detected as ALPROTO_TEMPLATE.");
202  return ALPROTO_TEMPLATE;
203  }
204 
205  SCLogNotice("Protocol not detected as ALPROTO_TEMPLATE.");
206  return ALPROTO_UNKNOWN;
207 }
208 
209 static int TemplateParseRequest(Flow *f, void *statev,
210  AppLayerParserState *pstate, uint8_t *input, uint32_t input_len,
211  void *local_data, const uint8_t flags)
212 {
213  TemplateState *state = statev;
214 
215  SCLogNotice("Parsing template request: len=%"PRIu32, input_len);
216 
217  /* Likely connection closed, we can just return here. */
218  if ((input == NULL || input_len == 0) &&
220  return 0;
221  }
222 
223  /* Probably don't want to create a transaction in this case
224  * either. */
225  if (input == NULL || input_len == 0) {
226  return 0;
227  }
228 
229  /* Normally you would parse out data here and store it in the
230  * transaction object, but as this is echo, we'll just record the
231  * request data. */
232 
233  /* Also, if this protocol may have a "protocol data unit" span
234  * multiple chunks of data, which is always a possibility with
235  * TCP, you may need to do some buffering here.
236  *
237  * For the sake of simplicity, buffering is left out here, but
238  * even for an echo protocol we may want to buffer until a new
239  * line is seen, assuming its text based.
240  */
241 
242  /* Allocate a transaction.
243  *
244  * But note that if a "protocol data unit" is not received in one
245  * chunk of data, and the buffering is done on the transaction, we
246  * may need to look for the transaction that this newly recieved
247  * data belongs to.
248  */
249  TemplateTransaction *tx = TemplateTxAlloc(state);
250  if (unlikely(tx == NULL)) {
251  SCLogNotice("Failed to allocate new Template tx.");
252  goto end;
253  }
254  SCLogNotice("Allocated Template tx %"PRIu64".", tx->tx_id);
255 
256  /* Make a copy of the request. */
257  tx->request_buffer = SCCalloc(1, input_len);
258  if (unlikely(tx->request_buffer == NULL)) {
259  goto end;
260  }
261  memcpy(tx->request_buffer, input, input_len);
262  tx->request_buffer_len = input_len;
263 
264  /* Here we check for an empty message and create an app-layer
265  * event. */
266  if ((input_len == 1 && tx->request_buffer[0] == '\n') ||
267  (input_len == 2 && tx->request_buffer[0] == '\r')) {
268  SCLogNotice("Creating event for empty message.");
271  }
272 
273 end:
274  return 0;
275 }
276 
277 static int TemplateParseResponse(Flow *f, void *statev, AppLayerParserState *pstate,
278  uint8_t *input, uint32_t input_len, void *local_data,
279  const uint8_t flags)
280 {
281  TemplateState *state = statev;
282  TemplateTransaction *tx = NULL, *ttx;
283 
284  SCLogNotice("Parsing Template response.");
285 
286  /* Likely connection closed, we can just return here. */
287  if ((input == NULL || input_len == 0) &&
289  return 0;
290  }
291 
292  /* Probably don't want to create a transaction in this case
293  * either. */
294  if (input == NULL || input_len == 0) {
295  return 0;
296  }
297 
298  /* Look up the existing transaction for this response. In the case
299  * of echo, it will be the most recent transaction on the
300  * TemplateState object. */
301 
302  /* We should just grab the last transaction, but this is to
303  * illustrate how you might traverse the transaction list to find
304  * the transaction associated with this response. */
305  TAILQ_FOREACH(ttx, &state->tx_list, next) {
306  tx = ttx;
307  }
308 
309  if (tx == NULL) {
310  SCLogNotice("Failed to find transaction for response on state %p.",
311  state);
312  goto end;
313  }
314 
315  SCLogNotice("Found transaction %"PRIu64" for response on state %p.",
316  tx->tx_id, state);
317 
318  /* If the protocol requires multiple chunks of data to complete, you may
319  * run into the case where you have existing response data.
320  *
321  * In this case, we just log that there is existing data and free it. But
322  * you might want to realloc the buffer and append the data.
323  */
324  if (tx->response_buffer != NULL) {
325  SCLogNotice("WARNING: Transaction already has response data, "
326  "existing data will be overwritten.");
327  SCFree(tx->response_buffer);
328  }
329 
330  /* Make a copy of the response. */
331  tx->response_buffer = SCCalloc(1, input_len);
332  if (unlikely(tx->response_buffer == NULL)) {
333  goto end;
334  }
335  memcpy(tx->response_buffer, input, input_len);
336  tx->response_buffer_len = input_len;
337 
338  /* Set the response_done flag for transaction state checking in
339  * TemplateGetStateProgress(). */
340  tx->response_done = 1;
341 
342 end:
343  return 0;
344 }
345 
346 static uint64_t TemplateGetTxCnt(void *statev)
347 {
348  const TemplateState *state = statev;
349  SCLogNotice("Current tx count is %"PRIu64".", state->transaction_max);
350  return state->transaction_max;
351 }
352 
353 static void *TemplateGetTx(void *statev, uint64_t tx_id)
354 {
355  TemplateState *state = statev;
357 
358  SCLogNotice("Requested tx ID %"PRIu64".", tx_id);
359 
360  TAILQ_FOREACH(tx, &state->tx_list, next) {
361  if (tx->tx_id == tx_id) {
362  SCLogNotice("Transaction %"PRIu64" found, returning tx object %p.",
363  tx_id, tx);
364  return tx;
365  }
366  }
367 
368  SCLogNotice("Transaction ID %"PRIu64" not found.", tx_id);
369  return NULL;
370 }
371 
372 static void TemplateSetTxLogged(void *state, void *vtx, LoggerId logged)
373 {
375  tx->logged = logged;
376 }
377 
378 static LoggerId TemplateGetTxLogged(void *state, void *vtx)
379 {
380  const TemplateTransaction *tx = (TemplateTransaction *)vtx;
381  return tx->logged;
382 }
383 
384 /**
385  * \brief Called by the application layer.
386  *
387  * In most cases 1 can be returned here.
388  */
389 static int TemplateGetAlstateProgressCompletionStatus(uint8_t direction) {
390  return 1;
391 }
392 
393 /**
394  * \brief Return the state of a transaction in a given direction.
395  *
396  * In the case of the echo protocol, the existence of a transaction
397  * means that the request is done. However, some protocols that may
398  * need multiple chunks of data to complete the request may need more
399  * than just the existence of a transaction for the request to be
400  * considered complete.
401  *
402  * For the response to be considered done, the response for a request
403  * needs to be seen. The response_done flag is set on response for
404  * checking here.
405  */
406 static int TemplateGetStateProgress(void *txv, uint8_t direction)
407 {
408  TemplateTransaction *tx = txv;
409 
410  SCLogNotice("Transaction progress requested for tx ID %"PRIu64
411  ", direction=0x%02x", tx->tx_id, direction);
412 
413  if (direction & STREAM_TOCLIENT && tx->response_done) {
414  return 1;
415  }
416  else if (direction & STREAM_TOSERVER) {
417  /* For the template, just the existence of the transaction means the
418  * request is done. */
419  return 1;
420  }
421 
422  return 0;
423 }
424 
425 /**
426  * \brief retrieve the detection engine per tx state
427  */
428 static DetectEngineState *TemplateGetTxDetectState(void *vtx)
429 {
430  TemplateTransaction *tx = vtx;
431  return tx->de_state;
432 }
433 
434 /**
435  * \brief get the detection engine per tx state
436  */
437 static int TemplateSetTxDetectState(void *vtx,
439 {
440  TemplateTransaction *tx = vtx;
441  tx->de_state = s;
442  return 0;
443 }
444 
446 {
447  const char *proto_name = "template";
448 
449  /* TEMPLATE_START_REMOVE */
450  if (ConfGetNode("app-layer.protocols.template") == NULL) {
451  return;
452  }
453  /* TEMPLATE_END_REMOVE */
454  /* Check if Template TCP detection is enabled. If it does not exist in
455  * the configuration file then it will be enabled by default. */
456  if (AppLayerProtoDetectConfProtoDetectionEnabled("tcp", proto_name)) {
457 
458  SCLogNotice("Template TCP protocol detection enabled.");
459 
461 
462  if (RunmodeIsUnittests()) {
463 
464  SCLogNotice("Unittest mode, registeringd default configuration.");
467  TemplateProbingParser, NULL);
468 
469  }
470  else {
471 
472  if (!AppLayerProtoDetectPPParseConfPorts("tcp", IPPROTO_TCP,
474  TemplateProbingParser, NULL)) {
475  SCLogNotice("No template app-layer configuration, enabling echo"
476  " detection TCP detection on port %s.",
478  AppLayerProtoDetectPPRegister(IPPROTO_TCP,
481  TemplateProbingParser, NULL);
482  }
483 
484  }
485 
486  }
487 
488  else {
489  SCLogNotice("Protocol detecter and parser disabled for Template.");
490  return;
491  }
492 
493  if (AppLayerParserConfParserEnabled("tcp", proto_name)) {
494 
495  SCLogNotice("Registering Template protocol parser.");
496 
497  /* Register functions for state allocation and freeing. A
498  * state is allocated for every new Template flow. */
500  TemplateStateAlloc, TemplateStateFree);
501 
502  /* Register request parser for parsing frame from server to client. */
504  STREAM_TOSERVER, TemplateParseRequest);
505 
506  /* Register response parser for parsing frames from server to client. */
508  STREAM_TOCLIENT, TemplateParseResponse);
509 
510  /* Register a function to be called by the application layer
511  * when a transaction is to be freed. */
513  TemplateStateTxFree);
514 
516  TemplateGetTxLogged, TemplateSetTxLogged);
517 
518  /* Register a function to return the current transaction count. */
520  TemplateGetTxCnt);
521 
522  /* Transaction handling. */
524  TemplateGetAlstateProgressCompletionStatus);
526  ALPROTO_TEMPLATE, TemplateGetStateProgress);
528  TemplateGetTx);
529 
530  /* What is this being registered for? */
532  TemplateGetTxDetectState, TemplateSetTxDetectState);
533 
535  TemplateStateGetEventInfo);
537  TemplateGetEvents);
538  }
539  else {
540  SCLogNotice("Template protocol parsing disabled.");
541  }
542 
543 #ifdef UNITTESTS
546 #endif
547 }
548 
549 #ifdef UNITTESTS
550 #endif
551 
553 {
554 #ifdef UNITTESTS
555 #endif
556 }
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:205
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:261
#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:236
#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:327
AppLayerDecoderEvents * decoder_events
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))