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"
26 #include "detect-engine-profile.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 */
RulesDumpTxMatchArray
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)
Definition: detect-engine-profile.c:34
DetectEngineThreadCtx_::match_array_cnt
SigIntId match_array_cnt
Definition: detect.h:1289
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1572
g_rule_dump_write_m
SCMutex g_rule_dump_write_m
Definition: detect-engine-profile.c:32
CreateEveHeader
SCJsonBuilder * CreateEveHeader(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:834
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:40
RuleMatchCandidateTx::id
SigIntId id
Definition: detect.h:1162
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:121
DetectEngineThreadCtx_::tx_candidates
RuleMatchCandidateTx * tx_candidates
Definition: detect.h:1291
DetectEngineThreadCtx_
Definition: detect.h:1197
output-json.h
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
detect-engine-profile.h
Packet_
Definition: decode.h:484
SCConfigGetLogDirectory
const char * SCConfigGetLogDirectory(void)
Definition: util-conf.c:38
RulesDumpMatchArray
void RulesDumpMatchArray(const DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, const Packet *p)
Definition: detect-engine-profile.c:78
util-conf.h
Packet_::flow
struct Flow_ * flow
Definition: decode.h:529
suricata-common.h
CreateEveHeaderWithTxId
SCJsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id, OutputJsonCtx *eve_ctx)
Definition: output-json.c:930
Signature_::id
uint32_t id
Definition: detect.h:714
Signature_
Signature container.
Definition: detect.h:669
DetectEngineThreadCtx_::de_ctx
DetectEngineCtx * de_ctx
Definition: detect.h:1313
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:938
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-eve-bindgen.h:32
SigIntId
#define SigIntId
Definition: suricata-common.h:324
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:450
SCMutex
#define SCMutex
Definition: threads-debug.h:114
SigGroupHead_::id
uint32_t id
Definition: detect.h:1580
DetectEngineThreadCtx_::match_array
Signature ** match_array
Definition: detect.h:1284