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  if (sgh == NULL)
39  return;
40 
41  SCJsonBuilder *js =
42  CreateEveHeaderWithTxId(p, LOG_DIR_PACKET, "inspectedrules", NULL, tx_id, NULL);
43  if (js == NULL)
44  return;
45 
46  SCJbSetString(js, "app_proto", AppProtoToString(p->flow->alproto));
47 
48  SCJbOpenObject(js, "inspectedrules");
49  SCJbSetString(js, "inspect_type", "tx");
50  SCJbSetUint(js, "rule_group_id", sgh->id);
51  SCJbSetUint(js, "rule_cnt", rule_cnt);
52  SCJbSetUint(js, "pkt_rule_cnt", pkt_prefilter_cnt);
53 
54  SCJbOpenArray(js, "rules");
55  for (uint32_t x = 0; x < rule_cnt; x++) {
56  SigIntId iid = det_ctx->tx_candidates[x].id;
57  const Signature *s = det_ctx->de_ctx->sig_array[iid];
58  if (s == NULL)
59  continue;
60  SCJbAppendUint(js, s->id);
61  }
62  SCJbClose(js); // close array
63  SCJbClose(js); // close inspectedrules object
64  SCJbClose(js); // final close
65 
66  const char *filename = "packet_inspected_rules.json";
67  const char *log_dir = SCConfigGetLogDirectory();
68  char log_path[PATH_MAX] = "";
69  snprintf(log_path, sizeof(log_path), "%s/%s", log_dir, filename);
70 
72  FILE *fp = fopen(log_path, "a");
73  if (fp != NULL) {
74  fwrite(SCJbPtr(js), SCJbLen(js), 1, fp);
75  fclose(fp);
76  }
78  SCJbFree(js);
79 }
80 
82  const SigGroupHead *sgh, const Packet *p)
83 {
84  if (sgh == NULL)
85  return;
86 
87  SCJsonBuilder *js = CreateEveHeader(p, LOG_DIR_PACKET, "inspectedrules", NULL, NULL);
88  if (js == NULL)
89  return;
90 
91  if (p->flow) {
92  SCJbSetString(js, "app_proto", AppProtoToString(p->flow->alproto));
93  }
94 
95  SCJbOpenObject(js, "inspectedrules");
96  SCJbSetString(js, "inspect_type", "packet");
97  SCJbSetUint(js, "rule_group_id", sgh->id);
98  SCJbSetUint(js, "rule_cnt", det_ctx->match_array_cnt);
99 
100  SCJbOpenArray(js, "rules");
101  for (uint32_t x = 0; x < det_ctx->match_array_cnt; x++) {
102  const Signature *s = det_ctx->match_array[x];
103  if (s == NULL)
104  continue;
105  SCJbAppendUint(js, s->id);
106  }
107  SCJbClose(js); // close array
108  SCJbClose(js); // close inspectedrules object
109  SCJbClose(js); // final close
110 
111  const char *filename = "packet_inspected_rules.json";
112  const char *log_dir = SCConfigGetLogDirectory();
113  char log_path[PATH_MAX] = "";
114  snprintf(log_path, sizeof(log_path), "%s/%s", log_dir, filename);
115 
117  FILE *fp = fopen(log_path, "a");
118  if (fp != NULL) {
119  fwrite(SCJbPtr(js), SCJbLen(js), 1, fp);
120  fclose(fp);
121  }
123  SCJbFree(js);
124 }
125 #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:1381
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1670
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:819
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:41
RuleMatchCandidateTx::id
SigIntId id
Definition: detect.h:1239
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:122
DetectEngineThreadCtx_::tx_candidates
RuleMatchCandidateTx * tx_candidates
Definition: detect.h:1383
DetectEngineThreadCtx_
Definition: detect.h:1284
output-json.h
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
detect-engine-profile.h
SigIntId
#define SigIntId
Definition: detect-engine-state.h:38
Packet_
Definition: decode.h:505
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:81
util-conf.h
Packet_::flow
struct Flow_ * flow
Definition: decode.h:553
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:939
Signature_::id
uint32_t id
Definition: detect.h:717
Signature_
Signature container.
Definition: detect.h:672
DetectEngineThreadCtx_::de_ctx
DetectEngineCtx * de_ctx
Definition: detect.h:1405
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:984
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-eve-bindgen.h:34
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:443
SCMutex
#define SCMutex
Definition: threads-debug.h:114
SigGroupHead_::id
uint32_t id
Definition: detect.h:1678
DetectEngineThreadCtx_::match_array
Signature ** match_array
Definition: detect.h:1373