1 /* packet-syslog.c
2 * Routines for syslog message dissection
3 *
4 * Copyright 2000, Gerald Combs <gerald[AT]wireshark.org>
5 *
6 * Support for passing SS7 MSUs (from the Cisco ITP Packet Logging
7 * facility) to the MTP3 dissector by Abhik Sarkar <sarkar.abhik[AT]gmail.com>
8 * with some rework by Jeff Morriss <jeff.morriss.ws [AT] gmail.com>
9 *
10 * Wireshark - Network traffic analyzer
11 * By Gerald Combs <gerald[AT]wireshark.org>
12 * Copyright 1998 Gerald Combs
13 *
14 * SPDX-License-Identifier: GPL-2.0-or-later
15 */
16
17 #include "config.h"
18
19
20 #include <epan/packet.h>
21 #include <epan/strutil.h>
22
23 #include "packet-syslog.h"
24 #include "packet-acdr.h"
25
26 #define UDP_PORT_SYSLOG 514
27
28 #define PRIORITY_MASK 0x0007 /* 0000 0111 */
29 #define FACILITY_MASK 0x03f8 /* 1111 1000 */
30
31 void proto_reg_handoff_syslog(void);
32 void proto_register_syslog(void);
33
34 /* The maximum number if priority digits to read in. */
35 #define MAX_DIGITS 3
36
37 static const value_string short_level_vals[] = {
38 { LEVEL_EMERG, "EMERG" },
39 { LEVEL_ALERT, "ALERT" },
40 { LEVEL_CRIT, "CRIT" },
41 { LEVEL_ERR, "ERR" },
42 { LEVEL_WARNING, "WARNING" },
43 { LEVEL_NOTICE, "NOTICE" },
44 { LEVEL_INFO, "INFO" },
45 { LEVEL_DEBUG, "DEBUG" },
46 { 0, NULL }
47 };
48
49 static const value_string short_facility_vals[] = {
50 { FAC_KERN, "KERN" },
51 { FAC_USER, "USER" },
52 { FAC_MAIL, "MAIL" },
53 { FAC_DAEMON, "DAEMON" },
54 { FAC_AUTH, "AUTH" },
55 { FAC_SYSLOG, "SYSLOG" },
56 { FAC_LPR, "LPR" },
57 { FAC_NEWS, "NEWS" },
58 { FAC_UUCP, "UUCP" },
59 { FAC_CRON, "CRON" }, /* The BSDs, Linux, and others */
60 { FAC_AUTHPRIV, "AUTHPRIV" },
61 { FAC_FTP, "FTP" },
62 { FAC_NTP, "NTP" },
63 { FAC_LOGAUDIT, "LOGAUDIT" },
64 { FAC_LOGALERT, "LOGALERT" },
65 { FAC_CRON_SOL, "CRON" }, /* Solaris */
66 { FAC_LOCAL0, "LOCAL0" },
67 { FAC_LOCAL1, "LOCAL1" },
68 { FAC_LOCAL2, "LOCAL2" },
69 { FAC_LOCAL3, "LOCAL3" },
70 { FAC_LOCAL4, "LOCAL4" },
71 { FAC_LOCAL5, "LOCAL5" },
72 { FAC_LOCAL6, "LOCAL6" },
73 { FAC_LOCAL7, "LOCAL7" },
74 { 0, NULL }
75 };
76
77 static gint proto_syslog = -1;
78 static gint hf_syslog_level = -1;
79 static gint hf_syslog_facility = -1;
80 static gint hf_syslog_msg = -1;
81 static gint hf_syslog_msu_present = -1;
82 static gint hf_syslog_version = -1;
83 static gint hf_syslog_timestamp = -1;
84 static gint hf_syslog_timestamp_old = -1;
85 static gint hf_syslog_hostname = -1;
86 static gint hf_syslog_appname = -1;
87 static gint hf_syslog_procid = -1;
88 static gint hf_syslog_msgid = -1;
89 static gint hf_syslog_msgid_utf8 = -1;
90 static gint hf_syslog_msgid_bom = -1;
91
92 static gint ett_syslog = -1;
93 static gint ett_syslog_msg = -1;
94
95 static dissector_handle_t syslog_handle;
96
97 static dissector_handle_t mtp_handle;
98
99 /* The Cisco ITP's packet logging facility allows selected (SS7) MSUs to be
100 * to be encapsulated in syslog UDP datagrams and sent to a monitoring tool.
101 * However, no actual tool to monitor/decode the MSUs is provided. The aim
102 * of this routine is to extract the hex dump of the MSU from the syslog
103 * packet so that it can be passed on to the mtp3 dissector for decoding.
104 */
105 static tvbuff_t *
mtp3_msu_present(tvbuff_t * tvb,packet_info * pinfo,gint fac,gint level,const char * msg_str,gint chars_truncated)106 mtp3_msu_present(tvbuff_t *tvb, packet_info *pinfo, gint fac, gint level, const char *msg_str, gint chars_truncated)
107 {
108 size_t nbytes;
109 size_t len;
110 gchar **split_string, *msu_hex_dump;
111 tvbuff_t *mtp3_tvb = NULL;
112 guint8 *byte_array;
113
114 /* In the sample capture I have, all MSUs are LOCAL0.DEBUG.
115 * Try to optimize this routine for most syslog users by short-cutting
116 * out here.
117 */
118 if (!(fac == FAC_LOCAL0 && level == LEVEL_DEBUG))
119 return NULL;
120
121 if (strstr(msg_str, "msu=") == NULL)
122 return NULL;
123
124 split_string = g_strsplit(msg_str, "msu=", 2);
125 msu_hex_dump = split_string[1];
126
127 if (msu_hex_dump && (len = strlen(msu_hex_dump))) {
128
129 /* convert_string_to_hex() will return NULL if it gets an incomplete
130 * byte. If we have an odd string length then chop off the remaining
131 * nibble so we can get at least a partial MSU (chances are the
132 * subdissector will except out, of course).
133 */
134 if (len % 2)
135 msu_hex_dump[len - 1] = '\0';
136
137 byte_array = convert_string_to_hex(msu_hex_dump, &nbytes);
138
139 if (byte_array) {
140 mtp3_tvb = tvb_new_child_real_data(tvb, byte_array, (guint)nbytes,
141 (guint)nbytes + chars_truncated / 2);
142 tvb_set_free_cb(mtp3_tvb, g_free);
143 /* ...and add the encapsulated MSU as a new data source so that it gets
144 * its own tab in the packet bytes pane.
145 */
146 add_new_data_source(pinfo, mtp3_tvb, "Encapsulated MSU");
147 }
148 }
149
150 g_strfreev(split_string);
151
152 return(mtp3_tvb);
153 }
154
dissect_syslog_info(proto_tree * tree,tvbuff_t * tvb,guint * offset,gint hfindex)155 static gboolean dissect_syslog_info(proto_tree* tree, tvbuff_t* tvb, guint* offset, gint hfindex)
156 {
157 gint end_offset = tvb_find_guint8(tvb, *offset, -1, ' ');
158 if (end_offset == -1)
159 return FALSE;
160 proto_tree_add_item(tree, hfindex, tvb, *offset, end_offset - *offset, ENC_NA);
161 *offset = end_offset + 1;
162 return TRUE;
163 }
164
165 /* Dissect message as defined in RFC5424 */
166 static void
dissect_syslog_message(proto_tree * tree,tvbuff_t * tvb,guint offset)167 dissect_syslog_message(proto_tree* tree, tvbuff_t* tvb, guint offset)
168 {
169 gint end_offset;
170
171 if (!dissect_syslog_info(tree, tvb, &offset, hf_syslog_version))
172 return;
173
174 end_offset = tvb_find_guint8(tvb, offset, -1, ' ');
175 if (end_offset == -1)
176 return;
177 if ((guint)end_offset != offset) {
178 /* do not call proto_tree_add_time_item with a length of 0 */
179 proto_tree_add_time_item(tree, hf_syslog_timestamp, tvb, offset, end_offset - offset, ENC_ISO_8601_DATE_TIME,
180 NULL, NULL, NULL);
181 }
182 offset = end_offset + 1;
183
184 if (!dissect_syslog_info(tree, tvb, &offset, hf_syslog_hostname))
185 return;
186 if (!dissect_syslog_info(tree, tvb, &offset, hf_syslog_appname))
187 return;
188 if (!dissect_syslog_info(tree, tvb, &offset, hf_syslog_procid))
189 return;
190 if (tvb_get_guint24(tvb, offset, ENC_BIG_ENDIAN) == 0xefbbbf) {
191 proto_tree_add_item(tree, hf_syslog_msgid_bom, tvb, offset, 3, ENC_BIG_ENDIAN);
192 offset += 3;
193 proto_tree_add_item(tree, hf_syslog_msgid_utf8, tvb, offset, tvb_reported_length_remaining(tvb, offset), ENC_UTF_8|ENC_NA);
194 } else {
195 proto_tree_add_item(tree, hf_syslog_msgid, tvb, offset, tvb_reported_length_remaining(tvb, offset), ENC_ASCII|ENC_NA);
196 }
197 }
198
199 /* Dissect message as defined in RFC3164 */
200 static void
dissect_rfc3164_syslog_message(proto_tree * tree,tvbuff_t * tvb,guint offset)201 dissect_rfc3164_syslog_message(proto_tree* tree, tvbuff_t* tvb, guint offset)
202 {
203 guint tvb_offset = 0;
204
205 /* Simple check if the first 16 bytes look like TIMESTAMP "Mmm dd hh:mm:ss"
206 * by checking for spaces and colons. Otherwise return without processing
207 * the message. */
208 if (tvb_get_guint8(tvb, offset + 3) == ' ' && tvb_get_guint8(tvb, offset + 6) == ' ' &&
209 tvb_get_guint8(tvb, offset + 9) == ':' && tvb_get_guint8(tvb, offset + 12) == ':' &&
210 tvb_get_guint8(tvb, offset + 15) == ' ') {
211 proto_tree_add_item(tree, hf_syslog_timestamp_old, tvb, offset, 15, ENC_ASCII|ENC_NA);
212 offset += 16;
213 } else {
214 return;
215 }
216
217 if (!dissect_syslog_info(tree, tvb, &offset, hf_syslog_hostname))
218 return;
219 for (tvb_offset=offset; tvb_offset < offset+32; tvb_offset++){
220 guint8 octet;
221 octet = tvb_get_guint8(tvb, tvb_offset);
222 if (!g_ascii_isalnum(octet)){
223 proto_tree_add_item(tree, hf_syslog_procid, tvb, offset, tvb_offset - offset, ENC_ASCII|ENC_NA);
224 offset = tvb_offset;
225 break;
226 }
227 }
228 proto_tree_add_item(tree, hf_syslog_msgid, tvb, offset, tvb_reported_length_remaining(tvb, offset), ENC_ASCII|ENC_NA);
229 }
230
231 /* The message format is defined in RFC 3164 */
232 static int
dissect_syslog(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)233 dissect_syslog(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
234 {
235 gint pri = -1, lev = -1, fac = -1;
236 gint msg_off = 0, msg_len, reported_msg_len;
237 proto_item *ti;
238 proto_tree *syslog_tree;
239 proto_tree *syslog_message_tree;
240 const char *msg_str;
241 tvbuff_t *mtp3_tvb;
242
243 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Syslog");
244 col_clear(pinfo->cinfo, COL_INFO);
245
246 if (tvb_get_guint8(tvb, msg_off) == '<') {
247 /* A facility and level follow. */
248 msg_off++;
249 pri = 0;
250 while (tvb_bytes_exist(tvb, msg_off, 1) &&
251 g_ascii_isdigit(tvb_get_guint8(tvb, msg_off)) && msg_off <= MAX_DIGITS) {
252 pri = pri * 10 + (tvb_get_guint8(tvb, msg_off) - '0');
253 msg_off++;
254 }
255 if (tvb_get_guint8(tvb, msg_off) == '>')
256 msg_off++;
257 fac = (pri & FACILITY_MASK) >> 3;
258 lev = pri & PRIORITY_MASK;
259 }
260
261 msg_len = tvb_ensure_captured_length_remaining(tvb, msg_off);
262 msg_str = tvb_format_text(pinfo->pool, tvb, msg_off, msg_len);
263 reported_msg_len = tvb_reported_length_remaining(tvb, msg_off);
264
265 mtp3_tvb = mtp3_msu_present(tvb, pinfo, fac, lev, msg_str,
266 (reported_msg_len - msg_len));
267
268 if (mtp3_tvb == NULL) {
269 if (pri >= 0) {
270 col_add_fstr(pinfo->cinfo, COL_INFO, "%s.%s: %s",
271 val_to_str_const(fac, short_facility_vals, "UNKNOWN"),
272 val_to_str_const(lev, short_level_vals, "UNKNOWN"), msg_str);
273 } else {
274 col_add_str(pinfo->cinfo, COL_INFO, msg_str);
275 }
276 }
277
278 if (tree) {
279 if (pri >= 0) {
280 ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0, -1,
281 "Syslog message: %s.%s: %s",
282 val_to_str_const(fac, short_facility_vals, "UNKNOWN"),
283 val_to_str_const(lev, short_level_vals, "UNKNOWN"), msg_str);
284 } else {
285 ti = proto_tree_add_protocol_format(tree, proto_syslog, tvb, 0, -1,
286 "Syslog message: (unknown): %s", msg_str);
287 }
288 syslog_tree = proto_item_add_subtree(ti, ett_syslog);
289 if (pri >= 0) {
290 proto_tree_add_uint(syslog_tree, hf_syslog_facility, tvb, 0,
291 msg_off, pri);
292 proto_tree_add_uint(syslog_tree, hf_syslog_level, tvb, 0,
293 msg_off, pri);
294 }
295 ti = proto_tree_add_item(syslog_tree, hf_syslog_msg, tvb, msg_off,
296 msg_len, ENC_UTF_8|ENC_NA);
297 syslog_message_tree = proto_item_add_subtree(ti, ett_syslog_msg);
298
299 /* RFC5424 defines a version field which is currently defined as '1'
300 * followed by a space (0x3120). Otherwise the message is probable
301 * a RFC3164 message.
302 */
303 if (msg_len > 2 && tvb_get_ntohs(tvb, msg_off) == 0x3120) {
304 dissect_syslog_message(syslog_message_tree, tvb, msg_off);
305 } else if ( msg_len > 15) {
306 dissect_rfc3164_syslog_message(syslog_message_tree, tvb, msg_off);
307 }
308
309 if (mtp3_tvb) {
310 proto_item *mtp3_item;
311 mtp3_item = proto_tree_add_boolean(syslog_tree, hf_syslog_msu_present,
312 tvb, msg_off, msg_len, TRUE);
313 proto_item_set_generated(mtp3_item);
314 }
315 }
316
317 /* Call MTP dissector if encapsulated MSU was found... */
318 if (mtp3_tvb) {
319 call_dissector(mtp_handle, mtp3_tvb, pinfo, tree);
320 }
321
322 return tvb_captured_length(tvb);
323 }
324
325 /* Register the protocol with Wireshark */
proto_register_syslog(void)326 void proto_register_syslog(void)
327 {
328
329 /* Setup list of header fields */
330 static hf_register_info hf[] = {
331 { &hf_syslog_facility,
332 { "Facility", "syslog.facility",
333 FT_UINT8, BASE_DEC, VALS(syslog_facility_vals), FACILITY_MASK,
334 "Message facility", HFILL }
335 },
336 { &hf_syslog_level,
337 { "Level", "syslog.level",
338 FT_UINT8, BASE_DEC, VALS(syslog_level_vals), PRIORITY_MASK,
339 "Message level", HFILL }
340 },
341 { &hf_syslog_msg,
342 { "Message", "syslog.msg",
343 FT_STRING, BASE_NONE, NULL, 0x0,
344 "Message Text", HFILL }
345 },
346 { &hf_syslog_msu_present,
347 { "SS7 MSU present", "syslog.msu_present",
348 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
349 "True if an SS7 MSU was detected in the syslog message",
350 HFILL }
351 },
352 { &hf_syslog_version,
353 { "Syslog version", "syslog.version",
354 FT_STRING, BASE_NONE, NULL, 0x0,
355 NULL,
356 HFILL }
357 },
358 { &hf_syslog_timestamp,
359 { "Syslog timestamp", "syslog.timestamp",
360 FT_ABSOLUTE_TIME, ABSOLUTE_TIME_UTC, NULL, 0x0,
361 NULL,
362 HFILL }
363 },
364 { &hf_syslog_timestamp_old,
365 { "Syslog timestamp (RFC3164)", "syslog.timestamp_rfc3164",
366 FT_STRING, ENC_ASCII, NULL, 0x0,
367 NULL,
368 HFILL }
369 },
370 { &hf_syslog_hostname,
371 { "Syslog hostname", "syslog.hostname",
372 FT_STRING, ENC_ASCII, NULL, 0x0,
373 NULL,
374 HFILL }
375 },
376 { &hf_syslog_appname,
377 { "Syslog app name", "syslog.appname",
378 FT_STRING, ENC_ASCII, NULL, 0x0,
379 "The name of the app that generated this message",
380 HFILL }
381 },
382 { &hf_syslog_procid,
383 { "Syslog process id", "syslog.procid",
384 FT_STRING, ENC_ASCII, NULL, 0x0,
385 NULL,
386 HFILL }
387 },
388 { &hf_syslog_msgid,
389 { "Syslog message id", "syslog.msgid",
390 FT_STRING, ENC_ASCII, NULL, 0x0,
391 NULL,
392 HFILL }
393 },
394 { &hf_syslog_msgid_utf8,
395 { "Syslog message id", "syslog.msgid",
396 FT_STRING, STR_UNICODE, NULL, 0x0,
397 NULL,
398 HFILL }
399 },
400 { &hf_syslog_msgid_bom,
401 { "Syslog BOM", "syslog.msgid.bom",
402 FT_UINT24, BASE_HEX, NULL, 0x0,
403 NULL,
404 HFILL }
405 }
406 };
407
408 /* Setup protocol subtree array */
409 static gint *ett[] = {
410 &ett_syslog,
411 &ett_syslog_msg
412 };
413
414 /* Register the protocol name and description */
415 proto_syslog = proto_register_protocol("Syslog message", "Syslog", "syslog");
416
417 /* Required function calls to register the header fields and subtrees used */
418 proto_register_field_array(proto_syslog, hf, array_length(hf));
419 proto_register_subtree_array(ett, array_length(ett));
420
421 syslog_handle = register_dissector("syslog", dissect_syslog, proto_syslog);
422 }
423
424 void
proto_reg_handoff_syslog(void)425 proto_reg_handoff_syslog(void)
426 {
427 dissector_add_uint_with_preference("udp.port", UDP_PORT_SYSLOG, syslog_handle);
428 dissector_add_for_decode_as_with_preference("tcp.port", syslog_handle);
429
430 dissector_add_uint("acdr.media_type", ACDR_Info, syslog_handle);
431
432 /* Find the mtp3 dissector */
433 mtp_handle = find_dissector_add_dependency("mtp3", proto_syslog);
434 }
435
436 /*
437 * Editor modelines - https://www.wireshark.org/tools/modelines.html
438 *
439 * Local Variables:
440 * c-basic-offset: 2
441 * tab-width: 8
442 * indent-tabs-mode: nil
443 * End:
444 *
445 * ex: set shiftwidth=2 tabstop=8 expandtab:
446 * :indentSize=2:tabSize=8:noTabs=true:
447 */
448