suricata
app-layer-tftp.c
Go to the documentation of this file.
1 /* Copyright (C) 2017 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 /**
20  * \file
21  *
22  * \author ClĂ©ment Galland <clement.galland@epita.fr>
23  *
24  * Parser for NTP application layer running on UDP port 69.
25  */
26 
27 
28 #include "suricata-common.h"
29 #include "stream.h"
30 #include "conf.h"
31 
32 #include "util-unittest.h"
33 
34 #include "app-layer-detect-proto.h"
35 #include "app-layer-parser.h"
36 
37 #include "app-layer-tftp.h"
38 
39 #ifndef HAVE_RUST
40 
42 {
43 }
44 
45 #else
46 
47 #include "rust-tftp-tftp-gen.h"
48 
49 /* The default port to probe if not provided in the configuration file. */
50 #define TFTP_DEFAULT_PORT "69"
51 
52 /* The minimum size for an message. For some protocols this might
53  * be the size of a header. */
54 #define TFTP_MIN_FRAME_LEN 4
55 
56 /* Enum of app-layer events for an echo protocol. Normally you might
57  * have events for errors in parsing data, like unexpected data being
58  * received. For echo we'll make something up, and log an app-layer
59  * level alert if an empty message is received.
60  *
61  * Example rule:
62  *
63  * alert tftp any any -> any any (msg:"SURICATA TFTP empty message"; \
64  * app-layer-event:tftp.empty_message; sid:X; rev:Y;)
65  */
66 enum {
67  TFTP_DECODER_EVENT_EMPTY_MESSAGE,
68 };
69 
70 SCEnumCharMap tftp_decoder_event_table[] = {
71  {"EMPTY_MESSAGE", TFTP_DECODER_EVENT_EMPTY_MESSAGE},
72 
73  // event table must be NULL-terminated
74  { NULL, -1 },
75 };
76 
77 static void *TFTPStateAlloc(void)
78 {
79  return rs_tftp_state_alloc();
80 }
81 
82 static void TFTPStateFree(void *state)
83 {
84  rs_tftp_state_free(state);
85 }
86 
87 /**
88  * \brief Callback from the application layer to have a transaction freed.
89  *
90  * \param state a void pointer to the TFTPState object.
91  * \param tx_id the transaction ID to free.
92  */
93 static void TFTPStateTxFree(void *state, uint64_t tx_id)
94 {
95  rs_tftp_state_tx_free(state, tx_id);
96 }
97 
98 static int TFTPStateGetEventInfo(const char *event_name, int *event_id,
100 {
101  return -1;
102 }
103 
104 static AppLayerDecoderEvents *TFTPGetEvents(void *state, uint64_t tx_id)
105 {
106  return NULL;
107 }
108 
109 /**
110  * \brief Probe the input to see if it looks like echo.
111  *
112  * \retval ALPROTO_TFTP if it looks like echo, otherwise
113  * ALPROTO_UNKNOWN.
114  */
115 static AppProto TFTPProbingParser(Flow *f, uint8_t *input, uint32_t input_len)
116 {
117  /* Very simple test - if there is input, this is tftp.
118  * Also check if it's starting by a zero */
119  if (input_len >= TFTP_MIN_FRAME_LEN && *input == 0) {
120  SCLogDebug("Detected as ALPROTO_TFTP.");
121  return ALPROTO_TFTP;
122  }
123 
124  SCLogDebug("Protocol not detected as ALPROTO_TFTP.");
125  return ALPROTO_UNKNOWN;
126 }
127 
128 static int TFTPParseRequest(Flow *f, void *state,
129  AppLayerParserState *pstate, uint8_t *input, uint32_t input_len,
130  void *local_data, const uint8_t flags)
131 {
132  SCLogDebug("Parsing echo request: len=%"PRIu32, input_len);
133 
134  /* Likely connection closed, we can just return here. */
135  if ((input == NULL || input_len == 0) &&
137  return 0;
138  }
139 
140  /* Probably don't want to create a transaction in this case
141  * either. */
142  if (input == NULL || input_len == 0) {
143  return 0;
144  }
145 
146  return rs_tftp_request(state, input, input_len);
147 }
148 
149 /**
150  * \brief Response parsing is not implemented
151  */
152 static int TFTPParseResponse(Flow *f, void *state, AppLayerParserState *pstate,
153  uint8_t *input, uint32_t input_len, void *local_data,
154  const uint8_t flags)
155 {
156  return 0;
157 }
158 
159 static uint64_t TFTPGetTxCnt(void *state)
160 {
161  return rs_tftp_get_tx_cnt(state);
162 }
163 
164 static void *TFTPGetTx(void *state, uint64_t tx_id)
165 {
166  return rs_tftp_get_tx(state, tx_id);
167 }
168 
169 static void TFTPSetTxLogged(void *state, void *vtx, uint32_t logger)
170 {
171  rs_tftp_set_tx_logged(state, vtx, logger);
172 }
173 
174 static LoggerId TFTPGetTxLogged(void *state, void *vtx)
175 {
176  return rs_tftp_get_tx_logged(state, vtx);
177 }
178 
179 /**
180  * \brief Called by the application layer.
181  *
182  * In most cases 1 can be returned here.
183  */
184 static int TFTPGetAlstateProgressCompletionStatus(uint8_t direction) {
185  return 1;
186 }
187 
188 /**
189  * \brief Return the state of a transaction in a given direction.
190  *
191  * In the case of the echo protocol, the existence of a transaction
192  * means that the request is done. However, some protocols that may
193  * need multiple chunks of data to complete the request may need more
194  * than just the existence of a transaction for the request to be
195  * considered complete.
196  *
197  * For the response to be considered done, the response for a request
198  * needs to be seen. The response_done flag is set on response for
199  * checking here.
200  */
201 static int TFTPGetStateProgress(void *tx, uint8_t direction)
202 {
203  return 1;
204 }
205 
206 static DetectEngineState *TFTPGetTxDetectState(void *vtx)
207 {
208  return NULL;
209 }
210 
211 static int TFTPSetTxDetectState(void *vtx,
213 {
214  return 0;
215 }
216 
217 void RegisterTFTPParsers(void)
218 {
219  const char *proto_name = "tftp";
220 
221  /* Check if TFTP UDP detection is enabled. If it does not exist in
222  * the configuration file then it will be enabled by default. */
223  if (AppLayerProtoDetectConfProtoDetectionEnabled("udp", proto_name)) {
224 
225  SCLogDebug("TFTP UDP protocol detection enabled.");
226 
228 
229  if (RunmodeIsUnittests()) {
230  SCLogDebug("Unittest mode, registeringd default configuration.");
231  AppLayerProtoDetectPPRegister(IPPROTO_UDP, TFTP_DEFAULT_PORT,
232  ALPROTO_TFTP, 0, TFTP_MIN_FRAME_LEN,
233  STREAM_TOSERVER, TFTPProbingParser,
234  NULL);
235  } else {
236  if (!AppLayerProtoDetectPPParseConfPorts("udp", IPPROTO_UDP,
237  proto_name, ALPROTO_TFTP,
238  0, TFTP_MIN_FRAME_LEN,
239  TFTPProbingParser, NULL)) {
240  SCLogDebug("No echo app-layer configuration, enabling echo"
241  " detection UDP detection on port %s.",
242  TFTP_DEFAULT_PORT);
243  AppLayerProtoDetectPPRegister(IPPROTO_UDP,
244  TFTP_DEFAULT_PORT, ALPROTO_TFTP,
245  0, TFTP_MIN_FRAME_LEN,
246  STREAM_TOSERVER,TFTPProbingParser,
247  NULL);
248  }
249  }
250  } else {
251  SCLogDebug("Protocol detecter and parser disabled for TFTP.");
252  return;
253  }
254 
255  if (AppLayerParserConfParserEnabled("udp", proto_name)) {
256 
257  SCLogDebug("Registering TFTP protocol parser.");
258 
259  /* Register functions for state allocation and freeing. A
260  * state is allocated for every new TFTP flow. */
262  TFTPStateAlloc, TFTPStateFree);
263 
264  /* Register request parser for parsing frame from server to client. */
266  STREAM_TOSERVER, TFTPParseRequest);
267 
268  /* Register response parser for parsing frames from server to client. */
270  STREAM_TOCLIENT, TFTPParseResponse);
271 
272  /* Register a function to be called by the application layer
273  * when a transaction is to be freed. */
275  TFTPStateTxFree);
276 
278  TFTPGetTxLogged, TFTPSetTxLogged);
279 
280  /* Register a function to return the current transaction count. */
282  TFTPGetTxCnt);
283 
284  /* Transaction handling. */
286  TFTPGetAlstateProgressCompletionStatus);
288  ALPROTO_TFTP,
289  TFTPGetStateProgress);
291  TFTPGetTx);
292 
293  /* What is this being registered for? */
295  TFTPGetTxDetectState,
296  TFTPSetTxDetectState);
297 
299  TFTPStateGetEventInfo);
301  TFTPGetEvents);
302  }
303  else {
304  SCLogDebug("TFTP protocol parsing disabled.");
305  }
306 
307 #ifdef UNITTESTS
310 #endif
311 }
312 
313 #ifdef UNITTESTS
314 #endif
315 
316 void TFTPParserRegisterTests(void)
317 {
318 #ifdef UNITTESTS
319 #endif
320 }
321 
322 #endif /* HAVE_RUST */
void RegisterTFTPParsers(void)
enum AppLayerEventType_ AppLayerEventType
uint16_t flags
#define SCLogDebug(...)
Definition: util-debug.h:335
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
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
uint32_t event_type
LoggerId
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 *))
void AppLayerParserRegisterLoggerFuncs(uint8_t ipproto, AppProto alproto, LoggerId(*StateGetTxLogged)(void *, void *), void(*StateSetTxLogged)(void *, void *, LoggerId))
uint16_t AppProto
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...
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.
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
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 STREAM_TOCLIENT
Definition: stream.h:32
void AppLayerParserRegisterGetStateProgressCompletionStatus(AppProto alproto, int(*StateGetProgressCompletionStatus)(uint8_t direction))
int RunmodeIsUnittests(void)
Definition: suricata.c:261
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
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))
void TFTPParserRegisterTests(void)
#define STREAM_TOSERVER
Definition: stream.h:31
#define APP_LAYER_PARSER_EOF
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 *))
Flow data structure.
Definition: flow.h:327
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))