suricata
detect-engine-profile.c
Go to the documentation of this file.
1/* Copyright (C) 2016-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 */
24
25#include "suricata-common.h"
27
28#ifdef PROFILING
29#include "output-json.h"
30#include "util-conf.h"
31
33
35 const Packet *p, const uint64_t tx_id, const uint32_t rule_cnt,
36 const uint32_t pkt_prefilter_cnt)
37{
38 SCJsonBuilder *js =
39 CreateEveHeaderWithTxId(p, LOG_DIR_PACKET, "inspectedrules", NULL, tx_id, NULL);
40 if (js == NULL)
41 return;
42
43 SCJbSetString(js, "app_proto", AppProtoToString(p->flow->alproto));
44
45 SCJbOpenObject(js, "inspectedrules");
46 SCJbSetString(js, "inspect_type", "tx");
47 SCJbSetUint(js, "rule_group_id", sgh->id);
48 SCJbSetUint(js, "rule_cnt", rule_cnt);
49 SCJbSetUint(js, "pkt_rule_cnt", pkt_prefilter_cnt);
50
51 SCJbOpenArray(js, "rules");
52 for (uint32_t x = 0; x < rule_cnt; x++) {
53 SigIntId iid = det_ctx->tx_candidates[x].id;
54 const Signature *s = det_ctx->de_ctx->sig_array[iid];
55 if (s == NULL)
56 continue;
57 SCJbAppendUint(js, s->id);
58 }
59 SCJbClose(js); // close array
60 SCJbClose(js); // close inspectedrules object
61 SCJbClose(js); // final close
62
63 const char *filename = "packet_inspected_rules.json";
64 const char *log_dir = SCConfigGetLogDirectory();
65 char log_path[PATH_MAX] = "";
66 snprintf(log_path, sizeof(log_path), "%s/%s", log_dir, filename);
67
69 FILE *fp = fopen(log_path, "a");
70 if (fp != NULL) {
71 fwrite(SCJbPtr(js), SCJbLen(js), 1, fp);
72 fclose(fp);
73 }
75 SCJbFree(js);
76}
77
79 const SigGroupHead *sgh, const Packet *p)
80{
81 SCJsonBuilder *js = CreateEveHeader(p, LOG_DIR_PACKET, "inspectedrules", NULL, NULL);
82 if (js == NULL)
83 return;
84
85 if (p->flow) {
86 SCJbSetString(js, "app_proto", AppProtoToString(p->flow->alproto));
87 }
88
89 SCJbOpenObject(js, "inspectedrules");
90 SCJbSetString(js, "inspect_type", "packet");
91 SCJbSetUint(js, "rule_group_id", sgh->id);
92 SCJbSetUint(js, "rule_cnt", det_ctx->match_array_cnt);
93
94 SCJbOpenArray(js, "rules");
95 for (uint32_t x = 0; x < det_ctx->match_array_cnt; x++) {
96 const Signature *s = det_ctx->match_array[x];
97 if (s == NULL)
98 continue;
99 SCJbAppendUint(js, s->id);
100 }
101 SCJbClose(js); // close array
102 SCJbClose(js); // close inspectedrules object
103 SCJbClose(js); // final close
104
105 const char *filename = "packet_inspected_rules.json";
106 const char *log_dir = SCConfigGetLogDirectory();
107 char log_path[PATH_MAX] = "";
108 snprintf(log_path, sizeof(log_path), "%s/%s", log_dir, filename);
109
111 FILE *fp = fopen(log_path, "a");
112 if (fp != NULL) {
113 fwrite(SCJbPtr(js), SCJbLen(js), 1, fp);
114 fclose(fp);
115 }
117 SCJbFree(js);
118}
119#endif /* PROFILING */
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
void RulesDumpTxMatchArray(const DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, const Packet *p, const uint64_t tx_id, const uint32_t rule_cnt, const uint32_t pkt_prefilter_cnt)
void RulesDumpMatchArray(const DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, const Packet *p)
SCMutex g_rule_dump_write_m
@ LOG_DIR_PACKET
SCJsonBuilder * CreateEveHeader(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
SCJsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id, OutputJsonCtx *eve_ctx)
Signature ** sig_array
Definition detect.h:950
RuleMatchCandidateTx * tx_candidates
Definition detect.h:1342
Signature ** match_array
Definition detect.h:1335
SigIntId match_array_cnt
Definition detect.h:1340
DetectEngineCtx * de_ctx
Definition detect.h:1364
AppProto alproto
application level protocol
Definition flow.h:450
struct Flow_ * flow
Definition decode.h:546
Container for matching data for a signature group.
Definition detect.h:1629
uint32_t id
Definition detect.h:1637
Signature container.
Definition detect.h:668
uint32_t id
Definition detect.h:713
#define SigIntId
#define SCMUTEX_INITIALIZER
#define SCMutex
#define SCMutexUnlock(mut)
#define SCMutexLock(mut)
const char * SCConfigGetLogDirectory(void)
Definition util-conf.c:38