61 static int rule_warnings_only = 0;
117 { 0,
false,
false,
true,
"http_uri",
"http uri" },
118 { 0,
false,
false,
false,
"http_raw_uri",
"http raw uri" },
119 { 0,
false,
true,
false,
"http_method",
"http method" },
120 { 0,
false,
false,
false,
"http_request_line",
"http request line" },
121 { 0,
false,
false,
false,
"http_client_body",
"http client body" },
122 { 0,
false,
false,
true,
"http_header",
"http header" },
123 { 0,
false,
false,
false,
"http_raw_header",
"http raw header" },
124 { 0,
false,
false,
true,
"http_cookie",
"http cookie" },
125 { 0,
false,
false,
false,
"http_user_agent",
"http user agent" },
126 { 0,
false,
false,
false,
"http_host",
"http host" },
127 { 0,
false,
false,
false,
"http_raw_host",
"http raw host" },
128 { 0,
false,
false,
false,
"http_accept_enc",
"http accept enc" },
129 { 0,
false,
false,
false,
"http_referer",
"http referer" },
130 { 0,
false,
false,
false,
"http_content_type",
"http content type" },
131 { 0,
false,
false,
false,
"http_header_names",
"http header names" },
134 { 0,
false,
false,
false,
"http_stat_msg",
"http stat msg" },
135 { 0,
false,
false,
false,
"http_stat_code",
"http stat code" },
136 { 0,
false,
true,
false,
"file_data",
"http server body" },
139 { 0,
false,
false,
false,
"http_request_line",
"http request line" },
140 { 0,
false,
false,
false,
"http_accept",
"http accept" },
141 { 0,
false,
false,
false,
"http_accept_lang",
"http accept lang" },
142 { 0,
false,
false,
false,
"http_connection",
"http connection" },
143 { 0,
false,
false,
false,
"http_content_len",
"http content len" },
144 { 0,
false,
false,
false,
"http_protocol",
"http protocol" },
145 { 0,
false,
false,
false,
"http_start",
"http start" },
148 { 0,
false,
false,
false,
"http_response_line",
"http response line" },
149 { 0,
false,
false,
false,
"http.server",
"http server" },
150 { 0,
false,
false,
false,
"http.location",
"http location" },
153 static void FpPatternStatsAdd(
FpPatternStats *fp,
int list, uint16_t patlen)
162 else if (patlen < f->min)
174 int fast_pattern_set = 0;
175 int fast_pattern_only_set = 0;
176 int fast_pattern_chop_set = 0;
181 if (mpm_sm != NULL) {
184 fast_pattern_set = 1;
186 fast_pattern_only_set = 1;
188 fast_pattern_chop_set = 1;
194 fprintf(fp,
"== Sid: %u ==\n", s->
id);
195 fprintf(fp,
"%s\n", line);
197 fprintf(fp,
" Fast Pattern analysis:\n");
199 fprintf(fp,
" Prefilter on: %s\n",
206 fprintf(fp,
" No content present\n");
211 fprintf(fp,
" Fast pattern matcher: ");
212 int list_type = mpm_sm_list;
214 fprintf(fp,
"content\n");
219 fprintf(fp,
"%s (%s)\n", desc,
name);
224 fprintf(fp,
" Flags:");
226 fprintf(fp,
" Offset");
229 fprintf(fp,
" Depth");
233 fprintf(fp,
" Within");
237 fprintf(fp,
" Distance");
241 fprintf(fp,
" Nocase");
245 fprintf(fp,
" Negated");
249 fprintf(fp,
" None");
252 fprintf(fp,
" Fast pattern set: %s\n", fast_pattern_set ?
"yes" :
"no");
253 fprintf(fp,
" Fast pattern only set: %s\n", fast_pattern_only_set ?
"yes" :
"no");
254 fprintf(fp,
" Fast pattern chop set: %s\n", fast_pattern_chop_set ?
"yes" :
"no");
255 if (fast_pattern_chop_set) {
256 fprintf(fp,
" Fast pattern offset, length: %u, %u\n", fp_cd->
fp_chop_offset,
267 fprintf(fp,
" Original content: ");
271 if (fast_pattern_chop_set) {
280 fprintf(fp,
" Final content: ");
286 fprintf(fp,
" Final content: ");
305 int fp_engine_analysis_set = 0;
307 if ((
SCConfGetBool(
"engine-analysis.rules-fast-pattern", &fp_engine_analysis_set)) == 0) {
311 if (fp_engine_analysis_set == 0)
315 char *log_path =
SCMalloc(PATH_MAX);
316 if (log_path == NULL) {
317 FatalError(
"Unable to allocate scratch memory for rule filename");
319 snprintf(log_path, PATH_MAX,
"%s/%s%s", log_dir,
322 FILE *fp = fopen(log_path,
"w");
324 SCLogError(
"failed to open %s: %s", log_path, strerror(errno));
331 SCLogInfo(
"Engine-Analysis for fast_pattern printed to file - %s",
336 gettimeofday(&tval, NULL);
338 struct tm *tms =
SCLocalTime(tval.tv_sec, &local_tm);
339 fprintf(fp,
"----------------------------------------------"
340 "---------------------\n");
342 "Date: %" PRId32
"/%" PRId32
"/%04d -- "
344 tms->tm_mday, tms->tm_mon + 1, tms->tm_year + 1900, tms->tm_hour, tms->tm_min,
346 fprintf(fp,
"----------------------------------------------"
347 "---------------------\n");
360 #define DETECT_PERCENT_ENCODING_REGEX "%[0-9|a-f|A-F]{2}"
366 PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
368 PCRE2_UCHAR errbuffer[256];
369 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
390 }
else if (value && strcasecmp(value,
"warnings-only") == 0) {
392 rule_warnings_only = 1;
397 char log_path[PATH_MAX];
398 snprintf(log_path,
sizeof(log_path),
"%s/%s%s", log_dir,
402 SCLogError(
"failed to open %s: %s", log_path, strerror(errno));
406 SCLogInfo(
"Engine-Analysis for rules printed to file - %s",
410 gettimeofday(&tval, NULL);
412 struct tm *tms =
SCLocalTime(tval.tv_sec, &local_tm);
414 "----------------------------------------------"
415 "---------------------\n");
417 "Date: %" PRId32
"/%" PRId32
"/%04d -- "
419 tms->tm_mday, tms->tm_mon + 1, tms->tm_year + 1900, tms->tm_hour, tms->tm_min,
422 "----------------------------------------------"
423 "---------------------\n");
426 if (!PerCentEncodingSetup(
de_ctx->
ea)) {
428 "Error compiling regex; can't check for percent encoding in normalized "
434 SCLogInfo(
"Conf parameter \"engine-analysis.rules\" not found. "
435 "Defaulting to not printing the rules analysis report.");
438 SCLogInfo(
"Engine-Analysis for rules disabled in conf file.");
447 fprintf(fp,
"============\n"
448 "Summary:\n============\n");
456 "%s, smallest pattern %u byte(s), longest pattern %u byte(s), number of patterns "
457 "%u, avg pattern len %.2f byte(s)\n",
459 (
float)((
double)f->
tot / (
float)f->
cnt));
479 *fp_analysis =
false;
480 *rule_analysis =
false;
484 FatalError(
"Unable to allocate per-engine analysis context");
489 if (cfg_prefix_len > 0) {
494 FatalError(
"Unable to allocate per-engine analysis context name buffer");
500 *fp_analysis = SetupFPAnalyzer(
de_ctx);
501 *rule_analysis = SetupRuleAnalyzer(
de_ctx);
503 if (!(*fp_analysis || *rule_analysis)) {
515 CleanupRuleAnalyzer(
de_ctx);
516 CleanupFPAnalyzer(
de_ctx);
533 static int PerCentEncodingMatch(
EngineAnalysisCtx *ea_ctx, uint8_t *content, uint16_t content_len)
537 pcre2_match_data *match = pcre2_match_data_create_from_pattern(ea_ctx->
percent_re, NULL);
538 ret = pcre2_match(ea_ctx->
percent_re, (PCRE2_SPTR8)content, content_len, 0, 0, match, NULL);
541 }
else if (ret < -1) {
542 SCLogError(
"Error parsing content - %s; error code is %d", content, ret);
545 pcre2_match_data_free(match);
555 if (mpm_sm != NULL) {
593 const int list_type = mpm_sm_list;
602 payload ? (stream ?
"payload and reassembled stream" :
"payload")
603 :
"reassembled stream");
610 }
else if (desc ||
name) {
630 fprintf(tmp_fp,
"== Sid: UNKNOWN ==\n");
631 fprintf(tmp_fp,
"%s\n", line);
632 fprintf(tmp_fp,
" FAILURE: invalid rule.\n");
633 fprintf(tmp_fp,
" File: %s.\n", file);
634 fprintf(tmp_fp,
" Line: %d.\n", lineno);
635 fprintf(tmp_fp,
"\n");
652 vsnprintf(
str,
sizeof(
str), fmt, ap);
656 ctx->js_notes = SCJbNewArray();
658 SCJbAppendString(
ctx->js_notes,
str);
667 vsnprintf(
str,
sizeof(
str), fmt, ap);
670 if (!
ctx->js_warnings)
671 ctx->js_warnings = SCJbNewArray();
672 if (
ctx->js_warnings)
673 SCJbAppendString(
ctx->js_warnings,
str);
676 #define CHECK(pat) if (strlen((pat)) <= len && memcmp((pat), buf, MIN(len, strlen((pat)))) == 0) return true;
678 static bool LooksLikeHTTPMethod(
const uint8_t *buf, uint16_t
len)
687 static bool LooksLikeHTTPUA(
const uint8_t *buf, uint16_t
len)
689 CHECK(
"User-Agent: ");
690 CHECK(
"\nUser-Agent: ");
696 char pattern_str[1024] =
"";
699 SCJbSetString(js,
"pattern", pattern_str);
708 SCJbSetUint(js,
"offset", cd->
offset);
711 SCJbSetUint(js,
"depth", cd->
depth);
714 SCJbSetInt(js,
"distance", cd->
distance);
717 SCJbSetInt(js,
"within", cd->
within);
736 SCJbOpenArray(js,
"matches");
740 SCJbSetString(js,
"name", mname);
746 SCJbOpenObject(js,
"content");
749 AnalyzerNote(
ctx, (
char *)
"'fast_pattern:only' option is silently ignored and "
750 "is interpreted as regular 'fast_pattern'");
754 (
char *)
"pattern looks like it inspects HTTP, use http.request_line or "
755 "http.method and http.uri instead for improved performance");
759 (
char *)
"pattern looks like it inspects HTTP, use http.user_agent "
760 "or http.header for improved performance");
763 AnalyzerNote(
ctx, (
char *)
"'within' option for pattern w/o previous content "
764 "was converted to 'depth'");
767 AnalyzerNote(
ctx, (
char *)
"'distance' option for pattern w/o previous content "
768 "was converted to 'offset'");
776 SCJbOpenObject(js,
"pcre");
781 (
char *)
"'/B' (rawbytes) option is a no-op and is silently ignored");
788 SCJbOpenObject(js,
"byte_jump");
789 SCJbSetUint(js,
"nbytes", cd->
nbytes);
790 SCJbSetInt(js,
"offset", cd->
offset);
791 SCJbSetUint(js,
"multiplier", cd->
multiplier);
795 SCJbSetString(js,
"base",
"unset");
798 SCJbSetString(js,
"base",
"oct");
801 SCJbSetString(js,
"base",
"dec");
804 SCJbSetString(js,
"base",
"hex");
807 SCJbOpenArray(js,
"flags");
809 SCJbAppendString(js,
"from_beginning");
811 SCJbAppendString(js,
"little_endian");
813 SCJbAppendString(js,
"big_endian");
815 SCJbAppendString(js,
"string");
817 SCJbAppendString(js,
"relative");
819 SCJbAppendString(js,
"align");
821 SCJbAppendString(js,
"dce");
823 SCJbAppendString(js,
"offset_be");
825 SCJbAppendString(js,
"from_end");
833 SCJbOpenObject(js,
"byte_test");
834 SCJbSetUint(js,
"nbytes", cd->
nbytes);
835 SCJbSetInt(js,
"offset", cd->
offset);
838 SCJbSetString(js,
"base",
"unset");
841 SCJbSetString(js,
"base",
"oct");
844 SCJbSetString(js,
"base",
"dec");
847 SCJbSetString(js,
"base",
"hex");
850 SCJbOpenArray(js,
"flags");
852 SCJbAppendString(js,
"little_endian");
854 SCJbAppendString(js,
"big_endian");
856 SCJbAppendString(js,
"string");
858 SCJbAppendString(js,
"relative");
860 SCJbAppendString(js,
"dce");
867 SCJbOpenObject(js,
"absent");
868 SCJbSetBool(js,
"or_else", dad->
or_else);
876 SCJbOpenObject(js,
"ipopts");
878 SCJbSetString(js,
"option", flag);
885 SCJbOpenObject(js,
"flowbits");
888 SCJbSetString(js,
"cmd",
"isset");
891 SCJbSetString(js,
"cmd",
"isnotset");
894 SCJbSetString(js,
"cmd",
"set");
897 SCJbSetString(js,
"cmd",
"unset");
900 SCJbSetString(js,
"cmd",
"toggle");
904 SCJbOpenArray(js,
"names");
910 const char *varname =
912 SCJbAppendString(js, varname);
917 SCJbSetString(js,
"operator",
"or");
925 SCJbOpenObject(js,
"xbits");
928 SCJbSetString(js,
"cmd",
"isset");
931 SCJbSetString(js,
"cmd",
"isnotset");
934 SCJbSetString(js,
"cmd",
"set");
937 SCJbSetString(js,
"cmd",
"unset");
940 SCJbSetString(js,
"cmd",
"toggle");
946 SCJbSetString(js,
"track",
"ip_src");
949 SCJbSetString(js,
"track",
"ip_dst");
952 SCJbSetString(js,
"track",
"ip_pair");
955 SCJbSetString(js,
"track",
"tx");
959 SCJbSetUint(js,
"expire", xd->
expire);
966 SCJbOpenObject(js,
"flowint");
969 SCJbSetString(js,
"cmd",
"set");
972 SCJbSetString(js,
"cmd",
"add");
975 SCJbSetString(js,
"cmd",
"sub");
978 SCJbSetString(js,
"cmd",
"lt");
981 SCJbSetString(js,
"cmd",
"lte");
984 SCJbSetString(js,
"cmd",
"eq");
987 SCJbSetString(js,
"cmd",
"ne");
990 SCJbSetString(js,
"cmd",
"gte");
993 SCJbSetString(js,
"cmd",
"gt");
996 SCJbSetString(js,
"cmd",
"isset");
999 SCJbSetString(js,
"cmd",
"isnotset");
1003 if (varname != NULL) {
1004 SCJbSetString(js,
"var", varname);
1018 SCJbOpenObject(js,
"ack");
1019 SCDetectU32ToJson(js, cd);
1025 SCJbOpenObject(js,
"seq");
1026 SCDetectU32ToJson(js, cd);
1032 SCJbOpenObject(js,
"tcp_mss");
1033 SCDetectU16ToJson(js, cd);
1039 SCJbOpenObject(js,
"dsize");
1040 SCDetectU16ToJson(js, cd);
1046 SCJbOpenObject(js,
"code");
1047 SCDetectU8ToJson(js, cd);
1053 SCJbOpenObject(js,
"ttl");
1054 SCDetectU8ToJson(js, cd);
1060 SCJbOpenObject(js,
"id");
1061 SCDetectU16ToJson(js, cd);
1067 SCJbOpenObject(js,
"window");
1068 SCDetectU16ToJson(js, cd);
1074 SCJbOpenObject(js,
"flow_age");
1075 SCDetectU32ToJson(js, cd);
1096 ctx.js = SCJbNewObject();
1107 SCJbSetUint(
ctx.js,
"id", s->
id);
1108 SCJbSetUint(
ctx.js,
"gid", s->
gid);
1109 SCJbSetUint(
ctx.js,
"rev", s->
rev);
1110 SCJbSetString(
ctx.js,
"msg", s->
msg);
1113 SCJbSetString(
ctx.js,
"app_proto", alproto);
1115 SCJbOpenArray(
ctx.js,
"requirements");
1117 SCJbAppendString(
ctx.js,
"payload");
1120 SCJbAppendString(
ctx.js,
"no_payload");
1123 SCJbAppendString(
ctx.js,
"flow");
1126 SCJbAppendString(
ctx.js,
"tcp_flags_init_deinit");
1129 SCJbAppendString(
ctx.js,
"tcp_flags_unusual");
1132 SCJbAppendString(
ctx.js,
"engine_event");
1135 SCJbAppendString(
ctx.js,
"real_pkt");
1139 SCJbOpenObject(
ctx.js,
"match_policy");
1140 SCJbOpenArray(
ctx.js,
"actions");
1142 SCJbAppendString(
ctx.js,
"alert");
1145 SCJbAppendString(
ctx.js,
"drop");
1148 SCJbAppendString(
ctx.js,
"reject");
1151 SCJbAppendString(
ctx.js,
"reject_dst");
1154 SCJbAppendString(
ctx.js,
"reject_both");
1157 SCJbAppendString(
ctx.js,
"config");
1160 SCJbAppendString(
ctx.js,
"pass");
1163 SCJbAppendString(
ctx.js,
"accept");
1169 switch (flow_action) {
1171 SCJbSetString(
ctx.js,
"scope",
"packet");
1174 SCJbSetString(
ctx.js,
"scope",
"flow");
1177 SCJbSetString(
ctx.js,
"scope",
"flow_if_stateful");
1184 SCJbSetString(
ctx.js,
"scope",
"packet");
1187 SCJbSetString(
ctx.js,
"scope",
"flow");
1190 SCJbSetString(
ctx.js,
"scope",
"hook");
1193 SCJbSetString(
ctx.js,
"scope",
"tx");
1203 SCJbSetString(
ctx.js,
"type",
"unset");
1206 SCJbSetString(
ctx.js,
"type",
"ip_only");
1209 SCJbSetString(
ctx.js,
"type",
"like_ip_only");
1212 SCJbSetString(
ctx.js,
"type",
"pd_only");
1215 SCJbSetString(
ctx.js,
"type",
"de_only");
1218 SCJbSetString(
ctx.js,
"type",
"pkt");
1221 SCJbSetString(
ctx.js,
"type",
"pkt_stream");
1224 SCJbSetString(
ctx.js,
"type",
"stream");
1227 SCJbSetString(
ctx.js,
"type",
"app_layer");
1230 SCJbSetString(
ctx.js,
"type",
"app_tx");
1233 SCJbSetString(
ctx.js,
"type",
"error");
1239 SCJbOpenObject(
ctx.js,
"dependencies");
1240 SCJbOpenObject(
ctx.js,
"flowbits");
1241 SCJbOpenObject(
ctx.js,
"upstream");
1243 SCJbOpenObject(
ctx.js,
"state_modifying_rules");
1244 SCJbOpenArray(
ctx.js,
"sids");
1249 SCJbOpenArray(
ctx.js,
"names");
1252 SCJbAppendString(
ctx.js,
1265 SCJbOpenArray(
ctx.js,
"flags");
1267 SCJbAppendString(
ctx.js,
"src_any");
1270 SCJbAppendString(
ctx.js,
"dst_any");
1273 SCJbAppendString(
ctx.js,
"sp_any");
1276 SCJbAppendString(
ctx.js,
"dp_any");
1279 SCJbAppendString(
ctx.js,
"noalert");
1282 SCJbAppendString(
ctx.js,
"dsize");
1285 SCJbAppendString(
ctx.js,
"applayer");
1288 SCJbAppendString(
ctx.js,
"need_packet");
1291 SCJbAppendString(
ctx.js,
"need_stream");
1294 SCJbAppendString(
ctx.js,
"negated_mpm");
1297 SCJbAppendString(
ctx.js,
"flush");
1300 SCJbAppendString(
ctx.js,
"need_flowvar");
1303 SCJbAppendString(
ctx.js,
"filestore");
1306 SCJbAppendString(
ctx.js,
"toserver");
1309 SCJbAppendString(
ctx.js,
"toclient");
1312 SCJbAppendString(
ctx.js,
"tlsstore");
1315 SCJbAppendString(
ctx.js,
"bypass");
1318 SCJbAppendString(
ctx.js,
"prefilter");
1321 SCJbAppendString(
ctx.js,
"src_is_target");
1324 SCJbAppendString(
ctx.js,
"dst_is_target");
1331 SCJbOpenArray(
ctx.js,
"pkt_engines");
1333 for ( ; pkt != NULL; pkt = pkt->
next) {
1348 SCJbStartObject(
ctx.js);
1349 SCJbSetString(
ctx.js,
"name",
name);
1350 SCJbSetBool(
ctx.js,
"is_mpm", pkt->
mpm);
1352 SCJbOpenArray(
ctx.js,
"transforms");
1354 SCJbStartObject(
ctx.js);
1355 SCJbSetString(
ctx.js,
"name",
1368 SCJbOpenArray(
ctx.js,
"frame_engines");
1370 for (; frame != NULL; frame = frame->
next) {
1372 SCJbStartObject(
ctx.js);
1373 SCJbSetString(
ctx.js,
"name",
name);
1374 SCJbSetBool(
ctx.js,
"is_mpm", frame->
mpm);
1376 SCJbOpenArray(
ctx.js,
"transforms");
1378 SCJbStartObject(
ctx.js);
1379 SCJbSetString(
ctx.js,
"name",
1391 bool has_stream =
false;
1392 bool has_client_body_mpm =
false;
1393 bool has_file_data_mpm =
false;
1395 SCJbOpenArray(
ctx.js,
"engines");
1397 for ( ; app != NULL; app = app->
next) {
1412 }
else if (app->
mpm && strcmp(
name,
"http_client_body") == 0) {
1413 has_client_body_mpm =
true;
1414 }
else if (app->
mpm && strcmp(
name,
"file_data") == 0) {
1415 has_file_data_mpm =
true;
1418 SCJbStartObject(
ctx.js);
1419 SCJbSetString(
ctx.js,
"name",
name);
1420 const char *direction = app->
dir == 0 ?
"toserver" :
"toclient";
1421 SCJbSetString(
ctx.js,
"direction", direction);
1422 SCJbSetBool(
ctx.js,
"is_mpm", app->
mpm);
1427 SCJbOpenArray(
ctx.js,
"transforms");
1429 SCJbStartObject(
ctx.js);
1430 SCJbSetString(
ctx.js,
"name",
1444 if (has_stream && has_client_body_mpm)
1445 AnalyzerNote(&
ctx, (
char *)
"mpm in http_client_body combined with stream match leads to stream buffering");
1446 if (has_stream && has_file_data_mpm)
1447 AnalyzerNote(&
ctx, (
char *)
"mpm in file_data combined with stream match leads to stream buffering");
1450 SCJbOpenObject(
ctx.js,
"lists");
1460 if (pkt_mpm || app_mpm) {
1461 SCJbOpenObject(
ctx.js,
"mpm");
1469 SCJbSetString(
ctx.js,
"buffer",
name);
1476 switch (smd->
type) {
1480 DumpContent(
ctx.js, cd);
1492 SCJbOpenObject(
ctx.js,
"prefilter");
1499 SCJbSetString(
ctx.js,
"buffer",
name);
1501 SCJbSetString(
ctx.js,
"name", mname);
1505 if (
ctx.js_warnings) {
1506 SCJbClose(
ctx.js_warnings);
1507 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
1508 SCJbFree(
ctx.js_warnings);
1509 ctx.js_warnings = NULL;
1512 SCJbClose(
ctx.js_notes);
1513 SCJbSetObject(
ctx.js,
"notes",
ctx.js_notes);
1514 SCJbFree(
ctx.js_notes);
1515 ctx.js_notes = NULL;
1519 const char *filename =
"rules.json";
1521 char json_path[PATH_MAX] =
"";
1522 snprintf(json_path,
sizeof(json_path),
"%s/%s%s", log_dir,
1526 FILE *fp = fopen(json_path,
"a");
1528 fwrite(SCJbPtr(
ctx.js), SCJbLen(
ctx.js), 1, fp);
1542 SCJsonBuilder *root_jb = SCJbNewObject();
1546 SCJbOpenArray(root_jb,
"buffers");
1550 char str[1024] =
"";
1554 SCJsonBuilder *jb = arrays[p->
sm_list];
1555 if (arrays[p->
sm_list] == NULL) {
1556 jb = arrays[p->
sm_list] = SCJbNewObject();
1562 SCJbSetString(jb,
"name",
name);
1563 SCJbSetUint(jb,
"list_id", p->
sm_list);
1565 SCJbOpenArray(jb,
"patterns");
1568 SCJbStartObject(jb);
1569 SCJbSetString(jb,
"pattern",
str);
1571 SCJbSetUint(jb,
"cnt", p->
cnt);
1572 SCJbSetUint(jb,
"mpm", p->
mpm);
1573 SCJbOpenObject(jb,
"flags");
1584 SCJsonBuilder *jb = arrays[i];
1591 SCJbAppendObject(root_jb, jb);
1597 const char *filename =
"patterns.json";
1599 char json_path[PATH_MAX] =
"";
1600 snprintf(json_path,
sizeof(json_path),
"%s/%s%s", log_dir,
1604 FILE *fp = fopen(json_path,
"a");
1606 fwrite(SCJbPtr(root_jb), SCJbLen(root_jb), 1, fp);
1627 EngineAnalysisItemsReset(ea_ctx);
1635 FatalError(
"Unable to allocate analysis scratch pad");
1645 analyzer_item->
item_id = (uint16_t)item_id;
1646 if (analyzer_item->
item_id == -1) {
1648 FatalError(
"unable to initialize engine-analysis table: detect buffer \"%s\" not "
1681 uint32_t rule_bidirectional = 0;
1682 uint32_t rule_pcre = 0;
1683 uint32_t rule_pcre_http = 0;
1684 uint32_t rule_content = 0;
1685 uint32_t rule_flow = 0;
1686 uint32_t rule_flags = 0;
1687 uint32_t rule_flow_toserver = 0;
1688 uint32_t rule_flow_toclient = 0;
1689 uint32_t rule_flow_nostream = 0;
1690 uint32_t rule_ipv4_only = 0;
1691 uint32_t rule_ipv6_only = 0;
1692 uint32_t rule_flowbits = 0;
1693 uint32_t rule_flowint = 0;
1694 uint32_t rule_content_http = 0;
1695 uint32_t rule_content_offset_depth = 0;
1696 int32_t list_id = 0;
1697 uint32_t rule_warning = 0;
1698 uint32_t stream_buf = 0;
1699 uint32_t packet_buf = 0;
1700 uint32_t file_store = 0;
1701 uint32_t warn_pcre_no_content = 0;
1702 uint32_t warn_pcre_http_content = 0;
1703 uint32_t warn_pcre_http = 0;
1704 uint32_t warn_content_http_content = 0;
1705 uint32_t warn_content_http = 0;
1706 uint32_t warn_tcp_no_flow = 0;
1707 uint32_t warn_client_ports = 0;
1708 uint32_t warn_direction = 0;
1709 uint32_t warn_method_toclient = 0;
1710 uint32_t warn_method_serverbody = 0;
1711 uint32_t warn_pcre_method = 0;
1712 uint32_t warn_encoding_norm_http_buf = 0;
1713 uint32_t warn_file_store_not_present = 0;
1714 uint32_t warn_offset_depth_pkt_stream = 0;
1715 uint32_t warn_offset_depth_alproto = 0;
1716 uint32_t warn_non_alproto_fp_for_alproto_sig = 0;
1717 uint32_t warn_no_direction = 0;
1718 uint32_t warn_both_direction = 0;
1720 EngineAnalysisItemsInit(
de_ctx->
ea);
1726 rule_bidirectional = 1;
1740 rule_ipv4_only += 1;
1743 rule_ipv6_only += 1;
1751 if (item_slot == -1) {
1759 if (item_slot == -1) {
1764 rule_content_offset_depth++;
1770 rule_content_http++;
1777 warn_encoding_norm_http_buf += 1;
1784 rule_flow_toserver = 1;
1787 rule_flow_toclient = 1;
1792 rule_flow_nostream = 1;
1806 if (sm->
ctx != NULL) {
1816 warn_file_store_not_present = 1;
1819 if (rule_pcre > 0 && rule_content == 0 && rule_content_http == 0) {
1821 warn_pcre_no_content = 1;
1824 if (rule_content_http > 0 && rule_pcre > 0 && rule_pcre_http == 0) {
1826 warn_pcre_http_content = 1;
1832 if (rule_content > 0 && rule_content_http > 0) {
1834 warn_content_http_content = 1;
1838 warn_content_http = 1;
1840 if (rule_content == 1) {
1845 (rule_content || rule_content_http || rule_pcre || rule_pcre_http || rule_flowbits ||
1848 warn_tcp_no_flow = 1;
1850 if (rule_flow && !rule_bidirectional && (rule_flow_toserver || rule_flow_toclient)
1855 warn_client_ports = 1;
1858 if (rule_flow && rule_bidirectional && (rule_flow_toserver || rule_flow_toclient)) {
1863 if (*http_method_item_seen_ptr) {
1864 if (rule_flow && rule_flow_toclient) {
1866 warn_method_toclient = 1;
1868 if (*http_server_body_item_seen_ptr) {
1870 warn_method_serverbody = 1;
1872 if (rule_content == 0 && rule_content_http == 0 && (rule_pcre > 0 || rule_pcre_http > 0)) {
1874 warn_pcre_method = 1;
1877 if (rule_content_offset_depth > 0 && stream_buf && packet_buf) {
1879 warn_offset_depth_pkt_stream = 1;
1883 warn_offset_depth_alproto = 1;
1888 warn_non_alproto_fp_for_alproto_sig = 1;
1892 warn_no_direction += 1;
1899 warn_both_direction += 1;
1904 if (!rule_warnings_only || (rule_warnings_only && rule_warning > 0)) {
1906 fprintf(fp,
"== Sid: %u ==\n", s->
id);
1907 fprintf(fp,
"%s\n", line);
1913 fprintf(fp,
" Rule is ip only.\n");
1916 fprintf(fp,
" Rule is like ip only.\n");
1919 fprintf(fp,
" Rule is PD only.\n");
1922 fprintf(fp,
" Rule is DE only.\n");
1925 fprintf(fp,
" Rule is packet inspecting.\n");
1928 fprintf(fp,
" Rule is packet and stream inspecting.\n");
1931 fprintf(fp,
" Rule is stream inspecting.\n");
1934 fprintf(fp,
" Rule is app-layer inspecting.\n");
1937 fprintf(fp,
" Rule is App-layer TX inspecting.\n");
1943 fprintf(fp,
" Rule is IPv6 only.\n");
1945 fprintf(fp,
" Rule is IPv4 only.\n");
1947 fprintf(fp,
" Rule matches on packets.\n");
1948 if (!rule_flow_nostream && stream_buf &&
1949 (rule_flow || rule_flowbits || rule_flowint || rule_content || rule_pcre)) {
1950 fprintf(fp,
" Rule matches on reassembled stream.\n");
1955 fprintf(fp,
" Rule matches on %s buffer.\n", ai->
display_name);
1961 if (rule_content || rule_content_http || rule_pcre || rule_pcre_http) {
1963 " Rule contains %u content options, %u http content options, %u pcre "
1964 "options, and %u pcre options with http modifiers.\n",
1965 rule_content, rule_content_http, rule_pcre, rule_pcre_http);
1970 fprintf(fp,
" Prefilter on: %s.\n",
1973 EngineAnalysisRulesPrintFP(
de_ctx, s);
1977 if (warn_pcre_no_content ) {
1978 fprintf(fp,
" Warning: Rule uses pcre without a content option present.\n"
1979 " -Consider adding a content to improve performance of this "
1982 if (warn_pcre_http_content ) {
1983 fprintf(fp,
" Warning: Rule uses content options with http_* and pcre options "
1984 "without http modifiers.\n"
1985 " -Consider adding http pcre modifier.\n");
1987 else if (warn_pcre_http ) {
1988 fprintf(fp,
" Warning: Rule app layer protocol is http, but pcre options do not "
1989 "have http modifiers.\n"
1990 " -Consider adding http pcre modifiers.\n");
1992 if (warn_content_http_content ) {
1994 " Warning: Rule contains content with http_* and content without http_*.\n"
1995 " -Consider adding http content modifiers.\n");
1997 if (warn_content_http ) {
1998 fprintf(fp,
" Warning: Rule app layer protocol is http, but content options do not "
1999 "have http_* modifiers.\n"
2000 " -Consider adding http content modifiers.\n");
2002 if (rule_content == 1) {
2005 if (warn_encoding_norm_http_buf) {
2006 fprintf(fp,
" Warning: Rule may contain percent encoded content for a normalized "
2007 "http buffer match.\n");
2009 if (warn_tcp_no_flow
2011 fprintf(fp,
" Warning: TCP rule without a flow or flags option.\n"
2012 " -Consider adding flow or flags to improve performance of "
2015 if (warn_client_ports
2020 " Warning: Rule contains ports or port variables only on the client side.\n"
2021 " -Flow direction possibly inconsistent with rule.\n");
2023 if (warn_direction ) {
2024 fprintf(fp,
" Warning: Rule is bidirectional and has a flow option with a specific "
2027 if (warn_method_toclient ) {
2028 fprintf(fp,
" Warning: Rule uses content or pcre for http_method with "
2029 "flow:to_client or from_server\n");
2031 if (warn_method_serverbody ) {
2032 fprintf(fp,
" Warning: Rule uses content or pcre for http_method with content or "
2033 "pcre for http_server_body.\n");
2035 if (warn_pcre_method
2037 fprintf(fp,
" Warning: Rule uses pcre with only a http_method content; possible "
2038 "performance issue.\n");
2040 if (warn_offset_depth_pkt_stream) {
2041 fprintf(fp,
" Warning: Rule has depth"
2042 "/offset with raw content keywords. Please note the "
2043 "offset/depth will be checked against both packet "
2044 "payloads and stream. If you meant to have the offset/"
2045 "depth checked against just the payload, you can update "
2046 "the signature as \"alert tcp-pkt...\"\n");
2048 if (warn_offset_depth_alproto) {
2050 " Warning: Rule has "
2051 "offset/depth set along with a match on a specific "
2052 "app layer protocol - %d. This can lead to FNs if we "
2053 "have a offset/depth content match on a packet payload "
2054 "before we can detect the app layer protocol for the "
2058 if (warn_non_alproto_fp_for_alproto_sig) {
2059 fprintf(fp,
" Warning: Rule app layer "
2060 "protocol is http, but the fast_pattern is set on the raw "
2061 "stream. Consider adding fast_pattern over a http "
2062 "buffer for increased performance.");
2064 if (warn_no_direction) {
2065 fprintf(fp,
" Warning: Rule has no direction indicator.\n");
2067 if (warn_both_direction) {
2068 fprintf(fp,
" Warning: Rule is inspecting both the request and the response.\n");
2070 if (warn_file_store_not_present) {
2071 fprintf(fp,
" Warning: Rule requires file-store but the output file-store is not "
2074 if (rule_warning == 0) {
2075 fprintf(fp,
" No warnings for this rule.\n");
2086 uint32_t accept_rules = 0;
2087 SCJbSetString(
ctx->js,
"policy",
"drop:flow");
2088 SCJbOpenArray(
ctx->js,
"rules");
2097 if (direction == STREAM_TOSERVER) {
2114 if (accept_rules == 0) {
2115 AnalyzerWarning(
ctx, (
char *)
"no accept rules for state, default policy will be applied");
2122 ctx.js = SCJbNewObject();
2126 SCJbOpenObject(
ctx.js,
"tables");
2127 SCJbOpenObject(
ctx.js,
"packet:filter");
2128 SCJbSetString(
ctx.js,
"policy",
"drop:packet");
2129 SCJbOpenArray(
ctx.js,
"rules");
2130 uint32_t accept_rules = 0;
2131 uint32_t last_sid = 0;
2138 if (last_sid == s->
id)
2145 if (accept_rules == 0) {
2146 AnalyzerWarning(&
ctx,
2147 (
char *)
"no accept rules for \'packet:filter\', default policy will be applied");
2149 if (
ctx.js_warnings) {
2150 SCJbClose(
ctx.js_warnings);
2151 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
2152 SCJbFree(
ctx.js_warnings);
2153 ctx.js_warnings = NULL;
2158 if (!AppProtoIsValid(a))
2167 const uint8_t complete_state_ts =
2169 for (uint8_t state = 0; state < complete_state_ts; state++) {
2172 char table_name[128];
2174 SCJbOpenObject(
ctx.js, table_name);
2175 FirewallAddRulesForState(
de_ctx, a, state, STREAM_TOSERVER, &
ctx);
2176 if (
ctx.js_warnings) {
2177 SCJbClose(
ctx.js_warnings);
2178 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
2179 SCJbFree(
ctx.js_warnings);
2180 ctx.js_warnings = NULL;
2184 const uint8_t complete_state_tc =
2186 for (uint8_t state = 0; state < complete_state_tc; state++) {
2189 char table_name[128];
2191 SCJbOpenObject(
ctx.js, table_name);
2192 FirewallAddRulesForState(
de_ctx, a, state, STREAM_TOCLIENT, &
ctx);
2193 if (
ctx.js_warnings) {
2194 SCJbClose(
ctx.js_warnings);
2195 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
2196 SCJbFree(
ctx.js_warnings);
2197 ctx.js_warnings = NULL;
2203 SCJbOpenObject(
ctx.js,
"packet:td");
2204 SCJbSetString(
ctx.js,
"policy",
"accept:hook");
2206 SCJbOpenArray(
ctx.js,
"rules");
2212 if (last_sid == s->
id)
2219 SCJbOpenObject(
ctx.js,
"app:td");
2220 SCJbSetString(
ctx.js,
"policy",
"accept:hook");
2222 SCJbOpenArray(
ctx.js,
"rules");
2228 if (last_sid == s->
id)
2237 SCJbOpenObject(
ctx.js,
"lists");
2238 SCJbOpenObject(
ctx.js,
"firewall");
2240 SCJbOpenArray(
ctx.js,
"rules");
2244 if (last_sid == s->
id)
2252 SCJbOpenObject(
ctx.js,
"td");
2254 SCJbOpenArray(
ctx.js,
"rules");
2258 if (last_sid == s->
id)
2266 SCJbOpenObject(
ctx.js,
"all");
2268 SCJbOpenArray(
ctx.js,
"rules");
2270 if (last_sid == s->
id)
2282 const char *filename =
"firewall.json";
2284 char json_path[PATH_MAX] =
"";
2285 snprintf(json_path,
sizeof(json_path),
"%s/%s", log_dir, filename);
2288 FILE *fp = fopen(json_path,
"w");
2290 fwrite(SCJbPtr(
ctx.js), SCJbLen(
ctx.js), 1, fp);