suricata
output-json-arp.c
Go to the documentation of this file.
1 /* Copyright (C) 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 /**
19  * \file
20  *
21  * \author Giuseppe Longo <giuseppe@glongo.it>
22  *
23  * Implement JSON/eve logging for ARP Protocol.
24  */
25 
26 #include "suricata-common.h"
27 #include "detect.h"
28 #include "flow.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "tm-threads.h"
33 #include "threadvars.h"
34 #include "util-debug.h"
35 
36 #include "decode-ipv4.h"
37 #include "detect-parse.h"
38 #include "detect-engine.h"
39 #include "detect-reference.h"
40 
41 #include "output.h"
42 #include "output-json.h"
43 #include "output-json-arp.h"
44 
46 #include "util-privs.h"
47 #include "util-print.h"
48 #include "util-proto-name.h"
49 #include "util-logopenfile.h"
50 #include "util-buffer.h"
51 
52 static const char *OpcodeToString(uint16_t opcode)
53 {
54  switch (opcode) {
55  case 1:
56  return "request";
57  case 2:
58  return "reply";
59  case 3:
60  return "request_reverse";
61  case 4:
62  return "reply_reverse";
63  default:
64  return "unknown";
65  }
66 }
67 
68 static int JsonArpLogger(ThreadVars *tv, void *thread_data, const Packet *p)
69 {
70  OutputJsonThreadCtx *thread = thread_data;
71  char srcip[JSON_ADDR_LEN] = "";
72  char dstip[JSON_ADDR_LEN] = "";
73  const ARPHdr *arph = PacketGetARP(p);
74 
75  SCJsonBuilder *jb = CreateEveHeader(p, LOG_DIR_PACKET, "arp", NULL, thread->ctx);
76  if (unlikely(jb == NULL)) {
77  return TM_ECODE_OK;
78  }
79 
80  PrintInet(AF_INET, arph->source_ip, srcip, sizeof(srcip));
81  PrintInet(AF_INET, arph->dest_ip, dstip, sizeof(dstip));
82 
83  SCJbOpenObject(jb, "arp");
84  JB_SET_STRING(jb, "hw_type", "ethernet");
85  JB_SET_STRING(jb, "proto_type", "ipv4");
86  SCJbSetString(jb, "opcode", OpcodeToString(ntohs(arph->opcode)));
87  JSONFormatAndAddMACAddr(jb, "src_mac", arph->source_mac, false);
88  SCJbSetString(jb, "src_ip", srcip);
89  JSONFormatAndAddMACAddr(jb, "dest_mac", arph->dest_mac, false);
90  SCJbSetString(jb, "dest_ip", dstip);
91  SCJbClose(jb); /* arp */
92  OutputJsonBuilderBuffer(tv, p, p->flow, jb, thread);
93  SCJbFree(jb);
94 
95  return TM_ECODE_OK;
96 }
97 
98 static bool JsonArpLogCondition(ThreadVars *tv, void *thread_data, const Packet *p)
99 {
100  return PacketIsARP(p);
101 }
102 
104 {
105  OutputPacketLoggerFunctions output_logger_functions = {
106  .LogFunc = JsonArpLogger,
107  .ConditionFunc = JsonArpLogCondition,
108  .ThreadInitFunc = JsonLogThreadInit,
109  .ThreadDeinitFunc = JsonLogThreadDeinit,
110  .ThreadExitPrintStatsFunc = NULL,
111  };
112 
113  OutputRegisterPacketSubModule(LOGGER_JSON_ARP, "eve-log", "JsonArpLog", "eve-log.arp",
114  OutputJsonLogInitSub, &output_logger_functions);
115 
116  SCLogDebug("ARP JSON logger registered.");
117 }
SCJsonBuilder
struct SCJsonBuilder SCJsonBuilder
Definition: detect-engine-helper.h:83
tm-threads.h
detect-engine.h
OutputJsonThreadCtx_::ctx
OutputJsonCtx * ctx
Definition: output-json.h:86
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
opcode
uint16_t opcode
Definition: decode-arp.h:4
CreateEveHeader
SCJsonBuilder * CreateEveHeader(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:816
JsonLogThreadInit
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
Definition: output-json-common.c:90
threads.h
LOGGER_JSON_ARP
@ LOGGER_JSON_ARP
Definition: suricata-common.h:515
output-json-arp.h
util-privs.h
p
Packet * p
Definition: fuzz_dataset.c:30
OutputJsonBuilderBuffer
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:1018
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
OutputJsonThreadCtx_
Definition: output-json.h:85
detect-reference.h
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:73
util-debug.h
JB_SET_STRING
#define JB_SET_STRING(jb, key, val)
Definition: rust.h:36
output-json.h
util-print.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:238
Packet_
Definition: decode.h:516
conf.h
JSONFormatAndAddMACAddr
void JSONFormatAndAddMACAddr(SCJsonBuilder *js, const char *key, const uint8_t *val, bool is_array)
Definition: output-json.c:694
util-proto-name.h
decode-ipv4.h
Packet_::flow
struct Flow_ * flow
Definition: decode.h:564
suricata-common.h
JSON_ADDR_LEN
#define JSON_ADDR_LEN
Definition: output-json.h:37
OutputPacketLoggerFunctions_::LogFunc
PacketLogger LogFunc
Definition: output.h:87
util-classification-config.h
OutputRegisterPacketSubModule
void OutputRegisterPacketSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, OutputPacketLoggerFunctions *output_logger_functions)
Register a packet output sub-module.
Definition: output.c:230
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
threadvars.h
util-logopenfile.h
detect-parse.h
util-buffer.h
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-eve-bindgen.h:34
JsonArpLogRegister
void JsonArpLogRegister(void)
Definition: output-json-arp.c:103
JsonLogThreadDeinit
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: output-json-common.c:123
flow.h
OutputPacketLoggerFunctions_
Definition: output.h:86
output.h