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) {
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");
1415 uint16_t pm_matches = AppLayerProtoDetectPMGetProto(
1416 tctx, f, buf, buflen,
flags, pm_results, reverse_flow);
1417 if (pm_matches > 0) {
1419 alproto = pm_results[0];
1422 uint8_t reverse_dir = (
flags & STREAM_TOSERVER) ? STREAM_TOCLIENT : STREAM_TOSERVER;
1435 pm_alproto = alproto;
1443 alproto = AppLayerProtoDetectPPGetProto(f, buf, buflen, ipproto,
flags, &rflow);
1444 if (AppProtoIsValid(alproto)) {
1446 *reverse_flow =
true;
1454 alproto = AppLayerProtoDetectPEGetProto(f,
flags);
1458 if (!AppProtoIsValid(alproto))
1459 alproto = pm_alproto;
1473 while (pp != NULL) {
1475 AppLayerProtoDetectProbingParserFree(pp);
1483 static void AppLayerProtoDetectFreeAliases(
void)
1488 if (cur_alias == NULL)
1492 while (cur_alias != NULL) {
1493 next_alias = cur_alias->
next;
1495 cur_alias = next_alias;
1515 for (j = 0; j < 2; j++) {
1518 if (AppLayerProtoDetectPMSetContentIDs(ctx_pm) < 0)
1524 if (AppLayerProtoDetectPMMapSignatures(ctx_pm) < 0)
1526 if (AppLayerProtoDetectPMPrepareMpm(ctx_pm) < 0)
1533 AppLayerProtoDetectPrintProbingParsers(alpd_ctx.
ctx_pp);
1551 const char *portstr,
1553 uint16_t min_depth, uint16_t max_depth,
1561 if (portstr == NULL) {
1564 AppLayerProtoDetectInsertNewProbingParser(&alpd_ctx.
ctx_pp, ipproto,
false, 0, alproto,
1565 min_depth, max_depth, direction, ProbingParser1, ProbingParser2);
1570 while (temp_dp != NULL) {
1571 uint16_t port = temp_dp->
port;
1572 if (port == 0 && temp_dp->
port2 != 0)
1575 AppLayerProtoDetectInsertNewProbingParser(&alpd_ctx.
ctx_pp, ipproto,
true, port,
1576 alproto, min_depth, max_depth, direction, ProbingParser1, ProbingParser2);
1577 if (port == temp_dp->
port2) {
1583 temp_dp = temp_dp->
next;
1592 const char *alproto_name,
1594 uint16_t min_depth, uint16_t max_depth,
1606 r = snprintf(param,
sizeof(param),
"%s%s%s",
"app-layer.protocols.",
1607 alproto_name,
".detection-ports");
1610 }
else if (r > (
int)
sizeof(param)) {
1611 FatalError(
"buffer not big enough to write param.");
1615 SCLogDebug(
"Entry for %s not found.", param);
1616 r = snprintf(param,
sizeof(param),
"%s%s%s%s%s",
"app-layer.protocols.",
1617 alproto_name,
".", ipproto_name,
".detection-ports");
1620 }
else if (r > (
int)
sizeof(param)) {
1621 FatalError(
"buffer not big enough to write param.");
1630 if (port_node == NULL)
1633 if (port_node != NULL && port_node->
val != NULL) {
1637 min_depth, max_depth,
1639 ProbingParserTs, ProbingParserTc);
1644 if (port_node == NULL)
1647 if (port_node != NULL && port_node->
val != NULL) {
1651 min_depth, max_depth,
1653 ProbingParserTc, ProbingParserTs);
1665 const char *pattern,
1666 uint16_t depth, uint16_t
offset,
1670 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1678 const char *pattern, uint16_t depth, uint16_t
offset,
1681 uint16_t pp_min_depth, uint16_t pp_max_depth)
1684 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1687 PPFunc, pp_min_depth, pp_max_depth);
1692 const char *pattern,
1693 uint16_t depth, uint16_t
offset,
1697 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1712 memset(&alpd_ctx, 0,
sizeof(alpd_ctx));
1719 FatalError(
"Unable to alloc SpmGlobalThreadCtx.");
1723 for (j = 0; j < 2; j++) {
1730 FatalError(
"Unable to alloc alproto_names.");
1735 FatalError(
"Unable to alloc expectation_proto.");
1749 int ipproto_map = 0;
1756 for (dir = 0; dir < 2; dir++) {
1759 for (
id = 0;
id < pm_ctx->
max_sig_id;
id++) {
1760 sig = pm_ctx->
map[id];
1761 AppLayerProtoDetectPMFreeSignature(sig);
1775 AppLayerProtoDetectFreeAliases();
1777 AppLayerProtoDetectFreeProbingParsers(alpd_ctx.
ctx_pp);
1804 new_alias->
next = NULL;
1806 if (alpda_ctx == NULL) {
1807 alpda_ctx = new_alias;
1810 while (cur_alias->
next != NULL) {
1811 cur_alias = cur_alias->
next;
1813 cur_alias->
next = new_alias;
1877 if (new_proto != f->
alproto) {
1905 const char *ipproto,
const char *alproto,
bool default_enabled)
1909 BUG_ON(ipproto == NULL || alproto == NULL);
1919 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1921 default_enabled =
true;
1924 r = snprintf(param,
sizeof(param),
"%s%s%s",
"app-layer.protocols.",
1925 alproto,
".enabled");
1928 }
else if (r > (
int)
sizeof(param)) {
1929 FatalError(
"buffer not big enough to write param.");
1934 SCLogDebug(
"Entry for %s not found.", param);
1935 r = snprintf(param,
sizeof(param),
"%s%s%s%s%s",
"app-layer.protocols.",
1936 alproto,
".", ipproto,
".enabled");
1939 }
else if (r > (
int)
sizeof(param)) {
1940 FatalError(
"buffer not big enough to write param.");
1945 SCLogDebug(
"Entry for %s not found.", param);
1946 if (default_enabled) {
1959 }
else if (strcasecmp(node->
val,
"detection-only") == 0) {
1965 SCLogError(
"Invalid value found for %s.", param);
1990 for (j = 0; j < 2; j++) {
1991 if (max_pat_id == 0) {
2011 for (j = 0; j < 2; j++) {
2038 int ipproto_map, dir;
2041 for (dir = 0; dir < 2; dir++) {
2070 AppLayerProtoDetectPMGetIpprotos(alproto, ipprotos);
2071 AppLayerProtoDetectPPGetIpprotos(alproto, ipprotos);
2072 AppLayerProtoDetectPEGetIpprotos(alproto, ipprotos);
2083 while (cur_alias != NULL) {
2084 if (strcasecmp(alproto_name, cur_alias->
proto_alias) == 0) {
2088 cur_alias = cur_alias->
next;
2094 if (alpd_ctx.
alproto_names[a] != NULL && AppProtoEquals(b, a)) {
2130 alprotos[alproto] = 1;
2136 static void AppLayerProtoDetectPEGetIpprotos(
AppProto alproto,
2140 ipprotos[IPPROTO_TCP / 8] |= 1 << (IPPROTO_TCP % 8);
2143 ipprotos[IPPROTO_UDP / 8] |= 1 << (IPPROTO_UDP % 8);
2151 SCLogError(
"Expectation on 2 IP protocols are not supported");
2169 alpd_ctx_ut = alpd_ctx;
2170 memset(&alpd_ctx, 0,
sizeof(alpd_ctx));
2177 alpd_ctx = alpd_ctx_ut;
2178 memset(&alpd_ctx_ut, 0,
sizeof(alpd_ctx_ut));
2182 static int AppLayerProtoDetectTest01(
void)
2187 const char *buf =
"HTTP";
2201 static int AppLayerProtoDetectTest02(
void)
2206 const char *buf =
"HTTP";
2226 static int AppLayerProtoDetectTest03(
void)
2231 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2233 memset(pm_results, 0,
sizeof(pm_results));
2235 memset(&f, 0x00,
sizeof(f));
2239 const char *buf =
"HTTP";
2258 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2259 &f, l7data,
sizeof(l7data),
2261 pm_results, &rflow);
2271 static int AppLayerProtoDetectTest04(
void)
2276 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2278 memset(&f, 0x00,
sizeof(f));
2280 memset(pm_results, 0,
sizeof(pm_results));
2283 const char *buf =
"200 ";
2299 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2300 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2311 static int AppLayerProtoDetectTest05(
void)
2316 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n<HTML><BODY>Blahblah</BODY></HTML>";
2318 memset(pm_results, 0,
sizeof(pm_results));
2320 memset(&f, 0x00,
sizeof(f));
2323 const char *buf =
"HTTP";
2342 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2343 &f, l7data,
sizeof(l7data),
2355 static int AppLayerProtoDetectTest06(
void)
2360 uint8_t l7data[] =
"220 Welcome to the OISF FTP server\r\n";
2362 memset(pm_results, 0,
sizeof(pm_results));
2364 memset(&f, 0x00,
sizeof(f));
2367 const char *buf =
"HTTP";
2386 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2387 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2398 static int AppLayerProtoDetectTest07(
void)
2403 uint8_t l7data[] =
"220 Welcome to the OISF HTTP/FTP server\r\n";
2405 memset(&f, 0x00,
sizeof(f));
2408 memset(pm_results, 0,
sizeof(pm_results));
2410 const char *buf =
"HTTP";
2425 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2426 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2436 static int AppLayerProtoDetectTest08(
void)
2441 uint8_t l7data[] = {
2442 0x00, 0x00, 0x00, 0x85, 0xff, 0x53, 0x4d, 0x42,
2443 0x72, 0x00, 0x00, 0x00, 0x00, 0x18, 0x53, 0xc8,
2444 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2445 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfe,
2446 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00, 0x02,
2447 0x50, 0x43, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f,
2448 0x52, 0x4b, 0x20, 0x50, 0x52, 0x4f, 0x47, 0x52,
2449 0x41, 0x4d, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02,
2450 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x31, 0x2e,
2451 0x30, 0x00, 0x02, 0x57, 0x69, 0x6e, 0x64, 0x6f,
2452 0x77, 0x73, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x57,
2453 0x6f, 0x72, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x70,
2454 0x73, 0x20, 0x33, 0x2e, 0x31, 0x61, 0x00, 0x02,
2455 0x4c, 0x4d, 0x31, 0x2e, 0x32, 0x58, 0x30, 0x30,
2456 0x32, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41,
2457 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x4e, 0x54,
2458 0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32,
2462 memset(pm_results, 0,
sizeof(pm_results));
2464 memset(&f, 0x00,
sizeof(f));
2467 const char *buf =
"|ff|SMB";
2483 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2484 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2495 static int AppLayerProtoDetectTest09(
void)
2500 uint8_t l7data[] = {
2501 0x00, 0x00, 0x00, 0x66, 0xfe, 0x53, 0x4d, 0x42,
2502 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2503 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00,
2504 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2505 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2506 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2507 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2508 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2509 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x01, 0x00,
2510 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2511 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2512 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2513 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2517 memset(pm_results, 0,
sizeof(pm_results));
2519 memset(&f, 0x00,
sizeof(f));
2522 const char *buf =
"|fe|SMB";
2538 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2539 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2550 static int AppLayerProtoDetectTest10(
void)
2555 uint8_t l7data[] = {
2556 0x05, 0x00, 0x0b, 0x03, 0x10, 0x00, 0x00, 0x00,
2557 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2558 0xd0, 0x16, 0xd0, 0x16, 0x00, 0x00, 0x00, 0x00,
2559 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
2560 0xb8, 0x4a, 0x9f, 0x4d, 0x1c, 0x7d, 0xcf, 0x11,
2561 0x86, 0x1e, 0x00, 0x20, 0xaf, 0x6e, 0x7c, 0x57,
2562 0x00, 0x00, 0x00, 0x00, 0x04, 0x5d, 0x88, 0x8a,
2563 0xeb, 0x1c, 0xc9, 0x11, 0x9f, 0xe8, 0x08, 0x00,
2564 0x2b, 0x10, 0x48, 0x60, 0x02, 0x00, 0x00, 0x00
2567 memset(pm_results, 0,
sizeof(pm_results));
2569 memset(&f, 0x00,
sizeof(f));
2572 const char *buf =
"|05 00|";
2588 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2589 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2604 static int AppLayerProtoDetectTest11(
void)
2609 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2610 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2612 memset(pm_results, 0,
sizeof(pm_results));
2614 memset(&f, 0x00,
sizeof(f));
2626 IPPROTO_TCP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2628 IPPROTO_TCP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2630 IPPROTO_TCP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2655 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2656 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2661 memset(pm_results, 0,
sizeof(pm_results));
2663 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2677 static int AppLayerProtoDetectTest12(
void)
2689 printf(
"failure 1\n");
2695 printf(
"failure 2\n");
2701 printf(
"failure 3\n");
2705 printf(
"failure 4\n");
2709 printf(
"failure 5\n");
2713 printf(
"failure 6\n");
2729 static int AppLayerProtoDetectTest13(
void)
2734 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2735 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2739 memset(&f, 0x00,
sizeof(f));
2751 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2753 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2755 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2776 memset(pm_results, 0,
sizeof(pm_results));
2778 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2779 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2783 memset(pm_results, 0,
sizeof(pm_results));
2785 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2800 static int AppLayerProtoDetectTest14(
void)
2805 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2806 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2810 memset(&f, 0x00,
sizeof(f));
2822 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2824 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2826 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2848 memset(pm_results, 0,
sizeof(pm_results));
2851 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2856 memset(pm_results, 0,
sizeof(pm_results));
2858 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2903 int i = -1, j = -1 , k = -1;
2907 for (i = 0; i < no_of_ip_proto; i++, pp = pp->
next) {
2908 if (pp->ipproto != ip_proto[i].
ipproto)
2912 for (k = 0; k < ip_proto[i].
no_of_port; k++, pp_port = pp_port->
next) {
2913 if (pp_port->port != ip_proto[i].
port[k].
port)
2929 j++, pp_element = pp_element->
next) {
2944 if (pp_element != NULL)
2947 pp_element = pp_port->sp;
2965 if (pp_element != NULL)
2968 if (pp_port != NULL)
2977 printf(
"i = %d, k = %d, j = %d(%s)\n", i, k, j, (dir == 0) ?
"ts" :
"tc");
2982 static uint16_t ProbingParserDummyForTesting(
Flow *f, uint8_t direction,
2983 const uint8_t *input,
2984 uint32_t input_len, uint8_t *rdir)
2989 static int AppLayerProtoDetectTest15(
void)
2997 ProbingParserDummyForTesting, NULL);
3003 ProbingParserDummyForTesting, NULL);
3009 ProbingParserDummyForTesting, NULL);
3016 ProbingParserDummyForTesting, NULL);
3022 ProbingParserDummyForTesting, NULL);
3028 ProbingParserDummyForTesting, NULL);
3034 ProbingParserDummyForTesting, NULL);
3041 ProbingParserDummyForTesting, NULL);
3047 ProbingParserDummyForTesting, NULL);
3055 ProbingParserDummyForTesting, NULL);
3063 ProbingParserDummyForTesting, NULL);
3069 ProbingParserDummyForTesting, NULL);
3076 ProbingParserDummyForTesting, NULL);
3082 ProbingParserDummyForTesting, NULL);
3088 ProbingParserDummyForTesting, NULL);
3090 ProbingParserDummyForTesting, NULL);
3096 ProbingParserDummyForTesting, NULL);
3102 ProbingParserDummyForTesting, NULL);
3108 ProbingParserDummyForTesting, NULL);
3114 ProbingParserDummyForTesting, NULL);
3120 ProbingParserDummyForTesting, NULL);
3126 ProbingParserDummyForTesting, NULL);
3246 23, element_ts_85, element_tc_85,
3254 23, element_ts_90, element_tc_90,
3262 23, element_ts_0, element_tc_0,
3272 element_ts_85_udp, element_tc_85_udp,
3290 if (AppLayerProtoDetectPPTestData(alpd_ctx.
ctx_pp, ip_proto,
3304 static int AppLayerProtoDetectTest16(
void)
3309 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3310 "User-Agent: Mozilla/1.0\r\n"
3311 "Cookie: hellocatch\r\n\r\n";
3312 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3326 printf(
"packet setup failed: ");
3330 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3332 printf(
"flow setup failed: ");
3336 f->
proto = IPPROTO_TCP;
3354 "(msg:\"Test content option\"; "
3366 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3371 if (http_state == NULL) {
3372 printf(
"no http state: ");
3380 printf(
"sig 1 didn't alert, but it should: ");
3387 if (det_ctx != NULL)
3403 static int AppLayerProtoDetectTest17(
void)
3408 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3409 "User-Agent: Mozilla/1.0\r\n"
3410 "Cookie: hellocatch\r\n\r\n";
3411 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3425 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3429 f->
proto = IPPROTO_TCP;
3445 "(msg:\"http over non standar port\"; "
3457 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3462 if (http_state == NULL) {
3463 printf(
"no http state: ");
3471 printf(
"sig 1 didn't alert, but it should: ");
3480 if (det_ctx != NULL)
3496 static int AppLayerProtoDetectTest18(
void)
3501 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3502 "User-Agent: Mozilla/1.0\r\n"
3503 "Cookie: hellocatch\r\n\r\n";
3504 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3518 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3522 f->
proto = IPPROTO_TCP;
3538 "(msg:\"Test content option\"; "
3550 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3555 if (http_state == NULL) {
3556 printf(
"no http state: ");
3564 printf(
"sig 1 alerted, but it should not (it's not ftp): ");
3572 if (det_ctx != NULL)
3588 static int AppLayerProtoDetectTest19(
void)
3592 uint8_t http_buf1[] =
"MPUT one\r\n";
3593 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3607 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3611 f->
proto = IPPROTO_TCP;
3627 "(msg:\"http over non standar port\"; "
3637 STREAM_TOSERVER, http_buf1, http_buf1_len);
3639 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3647 printf(
"sig 1 alerted, but it should not (it's ftp): ");
3656 if (det_ctx != NULL)
3673 UtRegisterTest(
"AppLayerProtoDetectTest01", AppLayerProtoDetectTest01);
3674 UtRegisterTest(
"AppLayerProtoDetectTest02", AppLayerProtoDetectTest02);
3675 UtRegisterTest(
"AppLayerProtoDetectTest03", AppLayerProtoDetectTest03);
3676 UtRegisterTest(
"AppLayerProtoDetectTest04", AppLayerProtoDetectTest04);
3677 UtRegisterTest(
"AppLayerProtoDetectTest05", AppLayerProtoDetectTest05);
3678 UtRegisterTest(
"AppLayerProtoDetectTest06", AppLayerProtoDetectTest06);
3679 UtRegisterTest(
"AppLayerProtoDetectTest07", AppLayerProtoDetectTest07);
3680 UtRegisterTest(
"AppLayerProtoDetectTest08", AppLayerProtoDetectTest08);
3681 UtRegisterTest(
"AppLayerProtoDetectTest09", AppLayerProtoDetectTest09);
3682 UtRegisterTest(
"AppLayerProtoDetectTest10", AppLayerProtoDetectTest10);
3683 UtRegisterTest(
"AppLayerProtoDetectTest11", AppLayerProtoDetectTest11);
3684 UtRegisterTest(
"AppLayerProtoDetectTest12", AppLayerProtoDetectTest12);
3685 UtRegisterTest(
"AppLayerProtoDetectTest13", AppLayerProtoDetectTest13);
3686 UtRegisterTest(
"AppLayerProtoDetectTest14", AppLayerProtoDetectTest14);
3687 UtRegisterTest(
"AppLayerProtoDetectTest15", AppLayerProtoDetectTest15);
3688 UtRegisterTest(
"AppLayerProtoDetectTest16", AppLayerProtoDetectTest16);
3689 UtRegisterTest(
"AppLayerProtoDetectTest17", AppLayerProtoDetectTest17);
3690 UtRegisterTest(
"AppLayerProtoDetectTest18", AppLayerProtoDetectTest18);
3691 UtRegisterTest(
"AppLayerProtoDetectTest19", AppLayerProtoDetectTest19);