39 static void DetectAppLayerProtocolRegisterTests(
void);
55 static inline uint32_t AlprotoBitmaskSize(
void)
60 static inline void AlprotoBitmaskSet(uint8_t *bm,
AppProto a)
62 bm[a >> 3] |= (uint8_t)(1u << (a & 7));
65 static inline bool AlprotoBitmaskTest(
const uint8_t *bm,
AppProto a)
67 return (bm[a >> 3] & (uint8_t)(1u << (a & 7))) != 0;
78 static inline bool DetectAppLayerProtocolCompare(
AppProto sigproto,
AppProto alproto,
bool exact)
80 return exact ? (sigproto == alproto) : AppProtoEquals(sigproto, alproto);
87 static void DetectAppLayerProtocolExpand(uint8_t *bm,
AppProto sigproto,
bool exact)
90 if (DetectAppLayerProtocolCompare(sigproto, a, exact))
91 AlprotoBitmaskSet(bm, a);
95 static int DetectAppLayerProtocolPacketMatch(
118 switch (data->
mode) {
161 r = AlprotoBitmaskTest(data->
alprotos, resolved_alproto);
173 #define MAX_ALPROTO_NAME 50
178 static int DetectAppLayerProtocolMapModeName(
const char *
name)
180 if (strcmp(
name,
"final") == 0)
182 if (strcmp(
name,
"original") == 0)
184 if (strcmp(
name,
"either") == 0)
186 if (strcmp(
name,
"to_server") == 0)
188 if (strcmp(
name,
"to_client") == 0)
190 if (strcmp(
name,
"direction") == 0)
222 if (AlprotoBitmaskTest(data->
alprotos, a))
231 static void DetectAppLayerProtocolBuildSupportedList(
char *buf,
size_t buflen)
242 if (alprotos[a] != 1)
248 if (w < 0 || (
size_t)w >= buflen -
offset)
257 static int DetectAppLayerProtocolResolveToken(
258 const char *token,
const char *arg,
bool negate,
AppProto *out)
260 size_t tlen = strlen(token);
262 SCLogError(
"app-layer-protocol keyword value \"%s\" contains an empty token", arg);
266 SCLogError(
"app-layer-protocol keyword token \"%s\" in \"%s\" exceeds the "
267 "maximum token length of %d characters",
271 if (strcmp(token,
"failed") == 0) {
275 if (strcmp(token,
"unknown") == 0) {
277 SCLogError(
"app-layer-protocol keyword can't use negation with protocol 'unknown'");
285 char supported[1024];
286 DetectAppLayerProtocolBuildSupportedList(supported,
sizeof(supported));
287 SCLogError(
"app-layer-protocol keyword supplied with unknown protocol "
288 "\"%s\" in \"%s\"; supported protocols: %s",
289 token, arg, supported);
299 SCLogError(
"app-layer-protocol keyword requires a value");
305 size_t arglen = strlen(arg);
307 SCLogError(
"app-layer-protocol keyword argument too long (\"%s\"): maximum "
308 "supported length is 1024 characters",
313 SCLogError(
"app-layer-protocol keyword value is empty (an empty value list "
314 "is not permitted)");
319 strlcpy(buf, arg,
sizeof(buf));
326 char *qualifiers = strchr(buf,
',');
327 if (qualifiers != NULL) {
330 bool mode_set =
false;
331 char *q = qualifiers;
332 while (q != NULL && *q !=
'\0') {
333 char *
next = strchr(q,
',');
336 if (strcmp(q,
"exact") == 0) {
339 int m = DetectAppLayerProtocolMapModeName(q);
341 SCLogError(
"app-layer-protocol keyword supplied with unknown "
342 "qualifier \"%s\" in \"%s\"",
347 SCLogError(
"app-layer-protocol keyword supplied with multiple "
348 "mode qualifiers in \"%s\"",
378 char *pipe = strchr(cur,
'|');
383 if (DetectAppLayerProtocolResolveToken(cur, arg, negate, &value) < 0)
389 SCLogError(
"app-layer-protocol keyword: 'http' with 'exact' never "
390 "matches (flows are classified as http1/http2); use "
395 if (value_count == 0)
397 DetectAppLayerProtocolExpand(data->
alprotos, value, exact);
405 data->
is_list = (value_count > 1);
412 DetectAppLayerProtocolFree(NULL, data);
419 static bool DetectAppLayerProtocolsConflict(
431 if (AlprotoBitmaskTest(us->
alprotos, a) && AlprotoBitmaskTest(them->
alprotos, a)) {
432 SCLogError(
"conflicting app-layer-protocol rules: "
433 "duplicate or overlapping negated entries under the same mode");
442 SCLogError(
"conflicting app-layer-protocol rules: "
443 "multiple non-negated entries under the same mode");
449 char conflict_buf[512];
450 size_t buf_offset = 0;
451 bool has_intersection =
false;
454 if (!AlprotoBitmaskTest(us->
alprotos, a) || !AlprotoBitmaskTest(them->
alprotos, a))
456 has_intersection =
true;
460 if (buf_offset > 0 && buf_offset <
sizeof(conflict_buf) - 2) {
461 conflict_buf[buf_offset++] =
',';
462 conflict_buf[buf_offset++] =
' ';
464 size_t name_len = strlen(
name);
465 if (buf_offset + name_len <
sizeof(conflict_buf) - 1) {
466 memcpy(conflict_buf + buf_offset,
name, name_len);
467 buf_offset += name_len;
470 conflict_buf[buf_offset] =
'\0';
472 if (has_intersection) {
473 SCLogError(
"conflicting app-layer-protocol rules: "
474 "can't mix positive match with negated match under the same "
475 "mode; intersecting protocol value(s): %s",
478 SCLogError(
"conflicting app-layer-protocol rules: "
479 "can't mix positive app-layer-protocol match with negated "
480 "match or match for 'failed'");
492 "have the rule match on an app layer protocol set through "
493 "other keywords that match on this protocol, or have "
494 "already seen a non-negated app-layer-protocol.");
503 for (; tsm != NULL; tsm = tsm->
next) {
507 if (DetectAppLayerProtocolsConflict(data, them)) {
508 SCLogError(
"conflicting app-layer-protocol options detected "
509 "(see preceding error for details).");
522 DetectAppLayerProtocolFree(
de_ctx, data);
539 static void PrefilterPacketAppProtoMatch(
544 if (!PrefilterPacketHeaderExtraMatch(
ctx,
p)) {
549 if (
p->
flow == NULL) {
561 bool negated = (bool)
ctx->v1.u8[2];
562 bool exact = (
bool)
ctx->v1.u8[4];
563 switch (
ctx->v1.u8[3]) {
588 !DetectAppLayerProtocolCompare(
ctx->v1.u16[0], f->
alproto_tc, exact)) {
589 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
591 !DetectAppLayerProtocolCompare(
ctx->v1.u16[0], f->
alproto_ts, exact)) {
592 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
595 if (DetectAppLayerProtocolCompare(
ctx->v1.u16[0], f->
alproto_tc, exact) ||
596 DetectAppLayerProtocolCompare(
ctx->v1.u16[0], f->
alproto_ts, exact)) {
597 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
606 if (!DetectAppLayerProtocolCompare(
ctx->v1.u16[0], alproto, exact)) {
607 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
611 if (DetectAppLayerProtocolCompare(
ctx->v1.u16[0], alproto, exact)) {
612 PrefilterAddSids(&det_ctx->
pmq,
ctx->sigs_array,
ctx->sigs_cnt);
639 PrefilterPacketAppProtoSet, PrefilterPacketAppProtoCompare,
640 PrefilterPacketAppProtoMatch);
643 static bool PrefilterAppProtoIsPrefilterable(
const Signature *s)
685 static int DetectAppLayerProtocolTest01(
void)
691 DetectAppLayerProtocolFree(NULL, data);
695 static int DetectAppLayerProtocolTest02(
void)
701 DetectAppLayerProtocolFree(NULL, data);
705 static int DetectAppLayerProtocolTest03(
void)
714 "(app-layer-protocol:http; sid:1;)");
729 static int DetectAppLayerProtocolTest04(
void)
738 "(app-layer-protocol:!http; sid:1;)");
755 static int DetectAppLayerProtocolTest05(
void)
764 "(app-layer-protocol:!http; app-layer-protocol:!smtp; sid:1;)");
786 static int DetectAppLayerProtocolTest06(
void)
794 "(app-layer-protocol:smtp; sid:1;)");
800 static int DetectAppLayerProtocolTest07(
void)
808 "(app-layer-protocol:!smtp; sid:1;)");
814 static int DetectAppLayerProtocolTest08(
void)
822 "(app-layer-protocol:!smtp; app-layer-protocol:http; sid:1;)");
828 static int DetectAppLayerProtocolTest09(
void)
836 "(app-layer-protocol:http; app-layer-protocol:!smtp; sid:1;)");
842 static int DetectAppLayerProtocolTest10(
void)
850 "(app-layer-protocol:smtp; app-layer-protocol:!http; sid:1;)");
856 static int DetectAppLayerProtocolTest11(
void)
862 DetectAppLayerProtocolFree(NULL, data);
866 static int DetectAppLayerProtocolTest12(
void)
872 DetectAppLayerProtocolFree(NULL, data);
876 static int DetectAppLayerProtocolTest13(
void)
885 "(app-layer-protocol:failed; sid:1;)");
900 static int DetectAppLayerProtocolTest14(
void)
908 "(app-layer-protocol:http; flowbits:set,blah; sid:1;)");
918 "(app-layer-protocol:http; flow:to_client; sid:2;)");
929 "(app-layer-protocol:http; flow:to_client,established; sid:3;)");
947 static int DetectAppLayerProtocolTest15(
void)
956 DetectAppLayerProtocolFree(NULL, data);
961 static int DetectAppLayerProtocolTest16(
void)
970 DetectAppLayerProtocolFree(NULL, data);
975 static int DetectAppLayerProtocolTest17(
void)
983 DetectAppLayerProtocolFree(NULL, data);
988 static int DetectAppLayerProtocolTest18(
void)
997 static int DetectAppLayerProtocolTest19(
void)
1005 DetectAppLayerProtocolFree(NULL, data);
1010 static int DetectAppLayerProtocolTest20(
void)
1018 static int DetectAppLayerProtocolTest21(
void)
1026 static int DetectAppLayerProtocolTest22(
void)
1030 memset(big,
'a',
sizeof(big) - 1);
1031 big[
sizeof(big) - 1] =
'\0';
1038 static int DetectAppLayerProtocolTest23(
void)
1046 static int DetectAppLayerProtocolTest24(
void)
1054 DetectAppLayerProtocolFree(NULL, data);
1059 static int DetectAppLayerProtocolTest25(
void)
1067 static int DetectAppLayerProtocolTest26(
void)
1074 "(app-layer-protocol:!tls; sid:1;)");
1091 static int DetectAppLayerProtocolTest27(
void)
1098 "(app-layer-protocol:tls|dns; sid:1;)");
1112 FAIL_IF(PrefilterAppProtoIsPrefilterable(s));
1119 static int DetectAppLayerProtocolTest28(
void)
1127 "alert tcp any any -> any any "
1128 "(tls.sni; content:\"example.com\"; app-layer-protocol:tls|dns; sid:1;)");
1136 static int DetectAppLayerProtocolTest29(
void)
1143 DetectAppLayerProtocolFree(NULL, data);
1148 static int DetectAppLayerProtocolTest30(
void)
1155 DetectAppLayerProtocolFree(NULL, data);
1160 static int DetectAppLayerProtocolTest31(
void)
1168 static int DetectAppLayerProtocolTest32(
void)
1178 DetectAppLayerProtocolFree(NULL, data);
1183 static int DetectAppLayerProtocolTest33(
void)
1190 "(app-layer-protocol:tls; sid:1;)");
1208 static void DetectAppLayerProtocolRegisterTests(
void)
1210 UtRegisterTest(
"DetectAppLayerProtocolTest01", DetectAppLayerProtocolTest01);
1211 UtRegisterTest(
"DetectAppLayerProtocolTest02", DetectAppLayerProtocolTest02);
1212 UtRegisterTest(
"DetectAppLayerProtocolTest03", DetectAppLayerProtocolTest03);
1213 UtRegisterTest(
"DetectAppLayerProtocolTest04", DetectAppLayerProtocolTest04);
1214 UtRegisterTest(
"DetectAppLayerProtocolTest05", DetectAppLayerProtocolTest05);
1215 UtRegisterTest(
"DetectAppLayerProtocolTest06", DetectAppLayerProtocolTest06);
1216 UtRegisterTest(
"DetectAppLayerProtocolTest07", DetectAppLayerProtocolTest07);
1217 UtRegisterTest(
"DetectAppLayerProtocolTest08", DetectAppLayerProtocolTest08);
1218 UtRegisterTest(
"DetectAppLayerProtocolTest09", DetectAppLayerProtocolTest09);
1219 UtRegisterTest(
"DetectAppLayerProtocolTest10", DetectAppLayerProtocolTest10);
1220 UtRegisterTest(
"DetectAppLayerProtocolTest11", DetectAppLayerProtocolTest11);
1221 UtRegisterTest(
"DetectAppLayerProtocolTest12", DetectAppLayerProtocolTest12);
1222 UtRegisterTest(
"DetectAppLayerProtocolTest13", DetectAppLayerProtocolTest13);
1223 UtRegisterTest(
"DetectAppLayerProtocolTest14", DetectAppLayerProtocolTest14);
1224 UtRegisterTest(
"DetectAppLayerProtocolTest15", DetectAppLayerProtocolTest15);
1225 UtRegisterTest(
"DetectAppLayerProtocolTest16", DetectAppLayerProtocolTest16);
1226 UtRegisterTest(
"DetectAppLayerProtocolTest17", DetectAppLayerProtocolTest17);
1227 UtRegisterTest(
"DetectAppLayerProtocolTest18", DetectAppLayerProtocolTest18);
1228 UtRegisterTest(
"DetectAppLayerProtocolTest19", DetectAppLayerProtocolTest19);
1229 UtRegisterTest(
"DetectAppLayerProtocolTest20", DetectAppLayerProtocolTest20);
1230 UtRegisterTest(
"DetectAppLayerProtocolTest21", DetectAppLayerProtocolTest21);
1231 UtRegisterTest(
"DetectAppLayerProtocolTest22", DetectAppLayerProtocolTest22);
1232 UtRegisterTest(
"DetectAppLayerProtocolTest23", DetectAppLayerProtocolTest23);
1233 UtRegisterTest(
"DetectAppLayerProtocolTest24", DetectAppLayerProtocolTest24);
1234 UtRegisterTest(
"DetectAppLayerProtocolTest25", DetectAppLayerProtocolTest25);
1235 UtRegisterTest(
"DetectAppLayerProtocolTest26", DetectAppLayerProtocolTest26);
1236 UtRegisterTest(
"DetectAppLayerProtocolTest27", DetectAppLayerProtocolTest27);
1237 UtRegisterTest(
"DetectAppLayerProtocolTest28", DetectAppLayerProtocolTest28);
1238 UtRegisterTest(
"DetectAppLayerProtocolTest29", DetectAppLayerProtocolTest29);
1239 UtRegisterTest(
"DetectAppLayerProtocolTest30", DetectAppLayerProtocolTest30);
1240 UtRegisterTest(
"DetectAppLayerProtocolTest31", DetectAppLayerProtocolTest31);
1241 UtRegisterTest(
"DetectAppLayerProtocolTest32", DetectAppLayerProtocolTest32);
1242 UtRegisterTest(
"DetectAppLayerProtocolTest33", DetectAppLayerProtocolTest33);