suricata
alert-fastlog.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 Victor Julien <victor@inliniac.net>
22  *
23  * Logs alerts in a line based text format compatible to Snort's
24  * alert_fast format.
25  */
26 
27 #include "suricata-common.h"
28 #include "detect.h"
29 #include "flow.h"
30 #include "conf.h"
31 
32 #include "threads.h"
33 #include "tm-threads.h"
34 #include "threadvars.h"
35 #include "util-debug.h"
36 
37 #include "util-unittest.h"
38 #include "util-unittest-helper.h"
39 
40 #include "detect-parse.h"
41 #include "detect-engine.h"
42 #include "detect-engine-build.h"
43 #include "detect-engine-mpm.h"
44 #include "detect-reference.h"
46 
47 #include "output.h"
48 #include "alert-fastlog.h"
49 
50 #include "util-privs.h"
51 #include "util-print.h"
52 #include "util-proto-name.h"
53 #include "util-optimize.h"
54 #include "util-logopenfile.h"
55 #include "util-time.h"
56 
57 #include "action-globals.h"
58 
59 #define DEFAULT_LOG_FILENAME "fast.log"
60 
61 #define MODULE_NAME "AlertFastLog"
62 
63 /* The largest that size allowed for one alert string. */
64 #define MAX_FASTLOG_ALERT_SIZE 2048
65 /* The largest alert buffer that will be written at one time, possibly
66  * holding multiple alerts. */
67 #define MAX_FASTLOG_BUFFER_SIZE (2 * MAX_FASTLOG_ALERT_SIZE)
68 
69 TmEcode AlertFastLogThreadInit(ThreadVars *, const void *, void **);
71 void AlertFastLogRegisterTests(void);
72 static void AlertFastLogDeInitCtx(OutputCtx *);
73 
74 static bool AlertFastLogCondition(ThreadVars *tv, void *thread_data, const Packet *p);
75 int AlertFastLogger(ThreadVars *tv, void *data, const Packet *p);
76 
78 {
80  AlertFastLogger, AlertFastLogCondition, AlertFastLogThreadInit,
83 }
84 
85 typedef struct AlertFastLogThread_ {
86  /** LogFileCtx has the pointer to the file and a mutex to allow multithreading */
89 
90 static bool AlertFastLogCondition(ThreadVars *tv, void *thread_data, const Packet *p)
91 {
92  return (p->alerts.cnt > 0);
93 }
94 
95 static inline void AlertFastLogOutputAlert(AlertFastLogThread *aft, char *buffer,
96  int alert_size)
97 {
98  /* Output the alert string and count alerts. Only need to lock here. */
99  aft->file_ctx->Write(buffer, alert_size, aft->file_ctx);
100 }
101 
102 int AlertFastLogger(ThreadVars *tv, void *data, const Packet *p)
103 {
104  AlertFastLogThread *aft = (AlertFastLogThread *)data;
105  int i;
106  char timebuf[64];
107  int decoder_event = 0;
108 
109  CreateTimeString(p->ts, timebuf, sizeof(timebuf));
110 
111  char srcip[46], dstip[46];
112  if (PacketIsIPv4(p)) {
113  PrintInet(AF_INET, (const void *)GET_IPV4_SRC_ADDR_PTR(p), srcip, sizeof(srcip));
114  PrintInet(AF_INET, (const void *)GET_IPV4_DST_ADDR_PTR(p), dstip, sizeof(dstip));
115  } else if (PacketIsIPv6(p)) {
116  PrintInet(AF_INET6, (const void *)GET_IPV6_SRC_ADDR(p), srcip, sizeof(srcip));
117  PrintInet(AF_INET6, (const void *)GET_IPV6_DST_ADDR(p), dstip, sizeof(dstip));
118  } else {
119  decoder_event = 1;
120  }
121 
122  /* Buffer to store the generated alert strings. The buffer is
123  * filled with alert strings until it doesn't have room to store
124  * another full alert, only then is the buffer written. This is
125  * more efficient for multiple alerts and only slightly slower for
126  * single alerts.
127  */
128  char alert_buffer[MAX_FASTLOG_BUFFER_SIZE];
129 
130  char proto[16] = "";
131  const char *protoptr;
132  if (SCProtoNameValid(PacketGetIPProto(p))) {
133  protoptr = known_proto[PacketGetIPProto(p)];
134  } else {
135  snprintf(proto, sizeof(proto), "PROTO:%03" PRIu32, PacketGetIPProto(p));
136  protoptr = proto;
137  }
138  uint16_t src_port_or_icmp = p->sp;
139  uint16_t dst_port_or_icmp = p->dp;
140  if (PacketGetIPProto(p) == IPPROTO_ICMP || PacketGetIPProto(p) == IPPROTO_ICMPV6) {
141  src_port_or_icmp = p->icmp_s.type;
142  dst_port_or_icmp = p->icmp_s.code;
143  }
144  for (i = 0; i < p->alerts.cnt; i++) {
145  const PacketAlert *pa = &p->alerts.alerts[i];
146  if (unlikely(pa->s == NULL)) {
147  continue;
148  }
149 
150  const char *action = "";
151  if ((pa->action & ACTION_DROP) && EngineModeIsIPS()) {
152  action = "[Drop] ";
153  } else if (pa->action & ACTION_DROP) {
154  action = "[wDrop] ";
155  }
156 
157  /* Create the alert string without locking. */
158  int size = 0;
159  if (likely(decoder_event == 0)) {
160  PrintBufferData(alert_buffer, &size, MAX_FASTLOG_ALERT_SIZE,
161  "%s %s[**] [%" PRIu32 ":%" PRIu32 ":%"
162  PRIu32 "] %s [**] [Classification: %s] [Priority: %"PRIu32"]"
163  " {%s} %s:%" PRIu32 " -> %s:%" PRIu32 "\n", timebuf, action,
164  pa->s->gid, pa->s->id, pa->s->rev, pa->s->msg, pa->s->class_msg, pa->s->prio,
165  protoptr, srcip, src_port_or_icmp, dstip, dst_port_or_icmp);
166  } else {
167  PrintBufferData(alert_buffer, &size, MAX_FASTLOG_ALERT_SIZE,
168  "%s %s[**] [%" PRIu32 ":%" PRIu32
169  ":%" PRIu32 "] %s [**] [Classification: %s] [Priority: "
170  "%" PRIu32 "] [**] [Raw pkt: ", timebuf, action, pa->s->gid,
171  pa->s->id, pa->s->rev, pa->s->msg, pa->s->class_msg, pa->s->prio);
172  PrintBufferRawLineHex(alert_buffer, &size, MAX_FASTLOG_ALERT_SIZE,
173  GET_PKT_DATA(p), GET_PKT_LEN(p) < 32 ? GET_PKT_LEN(p) : 32);
174  if (p->pcap_cnt != 0) {
175  PrintBufferData(alert_buffer, &size, MAX_FASTLOG_ALERT_SIZE,
176  "] [pcap file packet: %"PRIu64"]\n", p->pcap_cnt);
177  } else {
178  PrintBufferData(alert_buffer, &size, MAX_FASTLOG_ALERT_SIZE, "]\n");
179  }
180  }
181 
182  /* Write the alert to output file */
183  AlertFastLogOutputAlert(aft, alert_buffer, size);
184  }
185 
186  return TM_ECODE_OK;
187 }
188 
189 TmEcode AlertFastLogThreadInit(ThreadVars *t, const void *initdata, void **data)
190 {
192  if (unlikely(aft == NULL))
193  return TM_ECODE_FAILED;
194  if(initdata == NULL)
195  {
196  SCLogDebug("Error getting context for AlertFastLog. \"initdata\" argument NULL");
197  SCFree(aft);
198  return TM_ECODE_FAILED;
199  }
200  /** Use the Output Context (file pointer and mutex) */
201  aft->file_ctx = ((OutputCtx *)initdata)->data;
202 
203  *data = (void *)aft;
204  return TM_ECODE_OK;
205 }
206 
208 {
209  AlertFastLogThread *aft = (AlertFastLogThread *)data;
210  if (aft == NULL) {
211  return TM_ECODE_OK;
212  }
213 
214  /* clear memory */
215  memset(aft, 0, sizeof(AlertFastLogThread));
216 
217  SCFree(aft);
218  return TM_ECODE_OK;
219 }
220 
221 /**
222  * \brief Create a new LogFileCtx for "fast" output style.
223  * \param conf The configuration node for this output.
224  * \return A LogFileCtx pointer on success, NULL on failure.
225  */
227 {
228  OutputInitResult result = { NULL, false };
229  LogFileCtx *logfile_ctx = LogFileNewCtx();
230  if (logfile_ctx == NULL) {
231  SCLogDebug("AlertFastLogInitCtx2: Could not create new LogFileCtx");
232  return result;
233  }
234 
235  if (SCConfLogOpenGeneric(conf, logfile_ctx, DEFAULT_LOG_FILENAME, 1) < 0) {
236  LogFileFreeCtx(logfile_ctx);
237  return result;
238  }
239 
240  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
241  if (unlikely(output_ctx == NULL)) {
242  LogFileFreeCtx(logfile_ctx);
243  return result;
244  }
245 
246  output_ctx->data = logfile_ctx;
247  output_ctx->DeInit = AlertFastLogDeInitCtx;
248 
249  result.ctx = output_ctx;
250  result.ok = true;
251  return result;
252 }
253 
254 static void AlertFastLogDeInitCtx(OutputCtx *output_ctx)
255 {
256  LogFileCtx *logfile_ctx = (LogFileCtx *)output_ctx->data;
257  LogFileFreeCtx(logfile_ctx);
258  SCFree(output_ctx);
259 }
260 
261 /*------------------------------Unittests-------------------------------------*/
262 
263 #ifdef UNITTESTS
264 
265 static int AlertFastLogTest01(void)
266 {
267  uint8_t *buf = (uint8_t *) "GET /one/ HTTP/1.1\r\n"
268  "Host: one.example.org\r\n";
269 
270  uint16_t buflen = strlen((char *)buf);
271  Packet *p = NULL;
272  ThreadVars th_v;
273  DetectEngineThreadCtx *det_ctx;
274 
275  memset(&th_v, 0, sizeof(th_v));
276  p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
277 
279  FAIL_IF(de_ctx == NULL);
280 
281  de_ctx->flags |= DE_QUIET;
282 
285 
286  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
287  "(msg:\"FastLog test\"; content:\"GET\"; "
288  "Classtype:unknown; sid:1;)");
289 
291  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
292 
293  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
294  FAIL_IF_NOT(p->alerts.cnt == 1);
295  FAIL_IF_NOT(strcmp(p->alerts.alerts[0].s->class_msg, "Unknown are we") == 0);
296 
299  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
301 
302  UTHFreePackets(&p, 1);
303  PASS;
304 }
305 
306 static int AlertFastLogTest02(void)
307 {
308  uint8_t *buf = (uint8_t *) "GET /one/ HTTP/1.1\r\n"
309  "Host: one.example.org\r\n";
310  uint16_t buflen = strlen((char *)buf);
311  Packet *p = NULL;
312  ThreadVars th_v;
313  DetectEngineThreadCtx *det_ctx;
314 
315  memset(&th_v, 0, sizeof(th_v));
316 
317  p = UTHBuildPacket(buf, buflen, IPPROTO_TCP);
318 
320  FAIL_IF(de_ctx == NULL);
321 
322  de_ctx->flags |= DE_QUIET;
323 
326 
327  de_ctx->sig_list = SigInit(de_ctx, "alert tcp any any -> any any "
328  "(msg:\"FastLog test\"; content:\"GET\"; "
329  "Classtype:unknown; sid:1;)");
330 
332  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
333 
334  SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
335  FAIL_IF_NOT(p->alerts.cnt == 1);
336  FAIL_IF_NOT(strcmp(p->alerts.alerts[0].s->class_msg, "Unknown are we") == 0);
337 
340  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
342 
343  UTHFreePackets(&p, 1);
344  PASS;
345 }
346 
347 #endif /* UNITTESTS */
348 
349 /**
350  * \brief This function registers unit tests for AlertFastLog API.
351  */
353 {
354 
355 #ifdef UNITTESTS
356 
357  UtRegisterTest("AlertFastLogTest01", AlertFastLogTest01);
358  UtRegisterTest("AlertFastLogTest02", AlertFastLogTest02);
359 
360 #endif /* UNITTESTS */
361 
362 }
tm-threads.h
AlertFastLogThread_
Definition: alert-fastlog.c:85
AlertFastLogger
int AlertFastLogger(ThreadVars *tv, void *data, const Packet *p)
Definition: alert-fastlog.c:102
detect-engine.h
AlertFastLogRegisterTests
void AlertFastLogRegisterTests(void)
This function registers unit tests for AlertFastLog API.
Definition: alert-fastlog.c:352
PacketAlert_::s
const struct Signature_ * s
Definition: decode.h:245
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
AlertFastLogInitCtx
OutputInitResult AlertFastLogInitCtx(ConfNode *conf)
Create a new LogFileCtx for "fast" output style.
Definition: alert-fastlog.c:226
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
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:594
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:267
action-globals.h
threads.h
AlertFastLogRegister
void AlertFastLogRegister(void)
Definition: alert-fastlog.c:77
LogFileCtx_
Definition: util-logopenfile.h:72
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2597
PacketAlerts_::alerts
PacketAlert * alerts
Definition: decode.h:270
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
DE_QUIET
#define DE_QUIET
Definition: detect.h:323
util-privs.h
LogFileCtx_::Write
int(* Write)(const char *buffer, int buffer_len, struct LogFileCtx_ *fp)
Definition: util-logopenfile.h:87
known_proto
const char * known_proto[256]
Definition: util-proto-name.c:40
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:359
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1938
Packet_::icmp_s
struct Packet_::@29::@36 icmp_s
proto
uint8_t proto
Definition: decode-template.h:0
SigCleanSignatures
void SigCleanSignatures(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:55
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
AlertFastLogThreadInit
TmEcode AlertFastLogThreadInit(ThreadVars *, const void *, void **)
Definition: alert-fastlog.c:189
MAX_FASTLOG_BUFFER_SIZE
#define MAX_FASTLOG_BUFFER_SIZE
Definition: alert-fastlog.c:67
GET_IPV6_DST_ADDR
#define GET_IPV6_DST_ADDR(p)
Definition: decode.h:202
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:588
util-unittest.h
DEFAULT_LOG_FILENAME
#define DEFAULT_LOG_FILENAME
Definition: alert-fastlog.c:59
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
OutputCtx_::data
void * data
Definition: tm-modules.h:87
AlertFastLogThread_::file_ctx
LogFileCtx * file_ctx
Definition: alert-fastlog.c:87
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:84
SCConfLogOpenGeneric
int SCConfLogOpenGeneric(ConfNode *conf, LogFileCtx *log_ctx, const char *default_filename, int rotate)
open a generic output "log file", which may be a regular file or a socket
Definition: util-logopenfile.c:452
PacketAlert_::action
uint8_t action
Definition: decode.h:243
detect-reference.h
Signature_::gid
uint32_t gid
Definition: detect.h:637
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
GET_IPV4_DST_ADDR_PTR
#define GET_IPV4_DST_ADDR_PTR(p)
Definition: decode.h:197
PrintBufferRawLineHex
void PrintBufferRawLineHex(char *nbuf, int *offset, int max_size, const uint8_t *buf, uint32_t buflen)
print a buffer as hex on a single line
Definition: util-print.c:44
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
DetectEngineThreadCtx_
Definition: detect.h:1093
Packet_::ts
SCTime_t ts
Definition: decode.h:523
util-print.h
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:207
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:230
Packet_::sp
Port sp
Definition: decode.h:482
util-time.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
SigInit
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
Definition: detect-parse.c:2285
SigGroupCleanup
int SigGroupCleanup(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:2228
Packet_
Definition: decode.h:475
detect-engine-build.h
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:206
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
util-proto-name.h
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2161
alert-fastlog.h
Signature_::class_msg
char * class_msg
Definition: detect.h:662
MAX_FASTLOG_ALERT_SIZE
#define MAX_FASTLOG_ALERT_SIZE
Definition: alert-fastlog.c:64
OutputInitResult_
Definition: output.h:46
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:3323
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
GET_IPV4_SRC_ADDR_PTR
#define GET_IPV4_SRC_ADDR_PTR(p)
Definition: decode.h:196
PrintBufferData
#define PrintBufferData(buf, buf_offset_ptr, buf_size,...)
Definition: util-print.h:27
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3550
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
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:849
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
Signature_::prio
int prio
Definition: detect.h:639
util-optimize.h
threadvars.h
AlertFastLogThread
struct AlertFastLogThread_ AlertFastLogThread
GET_IPV6_SRC_ADDR
#define GET_IPV6_SRC_ADDR(p)
Definition: decode.h:201
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
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2558
MODULE_NAME
#define MODULE_NAME
Definition: alert-fastlog.c:61
EngineModeIsIPS
int EngineModeIsIPS(void)
Definition: suricata.c:228
SCClassConfGenerateValidDummyClassConfigFD01
FILE * SCClassConfGenerateValidDummyClassConfigFD01(void)
Creates a dummy classification file, with all valid Classtypes, for testing purposes.
Definition: util-classification-config.c:587
PacketAlert_
Definition: decode.h:241
likely
#define likely(expr)
Definition: util-optimize.h:32
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:843
Signature_::msg
char * msg
Definition: detect.h:659
flow.h
Packet_::dp
Port dp
Definition: decode.h:490
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
LOGGER_ALERT_FAST
@ LOGGER_ALERT_FAST
Definition: suricata-common.h:478
SCClassConfLoadClassificationConfigFile
bool SCClassConfLoadClassificationConfigFile(DetectEngineCtx *de_ctx, FILE *fd)
Loads the Classtype info from the classification.config file.
Definition: util-classification-config.c:524
AlertFastLogThreadDeinit
TmEcode AlertFastLogThreadDeinit(ThreadVars *, void *)
Definition: alert-fastlog.c:207
CreateTimeString
void CreateTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:272
output.h
UTHFreePackets
void UTHFreePackets(Packet **p, int numpkts)
UTHFreePackets: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:450