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