suricata
custom-logger.c
Go to the documentation of this file.
1/* Copyright (C) 2023-2024 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#include "suricata-common.h"
19#include "suricata-plugin.h"
20
21#include "output-packet.h"
22#include "output-flow.h"
23#include "output-tx.h"
24#include "util-print.h"
25#include "output.h"
26
27static int CustomPacketLogger(ThreadVars *tv, void *thread_data, const Packet *p)
28{
29 char src_ip[46] = { 0 }, dst_ip[46] = { 0 };
30
31 if (PacketIsIPv4(p)) {
32 PrintInet(AF_INET, (const void *)&(p->src.addr_data32[0]), src_ip, sizeof(src_ip));
33 PrintInet(AF_INET, (const void *)&(p->dst.addr_data32[0]), dst_ip, sizeof(dst_ip));
34 } else if (PacketIsIPv6(p)) {
35 PrintInet(AF_INET6, (const void *)&(p->src.address), src_ip, sizeof(src_ip));
36 PrintInet(AF_INET6, (const void *)&(p->dst.address), dst_ip, sizeof(dst_ip));
37 } else {
38 SCLogNotice("Packet is not IP");
39 return 0;
40 }
41 SCLogNotice("Packet: %s -> %s", src_ip, dst_ip);
42 return 0;
43}
44
45static bool CustomPacketLoggerCondition(ThreadVars *tv, void *thread_data, const Packet *)
46{
47 /* Always true for this example. */
48 return true;
49}
50
51static int CustomFlowLogger(ThreadVars *tv, void *thread_data, Flow *f)
52{
53 char src_ip[46] = { 0 }, dst_ip[46] = { 0 };
54 Port sp, dp;
55
56 if ((f->flags & FLOW_DIR_REVERSED) == 0) {
57 if (FLOW_IS_IPV4(f)) {
58 PrintInet(AF_INET, (const void *)&(f->src.addr_data32[0]), src_ip, sizeof(src_ip));
59 PrintInet(AF_INET, (const void *)&(f->dst.addr_data32[0]), dst_ip, sizeof(dst_ip));
60 } else if (FLOW_IS_IPV6(f)) {
61 PrintInet(AF_INET6, (const void *)&(f->src.address), src_ip, sizeof(src_ip));
62 PrintInet(AF_INET6, (const void *)&(f->dst.address), dst_ip, sizeof(dst_ip));
63 }
64 sp = f->sp;
65 dp = f->dp;
66 } else {
67 if (FLOW_IS_IPV4(f)) {
68 PrintInet(AF_INET, (const void *)&(f->dst.addr_data32[0]), src_ip, sizeof(src_ip));
69 PrintInet(AF_INET, (const void *)&(f->src.addr_data32[0]), dst_ip, sizeof(dst_ip));
70 } else if (FLOW_IS_IPV6(f)) {
71 PrintInet(AF_INET6, (const void *)&(f->dst.address), src_ip, sizeof(src_ip));
72 PrintInet(AF_INET6, (const void *)&(f->src.address), dst_ip, sizeof(dst_ip));
73 }
74 sp = f->dp;
75 dp = f->sp;
76 }
77
78 SCLogNotice("Flow: %s:%u -> %s:%u", src_ip, sp, dst_ip, dp);
79
80 return 0;
81}
82
83static int CustomDnsLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state,
84 void *tx, uint64_t tx_id)
85{
86 SCLogNotice("We have a DNS transaction");
87 return 0;
88}
89
90static TmEcode ThreadInit(ThreadVars *tv, const void *initdata, void **data)
91{
92 return TM_ECODE_OK;
93}
94
95static TmEcode ThreadDeinit(ThreadVars *tv, void *data)
96{
97 // Nothing to do. If we allocated data in ThreadInit we would free
98 // it here.
99 return TM_ECODE_OK;
100}
101
102static void OnLoggingReady(void *arg)
103{
104 SCOutputRegisterPacketLogger(LOGGER_USER, "custom-packet-logger", CustomPacketLogger,
105 CustomPacketLoggerCondition, NULL, ThreadInit, ThreadDeinit);
107 "custom-flow-logger", CustomFlowLogger, NULL, ThreadInit, ThreadDeinit);
108 SCOutputRegisterTxLogger(LOGGER_USER, "custom-dns-logger", ALPROTO_DNS, CustomDnsLogger, NULL,
109 -1, -1, NULL, ThreadInit, ThreadDeinit);
110}
111
112static void Init(void)
113{
114 // Register our callback for when logging is ready.
115 SCRegisterOnLoggingReady(OnLoggingReady, NULL);
116}
117
119 .version = SC_API_VERSION,
120 .suricata_version = SC_PACKAGE_VERSION,
121 .name = "CustomLogger",
122 .plugin_version = "1.0.0",
123 .author = "Firstname Lastname",
124 .license = "GPLv2",
125 .Init = Init,
126};
127
129{
130 return &PluginRegistration;
131}
@ ALPROTO_DNS
const SCPlugin * SCPluginRegister(void)
const SCPlugin PluginRegistration
uint16_t Port
Definition decode.h:218
#define FLOW_IS_IPV6(f)
Definition flow.h:172
#define FLOW_DIR_REVERSED
Definition flow.h:112
#define FLOW_IS_IPV4(f)
Definition flow.h:170
ThreadVars * tv
int SCOutputRegisterFlowLogger(const char *name, FlowLogger LogFunc, void *initdata, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit)
Register a flow logger.
Definition output-flow.c:58
int SCOutputRegisterPacketLogger(LoggerId logger_id, const char *name, PacketLogger LogFunc, PacketLogCondition ConditionFunc, void *initdata, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit)
Register a packet logger.
int SCOutputRegisterTxLogger(LoggerId id, const char *name, AppProto alproto, TxLogger LogFunc, void *initdata, int tc_log_progress, int ts_log_progress, TxLoggerCondition LogCondition, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit)
Register a transaction logger.
Definition output-tx.c:66
int SCRegisterOnLoggingReady(SCOnLoggingReadyCallback callback, void *arg)
Register a callback to be called when logging is ready.
Definition output.c:757
union Address_::@30 address
union FlowAddress_::@128 address
Flow data structure.
Definition flow.h:356
Port dp
Definition flow.h:372
uint32_t flags
Definition flow.h:421
FlowAddress src
Definition flow.h:359
Port sp
Definition flow.h:361
FlowAddress dst
Definition flow.h:359
Address src
Definition decode.h:505
Address dst
Definition decode.h:506
uint64_t version
Per thread variable structure.
Definition threadvars.h:58
@ LOGGER_USER
#define SC_PACKAGE_VERSION
@ TM_ECODE_OK
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition util-debug.h:243
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
Definition util-print.c:231