59 static int rule_warnings_only = 0;
115 { 0,
false,
false,
true,
"http_uri",
"http uri" },
116 { 0,
false,
false,
false,
"http_raw_uri",
"http raw uri" },
117 { 0,
false,
true,
false,
"http_method",
"http method" },
118 { 0,
false,
false,
false,
"http_request_line",
"http request line" },
119 { 0,
false,
false,
false,
"http_client_body",
"http client body" },
120 { 0,
false,
false,
true,
"http_header",
"http header" },
121 { 0,
false,
false,
false,
"http_raw_header",
"http raw header" },
122 { 0,
false,
false,
true,
"http_cookie",
"http cookie" },
123 { 0,
false,
false,
false,
"http_user_agent",
"http user agent" },
124 { 0,
false,
false,
false,
"http_host",
"http host" },
125 { 0,
false,
false,
false,
"http_raw_host",
"http raw host" },
126 { 0,
false,
false,
false,
"http_accept_enc",
"http accept enc" },
127 { 0,
false,
false,
false,
"http_referer",
"http referer" },
128 { 0,
false,
false,
false,
"http_content_type",
"http content type" },
129 { 0,
false,
false,
false,
"http_header_names",
"http header names" },
132 { 0,
false,
false,
false,
"http_stat_msg",
"http stat msg" },
133 { 0,
false,
false,
false,
"http_stat_code",
"http stat code" },
134 { 0,
false,
true,
false,
"file_data",
"http server body" },
137 { 0,
false,
false,
false,
"http_request_line",
"http request line" },
138 { 0,
false,
false,
false,
"http_accept",
"http accept" },
139 { 0,
false,
false,
false,
"http_accept_lang",
"http accept lang" },
140 { 0,
false,
false,
false,
"http_connection",
"http connection" },
141 { 0,
false,
false,
false,
"http_content_len",
"http content len" },
142 { 0,
false,
false,
false,
"http_protocol",
"http protocol" },
143 { 0,
false,
false,
false,
"http_start",
"http start" },
146 { 0,
false,
false,
false,
"http_response_line",
"http response line" },
147 { 0,
false,
false,
false,
"http.server",
"http server" },
148 { 0,
false,
false,
false,
"http.location",
"http location" },
151 static void FpPatternStatsAdd(
FpPatternStats *fp,
int list, uint16_t patlen)
160 else if (patlen < f->min)
172 int fast_pattern_set = 0;
173 int fast_pattern_only_set = 0;
174 int fast_pattern_chop_set = 0;
179 if (mpm_sm != NULL) {
182 fast_pattern_set = 1;
184 fast_pattern_only_set = 1;
186 fast_pattern_chop_set = 1;
192 fprintf(fp,
"== Sid: %u ==\n", s->
id);
193 fprintf(fp,
"%s\n", line);
195 fprintf(fp,
" Fast Pattern analysis:\n");
197 fprintf(fp,
" Prefilter on: %s\n",
204 fprintf(fp,
" No content present\n");
209 fprintf(fp,
" Fast pattern matcher: ");
210 int list_type = mpm_sm_list;
212 fprintf(fp,
"content\n");
217 fprintf(fp,
"%s (%s)\n", desc,
name);
222 fprintf(fp,
" Flags:");
224 fprintf(fp,
" Offset");
227 fprintf(fp,
" Depth");
231 fprintf(fp,
" Within");
235 fprintf(fp,
" Distance");
239 fprintf(fp,
" Nocase");
243 fprintf(fp,
" Negated");
247 fprintf(fp,
" None");
250 fprintf(fp,
" Fast pattern set: %s\n", fast_pattern_set ?
"yes" :
"no");
251 fprintf(fp,
" Fast pattern only set: %s\n", fast_pattern_only_set ?
"yes" :
"no");
252 fprintf(fp,
" Fast pattern chop set: %s\n", fast_pattern_chop_set ?
"yes" :
"no");
253 if (fast_pattern_chop_set) {
254 fprintf(fp,
" Fast pattern offset, length: %u, %u\n", fp_cd->
fp_chop_offset,
265 fprintf(fp,
" Original content: ");
269 if (fast_pattern_chop_set) {
278 fprintf(fp,
" Final content: ");
284 fprintf(fp,
" Final content: ");
303 int fp_engine_analysis_set = 0;
305 if ((
SCConfGetBool(
"engine-analysis.rules-fast-pattern", &fp_engine_analysis_set)) == 0) {
309 if (fp_engine_analysis_set == 0)
313 char *log_path =
SCMalloc(PATH_MAX);
314 if (log_path == NULL) {
315 FatalError(
"Unable to allocate scratch memory for rule filename");
317 snprintf(log_path, PATH_MAX,
"%s/%s%s", log_dir,
320 FILE *fp = fopen(log_path,
"w");
322 SCLogError(
"failed to open %s: %s", log_path, strerror(errno));
329 SCLogInfo(
"Engine-Analysis for fast_pattern printed to file - %s",
334 gettimeofday(&tval, NULL);
336 struct tm *tms =
SCLocalTime(tval.tv_sec, &local_tm);
337 fprintf(fp,
"----------------------------------------------"
338 "---------------------\n");
340 "Date: %" PRId32
"/%" PRId32
"/%04d -- "
342 tms->tm_mday, tms->tm_mon + 1, tms->tm_year + 1900, tms->tm_hour, tms->tm_min,
344 fprintf(fp,
"----------------------------------------------"
345 "---------------------\n");
358 #define DETECT_PERCENT_ENCODING_REGEX "%[0-9|a-f|A-F]{2}"
364 PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
366 PCRE2_UCHAR errbuffer[256];
367 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
388 }
else if (value && strcasecmp(value,
"warnings-only") == 0) {
390 rule_warnings_only = 1;
395 char log_path[PATH_MAX];
396 snprintf(log_path,
sizeof(log_path),
"%s/%s%s", log_dir,
400 SCLogError(
"failed to open %s: %s", log_path, strerror(errno));
404 SCLogInfo(
"Engine-Analysis for rules printed to file - %s",
408 gettimeofday(&tval, NULL);
410 struct tm *tms =
SCLocalTime(tval.tv_sec, &local_tm);
412 "----------------------------------------------"
413 "---------------------\n");
415 "Date: %" PRId32
"/%" PRId32
"/%04d -- "
417 tms->tm_mday, tms->tm_mon + 1, tms->tm_year + 1900, tms->tm_hour, tms->tm_min,
420 "----------------------------------------------"
421 "---------------------\n");
424 if (!PerCentEncodingSetup(
de_ctx->
ea)) {
426 "Error compiling regex; can't check for percent encoding in normalized "
432 SCLogInfo(
"Conf parameter \"engine-analysis.rules\" not found. "
433 "Defaulting to not printing the rules analysis report.");
436 SCLogInfo(
"Engine-Analysis for rules disabled in conf file.");
445 fprintf(fp,
"============\n"
446 "Summary:\n============\n");
454 "%s, smallest pattern %u byte(s), longest pattern %u byte(s), number of patterns "
455 "%u, avg pattern len %.2f byte(s)\n",
457 (
float)((
double)f->
tot / (
float)f->
cnt));
477 *fp_analysis =
false;
478 *rule_analysis =
false;
482 FatalError(
"Unable to allocate per-engine analysis context");
487 if (cfg_prefix_len > 0) {
492 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");
922 SCJbOpenObject(js,
"ack");
923 SCDetectU32ToJson(js, cd);
929 SCJbOpenObject(js,
"seq");
930 SCDetectU32ToJson(js, cd);
936 SCJbOpenObject(js,
"tcp_mss");
937 SCDetectU16ToJson(js, cd);
943 SCJbOpenObject(js,
"dsize");
944 SCDetectU16ToJson(js, cd);
950 SCJbOpenObject(js,
"code");
951 SCDetectU8ToJson(js, cd);
957 SCJbOpenObject(js,
"ttl");
958 SCDetectU8ToJson(js, cd);
964 SCJbOpenObject(js,
"id");
965 SCDetectU16ToJson(js, cd);
971 SCJbOpenObject(js,
"window");
972 SCDetectU16ToJson(js, cd);
978 SCJbOpenObject(js,
"flow_age");
979 SCDetectU32ToJson(js, cd);
1000 ctx.js = SCJbNewObject();
1011 SCJbSetUint(
ctx.js,
"id", s->
id);
1012 SCJbSetUint(
ctx.js,
"gid", s->
gid);
1013 SCJbSetUint(
ctx.js,
"rev", s->
rev);
1014 SCJbSetString(
ctx.js,
"msg", s->
msg);
1017 SCJbSetString(
ctx.js,
"app_proto", alproto);
1019 SCJbOpenArray(
ctx.js,
"requirements");
1021 SCJbAppendString(
ctx.js,
"payload");
1024 SCJbAppendString(
ctx.js,
"no_payload");
1027 SCJbAppendString(
ctx.js,
"flow");
1030 SCJbAppendString(
ctx.js,
"tcp_flags_init_deinit");
1033 SCJbAppendString(
ctx.js,
"tcp_flags_unusual");
1036 SCJbAppendString(
ctx.js,
"engine_event");
1039 SCJbAppendString(
ctx.js,
"real_pkt");
1043 SCJbOpenObject(
ctx.js,
"match_policy");
1044 SCJbOpenArray(
ctx.js,
"actions");
1046 SCJbAppendString(
ctx.js,
"alert");
1049 SCJbAppendString(
ctx.js,
"drop");
1052 SCJbAppendString(
ctx.js,
"reject");
1055 SCJbAppendString(
ctx.js,
"reject_dst");
1058 SCJbAppendString(
ctx.js,
"reject_both");
1061 SCJbAppendString(
ctx.js,
"config");
1064 SCJbAppendString(
ctx.js,
"pass");
1067 SCJbAppendString(
ctx.js,
"accept");
1073 switch (flow_action) {
1075 SCJbSetString(
ctx.js,
"scope",
"packet");
1078 SCJbSetString(
ctx.js,
"scope",
"flow");
1081 SCJbSetString(
ctx.js,
"scope",
"flow_if_stateful");
1088 SCJbSetString(
ctx.js,
"scope",
"packet");
1091 SCJbSetString(
ctx.js,
"scope",
"flow");
1094 SCJbSetString(
ctx.js,
"scope",
"hook");
1097 SCJbSetString(
ctx.js,
"scope",
"tx");
1107 SCJbSetString(
ctx.js,
"type",
"unset");
1110 SCJbSetString(
ctx.js,
"type",
"ip_only");
1113 SCJbSetString(
ctx.js,
"type",
"like_ip_only");
1116 SCJbSetString(
ctx.js,
"type",
"pd_only");
1119 SCJbSetString(
ctx.js,
"type",
"de_only");
1122 SCJbSetString(
ctx.js,
"type",
"pkt");
1125 SCJbSetString(
ctx.js,
"type",
"pkt_stream");
1128 SCJbSetString(
ctx.js,
"type",
"stream");
1131 SCJbSetString(
ctx.js,
"type",
"app_layer");
1134 SCJbSetString(
ctx.js,
"type",
"app_tx");
1137 SCJbSetString(
ctx.js,
"type",
"error");
1143 SCJbOpenObject(
ctx.js,
"dependencies");
1144 SCJbOpenObject(
ctx.js,
"flowbits");
1145 SCJbOpenObject(
ctx.js,
"upstream");
1147 SCJbOpenObject(
ctx.js,
"state_modifying_rules");
1148 SCJbOpenArray(
ctx.js,
"sids");
1153 SCJbOpenArray(
ctx.js,
"names");
1156 SCJbAppendString(
ctx.js,
1169 SCJbOpenArray(
ctx.js,
"flags");
1171 SCJbAppendString(
ctx.js,
"src_any");
1174 SCJbAppendString(
ctx.js,
"dst_any");
1177 SCJbAppendString(
ctx.js,
"sp_any");
1180 SCJbAppendString(
ctx.js,
"dp_any");
1183 SCJbAppendString(
ctx.js,
"noalert");
1186 SCJbAppendString(
ctx.js,
"dsize");
1189 SCJbAppendString(
ctx.js,
"applayer");
1192 SCJbAppendString(
ctx.js,
"need_packet");
1195 SCJbAppendString(
ctx.js,
"need_stream");
1198 SCJbAppendString(
ctx.js,
"negated_mpm");
1201 SCJbAppendString(
ctx.js,
"flush");
1204 SCJbAppendString(
ctx.js,
"need_flowvar");
1207 SCJbAppendString(
ctx.js,
"filestore");
1210 SCJbAppendString(
ctx.js,
"toserver");
1213 SCJbAppendString(
ctx.js,
"toclient");
1216 SCJbAppendString(
ctx.js,
"tlsstore");
1219 SCJbAppendString(
ctx.js,
"bypass");
1222 SCJbAppendString(
ctx.js,
"prefilter");
1225 SCJbAppendString(
ctx.js,
"src_is_target");
1228 SCJbAppendString(
ctx.js,
"dst_is_target");
1235 SCJbOpenArray(
ctx.js,
"pkt_engines");
1237 for ( ; pkt != NULL; pkt = pkt->
next) {
1252 SCJbStartObject(
ctx.js);
1253 SCJbSetString(
ctx.js,
"name",
name);
1254 SCJbSetBool(
ctx.js,
"is_mpm", pkt->
mpm);
1256 SCJbOpenArray(
ctx.js,
"transforms");
1258 SCJbStartObject(
ctx.js);
1259 SCJbSetString(
ctx.js,
"name",
1272 SCJbOpenArray(
ctx.js,
"frame_engines");
1274 for (; frame != NULL; frame = frame->
next) {
1276 SCJbStartObject(
ctx.js);
1277 SCJbSetString(
ctx.js,
"name",
name);
1278 SCJbSetBool(
ctx.js,
"is_mpm", frame->
mpm);
1280 SCJbOpenArray(
ctx.js,
"transforms");
1282 SCJbStartObject(
ctx.js);
1283 SCJbSetString(
ctx.js,
"name",
1295 bool has_stream =
false;
1296 bool has_client_body_mpm =
false;
1297 bool has_file_data_mpm =
false;
1299 SCJbOpenArray(
ctx.js,
"engines");
1301 for ( ; app != NULL; app = app->
next) {
1316 }
else if (app->
mpm && strcmp(
name,
"http_client_body") == 0) {
1317 has_client_body_mpm =
true;
1318 }
else if (app->
mpm && strcmp(
name,
"file_data") == 0) {
1319 has_file_data_mpm =
true;
1322 SCJbStartObject(
ctx.js);
1323 SCJbSetString(
ctx.js,
"name",
name);
1324 const char *direction = app->
dir == 0 ?
"toserver" :
"toclient";
1325 SCJbSetString(
ctx.js,
"direction", direction);
1326 SCJbSetBool(
ctx.js,
"is_mpm", app->
mpm);
1331 SCJbOpenArray(
ctx.js,
"transforms");
1333 SCJbStartObject(
ctx.js);
1334 SCJbSetString(
ctx.js,
"name",
1348 if (has_stream && has_client_body_mpm)
1349 AnalyzerNote(&
ctx, (
char *)
"mpm in http_client_body combined with stream match leads to stream buffering");
1350 if (has_stream && has_file_data_mpm)
1351 AnalyzerNote(&
ctx, (
char *)
"mpm in file_data combined with stream match leads to stream buffering");
1354 SCJbOpenObject(
ctx.js,
"lists");
1364 if (pkt_mpm || app_mpm) {
1365 SCJbOpenObject(
ctx.js,
"mpm");
1373 SCJbSetString(
ctx.js,
"buffer",
name);
1380 switch (smd->
type) {
1384 DumpContent(
ctx.js, cd);
1396 SCJbOpenObject(
ctx.js,
"prefilter");
1403 SCJbSetString(
ctx.js,
"buffer",
name);
1405 SCJbSetString(
ctx.js,
"name", mname);
1409 if (
ctx.js_warnings) {
1410 SCJbClose(
ctx.js_warnings);
1411 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
1412 SCJbFree(
ctx.js_warnings);
1413 ctx.js_warnings = NULL;
1416 SCJbClose(
ctx.js_notes);
1417 SCJbSetObject(
ctx.js,
"notes",
ctx.js_notes);
1418 SCJbFree(
ctx.js_notes);
1419 ctx.js_notes = NULL;
1423 const char *filename =
"rules.json";
1425 char json_path[PATH_MAX] =
"";
1426 snprintf(json_path,
sizeof(json_path),
"%s/%s%s", log_dir,
1430 FILE *fp = fopen(json_path,
"a");
1432 fwrite(SCJbPtr(
ctx.js), SCJbLen(
ctx.js), 1, fp);
1446 SCJsonBuilder *root_jb = SCJbNewObject();
1450 SCJbOpenArray(root_jb,
"buffers");
1454 char str[1024] =
"";
1458 SCJsonBuilder *jb = arrays[p->
sm_list];
1459 if (arrays[p->
sm_list] == NULL) {
1460 jb = arrays[p->
sm_list] = SCJbNewObject();
1466 SCJbSetString(jb,
"name",
name);
1467 SCJbSetUint(jb,
"list_id", p->
sm_list);
1469 SCJbOpenArray(jb,
"patterns");
1472 SCJbStartObject(jb);
1473 SCJbSetString(jb,
"pattern",
str);
1475 SCJbSetUint(jb,
"cnt", p->
cnt);
1476 SCJbSetUint(jb,
"mpm", p->
mpm);
1477 SCJbOpenObject(jb,
"flags");
1488 SCJsonBuilder *jb = arrays[i];
1495 SCJbAppendObject(root_jb, jb);
1501 const char *filename =
"patterns.json";
1503 char json_path[PATH_MAX] =
"";
1504 snprintf(json_path,
sizeof(json_path),
"%s/%s%s", log_dir,
1508 FILE *fp = fopen(json_path,
"a");
1510 fwrite(SCJbPtr(root_jb), SCJbLen(root_jb), 1, fp);
1531 EngineAnalysisItemsReset(ea_ctx);
1539 FatalError(
"Unable to allocate analysis scratch pad");
1549 analyzer_item->
item_id = (uint16_t)item_id;
1550 if (analyzer_item->
item_id == -1) {
1552 FatalError(
"unable to initialize engine-analysis table: detect buffer \"%s\" not "
1585 uint32_t rule_bidirectional = 0;
1586 uint32_t rule_pcre = 0;
1587 uint32_t rule_pcre_http = 0;
1588 uint32_t rule_content = 0;
1589 uint32_t rule_flow = 0;
1590 uint32_t rule_flags = 0;
1591 uint32_t rule_flow_toserver = 0;
1592 uint32_t rule_flow_toclient = 0;
1593 uint32_t rule_flow_nostream = 0;
1594 uint32_t rule_ipv4_only = 0;
1595 uint32_t rule_ipv6_only = 0;
1596 uint32_t rule_flowbits = 0;
1597 uint32_t rule_flowint = 0;
1598 uint32_t rule_content_http = 0;
1599 uint32_t rule_content_offset_depth = 0;
1600 int32_t list_id = 0;
1601 uint32_t rule_warning = 0;
1602 uint32_t stream_buf = 0;
1603 uint32_t packet_buf = 0;
1604 uint32_t file_store = 0;
1605 uint32_t warn_pcre_no_content = 0;
1606 uint32_t warn_pcre_http_content = 0;
1607 uint32_t warn_pcre_http = 0;
1608 uint32_t warn_content_http_content = 0;
1609 uint32_t warn_content_http = 0;
1610 uint32_t warn_tcp_no_flow = 0;
1611 uint32_t warn_client_ports = 0;
1612 uint32_t warn_direction = 0;
1613 uint32_t warn_method_toclient = 0;
1614 uint32_t warn_method_serverbody = 0;
1615 uint32_t warn_pcre_method = 0;
1616 uint32_t warn_encoding_norm_http_buf = 0;
1617 uint32_t warn_file_store_not_present = 0;
1618 uint32_t warn_offset_depth_pkt_stream = 0;
1619 uint32_t warn_offset_depth_alproto = 0;
1620 uint32_t warn_non_alproto_fp_for_alproto_sig = 0;
1621 uint32_t warn_no_direction = 0;
1622 uint32_t warn_both_direction = 0;
1624 EngineAnalysisItemsInit(
de_ctx->
ea);
1630 rule_bidirectional = 1;
1644 rule_ipv4_only += 1;
1647 rule_ipv6_only += 1;
1655 if (item_slot == -1) {
1663 if (item_slot == -1) {
1668 rule_content_offset_depth++;
1674 rule_content_http++;
1681 warn_encoding_norm_http_buf += 1;
1688 rule_flow_toserver = 1;
1691 rule_flow_toclient = 1;
1696 rule_flow_nostream = 1;
1710 if (sm->
ctx != NULL) {
1720 warn_file_store_not_present = 1;
1723 if (rule_pcre > 0 && rule_content == 0 && rule_content_http == 0) {
1725 warn_pcre_no_content = 1;
1728 if (rule_content_http > 0 && rule_pcre > 0 && rule_pcre_http == 0) {
1730 warn_pcre_http_content = 1;
1736 if (rule_content > 0 && rule_content_http > 0) {
1738 warn_content_http_content = 1;
1742 warn_content_http = 1;
1744 if (rule_content == 1) {
1749 (rule_content || rule_content_http || rule_pcre || rule_pcre_http || rule_flowbits ||
1752 warn_tcp_no_flow = 1;
1754 if (rule_flow && !rule_bidirectional && (rule_flow_toserver || rule_flow_toclient)
1759 warn_client_ports = 1;
1762 if (rule_flow && rule_bidirectional && (rule_flow_toserver || rule_flow_toclient)) {
1767 if (*http_method_item_seen_ptr) {
1768 if (rule_flow && rule_flow_toclient) {
1770 warn_method_toclient = 1;
1772 if (*http_server_body_item_seen_ptr) {
1774 warn_method_serverbody = 1;
1776 if (rule_content == 0 && rule_content_http == 0 && (rule_pcre > 0 || rule_pcre_http > 0)) {
1778 warn_pcre_method = 1;
1781 if (rule_content_offset_depth > 0 && stream_buf && packet_buf) {
1783 warn_offset_depth_pkt_stream = 1;
1787 warn_offset_depth_alproto = 1;
1792 warn_non_alproto_fp_for_alproto_sig = 1;
1796 warn_no_direction += 1;
1803 warn_both_direction += 1;
1808 if (!rule_warnings_only || (rule_warnings_only && rule_warning > 0)) {
1810 fprintf(fp,
"== Sid: %u ==\n", s->
id);
1811 fprintf(fp,
"%s\n", line);
1817 fprintf(fp,
" Rule is ip only.\n");
1820 fprintf(fp,
" Rule is like ip only.\n");
1823 fprintf(fp,
" Rule is PD only.\n");
1826 fprintf(fp,
" Rule is DE only.\n");
1829 fprintf(fp,
" Rule is packet inspecting.\n");
1832 fprintf(fp,
" Rule is packet and stream inspecting.\n");
1835 fprintf(fp,
" Rule is stream inspecting.\n");
1838 fprintf(fp,
" Rule is app-layer inspecting.\n");
1841 fprintf(fp,
" Rule is App-layer TX inspecting.\n");
1847 fprintf(fp,
" Rule is IPv6 only.\n");
1849 fprintf(fp,
" Rule is IPv4 only.\n");
1851 fprintf(fp,
" Rule matches on packets.\n");
1852 if (!rule_flow_nostream && stream_buf &&
1853 (rule_flow || rule_flowbits || rule_flowint || rule_content || rule_pcre)) {
1854 fprintf(fp,
" Rule matches on reassembled stream.\n");
1859 fprintf(fp,
" Rule matches on %s buffer.\n", ai->
display_name);
1865 if (rule_content || rule_content_http || rule_pcre || rule_pcre_http) {
1867 " Rule contains %u content options, %u http content options, %u pcre "
1868 "options, and %u pcre options with http modifiers.\n",
1869 rule_content, rule_content_http, rule_pcre, rule_pcre_http);
1874 fprintf(fp,
" Prefilter on: %s.\n",
1877 EngineAnalysisRulesPrintFP(
de_ctx, s);
1881 if (warn_pcre_no_content ) {
1882 fprintf(fp,
" Warning: Rule uses pcre without a content option present.\n"
1883 " -Consider adding a content to improve performance of this "
1886 if (warn_pcre_http_content ) {
1887 fprintf(fp,
" Warning: Rule uses content options with http_* and pcre options "
1888 "without http modifiers.\n"
1889 " -Consider adding http pcre modifier.\n");
1891 else if (warn_pcre_http ) {
1892 fprintf(fp,
" Warning: Rule app layer protocol is http, but pcre options do not "
1893 "have http modifiers.\n"
1894 " -Consider adding http pcre modifiers.\n");
1896 if (warn_content_http_content ) {
1898 " Warning: Rule contains content with http_* and content without http_*.\n"
1899 " -Consider adding http content modifiers.\n");
1901 if (warn_content_http ) {
1902 fprintf(fp,
" Warning: Rule app layer protocol is http, but content options do not "
1903 "have http_* modifiers.\n"
1904 " -Consider adding http content modifiers.\n");
1906 if (rule_content == 1) {
1909 if (warn_encoding_norm_http_buf) {
1910 fprintf(fp,
" Warning: Rule may contain percent encoded content for a normalized "
1911 "http buffer match.\n");
1913 if (warn_tcp_no_flow
1915 fprintf(fp,
" Warning: TCP rule without a flow or flags option.\n"
1916 " -Consider adding flow or flags to improve performance of "
1919 if (warn_client_ports
1924 " Warning: Rule contains ports or port variables only on the client side.\n"
1925 " -Flow direction possibly inconsistent with rule.\n");
1927 if (warn_direction ) {
1928 fprintf(fp,
" Warning: Rule is bidirectional and has a flow option with a specific "
1931 if (warn_method_toclient ) {
1932 fprintf(fp,
" Warning: Rule uses content or pcre for http_method with "
1933 "flow:to_client or from_server\n");
1935 if (warn_method_serverbody ) {
1936 fprintf(fp,
" Warning: Rule uses content or pcre for http_method with content or "
1937 "pcre for http_server_body.\n");
1939 if (warn_pcre_method
1941 fprintf(fp,
" Warning: Rule uses pcre with only a http_method content; possible "
1942 "performance issue.\n");
1944 if (warn_offset_depth_pkt_stream) {
1945 fprintf(fp,
" Warning: Rule has depth"
1946 "/offset with raw content keywords. Please note the "
1947 "offset/depth will be checked against both packet "
1948 "payloads and stream. If you meant to have the offset/"
1949 "depth checked against just the payload, you can update "
1950 "the signature as \"alert tcp-pkt...\"\n");
1952 if (warn_offset_depth_alproto) {
1954 " Warning: Rule has "
1955 "offset/depth set along with a match on a specific "
1956 "app layer protocol - %d. This can lead to FNs if we "
1957 "have a offset/depth content match on a packet payload "
1958 "before we can detect the app layer protocol for the "
1962 if (warn_non_alproto_fp_for_alproto_sig) {
1963 fprintf(fp,
" Warning: Rule app layer "
1964 "protocol is http, but the fast_pattern is set on the raw "
1965 "stream. Consider adding fast_pattern over a http "
1966 "buffer for increased performance.");
1968 if (warn_no_direction) {
1969 fprintf(fp,
" Warning: Rule has no direction indicator.\n");
1971 if (warn_both_direction) {
1972 fprintf(fp,
" Warning: Rule is inspecting both the request and the response.\n");
1974 if (warn_file_store_not_present) {
1975 fprintf(fp,
" Warning: Rule requires file-store but the output file-store is not "
1978 if (rule_warning == 0) {
1979 fprintf(fp,
" No warnings for this rule.\n");
1990 uint32_t accept_rules = 0;
1991 SCJbSetString(
ctx->js,
"policy",
"drop:flow");
1992 SCJbOpenArray(
ctx->js,
"rules");
2001 if (direction == STREAM_TOSERVER) {
2018 if (accept_rules == 0) {
2019 AnalyzerWarning(
ctx, (
char *)
"no accept rules for state, default policy will be applied");
2026 ctx.js = SCJbNewObject();
2030 SCJbOpenObject(
ctx.js,
"tables");
2031 SCJbOpenObject(
ctx.js,
"packet:filter");
2032 SCJbSetString(
ctx.js,
"policy",
"drop:packet");
2033 SCJbOpenArray(
ctx.js,
"rules");
2034 uint32_t accept_rules = 0;
2035 uint32_t last_sid = 0;
2042 if (last_sid == s->
id)
2049 if (accept_rules == 0) {
2050 AnalyzerWarning(&
ctx,
2051 (
char *)
"no accept rules for \'packet:filter\', default policy will be applied");
2053 if (
ctx.js_warnings) {
2054 SCJbClose(
ctx.js_warnings);
2055 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
2056 SCJbFree(
ctx.js_warnings);
2057 ctx.js_warnings = NULL;
2062 if (!AppProtoIsValid(a))
2071 const uint8_t complete_state_ts =
2073 for (uint8_t state = 0; state < complete_state_ts; state++) {
2076 char table_name[128];
2078 SCJbOpenObject(
ctx.js, table_name);
2079 FirewallAddRulesForState(
de_ctx, a, state, STREAM_TOSERVER, &
ctx);
2080 if (
ctx.js_warnings) {
2081 SCJbClose(
ctx.js_warnings);
2082 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
2083 SCJbFree(
ctx.js_warnings);
2084 ctx.js_warnings = NULL;
2088 const uint8_t complete_state_tc =
2090 for (uint8_t state = 0; state < complete_state_tc; state++) {
2093 char table_name[128];
2095 SCJbOpenObject(
ctx.js, table_name);
2096 FirewallAddRulesForState(
de_ctx, a, state, STREAM_TOCLIENT, &
ctx);
2097 if (
ctx.js_warnings) {
2098 SCJbClose(
ctx.js_warnings);
2099 SCJbSetObject(
ctx.js,
"warnings",
ctx.js_warnings);
2100 SCJbFree(
ctx.js_warnings);
2101 ctx.js_warnings = NULL;
2107 SCJbOpenObject(
ctx.js,
"packet:td");
2108 SCJbSetString(
ctx.js,
"policy",
"accept:hook");
2110 SCJbOpenArray(
ctx.js,
"rules");
2116 if (last_sid == s->
id)
2123 SCJbOpenObject(
ctx.js,
"app:td");
2124 SCJbSetString(
ctx.js,
"policy",
"accept:hook");
2126 SCJbOpenArray(
ctx.js,
"rules");
2132 if (last_sid == s->
id)
2141 SCJbOpenObject(
ctx.js,
"lists");
2142 SCJbOpenObject(
ctx.js,
"firewall");
2144 SCJbOpenArray(
ctx.js,
"rules");
2148 if (last_sid == s->
id)
2156 SCJbOpenObject(
ctx.js,
"td");
2158 SCJbOpenArray(
ctx.js,
"rules");
2162 if (last_sid == s->
id)
2170 SCJbOpenObject(
ctx.js,
"all");
2172 SCJbOpenArray(
ctx.js,
"rules");
2174 if (last_sid == s->
id)
2186 const char *filename =
"firewall.json";
2188 char json_path[PATH_MAX] =
"";
2189 snprintf(json_path,
sizeof(json_path),
"%s/%s", log_dir, filename);
2192 FILE *fp = fopen(json_path,
"w");
2194 fwrite(SCJbPtr(
ctx.js), SCJbLen(
ctx.js), 1, fp);