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  OutputInitResult result = { NULL, false };
91  const char *facility_s = ConfNodeLookupChildValue(conf, "facility");
92  if (facility_s == NULL) {
94  }
95 
96  LogFileCtx *logfile_ctx = LogFileNewCtx();
97  if (logfile_ctx == NULL) {
98  SCLogDebug("AlertSyslogInitCtx: Could not create new LogFileCtx");
99  return result;
100  }
101 
102  int facility = SCMapEnumNameToValue(facility_s, SCSyslogGetFacilityMap());
103  if (facility == -1) {
104  SCLogWarning("Invalid syslog facility: \"%s\","
105  " now using \"%s\" as syslog facility",
108  }
109 
110  const char *level_s = ConfNodeLookupChildValue(conf, "level");
111  if (level_s != NULL) {
112  int level = SCMapEnumNameToValue(level_s, SCSyslogGetLogLevelMap());
113  if (level != -1) {
114  alert_syslog_level = level;
115  }
116  }
117 
118  const char *ident = ConfNodeLookupChildValue(conf, "identity");
119  /* if null we just pass that to openlog, which will then
120  * figure it out by itself. */
121 
122  openlog(ident, LOG_PID|LOG_NDELAY, facility);
123 
124  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
125  if (unlikely(output_ctx == NULL)) {
126  SCLogDebug("could not create new OutputCtx");
127  LogFileFreeCtx(logfile_ctx);
128  return result;
129  }
130 
131  output_ctx->data = logfile_ctx;
132  output_ctx->DeInit = AlertSyslogDeInitCtx;
133 
134  SCLogInfo("Syslog output initialized");
135 
136  result.ctx = output_ctx;
137  result.ok = true;
138  return result;
139 }
140 
141 /**
142  * \brief Function to initialize the AlertSyslogThread and sets the output
143  * context pointer
144  *
145  * \param tv Pointer to the threadvars
146  * \param initdata Pointer to the output context
147  * \param data pointer to pointer to point to the AlertSyslogThread
148  */
149 static TmEcode AlertSyslogThreadInit(ThreadVars *t, const void *initdata, void **data)
150 {
151  if(initdata == NULL) {
152  SCLogDebug("Error getting context for AlertSyslog. \"initdata\" "
153  "argument NULL");
154  return TM_ECODE_FAILED;
155  }
156 
157  AlertSyslogThread *ast = SCCalloc(1, sizeof(AlertSyslogThread));
158  if (unlikely(ast == NULL))
159  return TM_ECODE_FAILED;
160 
161  /** Use the Output Context (file pointer and mutex) */
162  ast->file_ctx = ((OutputCtx *)initdata)->data;
163 
164  *data = (void *)ast;
165  return TM_ECODE_OK;
166 }
167 
168 /**
169  * \brief Function to deinitialize the AlertSyslogThread
170  *
171  * \param tv Pointer to the threadvars
172  * \param data pointer to the AlertSyslogThread to be cleared
173  */
174 static TmEcode AlertSyslogThreadDeinit(ThreadVars *t, void *data)
175 {
176  AlertSyslogThread *ast = (AlertSyslogThread *)data;
177  if (ast == NULL) {
178  return TM_ECODE_OK;
179  }
180 
181  /* clear memory */
182  memset(ast, 0, sizeof(AlertSyslogThread));
183 
184  SCFree(ast);
185  return TM_ECODE_OK;
186 }
187 
188 /**
189  * \brief Function which is called to print the IPv4 alerts to the syslog
190  *
191  * \param tv Pointer to the threadvars
192  * \param p Pointer to the packet
193  * \param data pointer to the AlertSyslogThread
194  *
195  * \return On succes return TM_ECODE_OK
196  */
197 static TmEcode AlertSyslogIPv4(ThreadVars *tv, const Packet *p, void *data)
198 {
199  AlertSyslogThread *ast = (AlertSyslogThread *)data;
200  const char *action = "";
201 
202  if (p->alerts.cnt == 0)
203  return TM_ECODE_OK;
204 
205  char proto[16] = "";
206  const char *protoptr;
207  const uint8_t ipproto = IPV4_GET_IPPROTO(p);
208  if (SCProtoNameValid(ipproto)) {
209  protoptr = known_proto[ipproto];
210  } else {
211  snprintf(proto, sizeof(proto), "PROTO:%03" PRIu8, ipproto);
212  protoptr = proto;
213  }
214 
215  char srcip[16], dstip[16];
216  PrintInet(AF_INET, (const void *)GET_IPV4_SRC_ADDR_PTR(p), srcip, sizeof(srcip));
217  PrintInet(AF_INET, (const void *)GET_IPV4_DST_ADDR_PTR(p), dstip, sizeof(dstip));
218 
219  for (int i = 0; i < p->alerts.cnt; i++) {
220  const PacketAlert *pa = &p->alerts.alerts[i];
221  if (unlikely(pa->s == NULL)) {
222  continue;
223  }
224 
225  if ((pa->action & ACTION_DROP) && EngineModeIsIPS()) {
226  action = "[Drop] ";
227  } else if (pa->action & ACTION_DROP) {
228  action = "[wDrop] ";
229  }
230 
231  /* Not sure if this mutex is needed around calls to syslog. */
232  SCMutexLock(&ast->file_ctx->fp_mutex);
233  syslog(alert_syslog_level, "%s[%" PRIu32 ":%" PRIu32 ":%"
234  PRIu32 "] %s [Classification: %s] [Priority: %"PRIu32"]"
235  " {%s} %s:%" PRIu32 " -> %s:%" PRIu32 "", action, pa->s->gid,
236  pa->s->id, pa->s->rev, pa->s->msg, pa->s->class_msg, pa->s->prio,
237  protoptr, srcip, p->sp, dstip, p->dp);
239  }
240 
241  return TM_ECODE_OK;
242 }
243 
244 /**
245  * \brief Function which is called to print the IPv6 alerts to the syslog
246  *
247  * \param tv Pointer to the threadvars
248  * \param p Pointer to the packet
249  * \param data pointer to the AlertSyslogThread
250  *
251  * \return On succes return TM_ECODE_OK
252  */
253 static TmEcode AlertSyslogIPv6(ThreadVars *tv, const Packet *p, void *data)
254 {
255  AlertSyslogThread *ast = (AlertSyslogThread *)data;
256  const char *action = "";
257 
258  if (p->alerts.cnt == 0)
259  return TM_ECODE_OK;
260 
261  char proto[16] = "";
262  const char *protoptr;
263  const uint8_t ipproto = IPV6_GET_L4PROTO(p);
264  if (SCProtoNameValid(ipproto)) {
265  protoptr = known_proto[ipproto];
266  } else {
267  snprintf(proto, sizeof(proto), "PROTO:03%" PRIu8, ipproto);
268  protoptr = proto;
269  }
270 
271  char srcip[46], dstip[46];
272  PrintInet(AF_INET6, (const void *)GET_IPV6_SRC_ADDR(p), srcip, sizeof(srcip));
273  PrintInet(AF_INET6, (const void *)GET_IPV6_DST_ADDR(p), dstip, sizeof(dstip));
274 
275  for (int i = 0; i < p->alerts.cnt; i++) {
276  const PacketAlert *pa = &p->alerts.alerts[i];
277  if (unlikely(pa->s == NULL)) {
278  continue;
279  }
280 
281  if ((pa->action & ACTION_DROP) && EngineModeIsIPS()) {
282  action = "[Drop] ";
283  } else if (pa->action & ACTION_DROP) {
284  action = "[wDrop] ";
285  }
286 
287  SCMutexLock(&ast->file_ctx->fp_mutex);
288  syslog(alert_syslog_level, "%s[%" PRIu32 ":%" PRIu32 ":%"
289  "" PRIu32 "] %s [Classification: %s] [Priority: %"
290  "" PRIu32 "] {%s} %s:%" PRIu32 " -> %s:%" PRIu32 "",
291  action, pa->s->gid, pa->s->id, pa->s->rev, pa->s->msg, pa->s->class_msg,
292  pa->s->prio, protoptr, srcip, p->sp,
293  dstip, p->dp);
295  }
296 
297  return TM_ECODE_OK;
298 }
299 
300 /**
301  * \brief Function which is called to print the decode alerts to the syslog
302  *
303  * \param tv Pointer to the threadvars
304  * \param p Pointer to the packet
305  * \param data pointer to the AlertSyslogThread
306  *
307  * \return On succes return TM_ECODE_OK
308  */
309 static TmEcode AlertSyslogDecoderEvent(ThreadVars *tv, const Packet *p, void *data)
310 {
311  AlertSyslogThread *ast = (AlertSyslogThread *)data;
312  const char *action = "";
313 
314  if (p->alerts.cnt == 0)
315  return TM_ECODE_OK;
316 
317  char temp_buf_hdr[512];
318  char temp_buf_pkt[65] = "";
319  char temp_buf_tail[64];
320  char alert[2048] = "";
321 
322  for (int i = 0; i < p->alerts.cnt; i++) {
323  const PacketAlert *pa = &p->alerts.alerts[i];
324  if (unlikely(pa->s == NULL)) {
325  continue;
326  }
327 
328  if ((pa->action & ACTION_DROP) && EngineModeIsIPS()) {
329  action = "[Drop] ";
330  } else if (pa->action & ACTION_DROP) {
331  action = "[wDrop] ";
332  }
333 
334  snprintf(temp_buf_hdr, sizeof(temp_buf_hdr), "%s[%" PRIu32 ":%" PRIu32
335  ":%" PRIu32 "] %s [Classification: %s] [Priority: %" PRIu32
336  "] [**] [Raw pkt: ", action, pa->s->gid, pa->s->id, pa->s->rev, pa->s->msg,
337  pa->s->class_msg, pa->s->prio);
338  strlcpy(alert, temp_buf_hdr, sizeof(alert));
339 
340  PrintRawLineHexBuf(temp_buf_pkt, sizeof(temp_buf_pkt), GET_PKT_DATA(p), GET_PKT_LEN(p) < 32 ? GET_PKT_LEN(p) : 32);
341  strlcat(alert, temp_buf_pkt, sizeof(alert));
342 
343  if (p->pcap_cnt != 0) {
344  snprintf(temp_buf_tail, sizeof(temp_buf_tail), "] [pcap file packet: %"PRIu64"]",
345  p->pcap_cnt);
346  } else {
347  temp_buf_tail[0] = ']';
348  temp_buf_tail[1] = '\0';
349  }
350  strlcat(alert, temp_buf_tail, sizeof(alert));
351 
352  SCMutexLock(&ast->file_ctx->fp_mutex);
353  syslog(alert_syslog_level, "%s", alert);
355  }
356 
357  return TM_ECODE_OK;
358 }
359 
360 static bool AlertSyslogCondition(ThreadVars *tv, void *thread_data, const Packet *p)
361 {
362  return (p->alerts.cnt > 0);
363 }
364 
365 static int AlertSyslogLogger(ThreadVars *tv, void *thread_data, const Packet *p)
366 {
367  if (PKT_IS_IPV4(p)) {
368  return AlertSyslogIPv4(tv, p, thread_data);
369  } else if (PKT_IS_IPV6(p)) {
370  return AlertSyslogIPv6(tv, p, thread_data);
371  } else if (p->events.cnt > 0) {
372  return AlertSyslogDecoderEvent(tv, p, thread_data);
373  }
374 
375  return TM_ECODE_OK;
376 }
377 
378 #endif /* !OS_WIN32 */
379 
380 /** \brief Function to register the AlertSyslog module */
382 {
383 #ifndef OS_WIN32
385  AlertSyslogInitCtx, AlertSyslogLogger, AlertSyslogCondition,
386  AlertSyslogThreadInit, AlertSyslogThreadDeinit, NULL);
387 #endif /* !OS_WIN32 */
388 }
tm-threads.h
detect-engine.h
PacketAlert_::s
const struct Signature_ * s
Definition: decode.h:268
syslog
#define syslog(__pri, __fmt, __param)
Definition: win32-syslog.h:78
PKT_IS_IPV6
#define PKT_IS_IPV6(p)
Definition: decode.h:247
LogFileCtx_::fp_mutex
SCMutex fp_mutex
Definition: util-logopenfile.h:98
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
LOGGER_ALERT_SYSLOG
@ LOGGER_ALERT_SYSLOG
Definition: suricata-common.h:478
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:607
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:290
action-globals.h
threads.h
util-syslog.h
LogFileCtx_
Definition: util-logopenfile.h:76
PacketAlerts_::alerts
PacketAlert * alerts
Definition: decode.h:293
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:454
AlertSyslogRegister
void AlertSyslogRegister(void)
Function to register the AlertSyslog module.
Definition: alert-syslog.c:381
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
IPV6_GET_L4PROTO
#define IPV6_GET_L4PROTO(p)
Definition: decode-ipv6.h:93
known_proto
const char * known_proto[256]
Definition: util-proto-name.c:40
proto
uint8_t proto
Definition: decode-template.h:0
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
GET_IPV6_DST_ADDR
#define GET_IPV6_DST_ADDR(p)
Definition: decode.h:216
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:601
alert-syslog.h
Packet_::events
PacketEngineEvents events
Definition: decode.h:611
OutputCtx_::data
void * data
Definition: tm-modules.h:88
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
closelog
#define closelog()
Definition: win32-syslog.h:75
OutputCtx_
Definition: tm-modules.h:85
DEFAULT_ALERT_SYSLOG_FACILITY
#define DEFAULT_ALERT_SYSLOG_FACILITY
Definition: util-syslog.h:35
PacketAlert_::action
uint8_t action
Definition: decode.h:266
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:632
DEFAULT_ALERT_SYSLOG_LEVEL
#define DEFAULT_ALERT_SYSLOG_LEVEL
Definition: util-syslog.h:36
IPV4_GET_IPPROTO
#define IPV4_GET_IPPROTO(p)
Definition: decode-ipv4.h:148
util-debug.h
GET_IPV4_DST_ADDR_PTR
#define GET_IPV4_DST_ADDR_PTR(p)
Definition: decode.h:211
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:221
detect-engine-mpm.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
PrintInet
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
Definition: util-print.c:241
Packet_::sp
Port sp
Definition: decode.h:444
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:437
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:220
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:83
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:657
OutputInitResult_
Definition: output.h:46
GET_IPV4_SRC_ADDR_PTR
#define GET_IPV4_SRC_ADDR_PTR(p)
Definition: decode.h:210
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:91
ACTION_DROP
#define ACTION_DROP
Definition: action-globals.h:30
Signature_::rev
uint32_t rev
Definition: detect.h:633
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:634
util-optimize.h
OutputRegisterPacketModule
void OutputRegisterPacketModule(LoggerId id, const char *name, const char *conf_name, OutputInitFunc InitFunc, PacketLogger PacketLogFunc, PacketLogCondition PacketConditionFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Register a packet output module.
Definition: output.c:169
threadvars.h
AlertSyslogThread_
Definition: alert-syslog.c:59
GET_IPV6_SRC_ADDR
#define GET_IPV6_SRC_ADDR(p)
Definition: decode.h:215
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:631
detect-parse.h
AlertSyslogThread_::file_ctx
LogFileCtx * file_ctx
Definition: alert-syslog.c:61
EngineModeIsIPS
int EngineModeIsIPS(void)
Definition: suricata.c:234
PacketAlert_
Definition: decode.h:264
MODULE_NAME
#define MODULE_NAME
Definition: alert-syslog.c:55
AlertSyslogThread
struct AlertSyslogThread_ AlertSyslogThread
Signature_::msg
char * msg
Definition: detect.h:654
flow.h
openlog
#define openlog(__ident, __option, __facility)
Definition: win32-syslog.h:76
Packet_::dp
Port dp
Definition: decode.h:452
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:246
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:309
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:814