38 static void DetectAppLayerProtocolRegisterTests(
void);
54 static inline uint32_t AlprotoBitmaskSize(
void)
59 static inline void AlprotoBitmaskSet(uint8_t *bm,
AppProto a)
61 bm[a >> 3] |= (uint8_t)(1u << (a & 7));
64 static inline bool AlprotoBitmaskTest(
const uint8_t *bm,
AppProto a)
66 return (bm[a >> 3] & (uint8_t)(1u << (a & 7))) != 0;
77 static inline bool DetectAppLayerProtocolCompare(
AppProto sigproto,
AppProto alproto,
bool exact)
79 return exact ? (sigproto == alproto) : AppProtoEquals(sigproto, alproto);
86 static void DetectAppLayerProtocolExpand(uint8_t *bm,
AppProto sigproto,
bool exact)
89 if (DetectAppLayerProtocolCompare(sigproto, a, exact))
90 AlprotoBitmaskSet(bm, a);
94 static int DetectAppLayerProtocolPacketMatch(
117 switch (data->
mode) {
160 r = AlprotoBitmaskTest(data->
alprotos, resolved_alproto);
172 #define MAX_ALPROTO_NAME 50
177 static int DetectAppLayerProtocolMapModeName(
const char *
name)
179 if (strcmp(
name,
"final") == 0)
181 if (strcmp(
name,
"original") == 0)
183 if (strcmp(
name,
"either") == 0)
185 if (strcmp(
name,
"to_server") == 0)
187 if (strcmp(
name,
"to_client") == 0)
189 if (strcmp(
name,
"direction") == 0)
221 if (AlprotoBitmaskTest(data->
alprotos, a))
230 static void DetectAppLayerProtocolBuildSupportedList(
char *buf,
size_t buflen)
241 if (alprotos[a] != 1)
247 if (w < 0 || (
size_t)w >= buflen -
offset)
256 static int DetectAppLayerProtocolResolveToken(
257 const char *token,
const char *arg,
bool negate,
AppProto *out)
259 size_t tlen = strlen(token);
261 SCLogError(
"app-layer-protocol keyword value \"%s\" contains an empty token", arg);
265 SCLogError(
"app-layer-protocol keyword token \"%s\" in \"%s\" exceeds the "
266 "maximum token length of %d characters",
270 if (strcmp(token,
"failed") == 0) {
274 if (strcmp(token,
"unknown") == 0) {
276 SCLogError(
"app-layer-protocol keyword can't use negation with protocol 'unknown'");
284 char supported[1024];
285 DetectAppLayerProtocolBuildSupportedList(supported,
sizeof(supported));
286 SCLogError(
"app-layer-protocol keyword supplied with unknown protocol "
287 "\"%s\" in \"%s\"; supported protocols: %s",
288 token, arg, supported);
298 SCLogError(
"app-layer-protocol keyword requires a value");
304 size_t arglen = strlen(arg);
306 SCLogError(
"app-layer-protocol keyword argument too long (\"%s\"): maximum "
307 "supported length is 1024 characters",
312 SCLogError(
"app-layer-protocol keyword value is empty (an empty value list "
313 "is not permitted)");
318 strlcpy(buf, arg,
sizeof(buf));
325 char *qualifiers = strchr(buf,
',');
326 if (qualifiers != NULL) {
329 bool mode_set =
false;
330 char *q = qualifiers;
331 while (q != NULL && *q !=
'\0') {
332 char *
next = strchr(q,
',');
335 if (strcmp(q,
"exact") == 0) {
338 int m = DetectAppLayerProtocolMapModeName(q);
340 SCLogError(
"app-layer-protocol keyword supplied with unknown "
341 "qualifier \"%s\" in \"%s\"",
346 SCLogError(
"app-layer-protocol keyword supplied with multiple "
347 "mode qualifiers in \"%s\"",
377 char *pipe = strchr(cur,
'|');
382 if (DetectAppLayerProtocolResolveToken(cur, arg, negate, &value) < 0)
388 SCLogError(
"app-layer-protocol keyword: 'http' with 'exact' never "
389 "matches (flows are classified as http1/http2); use "
394 if (value_count == 0)
396 DetectAppLayerProtocolExpand(data->
alprotos, value, exact);
404 data->
is_list = (value_count > 1);
411 DetectAppLayerProtocolFree(NULL, data);
418 static bool DetectAppLayerProtocolsConflict(
430 if (AlprotoBitmaskTest(us->
alprotos, a) && AlprotoBitmaskTest(them->
alprotos, a)) {
431 SCLogError(
"conflicting app-layer-protocol rules: "
432 "duplicate or overlapping negated entries under the same mode");
441 SCLogError(
"conflicting app-layer-protocol rules: "
442 "multiple non-negated entries under the same mode");
448 char conflict_buf[512];
449 size_t buf_offset = 0;
450 bool has_intersection =
false;
453 if (!AlprotoBitmaskTest(us->
alprotos, a) || !AlprotoBitmaskTest(them->
alprotos, a))
455 has_intersection =
true;
459 if (buf_offset > 0 && buf_offset <
sizeof(conflict_buf) - 2) {
460 conflict_buf[buf_offset++] =
',';
461 conflict_buf[buf_offset++] =
' ';
463 size_t name_len = strlen(
name);
464 if (buf_offset + name_len <
sizeof(conflict_buf) - 1) {
465 memcpy(conflict_buf + buf_offset,
name, name_len);
466 buf_offset += name_len;
469 conflict_buf[buf_offset] =
'\0';
471 if (has_intersection) {
472 SCLogError(
"conflicting app-layer-protocol rules: "
473 "can't mix positive match with negated match under the same "
474 "mode; intersecting protocol value(s): %s",
477 SCLogError(
"conflicting app-layer-protocol rules: "
478 "can't mix positive app-layer-protocol match with negated "
479 "match or match for 'failed'");
491 "have the rule match on an app layer protocol set through "
492 "other keywords that match on this protocol, or have "
493 "already seen a non-negated app-layer-protocol.");
502 for (; tsm != NULL; tsm = tsm->
next) {
506 if (DetectAppLayerProtocolsConflict(data, them)) {
507 SCLogError(
"conflicting app-layer-protocol options detected "
508 "(see preceding error for details).");
521 DetectAppLayerProtocolFree(
de_ctx, data);
538 static void PrefilterPacketAppProtoMatch(
543 if (!PrefilterPacketHeaderExtraMatch(
ctx,
p)) {
548 if (
p->
flow == NULL) {
560 bool negated = (bool)
ctx->v1.u8[2];
561 bool exact = (
bool)
ctx->v1.u8[4];
562 switch (
ctx->v1.u8[3]) {
587 !DetectAppLayerProtocolCompare(
ctx->v1.u16[0],
f->
alproto_tc, exact)) {
588 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
590 !DetectAppLayerProtocolCompare(
ctx->v1.u16[0],
f->
alproto_ts, exact)) {
591 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
594 if (DetectAppLayerProtocolCompare(
ctx->v1.u16[0],
f->
alproto_tc, exact) ||
595 DetectAppLayerProtocolCompare(
ctx->v1.u16[0],
f->
alproto_ts, exact)) {
596 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
605 if (!DetectAppLayerProtocolCompare(
ctx->v1.u16[0], alproto, exact)) {
606 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
610 if (DetectAppLayerProtocolCompare(
ctx->v1.u16[0], alproto, exact)) {
611 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
638 PrefilterPacketAppProtoSet, PrefilterPacketAppProtoCompare,
639 PrefilterPacketAppProtoMatch);
642 static bool PrefilterAppProtoIsPrefilterable(
const Signature *s)
684 static int DetectAppLayerProtocolTest01(
void)
690 DetectAppLayerProtocolFree(NULL, data);
694 static int DetectAppLayerProtocolTest02(
void)
700 DetectAppLayerProtocolFree(NULL, data);
704 static int DetectAppLayerProtocolTest03(
void)
713 "(app-layer-protocol:http; sid:1;)");
728 static int DetectAppLayerProtocolTest04(
void)
737 "(app-layer-protocol:!http; sid:1;)");
754 static int DetectAppLayerProtocolTest05(
void)
763 "(app-layer-protocol:!http; app-layer-protocol:!smtp; sid:1;)");
785 static int DetectAppLayerProtocolTest06(
void)
793 "(app-layer-protocol:smtp; sid:1;)");
799 static int DetectAppLayerProtocolTest07(
void)
807 "(app-layer-protocol:!smtp; sid:1;)");
813 static int DetectAppLayerProtocolTest08(
void)
821 "(app-layer-protocol:!smtp; app-layer-protocol:http; sid:1;)");
827 static int DetectAppLayerProtocolTest09(
void)
835 "(app-layer-protocol:http; app-layer-protocol:!smtp; sid:1;)");
841 static int DetectAppLayerProtocolTest10(
void)
849 "(app-layer-protocol:smtp; app-layer-protocol:!http; sid:1;)");
855 static int DetectAppLayerProtocolTest11(
void)
861 DetectAppLayerProtocolFree(NULL, data);
865 static int DetectAppLayerProtocolTest12(
void)
871 DetectAppLayerProtocolFree(NULL, data);
875 static int DetectAppLayerProtocolTest13(
void)
884 "(app-layer-protocol:failed; sid:1;)");
899 static int DetectAppLayerProtocolTest14(
void)
907 "(app-layer-protocol:http; flowbits:set,blah; sid:1;)");
917 "(app-layer-protocol:http; flow:to_client; sid:2;)");
928 "(app-layer-protocol:http; flow:to_client,established; sid:3;)");
946 static int DetectAppLayerProtocolTest15(
void)
955 DetectAppLayerProtocolFree(NULL, data);
960 static int DetectAppLayerProtocolTest16(
void)
969 DetectAppLayerProtocolFree(NULL, data);
974 static int DetectAppLayerProtocolTest17(
void)
982 DetectAppLayerProtocolFree(NULL, data);
987 static int DetectAppLayerProtocolTest18(
void)
996 static int DetectAppLayerProtocolTest19(
void)
1004 DetectAppLayerProtocolFree(NULL, data);
1009 static int DetectAppLayerProtocolTest20(
void)
1017 static int DetectAppLayerProtocolTest21(
void)
1025 static int DetectAppLayerProtocolTest22(
void)
1029 memset(big,
'a',
sizeof(big) - 1);
1030 big[
sizeof(big) - 1] =
'\0';
1037 static int DetectAppLayerProtocolTest23(
void)
1045 static int DetectAppLayerProtocolTest24(
void)
1053 DetectAppLayerProtocolFree(NULL, data);
1058 static int DetectAppLayerProtocolTest25(
void)
1066 static int DetectAppLayerProtocolTest26(
void)
1073 "(app-layer-protocol:!tls; sid:1;)");
1090 static int DetectAppLayerProtocolTest27(
void)
1097 "(app-layer-protocol:tls|dns; sid:1;)");
1111 FAIL_IF(PrefilterAppProtoIsPrefilterable(s));
1118 static int DetectAppLayerProtocolTest28(
void)
1126 "alert tcp any any -> any any "
1127 "(tls.sni; content:\"example.com\"; app-layer-protocol:tls|dns; sid:1;)");
1135 static int DetectAppLayerProtocolTest29(
void)
1142 DetectAppLayerProtocolFree(NULL, data);
1147 static int DetectAppLayerProtocolTest30(
void)
1154 DetectAppLayerProtocolFree(NULL, data);
1159 static int DetectAppLayerProtocolTest31(
void)
1167 static int DetectAppLayerProtocolTest32(
void)
1177 DetectAppLayerProtocolFree(NULL, data);
1182 static int DetectAppLayerProtocolTest33(
void)
1189 "(app-layer-protocol:tls; sid:1;)");
1207 static void DetectAppLayerProtocolRegisterTests(
void)
1209 UtRegisterTest(
"DetectAppLayerProtocolTest01", DetectAppLayerProtocolTest01);
1210 UtRegisterTest(
"DetectAppLayerProtocolTest02", DetectAppLayerProtocolTest02);
1211 UtRegisterTest(
"DetectAppLayerProtocolTest03", DetectAppLayerProtocolTest03);
1212 UtRegisterTest(
"DetectAppLayerProtocolTest04", DetectAppLayerProtocolTest04);
1213 UtRegisterTest(
"DetectAppLayerProtocolTest05", DetectAppLayerProtocolTest05);
1214 UtRegisterTest(
"DetectAppLayerProtocolTest06", DetectAppLayerProtocolTest06);
1215 UtRegisterTest(
"DetectAppLayerProtocolTest07", DetectAppLayerProtocolTest07);
1216 UtRegisterTest(
"DetectAppLayerProtocolTest08", DetectAppLayerProtocolTest08);
1217 UtRegisterTest(
"DetectAppLayerProtocolTest09", DetectAppLayerProtocolTest09);
1218 UtRegisterTest(
"DetectAppLayerProtocolTest10", DetectAppLayerProtocolTest10);
1219 UtRegisterTest(
"DetectAppLayerProtocolTest11", DetectAppLayerProtocolTest11);
1220 UtRegisterTest(
"DetectAppLayerProtocolTest12", DetectAppLayerProtocolTest12);
1221 UtRegisterTest(
"DetectAppLayerProtocolTest13", DetectAppLayerProtocolTest13);
1222 UtRegisterTest(
"DetectAppLayerProtocolTest14", DetectAppLayerProtocolTest14);
1223 UtRegisterTest(
"DetectAppLayerProtocolTest15", DetectAppLayerProtocolTest15);
1224 UtRegisterTest(
"DetectAppLayerProtocolTest16", DetectAppLayerProtocolTest16);
1225 UtRegisterTest(
"DetectAppLayerProtocolTest17", DetectAppLayerProtocolTest17);
1226 UtRegisterTest(
"DetectAppLayerProtocolTest18", DetectAppLayerProtocolTest18);
1227 UtRegisterTest(
"DetectAppLayerProtocolTest19", DetectAppLayerProtocolTest19);
1228 UtRegisterTest(
"DetectAppLayerProtocolTest20", DetectAppLayerProtocolTest20);
1229 UtRegisterTest(
"DetectAppLayerProtocolTest21", DetectAppLayerProtocolTest21);
1230 UtRegisterTest(
"DetectAppLayerProtocolTest22", DetectAppLayerProtocolTest22);
1231 UtRegisterTest(
"DetectAppLayerProtocolTest23", DetectAppLayerProtocolTest23);
1232 UtRegisterTest(
"DetectAppLayerProtocolTest24", DetectAppLayerProtocolTest24);
1233 UtRegisterTest(
"DetectAppLayerProtocolTest25", DetectAppLayerProtocolTest25);
1234 UtRegisterTest(
"DetectAppLayerProtocolTest26", DetectAppLayerProtocolTest26);
1235 UtRegisterTest(
"DetectAppLayerProtocolTest27", DetectAppLayerProtocolTest27);
1236 UtRegisterTest(
"DetectAppLayerProtocolTest28", DetectAppLayerProtocolTest28);
1237 UtRegisterTest(
"DetectAppLayerProtocolTest29", DetectAppLayerProtocolTest29);
1238 UtRegisterTest(
"DetectAppLayerProtocolTest30", DetectAppLayerProtocolTest30);
1239 UtRegisterTest(
"DetectAppLayerProtocolTest31", DetectAppLayerProtocolTest31);
1240 UtRegisterTest(
"DetectAppLayerProtocolTest32", DetectAppLayerProtocolTest32);
1241 UtRegisterTest(
"DetectAppLayerProtocolTest33", DetectAppLayerProtocolTest33);