suricata
alert-syslog.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2021 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  * \file
20  *
21  * \author Gurvinder Singh <gurvindersinghdahiya@gmail.com>
22  *
23  * Logs alerts in a line based text format into syslog.
24  *
25  */
26 
27 #include "suricata-common.h"
28 #include "flow.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "tm-threads.h"
33 #include "threadvars.h"
34 
35 #include "detect.h"
36 #include "detect-parse.h"
37 #include "detect-engine.h"
38 #include "detect-engine-mpm.h"
39 #include "detect-reference.h"
40 
41 #include "output.h"
42 #include "alert-syslog.h"
43 
45 #include "util-debug.h"
46 #include "util-print.h"
47 #include "util-proto-name.h"
48 #include "util-syslog.h"
49 #include "util-optimize.h"
50 #include "util-logopenfile.h"
51 #include "action-globals.h"
52 
53 #ifndef OS_WIN32
54 
55 #define MODULE_NAME "AlertSyslog"
56 
57 static int alert_syslog_level = DEFAULT_ALERT_SYSLOG_LEVEL;
58 
59 typedef struct AlertSyslogThread_ {
60  /** LogFileCtx has the pointer to the file and a mutex to allow multithreading */
63 
64 /**
65  * \brief Function to clear the memory of the output context and closes the
66  * syslog interface
67  *
68  * \param output_ctx pointer to the output context to be cleared
69  */
70 static void AlertSyslogDeInitCtx(OutputCtx *output_ctx)
71 {
72  if (output_ctx != NULL) {
73  LogFileCtx *logfile_ctx = (LogFileCtx *)output_ctx->data;
74  if (logfile_ctx != NULL) {
75  LogFileFreeCtx(logfile_ctx);
76  }
77  SCFree(output_ctx);
78  }
79  closelog();
80 }
81 
82 /**
83  * \brief Create a new LogFileCtx for "syslog" output style.
84  *
85  * \param conf The configuration node for this output.
86  * \return A OutputCtx pointer on success, NULL on failure.
87  */
88 static OutputInitResult AlertSyslogInitCtx(ConfNode *conf)
89 {
90  SCLogWarning("The syslog output has been deprecated and will be removed in Suricata 9.0.");
91 
92  OutputInitResult result = { NULL, false };
93  const char *facility_s = ConfNodeLookupChildValue(conf, "facility");
94  if (facility_s == NULL) {
96  }
97 
98  LogFileCtx *logfile_ctx = LogFileNewCtx();
99  if (logfile_ctx == NULL) {
100  SCLogDebug("AlertSyslogInitCtx: Could not create new LogFileCtx");
101  return result;
102  }
103 
104  int facility = SCMapEnumNameToValue(facility_s, SCSyslogGetFacilityMap());
105  if (facility == -1) {
106  SCLogWarning("Invalid syslog facility: \"%s\","
107  " now using \"%s\" as syslog facility",
110  }
111 
112  const char *level_s = ConfNodeLookupChildValue(conf, "level");
113  if (level_s != NULL) {
114  int level = SCMapEnumNameToValue(level_s, SCSyslogGetLogLevelMap());
115  if (level != -1) {
116  alert_syslog_level = level;
117  }
118  }
119 
120  const char *ident = ConfNodeLookupChildValue(conf, "identity");
121  /* if null we just pass that to openlog, which will then
122  * figure it out by itself. */
123 
124  openlog(ident, LOG_PID|LOG_NDELAY, facility);
125 
126  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
127  if (unlikely(output_ctx == NULL)) {
128  SCLogDebug("could not create new OutputCtx");
129  LogFileFreeCtx(logfile_ctx);
130  return result;
131  }
132 
133  output_ctx->data = logfile_ctx;
134  output_ctx->DeInit = AlertSyslogDeInitCtx;
135 
136  SCLogInfo("Syslog output initialized");
137 
138  result.ctx = output_ctx;
139  result.ok = true;
140  return result;
141 }
142 
143 /**
144  * \brief Function to initialize the AlertSyslogThread and sets the output
145  * context pointer
146  *
147  * \param tv Pointer to the threadvars
148  * \param initdata Pointer to the output context
149  * \param data pointer to pointer to point to the AlertSyslogThread
150  */
151 static TmEcode AlertSyslogThreadInit(ThreadVars *t, const void *initdata, void **data)
152 {
153  if(initdata == NULL) {
154  SCLogDebug("Error getting context for AlertSyslog. \"initdata\" "
155  "argument NULL");
156  return TM_ECODE_FAILED;
157  }
158 
159  AlertSyslogThread *ast = SCCalloc(1, sizeof(AlertSyslogThread));
160  if (unlikely(ast == NULL))
161  return TM_ECODE_FAILED;
162 
163  /** Use the Output Context (file pointer and mutex) */
164  ast->file_ctx = ((OutputCtx *)initdata)->data;
165 
166  *data = (void *)ast;
167  return TM_ECODE_OK;
168 }
169 
170 /**
171  * \brief Function to deinitialize the AlertSyslogThread
172  *
173  * \param tv Pointer to the threadvars
174  * \param data pointer to the AlertSyslogThread to be cleared
175  */
176 static TmEcode AlertSyslogThreadDeinit(ThreadVars *t, void *data)
177 {
178  AlertSyslogThread *ast = (AlertSyslogThread *)data;
179  if (ast == NULL) {
180  return TM_ECODE_OK;
181  }
182 
183  /* clear memory */
184  memset(ast, 0, sizeof(AlertSyslogThread));
185 
186  SCFree(ast);
187  return TM_ECODE_OK;
188 }
189 
190 /**
191  * \brief Function which is called to print the IPv4 alerts to the syslog
192  *
193  * \param tv Pointer to the threadvars
194  * \param p Pointer to the packet
195  * \param data pointer to the AlertSyslogThread
196  *
197  * \return On succes return TM_ECODE_OK
198  */
199 static TmEcode AlertSyslogIPv4(ThreadVars *tv, const Packet *p, void *data)
200 {
201  AlertSyslogThread *ast = (AlertSyslogThread *)data;
202  const char *action = "";
203 
204  if (p->alerts.cnt == 0)
205  return TM_ECODE_OK;
206 
207  char proto[16] = "";
208  const char *protoptr;
209  const IPV4Hdr *ipv4h = PacketGetIPv4(p);
210  const uint8_t ipproto = IPV4_GET_RAW_IPPROTO(ipv4h);
211  if (SCProtoNameValid(ipproto)) {
212  protoptr = known_proto[ipproto];
213  } else {
214  snprintf(proto, sizeof(proto), "PROTO:%03" PRIu8, ipproto);
215  protoptr = proto;
216  }
217 
218  char srcip[16], dstip[16];
219  PrintInet(AF_INET, (const void *)GET_IPV4_SRC_ADDR_PTR(p), srcip, sizeof(srcip));
220  PrintInet(AF_INET, (const void *)GET_IPV4_DST_ADDR_PTR(p), dstip, sizeof(dstip));
221 
222  for (int i = 0; i < p->alerts.cnt; i++) {
223  const PacketAlert *pa = &p->alerts.alerts[i];
224  if (unlikely(pa->s == NULL)) {
225  continue;
226  }
227 
228  if ((pa->action & ACTION_DROP) && EngineModeIsIPS()) {
229  action = "[Drop] ";
230  } else if (pa->action & ACTION_DROP) {
231  action = "[wDrop] ";
232  }
233 
234  /* Not sure if this mutex is needed around calls to syslog. */
235  SCMutexLock(&ast->file_ctx->fp_mutex);
236  syslog(alert_syslog_level, "%s[%" PRIu32 ":%" PRIu32 ":%"
237  PRIu32 "] %s [Classification: %s] [Priority: %"PRIu32"]"
238  " {%s} %s:%" PRIu32 " -> %s:%" PRIu32 "", action, pa->s->gid,
239  pa->s->id, pa->s->rev, pa->s->msg, pa->s->class_msg, pa->s->prio,
240  protoptr, srcip, p->sp, dstip, p->dp);
242  }
243 
244  return TM_ECODE_OK;
245 }
246 
247 /**
248  * \brief Function which is called to print the IPv6 alerts to the syslog
249  *
250  * \param tv Pointer to the threadvars
251  * \param p Pointer to the packet
252  * \param data pointer to the AlertSyslogThread
253  *
254  * \return On succes return TM_ECODE_OK
255  */
256 static TmEcode AlertSyslogIPv6(ThreadVars *tv, const Packet *p, void *data)
257 {
258  AlertSyslogThread *ast = (AlertSyslogThread *)data;
259  const char *action = "";
260 
261  if (p->alerts.cnt == 0)
262  return TM_ECODE_OK;
263 
264  char proto[16] = "";
265  const char *protoptr;
266  const uint8_t ipproto = IPV6_GET_L4PROTO(p);
267  if (SCProtoNameValid(ipproto)) {
268  protoptr = known_proto[ipproto];
269  } else {
270  snprintf(proto, sizeof(proto), "PROTO:03%" PRIu8, ipproto);
271  protoptr = proto;
272  }
273 
274  char srcip[46], dstip[46];
275  PrintInet(AF_INET6, (const void *)GET_IPV6_SRC_ADDR(p), srcip, sizeof(srcip));
276  PrintInet(AF_INET6, (const void *)GET_IPV6_DST_ADDR(p), dstip, sizeof(dstip));
277 
278  for (int i = 0; i < p->alerts.cnt; i++) {
279  const PacketAlert *pa = &p->alerts.alerts[i];
280  if (unlikely(pa->s == NULL)) {
281  continue;
282  }
283 
284  if ((pa->action & ACTION_DROP) && EngineModeIsIPS()) {
285  action = "[Drop] ";
286  } else if (pa->action & ACTION_DROP) {
287  action = "[wDrop] ";
288  }
289 
290  SCMutexLock(&ast->file_ctx->fp_mutex);
291  syslog(alert_syslog_level, "%s[%" PRIu32 ":%" PRIu32 ":%"
292  "" PRIu32 "] %s [Classification: %s] [Priority: %"
293  "" PRIu32 "] {%s} %s:%" PRIu32 " -> %s:%" PRIu32 "",
294  action, pa->s->gid, pa->s->id, pa->s->rev, pa->s->msg, pa->s->class_msg,
295  pa->s->prio, protoptr, srcip, p->sp,
296  dstip, p->dp);
298  }
299 
300  return TM_ECODE_OK;
301 }
302 
303 /**
304  * \brief Function which is called to print the decode alerts to the syslog
305  *
306  * \param tv Pointer to the threadvars
307  * \param p Pointer to the packet
308  * \param data pointer to the AlertSyslogThread
309  *
310  * \return On succes return TM_ECODE_OK
311  */
312 static TmEcode AlertSyslogDecoderEvent(ThreadVars *tv, const Packet *p, void *data)
313 {
314  AlertSyslogThread *ast = (AlertSyslogThread *)data;
315  const char *action = "";
316 
317  if (p->alerts.cnt == 0)
318  return TM_ECODE_OK;
319 
320  char temp_buf_hdr[512];
321  char temp_buf_pkt[65] = "";
322  char temp_buf_tail[64];
323  char alert[2048] = "";
324 
325  for (int i = 0; i < p->alerts.cnt; i++) {
326  const PacketAlert *pa = &p->alerts.alerts[i];
327  if (unlikely(pa->s == NULL)) {
328  continue;
329  }
330 
331  if ((pa->action & ACTION_DROP) && EngineModeIsIPS()) {
332  action = "[Drop] ";
333  } else if (pa->action & ACTION_DROP) {
334  action = "[wDrop] ";
335  }
336 
337  snprintf(temp_buf_hdr, sizeof(temp_buf_hdr), "%s[%" PRIu32 ":%" PRIu32
338  ":%" PRIu32 "] %s [Classification: %s] [Priority: %" PRIu32
339  "] [**] [Raw pkt: ", action, pa->s->gid, pa->s->id, pa->s->rev, pa->s->msg,
340  pa->s->class_msg, pa->s->prio);
341  strlcpy(alert, temp_buf_hdr, sizeof(alert));
342 
343  PrintRawLineHexBuf(temp_buf_pkt, sizeof(temp_buf_pkt), GET_PKT_DATA(p), GET_PKT_LEN(p) < 32 ? GET_PKT_LEN(p) : 32);
344  strlcat(alert, temp_buf_pkt, sizeof(alert));
345 
346  if (p->pcap_cnt != 0) {
347  snprintf(temp_buf_tail, sizeof(temp_buf_tail), "] [pcap file packet: %"PRIu64"]",
348  p->pcap_cnt);
349  } else {
350  temp_buf_tail[0] = ']';
351  temp_buf_tail[1] = '\0';
352  }
353  strlcat(alert, temp_buf_tail, sizeof(alert));
354 
355  SCMutexLock(&ast->file_ctx->fp_mutex);
356  syslog(alert_syslog_level, "%s", alert);
358  }
359 
360  return TM_ECODE_OK;
361 }
362 
363 static bool AlertSyslogCondition(ThreadVars *tv, void *thread_data, const Packet *p)
364 {
365  return (p->alerts.cnt > 0);
366 }
367 
368 static int AlertSyslogLogger(ThreadVars *tv, void *thread_data, const Packet *p)
369 {
370  if (PacketIsIPv4(p)) {
371  return AlertSyslogIPv4(tv, p, thread_data);
372  } else if (PacketIsIPv6(p)) {
373  return AlertSyslogIPv6(tv, p, thread_data);
374  } else if (p->events.cnt > 0) {
375  return AlertSyslogDecoderEvent(tv, p, thread_data);
376  }
377 
378  return TM_ECODE_OK;
379 }
380 
381 #endif /* !OS_WIN32 */
382 
383 /** \brief Function to register the AlertSyslog module */
385 {
386 #ifndef OS_WIN32
387  OutputRegisterPacketModule(LOGGER_ALERT_SYSLOG, MODULE_NAME, "syslog", AlertSyslogInitCtx,
388  AlertSyslogLogger, AlertSyslogCondition, AlertSyslogThreadInit,
389  AlertSyslogThreadDeinit);
390 #endif /* !OS_WIN32 */
391 }
tm-threads.h
detect-engine.h
PacketAlert_::s
const struct Signature_ * s
Definition: decode.h:243
syslog
#define syslog(__pri, __fmt, __param)
Definition: win32-syslog.h:78
IPV4_GET_RAW_IPPROTO
#define IPV4_GET_RAW_IPPROTO(ip4h)
Definition: decode-ipv4.h:103
LogFileCtx_::fp_mutex
SCMutex fp_mutex
Definition: util-logopenfile.h:94
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
LOGGER_ALERT_SYSLOG
@ LOGGER_ALERT_SYSLOG
Definition: suricata-common.h:479
LogFileNewCtx
LogFileCtx * LogFileNewCtx(void)
LogFileNewCtx() Get a new LogFileCtx.
Definition: util-logopenfile.c:659
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Packet_::pcap_cnt
uint64_t pcap_cnt
Definition: decode.h:592
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:265
action-globals.h
threads.h
util-syslog.h
LogFileCtx_
Definition: util-logopenfile.h:72
PacketAlerts_::alerts
PacketAlert * alerts
Definition: decode.h:268
SCProtoNameValid
bool SCProtoNameValid(uint16_t proto)
Function to check if the received protocol number is valid and do we have corresponding name entry fo...
Definition: util-proto-name.c:453
AlertSyslogRegister
void AlertSyslogRegister(void)
Function to register the AlertSyslog module.
Definition: alert-syslog.c:384
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
IPV6_GET_L4PROTO
#define IPV6_GET_L4PROTO(p)
Definition: decode-ipv6.h:75
known_proto
const char * known_proto[256]
Definition: util-proto-name.c:40
proto
uint8_t proto
Definition: decode-template.h:0
OutputRegisterPacketModule
void OutputRegisterPacketModule(LoggerId id, const char *name, const char *conf_name, OutputInitFunc InitFunc, PacketLogger PacketLogFunc, PacketLogCondition PacketConditionFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit)
Register a packet output module.
Definition: output.c:169
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
GET_IPV6_DST_ADDR
#define GET_IPV6_DST_ADDR(p)
Definition: decode.h:200
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:586
alert-syslog.h
Packet_::events
PacketEngineEvents events
Definition: decode.h:596
OutputCtx_::data
void * data
Definition: tm-modules.h:87
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
closelog
#define closelog()
Definition: win32-syslog.h:75
OutputCtx_
Definition: tm-modules.h:84
DEFAULT_ALERT_SYSLOG_FACILITY
#define DEFAULT_ALERT_SYSLOG_FACILITY
Definition: util-syslog.h:35
PacketAlert_::action
uint8_t action
Definition: decode.h:241
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
detect-reference.h
Signature_::gid
uint32_t gid
Definition: detect.h:637
DEFAULT_ALERT_SYSLOG_LEVEL
#define DEFAULT_ALERT_SYSLOG_LEVEL
Definition: util-syslog.h:36
util-debug.h
GET_IPV4_DST_ADDR_PTR
#define GET_IPV4_DST_ADDR_PTR(p)
Definition: decode.h:195
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
strlcat
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
SCSyslogGetFacilityMap
SCEnumCharMap * SCSyslogGetFacilityMap(void)
returns the syslog facility enum map
Definition: util-syslog.c:57
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
DEFAULT_ALERT_SYSLOG_FACILITY_STR
#define DEFAULT_ALERT_SYSLOG_FACILITY_STR
Definition: util-syslog.h:34
util-print.h
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:205
detect-engine-mpm.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
PrintInet
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
Definition: util-print.c:230
Packet_::sp
Port sp
Definition: decode.h:480
OutputInitResult_::ok
bool ok
Definition: output.h:48
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
Packet_
Definition: decode.h:473
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:204
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
SCSyslogGetLogLevelMap
SCEnumCharMap * SCSyslogGetLogLevelMap(void)
returns the syslog facility enum map
Definition: util-syslog.c:75
util-proto-name.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
Signature_::class_msg
char * class_msg
Definition: detect.h:662
IPV4Hdr_
Definition: decode-ipv4.h:72
OutputInitResult_
Definition: output.h:46
GET_IPV4_SRC_ADDR_PTR
#define GET_IPV4_SRC_ADDR_PTR(p)
Definition: decode.h:194
SCMapEnumNameToValue
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:40
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:90
ACTION_DROP
#define ACTION_DROP
Definition: action-globals.h:30
Signature_::rev
uint32_t rev
Definition: detect.h:638
LogFileFreeCtx
int LogFileFreeCtx(LogFileCtx *lf_ctx)
LogFileFreeCtx() Destroy a LogFileCtx (Close the file and free memory)
Definition: util-logopenfile.c:868
util-classification-config.h
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
Signature_::prio
int prio
Definition: detect.h:639
util-optimize.h
threadvars.h
AlertSyslogThread_
Definition: alert-syslog.c:59
GET_IPV6_SRC_ADDR
#define GET_IPV6_SRC_ADDR(p)
Definition: decode.h:199
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-logopenfile.h
Signature_::id
uint32_t id
Definition: detect.h:636
detect-parse.h
AlertSyslogThread_::file_ctx
LogFileCtx * file_ctx
Definition: alert-syslog.c:61
EngineModeIsIPS
int EngineModeIsIPS(void)
Definition: suricata.c:228
PacketAlert_
Definition: decode.h:239
MODULE_NAME
#define MODULE_NAME
Definition: alert-syslog.c:55
AlertSyslogThread
struct AlertSyslogThread_ AlertSyslogThread
Signature_::msg
char * msg
Definition: detect.h:659
flow.h
openlog
#define openlog(__ident, __option, __facility)
Definition: win32-syslog.h:76
Packet_::dp
Port dp
Definition: decode.h:488
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
PrintRawLineHexBuf
void PrintRawLineHexBuf(char *retbuf, uint32_t retbuflen, const uint8_t *buf, uint32_t buflen)
print a buffer as hex on a single line into retbuf buffer
Definition: util-print.c:61
output.h
PacketEngineEvents_::cnt
uint8_t cnt
Definition: decode.h:284
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:809