192 static void AppLayerProtoDetectPEGetIpprotos(
AppProto alproto,
203 uint32_t buflen, uint16_t searchlen,
bool *rflow)
208 SCLogDebug(
"s->co->offset (%"PRIu16
") > searchlen (%"PRIu16
")",
212 if (s->
cd->
depth > searchlen) {
213 SCLogDebug(
"s->co->depth (%"PRIu16
") > searchlen (%"PRIu16
")",
218 const uint8_t *sbuf = buf + s->
cd->
offset;
220 SCLogDebug(
"s->co->offset (%"PRIu16
") s->cd->depth (%"PRIu16
")",
229 uint8_t direction = (
flags & (STREAM_TOSERVER | STREAM_TOCLIENT));
230 SCLogDebug(
"matching, s->direction %s, our dir %s",
231 (s->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
232 (
flags & STREAM_TOSERVER) ?
"toserver" :
"toclient");
237 SCLogDebug(
"direction is wrong, rflow = true");
243 SCLogDebug(
"PP can't be run yet as pp_min_depth %u > buflen %u",
251 SCLogDebug(
"found %s/%u, rdir %02x reverse_flow? %s",
253 (rdir && direction != rdir) ?
"true" :
"false");
254 *rflow = (rdir && direction != rdir);
261 SCLogDebug(
"depth reached and answer inconclusive: fail");
276 uint32_t buflen, uint8_t
flags,
AppProto *pm_results,
bool *rflow)
282 SCLogDebug(
"searchlen %u buflen %u", searchlen, buflen);
288 if (search_cnt == 0) {
296 memset(pm_results_bf, 0,
sizeof(pm_results_bf));
305 s, tctx, f,
flags, buf, buflen, searchlen, rflow);
308 if (AppProtoIsValid(
proto) &&
309 !(pm_results_bf[
proto / 8] & (1 << (
proto % 8))) )
311 pm_results[pm_matches++] =
proto;
317 if (pm_matches == 0 && buflen >= pm_ctx->
pp_max_len) {
329 const uint8_t *buf, uint32_t buflen, uint8_t
flags,
AppProto *pm_results,
bool *rflow)
344 if (
flags & STREAM_TOSERVER) {
352 m = PMGetProtoInspect(tctx, pm_ctx, mpm_tctx, f, buf, buflen,
flags, pm_results, rflow);
373 if (
flags & STREAM_TOSERVER) {
380 SCLogDebug(
"no matches and in midstream mode, lets try the "
381 "*patterns for the other side");
385 om = PMGetProtoInspect(
386 tctx, pm_ctx, mpm_tctx, f, buf, buflen,
flags, pm_results, rflow);
394 }
else if (om < 0 &&
m &&
m < 0) {
399 }
else if (om == 0 ||
m == 0) {
421 while (pp_port != NULL) {
422 if (pp_port->
dp != NULL && pp_port->
dp->
alproto == alproto) {
423 pp_elem = pp_port->
dp;
426 if (pp_port->
sp != NULL && pp_port->
sp->
alproto == alproto) {
427 pp_elem = pp_port->
sp;
430 pp_port = pp_port->
next;
433 SCReturnPtr(pp_elem,
"AppLayerProtoDetectProbingParserElement *");
453 while (pp_port != NULL) {
458 pp_port = pp_port->
next;
462 SCReturnPtr(pp_port,
"AppLayerProtoDetectProbingParserPort *");
483 uint8_t
flags,
const uint8_t *buf, uint32_t buflen, uint32_t *alproto_masks, uint8_t *rdir)
486 if ((buflen < pe->min_depth) ||
498 if (AppProtoIsValid(alproto)) {
518 static AppProto AppLayerProtoDetectPPGetProto(
Flow *f,
const uint8_t *buf, uint32_t buflen,
519 uint8_t ipproto,
const uint8_t
flags,
bool *reverse_flow)
527 uint32_t *alproto_masks = NULL;
529 uint8_t idir = (
flags & (STREAM_TOSERVER | STREAM_TOCLIENT));
533 bool probe_is_found =
false;
540 (dir == STREAM_TOSERVER) ?
"toserver" :
"toclient");
542 if (dir == STREAM_TOSERVER) {
544 pp_port_dp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, dp);
546 if (pp_port_dp != NULL) {
547 SCLogDebug(
"toserver - Probing parser found for destination port %"PRIu16, dp);
550 pe1 = pp_port_dp->
dp;
552 SCLogDebug(
"toserver - No probing parser registered for dest port %"PRIu16, dp);
555 pp_port_sp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, sp);
556 if (pp_port_sp != NULL) {
557 SCLogDebug(
"toserver - Probing parser found for source port %"PRIu16, sp);
560 pe2 = pp_port_sp->
sp;
562 SCLogDebug(
"toserver - No probing parser registered for source port %"PRIu16, sp);
566 pp_port_dp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, dp);
572 if (pp_port_dp != NULL) {
573 SCLogDebug(
"toclient - Probing parser found for destination port %"PRIu16, dp);
576 pe1 = pp_port_dp->
dp;
578 SCLogDebug(
"toclient - No probing parser registered for dest port %"PRIu16, dp);
581 pp_port_sp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, sp);
582 if (pp_port_sp != NULL) {
583 SCLogDebug(
"toclient - Probing parser found for source port %"PRIu16, sp);
585 pe2 = pp_port_sp->
sp;
587 SCLogDebug(
"toclient - No probing parser registered for source port %"PRIu16, sp);
595 pe0 = AppLayerProtoDetectGetProbingParser(alpd_ctx.
ctx_pp, ipproto, f->
alproto_tc);
597 pe0 = AppLayerProtoDetectGetProbingParser(alpd_ctx.
ctx_pp, ipproto, f->
alproto_ts);
600 if (pe1 == NULL && pe2 == NULL && pe0 == NULL) {
601 SCLogDebug(
"%s - No probing parsers found for either port",
602 (dir == STREAM_TOSERVER) ?
"toserver":
"toclient");
605 probe_is_found =
true;
610 alproto = PPGetProto(pe0, f,
flags, buf, buflen, alproto_masks, &rdir);
611 if (AppProtoIsValid(alproto))
613 alproto = PPGetProto(pe1, f,
flags, buf, buflen, alproto_masks, &rdir);
614 if (AppProtoIsValid(alproto))
616 alproto = PPGetProto(pe2, f,
flags, buf, buflen, alproto_masks, &rdir);
617 if (AppProtoIsValid(alproto))
622 if (pp_port_dp && pp_port_sp)
629 if ((alproto_masks[0] & mask) == mask) {
631 SCLogDebug(
"%s, mask is now %08x, needed %08x, so done",
632 (dir == STREAM_TOSERVER) ?
"toserver":
"toclient",
633 alproto_masks[0], mask);
636 (dir == STREAM_TOSERVER) ?
"toserver":
"toclient",
637 alproto_masks[0], mask);
643 if (idir == STREAM_TOSERVER) {
644 dir = STREAM_TOCLIENT;
646 dir = STREAM_TOSERVER;
648 SCLogDebug(
"no match + midstream, retry the other direction %s",
649 (dir == STREAM_TOSERVER) ?
"toserver" :
"toclient");
650 goto again_midstream;
651 }
else if (!probe_is_found) {
656 if (AppProtoIsValid(alproto) && rdir != 0 && rdir != idir) {
657 SCLogDebug(
"PP found %u, is reverse flow", alproto);
658 *reverse_flow =
true;
662 (idir == STREAM_TOSERVER) ?
"toserver":
"toclient", alproto_masks[0]);
668 static void AppLayerProtoDetectPPGetIpprotos(
AppProto alproto,
677 for (pp = alpd_ctx.
ctx_pp; pp != NULL; pp = pp->
next) {
678 for (pp_port = pp->
port; pp_port != NULL; pp_port = pp_port->
next) {
679 for (pp_pe = pp_port->
dp; pp_pe != NULL; pp_pe = pp_pe->
next) {
683 for (pp_pe = pp_port->
sp; pp_pe != NULL; pp_pe = pp_pe->
next) {
693 static uint32_t AppLayerProtoDetectProbingParserGetMask(
AppProto alproto)
697 if (!AppProtoIsValid(alproto)) {
698 FatalError(
"Unknown protocol detected - %u", alproto);
714 SCReturnPtr(p,
"AppLayerProtoDetectProbingParserElement");
735 SCReturnPtr(p,
"AppLayerProtoDetectProbingParserPort");
747 AppLayerProtoDetectProbingParserElementFree(e);
754 AppLayerProtoDetectProbingParserElementFree(e);
772 SCReturnPtr(p,
"AppLayerProtoDetectProbingParser");
782 AppLayerProtoDetectProbingParserPortFree(pt);
792 AppProto alproto, uint16_t min_depth, uint16_t max_depth)
797 pe->
alproto_mask = AppLayerProtoDetectProbingParserGetMask(alproto);
802 if (max_depth != 0 && min_depth >= max_depth) {
804 "register the probing parser. min_depth >= max_depth");
807 if (alproto <= ALPROTO_UNKNOWN || alproto >=
g_alproto_max) {
808 SCLogError(
"Invalid arguments sent to register "
809 "the probing parser. Invalid alproto - %d",
814 SCReturnPtr(pe,
"AppLayerProtoDetectProbingParserElement");
816 AppLayerProtoDetectProbingParserElementFree(pe);
817 SCReturnPtr(NULL,
"AppLayerProtoDetectProbingParserElement");
835 SCReturnPtr(new_pe,
"AppLayerProtoDetectProbingParserElement");
846 printf(
"\nProtocol Detection Configuration\n");
848 for ( ; pp != NULL; pp = pp->
next) {
850 if (pp->
ipproto == IPPROTO_TCP)
851 printf(
"IPProto: TCP\n");
852 else if (pp->
ipproto == IPPROTO_UDP)
853 printf(
"IPProto: UDP\n");
855 printf(
"IPProto: %"PRIu8
"\n", pp->
ipproto);
858 for ( ; pp_port != NULL; pp_port = pp_port->
next) {
859 if (pp_port->
dp != NULL) {
860 printf(
" Port: %"PRIu16
"\n", pp_port->
port);
862 printf(
" Destination port: (max-depth: %"PRIu16
", "
863 "mask - %"PRIu32
")\n",
867 for ( ; pp_pe != NULL; pp_pe = pp_pe->
next) {
871 printf(
" min_depth: %"PRIu32
"\n", pp_pe->
min_depth);
872 printf(
" max_depth: %"PRIu32
"\n", pp_pe->
max_depth);
878 if (pp_port->
sp == NULL) {
882 printf(
" Source port: (max-depth: %"PRIu16
", "
883 "mask - %"PRIu32
")\n",
887 for ( ; pp_pe != NULL; pp_pe = pp_pe->
next) {
891 printf(
" min_depth: %"PRIu32
"\n", pp_pe->
min_depth);
892 printf(
" max_depth: %"PRIu32
"\n", pp_pe->
max_depth);
908 if (*head_pe == NULL) {
914 while (temp_pe->
next != NULL)
915 temp_pe = temp_pe->
next;
916 temp_pe->
next = new_pe;
926 if (*head_pp == NULL) {
932 while (temp_pp->
next != NULL)
933 temp_pp = temp_pp->
next;
934 temp_pp->
next = new_pp;
945 if (*head_port == NULL) {
946 *head_port = new_port;
951 if ((*head_port)->port == 0 && (*head_port)->use_ports) {
952 new_port->
next = *head_port;
953 *head_port = new_port;
956 while (temp_port->
next != NULL &&
958 temp_port = temp_port->
next;
961 temp_port->
next = new_port;
969 uint8_t ipproto,
bool use_ports, uint16_t port,
AppProto alproto, uint16_t min_depth,
977 while (curr_pp != NULL) {
978 if (curr_pp->
ipproto == ipproto)
980 curr_pp = curr_pp->
next;
982 if (curr_pp == NULL) {
985 AppLayerProtoDetectProbingParserAppend(pp, new_pp);
991 while (curr_port != NULL) {
993 if (curr_port->
port == port && use_ports)
995 curr_port = curr_port->
next;
997 if (curr_port == NULL) {
999 new_port->
port = port;
1001 AppLayerProtoDetectProbingParserPortAppend(&curr_pp->
port, new_port);
1002 curr_port = new_port;
1003 if (direction & STREAM_TOSERVER) {
1011 zero_port = curr_pp->
port;
1013 while (zero_port != NULL && !(zero_port->
port == 0 && zero_port->
use_ports)) {
1014 zero_port = zero_port->
next;
1016 if (zero_port != NULL) {
1019 zero_pe = zero_port->
dp;
1020 for ( ; zero_pe != NULL; zero_pe = zero_pe->
next) {
1021 if (curr_port->
dp == NULL)
1031 AppLayerProtoDetectProbingParserElementDuplicate(zero_pe);
1032 AppLayerProtoDetectProbingParserElementAppend(&curr_port->
dp, dup_pe);
1036 zero_pe = zero_port->
sp;
1037 for ( ; zero_pe != NULL; zero_pe = zero_pe->
next) {
1038 if (curr_port->
sp == NULL)
1048 AppLayerProtoDetectProbingParserElementDuplicate(zero_pe);
1049 AppLayerProtoDetectProbingParserElementAppend(&curr_port->
sp, dup_pe);
1057 if (direction & STREAM_TOSERVER)
1058 curr_pe = curr_port->
dp;
1060 curr_pe = curr_port->
sp;
1061 while (curr_pe != NULL) {
1062 if (curr_pe->
alproto == alproto) {
1064 "ipproto - %" PRIu8
" Port - %" PRIu16
" "
1065 "App Protocol - NULL, App Protocol(ID) - "
1066 "%" PRIu16
" min_depth - %" PRIu16
" "
1067 "max_dept - %" PRIu16
".",
1068 ipproto, port, alproto, min_depth, max_depth);
1071 curr_pe = curr_pe->
next;
1075 AppLayerProtoDetectProbingParserElementCreate(alproto, min_depth, max_depth);
1080 if (direction & STREAM_TOSERVER) {
1083 if (curr_port->
dp == NULL)
1092 head_pe = &curr_port->
dp;
1096 if (curr_port->
sp == NULL)
1105 head_pe = &curr_port->
sp;
1107 AppLayerProtoDetectProbingParserElementAppend(head_pe, new_pe);
1112 while (temp_port != NULL && !(temp_port->
port == 0 && temp_port->
use_ports)) {
1113 if (direction & STREAM_TOSERVER) {
1114 if (temp_port->
dp == NULL)
1122 AppLayerProtoDetectProbingParserElementAppend(&temp_port->
dp,
1123 AppLayerProtoDetectProbingParserElementDuplicate(curr_pe));
1126 if (temp_port->
sp == NULL)
1134 AppLayerProtoDetectProbingParserElementAppend(&temp_port->
sp,
1135 AppLayerProtoDetectProbingParserElementDuplicate(curr_pe));
1138 temp_port = temp_port->
next;
1148 static void AppLayerProtoDetectPMGetIpprotos(
AppProto alproto,
1155 for (
int j = 0; j < 2; j++) {
1161 ipprotos[ipproto / 8] |= 1 << (ipproto % 8);
1173 typedef struct TempContainer_ {
1175 uint16_t content_len;
1180 uint32_t struct_total_size = 0;
1181 uint32_t content_total_size = 0;
1183 uint8_t *ahb = NULL;
1184 uint8_t *content = NULL;
1185 uint16_t content_len = 0;
1187 TempContainer *struct_offset = NULL;
1188 uint8_t *content_offset = NULL;
1191 if (
ctx->head == NULL)
1194 for (s =
ctx->head; s != NULL; s = s->
next) {
1195 struct_total_size +=
sizeof(TempContainer);
1200 ahb =
SCMalloc(
sizeof(uint8_t) * (struct_total_size + content_total_size));
1204 struct_offset = (TempContainer *)ahb;
1205 content_offset = ahb + struct_total_size;
1206 for (s =
ctx->head; s != NULL; s = s->
next) {
1207 TempContainer *tcdup = (TempContainer *)ahb;
1211 for (; tcdup != struct_offset; tcdup++) {
1212 if (tcdup->content_len != content_len ||
1213 SCMemcmp(tcdup->content, content, tcdup->content_len) != 0)
1220 if (tcdup != struct_offset) {
1221 s->
cd->
id = tcdup->id;
1225 struct_offset->content_len = content_len;
1226 struct_offset->content = content_offset;
1227 content_offset += content_len;
1228 memcpy(struct_offset->content, content, content_len);
1229 struct_offset->id = max_id++;
1230 s->
cd->
id = struct_offset->id;
1235 ctx->max_pat_id = max_id;
1256 if (
ctx->map == NULL)
1260 for (s =
ctx->head; s != NULL; ) {
1280 ctx->map[s->
id] = s;
1322 AppProto alproto, uint8_t direction,
1324 uint16_t pp_min_depth, uint16_t pp_max_depth)
1346 static int AppLayerProtoDetectPMRegisterPattern(uint8_t ipproto,
AppProto alproto,
1347 const char *pattern,
1348 uint16_t depth, uint16_t
offset,
1352 uint16_t pp_min_depth, uint16_t pp_max_depth)
1376 if (depth < cd->content_len)
1379 if (direction & STREAM_TOSERVER)
1386 if (depth < ctx_pm->min_len)
1390 AppLayerProtoDetectPMAddSignature(ctx_pm, cd, alproto, direction,
1391 PPFunc, pp_min_depth, pp_max_depth);
1404 const uint8_t *buf, uint32_t buflen, uint8_t ipproto, uint8_t
flags,
bool *reverse_flow)
1407 SCLogDebug(
"buflen %u for %s direction", buflen,
1408 (
flags & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1414 uint16_t pm_matches = AppLayerProtoDetectPMGetProto(
1415 tctx, f, buf, buflen,
flags, pm_results, reverse_flow);
1416 if (pm_matches > 0) {
1418 alproto = pm_results[0];
1421 uint8_t reverse_dir = (
flags & STREAM_TOSERVER) ? STREAM_TOCLIENT : STREAM_TOSERVER;
1434 alproto = AppLayerProtoDetectPPGetProto(f, buf, buflen, ipproto,
flags, reverse_flow);
1435 if (AppProtoIsValid(alproto)) {
1443 alproto = AppLayerProtoDetectPEGetProto(f,
flags);
1458 while (pp != NULL) {
1460 AppLayerProtoDetectProbingParserFree(pp);
1468 static void AppLayerProtoDetectFreeAliases(
void)
1473 if (cur_alias == NULL)
1477 while (cur_alias != NULL) {
1478 next_alias = cur_alias->
next;
1480 cur_alias = next_alias;
1500 for (j = 0; j < 2; j++) {
1503 if (AppLayerProtoDetectPMSetContentIDs(ctx_pm) < 0)
1509 if (AppLayerProtoDetectPMMapSignatures(ctx_pm) < 0)
1511 if (AppLayerProtoDetectPMPrepareMpm(ctx_pm) < 0)
1518 AppLayerProtoDetectPrintProbingParsers(alpd_ctx.
ctx_pp);
1536 const char *portstr,
1538 uint16_t min_depth, uint16_t max_depth,
1546 if (portstr == NULL) {
1549 AppLayerProtoDetectInsertNewProbingParser(&alpd_ctx.
ctx_pp, ipproto,
false, 0, alproto,
1550 min_depth, max_depth, direction, ProbingParser1, ProbingParser2);
1555 while (temp_dp != NULL) {
1556 uint16_t port = temp_dp->
port;
1557 if (port == 0 && temp_dp->
port2 != 0)
1560 AppLayerProtoDetectInsertNewProbingParser(&alpd_ctx.
ctx_pp, ipproto,
true, port,
1561 alproto, min_depth, max_depth, direction, ProbingParser1, ProbingParser2);
1562 if (port == temp_dp->
port2) {
1568 temp_dp = temp_dp->
next;
1577 const char *alproto_name,
1579 uint16_t min_depth, uint16_t max_depth,
1591 r = snprintf(param,
sizeof(param),
"%s%s%s",
"app-layer.protocols.",
1592 alproto_name,
".detection-ports");
1595 }
else if (r > (
int)
sizeof(param)) {
1596 FatalError(
"buffer not big enough to write param.");
1600 SCLogDebug(
"Entry for %s not found.", param);
1601 r = snprintf(param,
sizeof(param),
"%s%s%s%s%s",
"app-layer.protocols.",
1602 alproto_name,
".", ipproto_name,
".detection-ports");
1605 }
else if (r > (
int)
sizeof(param)) {
1606 FatalError(
"buffer not big enough to write param.");
1615 if (port_node == NULL)
1618 if (port_node != NULL && port_node->
val != NULL) {
1622 min_depth, max_depth,
1624 ProbingParserTs, ProbingParserTc);
1629 if (port_node == NULL)
1632 if (port_node != NULL && port_node->
val != NULL) {
1636 min_depth, max_depth,
1638 ProbingParserTc, ProbingParserTs);
1650 const char *pattern,
1651 uint16_t depth, uint16_t
offset,
1655 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1663 const char *pattern, uint16_t depth, uint16_t
offset,
1666 uint16_t pp_min_depth, uint16_t pp_max_depth)
1669 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1672 PPFunc, pp_min_depth, pp_max_depth);
1677 const char *pattern,
1678 uint16_t depth, uint16_t
offset,
1682 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1697 memset(&alpd_ctx, 0,
sizeof(alpd_ctx));
1704 FatalError(
"Unable to alloc SpmGlobalThreadCtx.");
1708 for (j = 0; j < 2; j++) {
1715 FatalError(
"Unable to alloc alproto_names.");
1720 FatalError(
"Unable to alloc expectation_proto.");
1734 int ipproto_map = 0;
1741 for (dir = 0; dir < 2; dir++) {
1744 for (
id = 0;
id < pm_ctx->
max_sig_id;
id++) {
1745 sig = pm_ctx->
map[id];
1746 AppLayerProtoDetectPMFreeSignature(sig);
1760 AppLayerProtoDetectFreeAliases();
1762 AppLayerProtoDetectFreeProbingParsers(alpd_ctx.
ctx_pp);
1789 new_alias->
next = NULL;
1791 if (alpda_ctx == NULL) {
1792 alpda_ctx = new_alias;
1795 while (cur_alias->
next != NULL) {
1796 cur_alias = cur_alias->
next;
1798 cur_alias->
next = new_alias;
1862 if (new_proto != f->
alproto) {
1890 const char *ipproto,
const char *alproto,
bool default_enabled)
1894 BUG_ON(ipproto == NULL || alproto == NULL);
1904 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1906 default_enabled =
true;
1909 r = snprintf(param,
sizeof(param),
"%s%s%s",
"app-layer.protocols.",
1910 alproto,
".enabled");
1913 }
else if (r > (
int)
sizeof(param)) {
1914 FatalError(
"buffer not big enough to write param.");
1919 SCLogDebug(
"Entry for %s not found.", param);
1920 r = snprintf(param,
sizeof(param),
"%s%s%s%s%s",
"app-layer.protocols.",
1921 alproto,
".", ipproto,
".enabled");
1924 }
else if (r > (
int)
sizeof(param)) {
1925 FatalError(
"buffer not big enough to write param.");
1930 SCLogDebug(
"Entry for %s not found.", param);
1931 if (default_enabled) {
1944 }
else if (strcasecmp(node->
val,
"detection-only") == 0) {
1950 SCLogError(
"Invalid value found for %s.", param);
1975 for (j = 0; j < 2; j++) {
1976 if (max_pat_id == 0) {
1996 for (j = 0; j < 2; j++) {
2023 int ipproto_map, dir;
2026 for (dir = 0; dir < 2; dir++) {
2055 AppLayerProtoDetectPMGetIpprotos(alproto, ipprotos);
2056 AppLayerProtoDetectPPGetIpprotos(alproto, ipprotos);
2057 AppLayerProtoDetectPEGetIpprotos(alproto, ipprotos);
2068 while (cur_alias != NULL) {
2069 if (strcasecmp(alproto_name, cur_alias->
proto_alias) == 0) {
2073 cur_alias = cur_alias->
next;
2079 if (alpd_ctx.
alproto_names[a] != NULL && AppProtoEquals(b, a)) {
2115 alprotos[alproto] = 1;
2121 static void AppLayerProtoDetectPEGetIpprotos(
AppProto alproto,
2125 ipprotos[IPPROTO_TCP / 8] |= 1 << (IPPROTO_TCP % 8);
2128 ipprotos[IPPROTO_UDP / 8] |= 1 << (IPPROTO_UDP % 8);
2136 SCLogError(
"Expectation on 2 IP protocols are not supported");
2154 alpd_ctx_ut = alpd_ctx;
2155 memset(&alpd_ctx, 0,
sizeof(alpd_ctx));
2162 alpd_ctx = alpd_ctx_ut;
2163 memset(&alpd_ctx_ut, 0,
sizeof(alpd_ctx_ut));
2167 static int AppLayerProtoDetectTest01(
void)
2172 const char *buf =
"HTTP";
2186 static int AppLayerProtoDetectTest02(
void)
2191 const char *buf =
"HTTP";
2211 static int AppLayerProtoDetectTest03(
void)
2216 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2218 memset(pm_results, 0,
sizeof(pm_results));
2220 memset(&f, 0x00,
sizeof(f));
2224 const char *buf =
"HTTP";
2243 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2244 &f, l7data,
sizeof(l7data),
2246 pm_results, &rflow);
2256 static int AppLayerProtoDetectTest04(
void)
2261 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2263 memset(&f, 0x00,
sizeof(f));
2265 memset(pm_results, 0,
sizeof(pm_results));
2268 const char *buf =
"200 ";
2284 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2285 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2296 static int AppLayerProtoDetectTest05(
void)
2301 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n<HTML><BODY>Blahblah</BODY></HTML>";
2303 memset(pm_results, 0,
sizeof(pm_results));
2305 memset(&f, 0x00,
sizeof(f));
2308 const char *buf =
"HTTP";
2327 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2328 &f, l7data,
sizeof(l7data),
2340 static int AppLayerProtoDetectTest06(
void)
2345 uint8_t l7data[] =
"220 Welcome to the OISF FTP server\r\n";
2347 memset(pm_results, 0,
sizeof(pm_results));
2349 memset(&f, 0x00,
sizeof(f));
2352 const char *buf =
"HTTP";
2371 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2372 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2383 static int AppLayerProtoDetectTest07(
void)
2388 uint8_t l7data[] =
"220 Welcome to the OISF HTTP/FTP server\r\n";
2390 memset(&f, 0x00,
sizeof(f));
2393 memset(pm_results, 0,
sizeof(pm_results));
2395 const char *buf =
"HTTP";
2410 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2411 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2421 static int AppLayerProtoDetectTest08(
void)
2426 uint8_t l7data[] = {
2427 0x00, 0x00, 0x00, 0x85, 0xff, 0x53, 0x4d, 0x42,
2428 0x72, 0x00, 0x00, 0x00, 0x00, 0x18, 0x53, 0xc8,
2429 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2430 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfe,
2431 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00, 0x02,
2432 0x50, 0x43, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f,
2433 0x52, 0x4b, 0x20, 0x50, 0x52, 0x4f, 0x47, 0x52,
2434 0x41, 0x4d, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02,
2435 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x31, 0x2e,
2436 0x30, 0x00, 0x02, 0x57, 0x69, 0x6e, 0x64, 0x6f,
2437 0x77, 0x73, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x57,
2438 0x6f, 0x72, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x70,
2439 0x73, 0x20, 0x33, 0x2e, 0x31, 0x61, 0x00, 0x02,
2440 0x4c, 0x4d, 0x31, 0x2e, 0x32, 0x58, 0x30, 0x30,
2441 0x32, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41,
2442 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x4e, 0x54,
2443 0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32,
2447 memset(pm_results, 0,
sizeof(pm_results));
2449 memset(&f, 0x00,
sizeof(f));
2452 const char *buf =
"|ff|SMB";
2468 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2469 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2480 static int AppLayerProtoDetectTest09(
void)
2485 uint8_t l7data[] = {
2486 0x00, 0x00, 0x00, 0x66, 0xfe, 0x53, 0x4d, 0x42,
2487 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2488 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00,
2489 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2490 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2491 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2492 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2493 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2494 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x01, 0x00,
2495 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2496 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2497 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2498 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2502 memset(pm_results, 0,
sizeof(pm_results));
2504 memset(&f, 0x00,
sizeof(f));
2507 const char *buf =
"|fe|SMB";
2523 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2524 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2535 static int AppLayerProtoDetectTest10(
void)
2540 uint8_t l7data[] = {
2541 0x05, 0x00, 0x0b, 0x03, 0x10, 0x00, 0x00, 0x00,
2542 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2543 0xd0, 0x16, 0xd0, 0x16, 0x00, 0x00, 0x00, 0x00,
2544 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
2545 0xb8, 0x4a, 0x9f, 0x4d, 0x1c, 0x7d, 0xcf, 0x11,
2546 0x86, 0x1e, 0x00, 0x20, 0xaf, 0x6e, 0x7c, 0x57,
2547 0x00, 0x00, 0x00, 0x00, 0x04, 0x5d, 0x88, 0x8a,
2548 0xeb, 0x1c, 0xc9, 0x11, 0x9f, 0xe8, 0x08, 0x00,
2549 0x2b, 0x10, 0x48, 0x60, 0x02, 0x00, 0x00, 0x00
2552 memset(pm_results, 0,
sizeof(pm_results));
2554 memset(&f, 0x00,
sizeof(f));
2557 const char *buf =
"|05 00|";
2573 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2574 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2589 static int AppLayerProtoDetectTest11(
void)
2594 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2595 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2597 memset(pm_results, 0,
sizeof(pm_results));
2599 memset(&f, 0x00,
sizeof(f));
2611 IPPROTO_TCP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2613 IPPROTO_TCP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2615 IPPROTO_TCP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2640 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2641 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2646 memset(pm_results, 0,
sizeof(pm_results));
2648 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2662 static int AppLayerProtoDetectTest12(
void)
2674 printf(
"failure 1\n");
2680 printf(
"failure 2\n");
2686 printf(
"failure 3\n");
2690 printf(
"failure 4\n");
2694 printf(
"failure 5\n");
2698 printf(
"failure 6\n");
2714 static int AppLayerProtoDetectTest13(
void)
2719 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2720 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2724 memset(&f, 0x00,
sizeof(f));
2736 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2738 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2740 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2761 memset(pm_results, 0,
sizeof(pm_results));
2763 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2764 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2768 memset(pm_results, 0,
sizeof(pm_results));
2770 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2785 static int AppLayerProtoDetectTest14(
void)
2790 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2791 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2795 memset(&f, 0x00,
sizeof(f));
2807 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2809 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2811 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2833 memset(pm_results, 0,
sizeof(pm_results));
2836 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2841 memset(pm_results, 0,
sizeof(pm_results));
2843 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2888 int i = -1, j = -1 , k = -1;
2892 for (i = 0; i < no_of_ip_proto; i++, pp = pp->
next) {
2893 if (pp->ipproto != ip_proto[i].
ipproto)
2897 for (k = 0; k < ip_proto[i].
no_of_port; k++, pp_port = pp_port->
next) {
2898 if (pp_port->port != ip_proto[i].
port[k].
port)
2914 j++, pp_element = pp_element->
next) {
2929 if (pp_element != NULL)
2932 pp_element = pp_port->sp;
2950 if (pp_element != NULL)
2953 if (pp_port != NULL)
2962 printf(
"i = %d, k = %d, j = %d(%s)\n", i, k, j, (dir == 0) ?
"ts" :
"tc");
2967 static uint16_t ProbingParserDummyForTesting(
Flow *f, uint8_t direction,
2968 const uint8_t *input,
2969 uint32_t input_len, uint8_t *rdir)
2974 static int AppLayerProtoDetectTest15(
void)
2982 ProbingParserDummyForTesting, NULL);
2988 ProbingParserDummyForTesting, NULL);
2994 ProbingParserDummyForTesting, NULL);
3001 ProbingParserDummyForTesting, NULL);
3007 ProbingParserDummyForTesting, NULL);
3013 ProbingParserDummyForTesting, NULL);
3019 ProbingParserDummyForTesting, NULL);
3026 ProbingParserDummyForTesting, NULL);
3032 ProbingParserDummyForTesting, NULL);
3040 ProbingParserDummyForTesting, NULL);
3048 ProbingParserDummyForTesting, NULL);
3054 ProbingParserDummyForTesting, NULL);
3061 ProbingParserDummyForTesting, NULL);
3067 ProbingParserDummyForTesting, NULL);
3073 ProbingParserDummyForTesting, NULL);
3075 ProbingParserDummyForTesting, NULL);
3081 ProbingParserDummyForTesting, NULL);
3087 ProbingParserDummyForTesting, NULL);
3093 ProbingParserDummyForTesting, NULL);
3099 ProbingParserDummyForTesting, NULL);
3105 ProbingParserDummyForTesting, NULL);
3111 ProbingParserDummyForTesting, NULL);
3231 23, element_ts_85, element_tc_85,
3239 23, element_ts_90, element_tc_90,
3247 23, element_ts_0, element_tc_0,
3257 element_ts_85_udp, element_tc_85_udp,
3275 if (AppLayerProtoDetectPPTestData(alpd_ctx.
ctx_pp, ip_proto,
3289 static int AppLayerProtoDetectTest16(
void)
3294 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3295 "User-Agent: Mozilla/1.0\r\n"
3296 "Cookie: hellocatch\r\n\r\n";
3297 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3311 printf(
"packet setup failed: ");
3315 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3317 printf(
"flow setup failed: ");
3321 f->
proto = IPPROTO_TCP;
3339 "(msg:\"Test content option\"; "
3351 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3356 if (http_state == NULL) {
3357 printf(
"no http state: ");
3365 printf(
"sig 1 didn't alert, but it should: ");
3372 if (det_ctx != NULL)
3388 static int AppLayerProtoDetectTest17(
void)
3393 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3394 "User-Agent: Mozilla/1.0\r\n"
3395 "Cookie: hellocatch\r\n\r\n";
3396 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3410 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3414 f->
proto = IPPROTO_TCP;
3430 "(msg:\"http over non standar port\"; "
3442 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3447 if (http_state == NULL) {
3448 printf(
"no http state: ");
3456 printf(
"sig 1 didn't alert, but it should: ");
3465 if (det_ctx != NULL)
3481 static int AppLayerProtoDetectTest18(
void)
3486 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3487 "User-Agent: Mozilla/1.0\r\n"
3488 "Cookie: hellocatch\r\n\r\n";
3489 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3503 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3507 f->
proto = IPPROTO_TCP;
3523 "(msg:\"Test content option\"; "
3535 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3540 if (http_state == NULL) {
3541 printf(
"no http state: ");
3549 printf(
"sig 1 alerted, but it should not (it's not ftp): ");
3557 if (det_ctx != NULL)
3573 static int AppLayerProtoDetectTest19(
void)
3577 uint8_t http_buf1[] =
"MPUT one\r\n";
3578 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3592 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3596 f->
proto = IPPROTO_TCP;
3612 "(msg:\"http over non standar port\"; "
3622 STREAM_TOSERVER, http_buf1, http_buf1_len);
3624 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3632 printf(
"sig 1 alerted, but it should not (it's ftp): ");
3641 if (det_ctx != NULL)
3658 UtRegisterTest(
"AppLayerProtoDetectTest01", AppLayerProtoDetectTest01);
3659 UtRegisterTest(
"AppLayerProtoDetectTest02", AppLayerProtoDetectTest02);
3660 UtRegisterTest(
"AppLayerProtoDetectTest03", AppLayerProtoDetectTest03);
3661 UtRegisterTest(
"AppLayerProtoDetectTest04", AppLayerProtoDetectTest04);
3662 UtRegisterTest(
"AppLayerProtoDetectTest05", AppLayerProtoDetectTest05);
3663 UtRegisterTest(
"AppLayerProtoDetectTest06", AppLayerProtoDetectTest06);
3664 UtRegisterTest(
"AppLayerProtoDetectTest07", AppLayerProtoDetectTest07);
3665 UtRegisterTest(
"AppLayerProtoDetectTest08", AppLayerProtoDetectTest08);
3666 UtRegisterTest(
"AppLayerProtoDetectTest09", AppLayerProtoDetectTest09);
3667 UtRegisterTest(
"AppLayerProtoDetectTest10", AppLayerProtoDetectTest10);
3668 UtRegisterTest(
"AppLayerProtoDetectTest11", AppLayerProtoDetectTest11);
3669 UtRegisterTest(
"AppLayerProtoDetectTest12", AppLayerProtoDetectTest12);
3670 UtRegisterTest(
"AppLayerProtoDetectTest13", AppLayerProtoDetectTest13);
3671 UtRegisterTest(
"AppLayerProtoDetectTest14", AppLayerProtoDetectTest14);
3672 UtRegisterTest(
"AppLayerProtoDetectTest15", AppLayerProtoDetectTest15);
3673 UtRegisterTest(
"AppLayerProtoDetectTest16", AppLayerProtoDetectTest16);
3674 UtRegisterTest(
"AppLayerProtoDetectTest17", AppLayerProtoDetectTest17);
3675 UtRegisterTest(
"AppLayerProtoDetectTest18", AppLayerProtoDetectTest18);
3676 UtRegisterTest(
"AppLayerProtoDetectTest19", AppLayerProtoDetectTest19);