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
57static int alert_syslog_level = DEFAULT_ALERT_SYSLOG_LEVEL;
58
59typedef 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 */
70static 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 */
88static OutputInitResult AlertSyslogInitCtx(SCConfNode *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 = SCConfNodeLookupChildValue(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 = SCConfNodeLookupChildValue(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 = SCConfNodeLookupChildValue(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 */
151static 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
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 */
176static TmEcode AlertSyslogThreadDeinit(ThreadVars *t, void *data)
177{
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 */
199static TmEcode AlertSyslogIPv4(ThreadVars *tv, const Packet *p, void *data)
200{
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. */
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 */
256static TmEcode AlertSyslogIPv6(ThreadVars *tv, const Packet *p, void *data)
257{
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
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 */
312static TmEcode AlertSyslogDecoderEvent(ThreadVars *tv, const Packet *p, void *data)
313{
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
356 syslog(alert_syslog_level, "%s", alert);
358 }
359
360 return TM_ECODE_OK;
361}
362
363static bool AlertSyslogCondition(ThreadVars *tv, void *thread_data, const Packet *p)
364{
365 return (p->alerts.cnt > 0);
366}
367
368static 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 OutputPacketLoggerFunctions output_logger_functions = {
388 .LogFunc = AlertSyslogLogger,
389 .FlushFunc = NULL,
390 .ConditionFunc = AlertSyslogCondition,
391 .ThreadInitFunc = AlertSyslogThreadInit,
392 .ThreadDeinitFunc = AlertSyslogThreadDeinit,
393 .ThreadExitPrintStatsFunc = NULL,
394 };
395 OutputRegisterPacketModule(LOGGER_ALERT_SYSLOG, MODULE_NAME, "syslog", AlertSyslogInitCtx,
396 &output_logger_functions);
397#endif /* !OS_WIN32 */
398}
#define ACTION_DROP
#define MODULE_NAME
void AlertSyslogRegister(void)
Function to register the AlertSyslog module.
struct AlertSyslogThread_ AlertSyslogThread
const char * SCConfNodeLookupChildValue(const SCConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition conf.c:824
#define IPV4_GET_RAW_IPPROTO(ip4h)
#define IPV6_GET_L4PROTO(p)
Definition decode-ipv6.h:75
uint8_t proto
#define GET_IPV6_DST_ADDR(p)
Definition decode.h:204
#define GET_IPV4_SRC_ADDR_PTR(p)
Definition decode.h:198
#define GET_PKT_DATA(p)
Definition decode.h:209
#define GET_IPV6_SRC_ADDR(p)
Definition decode.h:203
#define GET_PKT_LEN(p)
Definition decode.h:208
#define GET_IPV4_DST_ADDR_PTR(p)
Definition decode.h:199
ThreadVars * tv
void OutputRegisterPacketModule(LoggerId id, const char *name, const char *conf_name, OutputInitFunc InitFunc, OutputPacketLoggerFunctions *output_module_functions)
Register a packet output module.
Definition output.c:196
LogFileCtx * file_ctx
void * data
Definition tm-modules.h:91
void(* DeInit)(struct OutputCtx_ *)
Definition tm-modules.h:94
OutputCtx * ctx
Definition output.h:47
const struct Signature_ * s
Definition decode.h:252
uint8_t action
Definition decode.h:250
uint16_t cnt
Definition decode.h:287
PacketAlert * alerts
Definition decode.h:290
uint64_t pcap_cnt
Definition decode.h:626
Port sp
Definition decode.h:508
PacketAlerts alerts
Definition decode.h:620
PacketEngineEvents events
Definition decode.h:630
Port dp
Definition decode.h:516
uint32_t rev
Definition detect.h:715
int prio
Definition detect.h:716
char * class_msg
Definition detect.h:739
uint32_t id
Definition detect.h:713
char * msg
Definition detect.h:736
uint32_t gid
Definition detect.h:714
Per thread variable structure.
Definition threadvars.h:58
size_t strlcat(char *, const char *src, size_t siz)
@ LOGGER_ALERT_SYSLOG
size_t strlcpy(char *dst, const char *src, size_t siz)
int EngineModeIsIPS(void)
Definition suricata.c:242
#define SCMutexUnlock(mut)
#define SCMutexLock(mut)
@ TM_ECODE_FAILED
@ TM_ECODE_OK
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition util-debug.h:255
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition util-debug.h:225
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
int LogFileFreeCtx(LogFileCtx *lf_ctx)
LogFileFreeCtx() Destroy a LogFileCtx (Close the file and free memory)
LogFileCtx * LogFileNewCtx(void)
LogFileNewCtx() Get a new LogFileCtx.
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define unlikely(expr)
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
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
Definition util-print.c:231
bool SCProtoNameValid(uint16_t proto)
Function to check if the received protocol number is valid and do we have corresponding name entry fo...
const char * known_proto[256]
SCEnumCharMap * SCSyslogGetLogLevelMap(void)
returns the syslog facility enum map
Definition util-syslog.c:75
SCEnumCharMap * SCSyslogGetFacilityMap(void)
returns the syslog facility enum map
Definition util-syslog.c:57
#define DEFAULT_ALERT_SYSLOG_FACILITY_STR
Definition util-syslog.h:34
#define DEFAULT_ALERT_SYSLOG_LEVEL
Definition util-syslog.h:36
#define DEFAULT_ALERT_SYSLOG_FACILITY
Definition util-syslog.h:35
#define closelog()
#define syslog(__pri, __fmt, __param)
#define openlog(__ident, __option, __facility)