58 static int rule_warnings_only = 0;
114 { 0,
false,
false,
true,
"http_uri",
"http uri" },
115 { 0,
false,
false,
false,
"http_raw_uri",
"http raw uri" },
116 { 0,
false,
true,
false,
"http_method",
"http method" },
117 { 0,
false,
false,
false,
"http_request_line",
"http request line" },
118 { 0,
false,
false,
false,
"http_client_body",
"http client body" },
119 { 0,
false,
false,
true,
"http_header",
"http header" },
120 { 0,
false,
false,
false,
"http_raw_header",
"http raw header" },
121 { 0,
false,
false,
true,
"http_cookie",
"http cookie" },
122 { 0,
false,
false,
false,
"http_user_agent",
"http user agent" },
123 { 0,
false,
false,
false,
"http_host",
"http host" },
124 { 0,
false,
false,
false,
"http_raw_host",
"http raw host" },
125 { 0,
false,
false,
false,
"http_accept_enc",
"http accept enc" },
126 { 0,
false,
false,
false,
"http_referer",
"http referer" },
127 { 0,
false,
false,
false,
"http_content_type",
"http content type" },
128 { 0,
false,
false,
false,
"http_header_names",
"http header names" },
131 { 0,
false,
false,
false,
"http_stat_msg",
"http stat msg" },
132 { 0,
false,
false,
false,
"http_stat_code",
"http stat code" },
133 { 0,
false,
true,
false,
"file_data",
"http server body" },
136 { 0,
false,
false,
false,
"http_request_line",
"http request line" },
137 { 0,
false,
false,
false,
"http_accept",
"http accept" },
138 { 0,
false,
false,
false,
"http_accept_lang",
"http accept lang" },
139 { 0,
false,
false,
false,
"http_connection",
"http connection" },
140 { 0,
false,
false,
false,
"http_content_len",
"http content len" },
141 { 0,
false,
false,
false,
"http_protocol",
"http protocol" },
142 { 0,
false,
false,
false,
"http_start",
"http start" },
145 { 0,
false,
false,
false,
"http_response_line",
"http response line" },
146 { 0,
false,
false,
false,
"http.server",
"http server" },
147 { 0,
false,
false,
false,
"http.location",
"http location" },
150 static void FpPatternStatsAdd(
FpPatternStats *fp,
int list, uint16_t patlen)
159 else if (patlen < f->min)
171 int fast_pattern_set = 0;
172 int fast_pattern_only_set = 0;
173 int fast_pattern_chop_set = 0;
178 if (mpm_sm != NULL) {
181 fast_pattern_set = 1;
183 fast_pattern_only_set = 1;
185 fast_pattern_chop_set = 1;
191 fprintf(fp,
"== Sid: %u ==\n", s->
id);
192 fprintf(fp,
"%s\n", line);
194 fprintf(fp,
" Fast Pattern analysis:\n");
196 fprintf(fp,
" Prefilter on: %s\n",
203 fprintf(fp,
" No content present\n");
208 fprintf(fp,
" Fast pattern matcher: ");
209 int list_type = mpm_sm_list;
211 fprintf(fp,
"content\n");
216 fprintf(fp,
"%s (%s)\n", desc,
name);
221 fprintf(fp,
" Flags:");
223 fprintf(fp,
" Offset");
226 fprintf(fp,
" Depth");
230 fprintf(fp,
" Within");
234 fprintf(fp,
" Distance");
238 fprintf(fp,
" Nocase");
242 fprintf(fp,
" Negated");
246 fprintf(fp,
" None");
249 fprintf(fp,
" Fast pattern set: %s\n", fast_pattern_set ?
"yes" :
"no");
250 fprintf(fp,
" Fast pattern only set: %s\n", fast_pattern_only_set ?
"yes" :
"no");
251 fprintf(fp,
" Fast pattern chop set: %s\n", fast_pattern_chop_set ?
"yes" :
"no");
252 if (fast_pattern_chop_set) {
253 fprintf(fp,
" Fast pattern offset, length: %u, %u\n", fp_cd->
fp_chop_offset,
264 fprintf(fp,
" Original content: ");
268 if (fast_pattern_chop_set) {
277 fprintf(fp,
" Final content: ");
283 fprintf(fp,
" Final content: ");
302 int fp_engine_analysis_set = 0;
304 if ((
SCConfGetBool(
"engine-analysis.rules-fast-pattern", &fp_engine_analysis_set)) == 0) {
308 if (fp_engine_analysis_set == 0)
312 char *log_path =
SCMalloc(PATH_MAX);
313 if (log_path == NULL) {
314 FatalError(
"Unable to allocate scratch memory for rule filename");
316 snprintf(log_path, PATH_MAX,
"%s/%s%s", log_dir,
319 FILE *fp = fopen(log_path,
"w");
321 SCLogError(
"failed to open %s: %s", log_path, strerror(errno));
328 SCLogInfo(
"Engine-Analysis for fast_pattern printed to file - %s",
333 gettimeofday(&tval, NULL);
335 struct tm *tms =
SCLocalTime(tval.tv_sec, &local_tm);
336 fprintf(fp,
"----------------------------------------------"
337 "---------------------\n");
339 "Date: %" PRId32
"/%" PRId32
"/%04d -- "
341 tms->tm_mday, tms->tm_mon + 1, tms->tm_year + 1900, tms->tm_hour, tms->tm_min,
343 fprintf(fp,
"----------------------------------------------"
344 "---------------------\n");
357 #define DETECT_PERCENT_ENCODING_REGEX "%[0-9|a-f|A-F]{2}"
363 PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
365 PCRE2_UCHAR errbuffer[256];
366 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
387 }
else if (value && strcasecmp(value,
"warnings-only") == 0) {
389 rule_warnings_only = 1;
394 char log_path[PATH_MAX];
395 snprintf(log_path,
sizeof(log_path),
"%s/%s%s", log_dir,
399 SCLogError(
"failed to open %s: %s", log_path, strerror(errno));
403 SCLogInfo(
"Engine-Analysis for rules printed to file - %s",
407 gettimeofday(&tval, NULL);
409 struct tm *tms =
SCLocalTime(tval.tv_sec, &local_tm);
411 "----------------------------------------------"
412 "---------------------\n");
414 "Date: %" PRId32
"/%" PRId32
"/%04d -- "
416 tms->tm_mday, tms->tm_mon + 1, tms->tm_year + 1900, tms->tm_hour, tms->tm_min,
419 "----------------------------------------------"
420 "---------------------\n");
423 if (!PerCentEncodingSetup(
de_ctx->
ea)) {
425 "Error compiling regex; can't check for percent encoding in normalized "
431 SCLogInfo(
"Conf parameter \"engine-analysis.rules\" not found. "
432 "Defaulting to not printing the rules analysis report.");
435 SCLogInfo(
"Engine-Analysis for rules disabled in conf file.");
444 fprintf(fp,
"============\n"
445 "Summary:\n============\n");
453 "%s, smallest pattern %u byte(s), longest pattern %u byte(s), number of patterns "
454 "%u, avg pattern len %.2f byte(s)\n",
456 (
float)((
double)f->
tot / (
float)f->
cnt));
476 *fp_analysis =
false;
477 *rule_analysis =
false;
481 FatalError(
"Unable to allocate per-engine analysis context");
486 if (cfg_prefix_len > 0) {
490 FatalError(
"Unable to allocate per-engine analysis context name buffer");
498 *fp_analysis = SetupFPAnalyzer(
de_ctx);
499 *rule_analysis = SetupRuleAnalyzer(
de_ctx);
501 if (!(*fp_analysis || *rule_analysis)) {
513 CleanupRuleAnalyzer(
de_ctx);
514 CleanupFPAnalyzer(
de_ctx);
531 static int PerCentEncodingMatch(
EngineAnalysisCtx *ea_ctx, uint8_t *content, uint16_t content_len)
535 pcre2_match_data *match = pcre2_match_data_create_from_pattern(ea_ctx->
percent_re, NULL);
536 ret = pcre2_match(ea_ctx->
percent_re, (PCRE2_SPTR8)content, content_len, 0, 0, match, NULL);
539 }
else if (ret < -1) {
540 SCLogError(
"Error parsing content - %s; error code is %d", content, ret);
543 pcre2_match_data_free(match);
553 if (mpm_sm != NULL) {
591 const int list_type = mpm_sm_list;
600 payload ? (stream ?
"payload and reassembled stream" :
"payload")
601 :
"reassembled stream");
608 }
else if (desc ||
name) {
628 fprintf(tmp_fp,
"== Sid: UNKNOWN ==\n");
629 fprintf(tmp_fp,
"%s\n", line);
630 fprintf(tmp_fp,
" FAILURE: invalid rule.\n");
631 fprintf(tmp_fp,
" File: %s.\n", file);
632 fprintf(tmp_fp,
" Line: %d.\n", lineno);
633 fprintf(tmp_fp,
"\n");
650 vsnprintf(
str,
sizeof(
str), fmt, ap);
654 ctx->js_notes = SCJbNewArray();
656 SCJbAppendString(
ctx->js_notes,
str);
665 vsnprintf(
str,
sizeof(
str), fmt, ap);
668 if (!
ctx->js_warnings)
669 ctx->js_warnings = SCJbNewArray();
670 if (
ctx->js_warnings)
671 SCJbAppendString(
ctx->js_warnings,
str);
674 #define CHECK(pat) if (strlen((pat)) <= len && memcmp((pat), buf, MIN(len, strlen((pat)))) == 0) return true;
676 static bool LooksLikeHTTPMethod(
const uint8_t *buf, uint16_t
len)
685 static bool LooksLikeHTTPUA(
const uint8_t *buf, uint16_t
len)
687 CHECK(
"User-Agent: ");
688 CHECK(
"\nUser-Agent: ");
694 char pattern_str[1024] =
"";
697 SCJbSetString(js,
"pattern", pattern_str);
706 SCJbSetUint(js,
"offset", cd->
offset);
709 SCJbSetUint(js,
"depth", cd->
depth);
712 SCJbSetInt(js,
"distance", cd->
distance);
715 SCJbSetInt(js,
"within", cd->
within);
734 SCJbOpenArray(js,
"matches");
738 SCJbSetString(js,
"name", mname);
744 SCJbOpenObject(js,
"content");
747 AnalyzerNote(
ctx, (
char *)
"'fast_pattern:only' option is silently ignored and "
748 "is interpreted as regular 'fast_pattern'");
752 (
char *)
"pattern looks like it inspects HTTP, use http.request_line or "
753 "http.method and http.uri instead for improved performance");
757 (
char *)
"pattern looks like it inspects HTTP, use http.user_agent "
758 "or http.header for improved performance");
761 AnalyzerNote(
ctx, (
char *)
"'within' option for pattern w/o previous content "
762 "was converted to 'depth'");
765 AnalyzerNote(
ctx, (
char *)
"'distance' option for pattern w/o previous content "
766 "was converted to 'offset'");
774 SCJbOpenObject(js,
"pcre");
779 (
char *)
"'/B' (rawbytes) option is a no-op and is silently ignored");
786 SCJbOpenObject(js,
"byte_jump");
787 SCJbSetUint(js,
"nbytes", cd->
nbytes);
788 SCJbSetInt(js,
"offset", cd->
offset);
789 SCJbSetUint(js,
"multiplier", cd->
multiplier);
793 SCJbSetString(js,
"base",
"unset");
796 SCJbSetString(js,
"base",
"oct");
799 SCJbSetString(js,
"base",
"dec");
802 SCJbSetString(js,
"base",
"hex");
805 SCJbOpenArray(js,
"flags");
807 SCJbAppendString(js,
"from_beginning");
809 SCJbAppendString(js,
"little_endian");
811 SCJbAppendString(js,
"big_endian");
813 SCJbAppendString(js,
"string");
815 SCJbAppendString(js,
"relative");
817 SCJbAppendString(js,
"align");
819 SCJbAppendString(js,
"dce");
821 SCJbAppendString(js,
"offset_be");
823 SCJbAppendString(js,
"from_end");
831 SCJbOpenObject(js,
"byte_test");
832 SCJbSetUint(js,
"nbytes", cd->
nbytes);
833 SCJbSetInt(js,
"offset", cd->
offset);
836 SCJbSetString(js,
"base",
"unset");
839 SCJbSetString(js,
"base",
"oct");
842 SCJbSetString(js,
"base",
"dec");
845 SCJbSetString(js,
"base",
"hex");
848 SCJbOpenArray(js,
"flags");
850 SCJbAppendString(js,
"little_endian");
852 SCJbAppendString(js,
"big_endian");
854 SCJbAppendString(js,
"string");
856 SCJbAppendString(js,
"relative");
858 SCJbAppendString(js,
"dce");
865 SCJbOpenObject(js,
"absent");
866 SCJbSetBool(js,
"or_else", dad->
or_else);
874 SCJbOpenObject(js,
"ipopts");
876 SCJbSetString(js,
"option", flag);
883 SCJbOpenObject(js,
"flowbits");
886 SCJbSetString(js,
"cmd",
"isset");
889 SCJbSetString(js,
"cmd",
"isnotset");
892 SCJbSetString(js,
"cmd",
"set");
895 SCJbSetString(js,
"cmd",
"unset");
898 SCJbSetString(js,
"cmd",
"toggle");
902 SCJbOpenArray(js,
"names");
908 const char *varname =
910 SCJbAppendString(js, varname);
915 SCJbSetString(js,
"operator",
"or");
923 SCJbOpenObject(js,
"ack");
924 SCJbSetUint(js,
"number", cd->
ack);
930 SCJbOpenObject(js,
"seq");
931 SCJbSetUint(js,
"number", cd->
seq);
937 SCJbOpenObject(js,
"tcp_mss");
938 SCDetectU16ToJson(js, cd);
944 SCJbOpenObject(js,
"dsize");
945 SCDetectU16ToJson(js, cd);
951 SCJbOpenObject(js,
"code");
952 SCDetectU8ToJson(js, cd);
958 SCJbOpenObject(js,
"id");
959 SCJbSetUint(js,
"number",
SCNtohs(cd->
id));
965 SCJbOpenObject(js,
"window");
966 SCJbSetUint(js,
"size", wd->
size);
967 SCJbSetBool(js,
"negated", wd->
negated);
973 SCJbOpenObject(js,
"flow_age");
974 SCDetectU32ToJson(js, cd);
995 ctx.js = SCJbNewObject();
1006 SCJbSetUint(
ctx.js,
"id", s->
id);
1007 SCJbSetUint(
ctx.js,
"gid", s->
gid);
1008 SCJbSetUint(
ctx.js,
"rev", s->
rev);
1009 SCJbSetString(
ctx.js,
"msg", s->
msg);
1012 SCJbSetString(
ctx.js,
"app_proto", alproto);
1014 SCJbOpenArray(
ctx.js,
"requirements");
1016 SCJbAppendString(
ctx.js,
"payload");
1019 SCJbAppendString(
ctx.js,
"no_payload");
1022 SCJbAppendString(
ctx.js,
"flow");
1025 SCJbAppendString(
ctx.js,
"tcp_flags_init_deinit");
1028 SCJbAppendString(
ctx.js,
"tcp_flags_unusual");
1031 SCJbAppendString(
ctx.js,
"engine_event");
1034 SCJbAppendString(
ctx.js,
"real_pkt");
1038 SCJbOpenObject(
ctx.js,
"match_policy");
1039 SCJbOpenArray(
ctx.js,
"actions");
1041 SCJbAppendString(
ctx.js,
"alert");
1044 SCJbAppendString(
ctx.js,
"drop");
1047 SCJbAppendString(
ctx.js,
"reject");
1050 SCJbAppendString(
ctx.js,
"reject_dst");
1053 SCJbAppendString(
ctx.js,
"reject_both");
1056 SCJbAppendString(
ctx.js,
"config");
1059 SCJbAppendString(
ctx.js,
"pass");
1062 SCJbAppendString(
ctx.js,
"accept");
1068 switch (flow_action) {
1070 SCJbSetString(
ctx.js,
"scope",
"packet");
1073 SCJbSetString(
ctx.js,
"scope",
"flow");
1076 SCJbSetString(
ctx.js,
"scope",
"flow_if_stateful");
1083 SCJbSetString(
ctx.js,
"scope",
"packet");
1086 SCJbSetString(
ctx.js,
"scope",
"flow");
1089 SCJbSetString(
ctx.js,
"scope",
"hook");
1092 SCJbSetString(
ctx.js,
"scope",
"tx");
1102 SCJbSetString(
ctx.js,
"type",
"unset");
1105 SCJbSetString(
ctx.js,
"type",
"ip_only");
1108 SCJbSetString(
ctx.js,
"type",
"like_ip_only");
1111 SCJbSetString(
ctx.js,
"type",
"pd_only");
1114 SCJbSetString(
ctx.js,
"type",
"de_only");
1117 SCJbSetString(
ctx.js,
"type",
"pkt");
1120 SCJbSetString(
ctx.js,
"type",
"pkt_stream");
1123 SCJbSetString(
ctx.js,
"type",
"stream");
1126 SCJbSetString(
ctx.js,
"type",
"app_layer");
1129 SCJbSetString(
ctx.js,
"type",
"app_tx");
1132 SCJbSetString(
ctx.js,
"type",
"error");
1138 SCJbOpenObject(
ctx.js,
"dependencies");
1139 SCJbOpenObject(
ctx.js,
"flowbits");
1140 SCJbOpenObject(
ctx.js,
"upstream");
1142 SCJbOpenObject(
ctx.js,
"state_modifying_rules");
1143 SCJbOpenArray(
ctx.js,
"sids");
1148 SCJbOpenArray(
ctx.js,
"names");
1151 SCJbAppendString(
ctx.js,
1164 SCJbOpenArray(
ctx.js,
"flags");
1166 SCJbAppendString(
ctx.js,
"src_any");
1169 SCJbAppendString(
ctx.js,
"dst_any");
1172 SCJbAppendString(
ctx.js,
"sp_any");
1175 SCJbAppendString(
ctx.js,
"dp_any");
1178 SCJbAppendString(
ctx.js,
"noalert");
1181 SCJbAppendString(
ctx.js,
"dsize");
1184 SCJbAppendString(
ctx.js,
"applayer");
1187 SCJbAppendString(
ctx.js,
"need_packet");
1190 SCJbAppendString(
ctx.js,
"need_stream");
1193 SCJbAppendString(
ctx.js,
"negated_mpm");
1196 SCJbAppendString(
ctx.js,
"flush");
1199 SCJbAppendString(
ctx.js,
"need_flowvar");
1202 SCJbAppendString(
ctx.js,
"filestore");
1205 SCJbAppendString(
ctx.js,
"toserver");
1208 SCJbAppendString(
ctx.js,
"toclient");
1211 SCJbAppendString(
ctx.js,
"tlsstore");
1214 SCJbAppendString(
ctx.js,
"bypass");
1217 SCJbAppendString(
ctx.js,
"prefilter");
1220 SCJbAppendString(
ctx.js,
"src_is_target");
1223 SCJbAppendString(
ctx.js,
"dst_is_target");
1230 SCJbOpenArray(
ctx.js,
"pkt_engines");
1232 for ( ; pkt != NULL; pkt = pkt->
next) {
1247 SCJbStartObject(
ctx.js);
1248 SCJbSetString(
ctx.js,
"name",
name);
1249 SCJbSetBool(
ctx.js,
"is_mpm", pkt->
mpm);
1251 SCJbOpenArray(
ctx.js,
"transforms");
1253 SCJbStartObject(
ctx.js);
1254 SCJbSetString(
ctx.js,
"name",
1267 SCJbOpenArray(
ctx.js,
"frame_engines");
1269 for (; frame != NULL; frame = frame->
next) {
1271 SCJbStartObject(
ctx.js);
1272 SCJbSetString(
ctx.js,
"name",
name);
1273 SCJbSetBool(
ctx.js,
"is_mpm", frame->
mpm);
1275 SCJbOpenArray(
ctx.js,
"transforms");
1277 SCJbStartObject(
ctx.js);
1278 SCJbSetString(
ctx.js,
"name",
1290 bool has_stream =
false;
1291 bool has_client_body_mpm =
false;
1292 bool has_file_data_mpm =
false;
1294 SCJbOpenArray(
ctx.js,
"engines");
1296 for ( ; app != NULL; app = app->
next) {
1311 }
else if (app->
mpm && strcmp(
name,
"http_client_body") == 0) {
1312 has_client_body_mpm =
true;
1313 }
else if (app->
mpm && strcmp(
name,
"file_data") == 0) {
1314 has_file_data_mpm =
true;
1317 SCJbStartObject(
ctx.js);
1318 SCJbSetString(
ctx.js,
"name",
name);
1319 const char *direction = app->
dir == 0 ?
"toserver" :
"toclient";
1320 SCJbSetString(
ctx.js,
"direction", direction);
1321 SCJbSetBool(
ctx.js,
"is_mpm", app->
mpm);
1326 SCJbOpenArray(
ctx.js,
"transforms");
1328 SCJbStartObject(
ctx.js);
1329 SCJbSetString(
ctx.js,
"name",
1343 if (has_stream && has_client_body_mpm)
1344 AnalyzerNote(&
ctx, (
char *)
"mpm in http_client_body combined with stream match leads to stream buffering");
1345 if (has_stream && has_file_data_mpm)
1346 AnalyzerNote(&
ctx, (
char *)
"mpm in file_data combined with stream match leads to stream buffering");
1349 SCJbOpenObject(
ctx.js,
"lists");
1359 if (pkt_mpm || app_mpm) {
1360 SCJbOpenObject(
ctx.js,
"mpm");
1368 SCJbSetString(
ctx.js,
"buffer",
name);
1375 switch (smd->
type) {
1379 DumpContent(
ctx.js, cd);
1391 SCJbOpenObject(
ctx.js,
"prefilter");
1398 SCJbSetString(
ctx.js,
"buffer",
name);
1400 SCJbSetString(
ctx.js,
"name", mname);
1404 if (
ctx.js_warnings) {
1405 SCJbClose(
ctx.js_warnings);
1406 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
1407 SCJbFree(
ctx.js_warnings);
1408 ctx.js_warnings = NULL;
1411 SCJbClose(
ctx.js_notes);
1412 SCJbSetObject(
ctx.js,
"notes",
ctx.js_notes);
1413 SCJbFree(
ctx.js_notes);
1414 ctx.js_notes = NULL;
1418 const char *filename =
"rules.json";
1420 char json_path[PATH_MAX] =
"";
1421 snprintf(json_path,
sizeof(json_path),
"%s/%s%s", log_dir,
1425 FILE *fp = fopen(json_path,
"a");
1427 fwrite(SCJbPtr(
ctx.js), SCJbLen(
ctx.js), 1, fp);
1441 SCJsonBuilder *root_jb = SCJbNewObject();
1445 SCJbOpenArray(root_jb,
"buffers");
1449 char str[1024] =
"";
1453 SCJsonBuilder *jb = arrays[p->
sm_list];
1454 if (arrays[p->
sm_list] == NULL) {
1455 jb = arrays[p->
sm_list] = SCJbNewObject();
1461 SCJbSetString(jb,
"name",
name);
1462 SCJbSetUint(jb,
"list_id", p->
sm_list);
1464 SCJbOpenArray(jb,
"patterns");
1467 SCJbStartObject(jb);
1468 SCJbSetString(jb,
"pattern",
str);
1470 SCJbSetUint(jb,
"cnt", p->
cnt);
1471 SCJbSetUint(jb,
"mpm", p->
mpm);
1472 SCJbOpenObject(jb,
"flags");
1483 SCJsonBuilder *jb = arrays[i];
1490 SCJbAppendObject(root_jb, jb);
1496 const char *filename =
"patterns.json";
1498 char json_path[PATH_MAX] =
"";
1499 snprintf(json_path,
sizeof(json_path),
"%s/%s%s", log_dir,
1503 FILE *fp = fopen(json_path,
"a");
1505 fwrite(SCJbPtr(root_jb), SCJbLen(root_jb), 1, fp);
1526 EngineAnalysisItemsReset(ea_ctx);
1534 FatalError(
"Unable to allocate analysis scratch pad");
1544 analyzer_item->
item_id = (uint16_t)item_id;
1545 if (analyzer_item->
item_id == -1) {
1547 FatalError(
"unable to initialize engine-analysis table: detect buffer \"%s\" not "
1580 uint32_t rule_bidirectional = 0;
1581 uint32_t rule_pcre = 0;
1582 uint32_t rule_pcre_http = 0;
1583 uint32_t rule_content = 0;
1584 uint32_t rule_flow = 0;
1585 uint32_t rule_flags = 0;
1586 uint32_t rule_flow_toserver = 0;
1587 uint32_t rule_flow_toclient = 0;
1588 uint32_t rule_flow_nostream = 0;
1589 uint32_t rule_ipv4_only = 0;
1590 uint32_t rule_ipv6_only = 0;
1591 uint32_t rule_flowbits = 0;
1592 uint32_t rule_flowint = 0;
1593 uint32_t rule_content_http = 0;
1594 uint32_t rule_content_offset_depth = 0;
1595 int32_t list_id = 0;
1596 uint32_t rule_warning = 0;
1597 uint32_t stream_buf = 0;
1598 uint32_t packet_buf = 0;
1599 uint32_t file_store = 0;
1600 uint32_t warn_pcre_no_content = 0;
1601 uint32_t warn_pcre_http_content = 0;
1602 uint32_t warn_pcre_http = 0;
1603 uint32_t warn_content_http_content = 0;
1604 uint32_t warn_content_http = 0;
1605 uint32_t warn_tcp_no_flow = 0;
1606 uint32_t warn_client_ports = 0;
1607 uint32_t warn_direction = 0;
1608 uint32_t warn_method_toclient = 0;
1609 uint32_t warn_method_serverbody = 0;
1610 uint32_t warn_pcre_method = 0;
1611 uint32_t warn_encoding_norm_http_buf = 0;
1612 uint32_t warn_file_store_not_present = 0;
1613 uint32_t warn_offset_depth_pkt_stream = 0;
1614 uint32_t warn_offset_depth_alproto = 0;
1615 uint32_t warn_non_alproto_fp_for_alproto_sig = 0;
1616 uint32_t warn_no_direction = 0;
1617 uint32_t warn_both_direction = 0;
1619 EngineAnalysisItemsInit(
de_ctx->
ea);
1625 rule_bidirectional = 1;
1639 rule_ipv4_only += 1;
1642 rule_ipv6_only += 1;
1650 if (item_slot == -1) {
1658 if (item_slot == -1) {
1663 rule_content_offset_depth++;
1669 rule_content_http++;
1676 warn_encoding_norm_http_buf += 1;
1683 rule_flow_toserver = 1;
1686 rule_flow_toclient = 1;
1691 rule_flow_nostream = 1;
1716 warn_file_store_not_present = 1;
1719 if (rule_pcre > 0 && rule_content == 0 && rule_content_http == 0) {
1721 warn_pcre_no_content = 1;
1724 if (rule_content_http > 0 && rule_pcre > 0 && rule_pcre_http == 0) {
1726 warn_pcre_http_content = 1;
1732 if (rule_content > 0 && rule_content_http > 0) {
1734 warn_content_http_content = 1;
1738 warn_content_http = 1;
1740 if (rule_content == 1) {
1745 (rule_content || rule_content_http || rule_pcre || rule_pcre_http || rule_flowbits ||
1748 warn_tcp_no_flow = 1;
1750 if (rule_flow && !rule_bidirectional && (rule_flow_toserver || rule_flow_toclient)
1755 warn_client_ports = 1;
1758 if (rule_flow && rule_bidirectional && (rule_flow_toserver || rule_flow_toclient)) {
1763 if (*http_method_item_seen_ptr) {
1764 if (rule_flow && rule_flow_toclient) {
1766 warn_method_toclient = 1;
1768 if (*http_server_body_item_seen_ptr) {
1770 warn_method_serverbody = 1;
1772 if (rule_content == 0 && rule_content_http == 0 && (rule_pcre > 0 || rule_pcre_http > 0)) {
1774 warn_pcre_method = 1;
1777 if (rule_content_offset_depth > 0 && stream_buf && packet_buf) {
1779 warn_offset_depth_pkt_stream = 1;
1783 warn_offset_depth_alproto = 1;
1788 warn_non_alproto_fp_for_alproto_sig = 1;
1792 warn_no_direction += 1;
1799 warn_both_direction += 1;
1804 if (!rule_warnings_only || (rule_warnings_only && rule_warning > 0)) {
1806 fprintf(fp,
"== Sid: %u ==\n", s->
id);
1807 fprintf(fp,
"%s\n", line);
1813 fprintf(fp,
" Rule is ip only.\n");
1816 fprintf(fp,
" Rule is like ip only.\n");
1819 fprintf(fp,
" Rule is PD only.\n");
1822 fprintf(fp,
" Rule is DE only.\n");
1825 fprintf(fp,
" Rule is packet inspecting.\n");
1828 fprintf(fp,
" Rule is packet and stream inspecting.\n");
1831 fprintf(fp,
" Rule is stream inspecting.\n");
1834 fprintf(fp,
" Rule is app-layer inspecting.\n");
1837 fprintf(fp,
" Rule is App-layer TX inspecting.\n");
1843 fprintf(fp,
" Rule is IPv6 only.\n");
1845 fprintf(fp,
" Rule is IPv4 only.\n");
1847 fprintf(fp,
" Rule matches on packets.\n");
1848 if (!rule_flow_nostream && stream_buf &&
1849 (rule_flow || rule_flowbits || rule_flowint || rule_content || rule_pcre)) {
1850 fprintf(fp,
" Rule matches on reassembled stream.\n");
1855 fprintf(fp,
" Rule matches on %s buffer.\n", ai->
display_name);
1861 if (rule_content || rule_content_http || rule_pcre || rule_pcre_http) {
1863 " Rule contains %u content options, %u http content options, %u pcre "
1864 "options, and %u pcre options with http modifiers.\n",
1865 rule_content, rule_content_http, rule_pcre, rule_pcre_http);
1870 fprintf(fp,
" Prefilter on: %s.\n",
1873 EngineAnalysisRulesPrintFP(
de_ctx, s);
1877 if (warn_pcre_no_content ) {
1878 fprintf(fp,
" Warning: Rule uses pcre without a content option present.\n"
1879 " -Consider adding a content to improve performance of this "
1882 if (warn_pcre_http_content ) {
1883 fprintf(fp,
" Warning: Rule uses content options with http_* and pcre options "
1884 "without http modifiers.\n"
1885 " -Consider adding http pcre modifier.\n");
1887 else if (warn_pcre_http ) {
1888 fprintf(fp,
" Warning: Rule app layer protocol is http, but pcre options do not "
1889 "have http modifiers.\n"
1890 " -Consider adding http pcre modifiers.\n");
1892 if (warn_content_http_content ) {
1894 " Warning: Rule contains content with http_* and content without http_*.\n"
1895 " -Consider adding http content modifiers.\n");
1897 if (warn_content_http ) {
1898 fprintf(fp,
" Warning: Rule app layer protocol is http, but content options do not "
1899 "have http_* modifiers.\n"
1900 " -Consider adding http content modifiers.\n");
1902 if (rule_content == 1) {
1905 if (warn_encoding_norm_http_buf) {
1906 fprintf(fp,
" Warning: Rule may contain percent encoded content for a normalized "
1907 "http buffer match.\n");
1909 if (warn_tcp_no_flow
1911 fprintf(fp,
" Warning: TCP rule without a flow or flags option.\n"
1912 " -Consider adding flow or flags to improve performance of "
1915 if (warn_client_ports
1920 " Warning: Rule contains ports or port variables only on the client side.\n"
1921 " -Flow direction possibly inconsistent with rule.\n");
1923 if (warn_direction ) {
1924 fprintf(fp,
" Warning: Rule is bidirectional and has a flow option with a specific "
1927 if (warn_method_toclient ) {
1928 fprintf(fp,
" Warning: Rule uses content or pcre for http_method with "
1929 "flow:to_client or from_server\n");
1931 if (warn_method_serverbody ) {
1932 fprintf(fp,
" Warning: Rule uses content or pcre for http_method with content or "
1933 "pcre for http_server_body.\n");
1935 if (warn_pcre_method
1937 fprintf(fp,
" Warning: Rule uses pcre with only a http_method content; possible "
1938 "performance issue.\n");
1940 if (warn_offset_depth_pkt_stream) {
1941 fprintf(fp,
" Warning: Rule has depth"
1942 "/offset with raw content keywords. Please note the "
1943 "offset/depth will be checked against both packet "
1944 "payloads and stream. If you meant to have the offset/"
1945 "depth checked against just the payload, you can update "
1946 "the signature as \"alert tcp-pkt...\"\n");
1948 if (warn_offset_depth_alproto) {
1950 " Warning: Rule has "
1951 "offset/depth set along with a match on a specific "
1952 "app layer protocol - %d. This can lead to FNs if we "
1953 "have a offset/depth content match on a packet payload "
1954 "before we can detect the app layer protocol for the "
1958 if (warn_non_alproto_fp_for_alproto_sig) {
1959 fprintf(fp,
" Warning: Rule app layer "
1960 "protocol is http, but the fast_pattern is set on the raw "
1961 "stream. Consider adding fast_pattern over a http "
1962 "buffer for increased performance.");
1964 if (warn_no_direction) {
1965 fprintf(fp,
" Warning: Rule has no direction indicator.\n");
1967 if (warn_both_direction) {
1968 fprintf(fp,
" Warning: Rule is inspecting both the request and the response.\n");
1970 if (warn_file_store_not_present) {
1971 fprintf(fp,
" Warning: Rule requires file-store but the output file-store is not "
1974 if (rule_warning == 0) {
1975 fprintf(fp,
" No warnings for this rule.\n");
1986 uint32_t accept_rules = 0;
1987 SCJbSetString(
ctx->js,
"policy",
"drop:flow");
1988 SCJbOpenArray(
ctx->js,
"rules");
1997 if (direction == STREAM_TOSERVER) {
2014 if (accept_rules == 0) {
2015 AnalyzerWarning(
ctx, (
char *)
"no accept rules for state, default policy will be applied");
2022 ctx.js = SCJbNewObject();
2026 SCJbOpenObject(
ctx.js,
"tables");
2027 SCJbOpenObject(
ctx.js,
"packet:filter");
2028 SCJbSetString(
ctx.js,
"policy",
"drop:packet");
2029 SCJbOpenArray(
ctx.js,
"rules");
2030 uint32_t accept_rules = 0;
2031 uint32_t last_sid = 0;
2038 if (last_sid == s->
id)
2045 if (accept_rules == 0) {
2046 AnalyzerWarning(&
ctx,
2047 (
char *)
"no accept rules for \'packet:filter\', default policy will be applied");
2049 if (
ctx.js_warnings) {
2050 SCJbClose(
ctx.js_warnings);
2051 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
2052 SCJbFree(
ctx.js_warnings);
2053 ctx.js_warnings = NULL;
2058 if (!AppProtoIsValid(a))
2067 const uint8_t complete_state_ts =
2069 for (uint8_t state = 0; state < complete_state_ts; state++) {
2072 char table_name[128];
2074 SCJbOpenObject(
ctx.js, table_name);
2075 FirewallAddRulesForState(
de_ctx, a, state, STREAM_TOSERVER, &
ctx);
2076 if (
ctx.js_warnings) {
2077 SCJbClose(
ctx.js_warnings);
2078 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
2079 SCJbFree(
ctx.js_warnings);
2080 ctx.js_warnings = NULL;
2084 const uint8_t complete_state_tc =
2086 for (uint8_t state = 0; state < complete_state_tc; state++) {
2089 char table_name[128];
2091 SCJbOpenObject(
ctx.js, table_name);
2092 FirewallAddRulesForState(
de_ctx, a, state, STREAM_TOCLIENT, &
ctx);
2093 if (
ctx.js_warnings) {
2094 SCJbClose(
ctx.js_warnings);
2095 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
2096 SCJbFree(
ctx.js_warnings);
2097 ctx.js_warnings = NULL;
2103 SCJbOpenObject(
ctx.js,
"packet:td");
2104 SCJbSetString(
ctx.js,
"policy",
"accept:hook");
2106 SCJbOpenArray(
ctx.js,
"rules");
2112 if (last_sid == s->
id)
2119 SCJbOpenObject(
ctx.js,
"app:td");
2120 SCJbSetString(
ctx.js,
"policy",
"accept:hook");
2122 SCJbOpenArray(
ctx.js,
"rules");
2128 if (last_sid == s->
id)
2137 SCJbOpenObject(
ctx.js,
"lists");
2138 SCJbOpenObject(
ctx.js,
"firewall");
2140 SCJbOpenArray(
ctx.js,
"rules");
2144 if (last_sid == s->
id)
2152 SCJbOpenObject(
ctx.js,
"td");
2154 SCJbOpenArray(
ctx.js,
"rules");
2158 if (last_sid == s->
id)
2166 SCJbOpenObject(
ctx.js,
"all");
2168 SCJbOpenArray(
ctx.js,
"rules");
2170 if (last_sid == s->
id)
2182 const char *filename =
"firewall.json";
2184 char json_path[PATH_MAX] =
"";
2185 snprintf(json_path,
sizeof(json_path),
"%s/%s", log_dir, filename);
2188 FILE *fp = fopen(json_path,
"w");
2190 fwrite(SCJbPtr(
ctx.js), SCJbLen(
ctx.js), 1, fp);