190 static void AppLayerProtoDetectPEGetIpprotos(
AppProto alproto,
201 uint32_t buflen, uint16_t searchlen,
bool *rflow)
206 SCLogDebug(
"s->co->offset (%"PRIu16
") > searchlen (%"PRIu16
")",
210 if (s->
cd->
depth > searchlen) {
211 SCLogDebug(
"s->co->depth (%"PRIu16
") > searchlen (%"PRIu16
")",
216 const uint8_t *sbuf = buf + s->
cd->
offset;
218 SCLogDebug(
"s->co->offset (%"PRIu16
") s->cd->depth (%"PRIu16
")",
227 uint8_t direction = (
flags & (STREAM_TOSERVER | STREAM_TOCLIENT));
228 SCLogDebug(
"matching, s->direction %s, our dir %s",
229 (s->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
230 (
flags & STREAM_TOSERVER) ?
"toserver" :
"toclient");
235 SCLogDebug(
"direction is wrong, rflow = true");
241 SCLogDebug(
"PP can't be run yet as pp_min_depth %u > buflen %u",
249 SCLogDebug(
"found %s/%u, rdir %02x reverse_flow? %s",
251 (rdir && direction != rdir) ?
"true" :
"false");
252 *rflow = (rdir && direction != rdir);
259 SCLogDebug(
"depth reached and answer inconclusive: fail");
274 uint32_t buflen, uint8_t
flags,
AppProto *pm_results,
bool *rflow)
280 SCLogDebug(
"searchlen %u buflen %u", searchlen, buflen);
286 if (search_cnt == 0) {
294 memset(pm_results_bf, 0,
sizeof(pm_results_bf));
303 s, tctx, f,
flags, buf, buflen, searchlen, rflow);
306 if (AppProtoIsValid(
proto) &&
307 !(pm_results_bf[
proto / 8] & (1 << (
proto % 8))) )
309 pm_results[pm_matches++] =
proto;
315 if (pm_matches == 0 && buflen >= pm_ctx->
pp_max_len) {
327 const uint8_t *buf, uint32_t buflen, uint8_t
flags,
AppProto *pm_results,
bool *rflow)
342 if (
flags & STREAM_TOSERVER) {
350 m = PMGetProtoInspect(tctx, pm_ctx, mpm_tctx, f, buf, buflen,
flags, pm_results, rflow);
371 if (
flags & STREAM_TOSERVER) {
378 SCLogDebug(
"no matches and in midstream mode, lets try the "
379 "*patterns for the other side");
383 om = PMGetProtoInspect(
384 tctx, pm_ctx, mpm_tctx, f, buf, buflen,
flags, pm_results, rflow);
392 }
else if (om < 0 &&
m &&
m < 0) {
397 }
else if (om == 0 ||
m == 0) {
419 while (pp_port != NULL) {
420 if (pp_port->
dp != NULL && pp_port->
dp->
alproto == alproto) {
421 pp_elem = pp_port->
dp;
424 if (pp_port->
sp != NULL && pp_port->
sp->
alproto == alproto) {
425 pp_elem = pp_port->
sp;
428 pp_port = pp_port->
next;
431 SCReturnPtr(pp_elem,
"AppLayerProtoDetectProbingParserElement *");
451 while (pp_port != NULL) {
456 pp_port = pp_port->
next;
460 SCReturnPtr(pp_port,
"AppLayerProtoDetectProbingParserPort *");
466 AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, port);
496 uint8_t
flags,
const uint8_t *buf, uint32_t buflen, uint32_t *alproto_masks, uint8_t *rdir,
501 if (alproto_masks != NULL) {
503 if (buflen < pe->min_depth || (alproto_masks[0] &
BIT_U32(*nb_tried))) {
506 *nb_tried = *nb_tried + 1;
509 }
else if (buflen < pe->min_depth) {
520 if (AppProtoIsValid(alproto)) {
523 if (alproto_masks != NULL) {
526 alproto_masks[0] |=
BIT_U32(*nb_tried);
528 *nb_tried = *nb_tried + 1;
543 static AppProto AppLayerProtoDetectPPGetProto(
Flow *f,
const uint8_t *buf, uint32_t buflen,
544 uint8_t ipproto,
const uint8_t
flags,
bool *reverse_flow)
564 uint8_t nb_tried = 0;
565 uint32_t *alproto_masks = NULL;
566 uint8_t idir = (
flags & (STREAM_TOSERVER | STREAM_TOCLIENT));
570 bool probe_is_found =
false;
577 (dir == STREAM_TOSERVER) ?
"toserver" :
"toclient");
579 if (dir == STREAM_TOSERVER) {
581 pp_port_dp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, dp);
583 if (pp_port_dp != NULL) {
584 SCLogDebug(
"toserver - Probing parser found for destination port %"PRIu16, dp);
587 pe1 = pp_port_dp->
dp;
589 SCLogDebug(
"toserver - No probing parser registered for dest port %"PRIu16, dp);
592 pp_port_sp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, sp);
593 if (pp_port_sp != NULL) {
594 SCLogDebug(
"toserver - Probing parser found for source port %"PRIu16, sp);
597 pe2 = pp_port_sp->
sp;
599 SCLogDebug(
"toserver - No probing parser registered for source port %"PRIu16, sp);
603 pp_port_dp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, dp);
609 if (pp_port_dp != NULL) {
610 SCLogDebug(
"toclient - Probing parser found for destination port %"PRIu16, dp);
613 pe1 = pp_port_dp->
dp;
615 SCLogDebug(
"toclient - No probing parser registered for dest port %"PRIu16, dp);
618 pp_port_sp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, sp);
619 if (pp_port_sp != NULL) {
620 SCLogDebug(
"toclient - Probing parser found for source port %"PRIu16, sp);
622 pe2 = pp_port_sp->
sp;
624 SCLogDebug(
"toclient - No probing parser registered for source port %"PRIu16, sp);
632 pe0 = AppLayerProtoDetectGetProbingParser(alpd_ctx.
ctx_pp, ipproto, f->
alproto_tc);
634 pe0 = AppLayerProtoDetectGetProbingParser(alpd_ctx.
ctx_pp, ipproto, f->
alproto_ts);
637 if (pe1 == NULL && pe2 == NULL && pe0 == NULL) {
638 SCLogDebug(
"%s - No probing parsers found for either port",
639 (dir == STREAM_TOSERVER) ?
"toserver":
"toclient");
642 probe_is_found =
true;
648 alproto = PPGetProto(pe0, f,
flags, buf, buflen, NULL, &rdir, NULL);
649 if (AppProtoIsValid(alproto))
651 alproto = PPGetProto(pe1, f,
flags, buf, buflen, alproto_masks, &rdir, &nb_tried);
652 if (AppProtoIsValid(alproto))
654 alproto = PPGetProto(pe2, f,
flags, buf, buflen, alproto_masks, &rdir, &nb_tried);
655 if (AppProtoIsValid(alproto))
662 if (alproto_masks[0] ==
BIT_U32(nb_tried) - 1) {
664 SCLogDebug(
"%s, mask is now %08x, needed %08x, so done",
665 (dir == STREAM_TOSERVER) ?
"toserver" :
"toclient", alproto_masks[0],
669 (dir == STREAM_TOSERVER) ?
"toserver" :
"toclient", alproto_masks[0],
676 if (idir == STREAM_TOSERVER) {
677 dir = STREAM_TOCLIENT;
679 dir = STREAM_TOSERVER;
681 SCLogDebug(
"no match + midstream, retry the other direction %s",
682 (dir == STREAM_TOSERVER) ?
"toserver" :
"toclient");
683 goto again_midstream;
684 }
else if (!probe_is_found) {
689 if (AppProtoIsValid(alproto) && rdir != 0 && rdir != idir) {
690 SCLogDebug(
"PP found %u, is reverse flow", alproto);
691 *reverse_flow =
true;
695 (idir == STREAM_TOSERVER) ?
"toserver":
"toclient", alproto_masks[0]);
701 static void AppLayerProtoDetectPPGetIpprotos(
AppProto alproto,
710 for (pp = alpd_ctx.
ctx_pp; pp != NULL; pp = pp->
next) {
711 for (pp_port = pp->
port; pp_port != NULL; pp_port = pp_port->
next) {
712 for (pp_pe = pp_port->
dp; pp_pe != NULL; pp_pe = pp_pe->
next) {
716 for (pp_pe = pp_port->
sp; pp_pe != NULL; pp_pe = pp_pe->
next) {
736 SCReturnPtr(p,
"AppLayerProtoDetectProbingParserElement");
757 SCReturnPtr(p,
"AppLayerProtoDetectProbingParserPort");
769 AppLayerProtoDetectProbingParserElementFree(e);
776 AppLayerProtoDetectProbingParserElementFree(e);
794 SCReturnPtr(p,
"AppLayerProtoDetectProbingParser");
804 AppLayerProtoDetectProbingParserPortFree(pt);
814 AppProto alproto, uint16_t min_depth, uint16_t max_depth)
823 if (max_depth != 0 && min_depth >= max_depth) {
825 "register the probing parser. min_depth >= max_depth");
828 if (alproto <= ALPROTO_UNKNOWN || alproto >=
g_alproto_max) {
829 SCLogError(
"Invalid arguments sent to register "
830 "the probing parser. Invalid alproto - %d",
835 SCReturnPtr(pe,
"AppLayerProtoDetectProbingParserElement");
837 AppLayerProtoDetectProbingParserElementFree(pe);
838 SCReturnPtr(NULL,
"AppLayerProtoDetectProbingParserElement");
855 SCReturnPtr(new_pe,
"AppLayerProtoDetectProbingParserElement");
866 printf(
"\nProtocol Detection Configuration\n");
868 for ( ; pp != NULL; pp = pp->
next) {
870 if (pp->
ipproto == IPPROTO_TCP)
871 printf(
"IPProto: TCP\n");
872 else if (pp->
ipproto == IPPROTO_UDP)
873 printf(
"IPProto: UDP\n");
875 printf(
"IPProto: %"PRIu8
"\n", pp->
ipproto);
878 for ( ; pp_port != NULL; pp_port = pp_port->
next) {
879 if (pp_port->
dp != NULL) {
880 printf(
" Port: %"PRIu16
"\n", pp_port->
port);
882 printf(
" Destination port: (max-depth: %" PRIu16
")\n",
885 for ( ; pp_pe != NULL; pp_pe = pp_pe->
next) {
888 printf(
" min_depth: %"PRIu32
"\n", pp_pe->
min_depth);
889 printf(
" max_depth: %"PRIu32
"\n", pp_pe->
max_depth);
895 if (pp_port->
sp == NULL) {
899 printf(
" Source port: (max-depth: %" PRIu16
")\n", pp_port->
sp_max_depth);
901 for ( ; pp_pe != NULL; pp_pe = pp_pe->
next) {
904 printf(
" min_depth: %"PRIu32
"\n", pp_pe->
min_depth);
905 printf(
" max_depth: %"PRIu32
"\n", pp_pe->
max_depth);
921 if (*head_pe == NULL) {
927 while (temp_pe->
next != NULL)
928 temp_pe = temp_pe->
next;
929 temp_pe->
next = new_pe;
939 if (*head_pp == NULL) {
945 while (temp_pp->
next != NULL)
946 temp_pp = temp_pp->
next;
947 temp_pp->
next = new_pp;
958 if (*head_port == NULL) {
959 *head_port = new_port;
964 if ((*head_port)->port == 0 && (*head_port)->use_ports) {
965 new_port->
next = *head_port;
966 *head_port = new_port;
969 while (temp_port->
next != NULL &&
971 temp_port = temp_port->
next;
974 temp_port->
next = new_port;
982 uint8_t ipproto,
bool use_ports, uint16_t port,
AppProto alproto, uint16_t min_depth,
990 while (curr_pp != NULL) {
991 if (curr_pp->
ipproto == ipproto)
993 curr_pp = curr_pp->
next;
995 if (curr_pp == NULL) {
998 AppLayerProtoDetectProbingParserAppend(pp, new_pp);
1004 while (curr_port != NULL) {
1006 if (curr_port->
port == port && use_ports)
1008 curr_port = curr_port->
next;
1010 if (curr_port == NULL) {
1012 new_port->
port = port;
1014 AppLayerProtoDetectProbingParserPortAppend(&curr_pp->
port, new_port);
1015 curr_port = new_port;
1016 if (direction & STREAM_TOSERVER) {
1024 zero_port = curr_pp->
port;
1026 while (zero_port != NULL && !(zero_port->
port == 0 && zero_port->
use_ports)) {
1027 zero_port = zero_port->
next;
1029 if (zero_port != NULL) {
1032 zero_pe = zero_port->
dp;
1033 for ( ; zero_pe != NULL; zero_pe = zero_pe->
next) {
1034 if (curr_port->
dp == NULL)
1044 AppLayerProtoDetectProbingParserElementDuplicate(zero_pe);
1045 AppLayerProtoDetectProbingParserElementAppend(&curr_port->
dp, dup_pe);
1048 zero_pe = zero_port->
sp;
1049 for ( ; zero_pe != NULL; zero_pe = zero_pe->
next) {
1050 if (curr_port->
sp == NULL)
1060 AppLayerProtoDetectProbingParserElementDuplicate(zero_pe);
1061 AppLayerProtoDetectProbingParserElementAppend(&curr_port->
sp, dup_pe);
1068 if (direction & STREAM_TOSERVER)
1069 curr_pe = curr_port->
dp;
1071 curr_pe = curr_port->
sp;
1072 while (curr_pe != NULL) {
1073 if (curr_pe->
alproto == alproto) {
1075 "ipproto - %" PRIu8
" Port - %" PRIu16
" "
1076 "App Protocol - NULL, App Protocol(ID) - "
1077 "%" PRIu16
" min_depth - %" PRIu16
" "
1078 "max_dept - %" PRIu16
".",
1079 ipproto, port, alproto, min_depth, max_depth);
1082 curr_pe = curr_pe->
next;
1086 AppLayerProtoDetectProbingParserElementCreate(alproto, min_depth, max_depth);
1091 if (direction & STREAM_TOSERVER) {
1094 if (curr_port->
dp == NULL)
1102 head_pe = &curr_port->
dp;
1106 if (curr_port->
sp == NULL)
1114 head_pe = &curr_port->
sp;
1116 AppLayerProtoDetectProbingParserElementAppend(head_pe, new_pe);
1121 while (temp_port != NULL && !(temp_port->
port == 0 && temp_port->
use_ports)) {
1122 if (direction & STREAM_TOSERVER) {
1123 if (temp_port->
dp == NULL)
1131 AppLayerProtoDetectProbingParserElementAppend(
1132 &temp_port->
dp, AppLayerProtoDetectProbingParserElementDuplicate(curr_pe));
1134 if (temp_port->
sp == NULL)
1142 AppLayerProtoDetectProbingParserElementAppend(
1143 &temp_port->
sp, AppLayerProtoDetectProbingParserElementDuplicate(curr_pe));
1145 temp_port = temp_port->
next;
1155 static void AppLayerProtoDetectPMGetIpprotos(
AppProto alproto,
1162 for (
int j = 0; j < 2; j++) {
1168 ipprotos[ipproto / 8] |= 1 << (ipproto % 8);
1180 typedef struct TempContainer_ {
1182 uint16_t content_len;
1187 uint32_t struct_total_size = 0;
1188 uint32_t content_total_size = 0;
1190 uint8_t *ahb = NULL;
1191 uint8_t *content = NULL;
1192 uint16_t content_len = 0;
1194 TempContainer *struct_offset = NULL;
1195 uint8_t *content_offset = NULL;
1198 if (
ctx->head == NULL)
1201 for (s =
ctx->head; s != NULL; s = s->
next) {
1202 struct_total_size +=
sizeof(TempContainer);
1207 ahb =
SCMalloc(
sizeof(uint8_t) * (struct_total_size + content_total_size));
1211 struct_offset = (TempContainer *)ahb;
1212 content_offset = ahb + struct_total_size;
1213 for (s =
ctx->head; s != NULL; s = s->
next) {
1214 TempContainer *tcdup = (TempContainer *)ahb;
1218 for (; tcdup != struct_offset; tcdup++) {
1219 if (tcdup->content_len != content_len ||
1220 SCMemcmp(tcdup->content, content, tcdup->content_len) != 0)
1227 if (tcdup != struct_offset) {
1228 s->
cd->
id = tcdup->id;
1232 struct_offset->content_len = content_len;
1233 struct_offset->content = content_offset;
1234 content_offset += content_len;
1235 memcpy(struct_offset->content, content, content_len);
1236 struct_offset->id = max_id++;
1237 s->
cd->
id = struct_offset->id;
1242 ctx->max_pat_id = max_id;
1263 if (
ctx->map == NULL)
1267 for (s =
ctx->head; s != NULL; ) {
1286 ctx->map[s->
id] = s;
1328 AppProto alproto, uint8_t direction,
1330 uint16_t pp_min_depth, uint16_t pp_max_depth)
1352 static int AppLayerProtoDetectPMRegisterPattern(uint8_t ipproto,
AppProto alproto,
1353 const char *pattern,
1354 uint16_t depth, uint16_t
offset,
1358 uint16_t pp_min_depth, uint16_t pp_max_depth)
1382 if (depth < cd->content_len)
1385 if (direction & STREAM_TOSERVER)
1392 if (depth < ctx_pm->min_len)
1396 AppLayerProtoDetectPMAddSignature(ctx_pm, cd, alproto, direction,
1397 PPFunc, pp_min_depth, pp_max_depth);
1410 const uint8_t *buf, uint32_t buflen, uint8_t ipproto, uint8_t
flags,
bool *reverse_flow)
1413 SCLogDebug(
"buflen %u for %s direction", buflen,
1414 (
flags & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1420 uint16_t pm_matches = AppLayerProtoDetectPMGetProto(
1421 tctx, f, buf, buflen,
flags, pm_results, reverse_flow);
1422 if (pm_matches > 0) {
1424 alproto = pm_results[0];
1427 uint8_t reverse_dir = (
flags & STREAM_TOSERVER) ? STREAM_TOCLIENT : STREAM_TOSERVER;
1440 alproto = AppLayerProtoDetectPPGetProto(f, buf, buflen, ipproto,
flags, reverse_flow);
1441 if (AppProtoIsValid(alproto)) {
1449 alproto = AppLayerProtoDetectPEGetProto(f,
flags);
1464 while (pp != NULL) {
1466 AppLayerProtoDetectProbingParserFree(pp);
1474 static void AppLayerProtoDetectFreeAliases(
void)
1479 if (cur_alias == NULL)
1483 while (cur_alias != NULL) {
1484 next_alias = cur_alias->
next;
1486 cur_alias = next_alias;
1506 for (j = 0; j < 2; j++) {
1509 if (AppLayerProtoDetectPMSetContentIDs(ctx_pm) < 0)
1515 if (AppLayerProtoDetectPMMapSignatures(ctx_pm) < 0)
1517 if (AppLayerProtoDetectPMPrepareMpm(ctx_pm) < 0)
1524 AppLayerProtoDetectPrintProbingParsers(alpd_ctx.
ctx_pp);
1542 uint16_t min_depth, uint16_t max_depth, uint8_t direction,
ProbingParserFPtr ProbingParser1,
1548 if (portstr == NULL) {
1551 AppLayerProtoDetectInsertNewProbingParser(&alpd_ctx.
ctx_pp, ipproto,
false, 0, alproto,
1552 min_depth, max_depth, direction, ProbingParser1, ProbingParser2);
1557 while (temp_dp != NULL) {
1558 uint16_t port = temp_dp->
port;
1559 if (port == 0 && temp_dp->
port2 != 0)
1562 AppLayerProtoDetectInsertNewProbingParser(&alpd_ctx.
ctx_pp, ipproto,
true, port,
1563 alproto, min_depth, max_depth, direction, ProbingParser1, ProbingParser2);
1564 if (port == temp_dp->
port2) {
1570 temp_dp = temp_dp->
next;
1578 const char *alproto_name,
AppProto alproto, uint16_t min_depth, uint16_t max_depth,
1589 r = snprintf(param,
sizeof(param),
"%s%s%s",
"app-layer.protocols.",
1590 alproto_name,
".detection-ports");
1593 }
else if (r > (
int)
sizeof(param)) {
1594 FatalError(
"buffer not big enough to write param.");
1598 SCLogDebug(
"Entry for %s not found.", param);
1599 r = snprintf(param,
sizeof(param),
"%s%s%s%s%s",
"app-layer.protocols.",
1600 alproto_name,
".", ipproto_name,
".detection-ports");
1603 }
else if (r > (
int)
sizeof(param)) {
1604 FatalError(
"buffer not big enough to write param.");
1613 if (port_node == NULL)
1616 if (port_node != NULL && port_node->
val != NULL) {
1619 ProbingParserTs, ProbingParserTc);
1624 if (port_node == NULL)
1627 if (port_node != NULL && port_node->
val != NULL) {
1630 ProbingParserTc, ProbingParserTs);
1641 uint16_t depth, uint16_t
offset, uint8_t direction)
1644 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1652 const char *pattern, uint16_t depth, uint16_t
offset, uint8_t direction,
1656 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1659 PPFunc, pp_min_depth, pp_max_depth);
1664 const char *pattern, uint16_t depth, uint16_t
offset, uint8_t direction,
1668 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto, pattern, depth,
offset,
1669 direction, 0 , PPFunc, pp_min_depth, pp_max_depth);
1674 uint16_t depth, uint16_t
offset, uint8_t direction)
1677 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1692 memset(&alpd_ctx, 0,
sizeof(alpd_ctx));
1699 FatalError(
"Unable to alloc SpmGlobalThreadCtx.");
1703 for (j = 0; j < 2; j++) {
1710 FatalError(
"Unable to alloc alproto_names.");
1716 FatalError(
"Unable to alloc expectation_proto.");
1747 int ipproto_map = 0;
1754 for (dir = 0; dir < 2; dir++) {
1757 for (
id = 0;
id < pm_ctx->
max_sig_id;
id++) {
1758 sig = pm_ctx->
map[id];
1759 AppLayerProtoDetectPMFreeSignature(sig);
1775 AppLayerProtoDetectFreeAliases();
1777 AppLayerProtoDetectFreeProbingParsers(alpd_ctx.
ctx_pp);
1789 FatalError(
"Unable to realloc alproto_names.");
1813 new_alias->
next = NULL;
1815 if (alpda_ctx == NULL) {
1816 alpda_ctx = new_alias;
1819 while (cur_alias->
next != NULL) {
1820 cur_alias = cur_alias->
next;
1822 cur_alias->
next = new_alias;
1886 if (new_proto != f->
alproto) {
1913 const char *ipproto,
const char *alproto,
bool default_enabled)
1917 BUG_ON(ipproto == NULL || alproto == NULL);
1927 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1929 default_enabled =
true;
1932 r = snprintf(param,
sizeof(param),
"%s%s%s",
"app-layer.protocols.",
1933 alproto,
".enabled");
1936 }
else if (r > (
int)
sizeof(param)) {
1937 FatalError(
"buffer not big enough to write param.");
1942 SCLogDebug(
"Entry for %s not found.", param);
1943 r = snprintf(param,
sizeof(param),
"%s%s%s%s%s",
"app-layer.protocols.",
1944 alproto,
".", ipproto,
".enabled");
1947 }
else if (r > (
int)
sizeof(param)) {
1948 FatalError(
"buffer not big enough to write param.");
1953 SCLogDebug(
"Entry for %s not found.", param);
1954 if (default_enabled) {
1967 }
else if (strcasecmp(node->
val,
"detection-only") == 0) {
1973 SCLogError(
"Invalid value found for %s.", param);
1998 for (j = 0; j < 2; j++) {
1999 if (max_pat_id == 0) {
2019 for (j = 0; j < 2; j++) {
2046 int ipproto_map, dir;
2049 for (dir = 0; dir < 2; dir++) {
2078 AppLayerProtoDetectPMGetIpprotos(alproto, ipprotos);
2079 AppLayerProtoDetectPPGetIpprotos(alproto, ipprotos);
2080 AppLayerProtoDetectPEGetIpprotos(alproto, ipprotos);
2091 while (cur_alias != NULL) {
2092 if (strcasecmp(alproto_name, cur_alias->
proto_alias) == 0) {
2096 cur_alias = cur_alias->
next;
2102 if (alpd_ctx.
alproto_names[a] != NULL && AppProtoEquals(b, a)) {
2138 alprotos[alproto] = 1;
2144 static void AppLayerProtoDetectPEGetIpprotos(
AppProto alproto,
2151 ipprotos[IPPROTO_TCP / 8] |= 1 << (IPPROTO_TCP % 8);
2154 ipprotos[IPPROTO_UDP / 8] |= 1 << (IPPROTO_UDP % 8);
2162 SCLogError(
"Expectation on 2 IP protocols are not supported");
2180 alpd_ctx_ut = alpd_ctx;
2181 memset(&alpd_ctx, 0,
sizeof(alpd_ctx));
2188 alpd_ctx = alpd_ctx_ut;
2189 memset(&alpd_ctx_ut, 0,
sizeof(alpd_ctx_ut));
2193 static int AppLayerProtoDetectTest01(
void)
2198 const char *buf =
"HTTP";
2214 static int AppLayerProtoDetectTest02(
void)
2219 const char *buf =
"HTTP";
2240 static int AppLayerProtoDetectTest03(
void)
2245 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2247 memset(pm_results, 0,
sizeof(pm_results));
2249 memset(&f, 0x00,
sizeof(f));
2253 const char *buf =
"HTTP";
2273 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2274 &f, l7data,
sizeof(l7data),
2276 pm_results, &rflow);
2286 static int AppLayerProtoDetectTest04(
void)
2291 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2293 memset(&f, 0x00,
sizeof(f));
2295 memset(pm_results, 0,
sizeof(pm_results));
2298 const char *buf =
"200 ";
2315 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2316 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2327 static int AppLayerProtoDetectTest05(
void)
2332 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n<HTML><BODY>Blahblah</BODY></HTML>";
2334 memset(pm_results, 0,
sizeof(pm_results));
2336 memset(&f, 0x00,
sizeof(f));
2339 const char *buf =
"HTTP";
2359 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2360 &f, l7data,
sizeof(l7data),
2372 static int AppLayerProtoDetectTest06(
void)
2377 uint8_t l7data[] =
"220 Welcome to the OISF FTP server\r\n";
2379 memset(pm_results, 0,
sizeof(pm_results));
2381 memset(&f, 0x00,
sizeof(f));
2384 const char *buf =
"HTTP";
2404 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2405 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2416 static int AppLayerProtoDetectTest07(
void)
2421 uint8_t l7data[] =
"220 Welcome to the OISF HTTP/FTP server\r\n";
2423 memset(&f, 0x00,
sizeof(f));
2426 memset(pm_results, 0,
sizeof(pm_results));
2428 const char *buf =
"HTTP";
2444 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2445 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2455 static int AppLayerProtoDetectTest08(
void)
2460 uint8_t l7data[] = {
2461 0x00, 0x00, 0x00, 0x85, 0xff, 0x53, 0x4d, 0x42,
2462 0x72, 0x00, 0x00, 0x00, 0x00, 0x18, 0x53, 0xc8,
2463 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2464 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfe,
2465 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00, 0x02,
2466 0x50, 0x43, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f,
2467 0x52, 0x4b, 0x20, 0x50, 0x52, 0x4f, 0x47, 0x52,
2468 0x41, 0x4d, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02,
2469 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x31, 0x2e,
2470 0x30, 0x00, 0x02, 0x57, 0x69, 0x6e, 0x64, 0x6f,
2471 0x77, 0x73, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x57,
2472 0x6f, 0x72, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x70,
2473 0x73, 0x20, 0x33, 0x2e, 0x31, 0x61, 0x00, 0x02,
2474 0x4c, 0x4d, 0x31, 0x2e, 0x32, 0x58, 0x30, 0x30,
2475 0x32, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41,
2476 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x4e, 0x54,
2477 0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32,
2481 memset(pm_results, 0,
sizeof(pm_results));
2483 memset(&f, 0x00,
sizeof(f));
2486 const char *buf =
"|ff|SMB";
2502 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2503 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2514 static int AppLayerProtoDetectTest09(
void)
2519 uint8_t l7data[] = {
2520 0x00, 0x00, 0x00, 0x66, 0xfe, 0x53, 0x4d, 0x42,
2521 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2522 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00,
2523 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2524 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2525 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2526 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2527 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2528 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x01, 0x00,
2529 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2530 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2531 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2532 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2536 memset(pm_results, 0,
sizeof(pm_results));
2538 memset(&f, 0x00,
sizeof(f));
2541 const char *buf =
"|fe|SMB";
2557 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2558 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2569 static int AppLayerProtoDetectTest10(
void)
2574 uint8_t l7data[] = {
2575 0x05, 0x00, 0x0b, 0x03, 0x10, 0x00, 0x00, 0x00,
2576 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2577 0xd0, 0x16, 0xd0, 0x16, 0x00, 0x00, 0x00, 0x00,
2578 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
2579 0xb8, 0x4a, 0x9f, 0x4d, 0x1c, 0x7d, 0xcf, 0x11,
2580 0x86, 0x1e, 0x00, 0x20, 0xaf, 0x6e, 0x7c, 0x57,
2581 0x00, 0x00, 0x00, 0x00, 0x04, 0x5d, 0x88, 0x8a,
2582 0xeb, 0x1c, 0xc9, 0x11, 0x9f, 0xe8, 0x08, 0x00,
2583 0x2b, 0x10, 0x48, 0x60, 0x02, 0x00, 0x00, 0x00
2586 memset(pm_results, 0,
sizeof(pm_results));
2588 memset(&f, 0x00,
sizeof(f));
2591 const char *buf =
"|05 00|";
2608 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2609 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2624 static int AppLayerProtoDetectTest11(
void)
2629 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2630 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2632 memset(pm_results, 0,
sizeof(pm_results));
2634 memset(&f, 0x00,
sizeof(f));
2646 IPPROTO_TCP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2648 IPPROTO_TCP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2650 IPPROTO_TCP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2675 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2676 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2681 memset(pm_results, 0,
sizeof(pm_results));
2683 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2697 static int AppLayerProtoDetectTest12(
void)
2709 printf(
"failure 1\n");
2715 printf(
"failure 2\n");
2721 printf(
"failure 3\n");
2725 printf(
"failure 4\n");
2729 printf(
"failure 5\n");
2733 printf(
"failure 6\n");
2749 static int AppLayerProtoDetectTest13(
void)
2754 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2755 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2759 memset(&f, 0x00,
sizeof(f));
2771 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2773 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2775 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2796 memset(pm_results, 0,
sizeof(pm_results));
2798 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2799 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2803 memset(pm_results, 0,
sizeof(pm_results));
2805 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2820 static int AppLayerProtoDetectTest14(
void)
2825 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2826 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2830 memset(&f, 0x00,
sizeof(f));
2842 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2844 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2846 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2868 memset(pm_results, 0,
sizeof(pm_results));
2871 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2876 memset(pm_results, 0,
sizeof(pm_results));
2878 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2921 int i = -1, j = -1 , k = -1;
2925 for (i = 0; i < no_of_ip_proto; i++, pp = pp->
next) {
2926 if (pp->ipproto != ip_proto[i].
ipproto)
2930 for (k = 0; k < ip_proto[i].
no_of_port; k++, pp_port = pp_port->
next) {
2931 if (pp_port->port != ip_proto[i].
port[k].
port)
2943 j++, pp_element = pp_element->
next) {
2955 if (pp_element != NULL)
2958 pp_element = pp_port->sp;
2973 if (pp_element != NULL)
2976 if (pp_port != NULL)
2985 printf(
"i = %d, k = %d, j = %d(%s)\n", i, k, j, (dir == 0) ?
"ts" :
"tc");
2990 static uint16_t ProbingParserDummyForTesting(
2991 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t input_len, uint8_t *rdir)
2996 static int AppLayerProtoDetectTest15(
void)
3004 ProbingParserDummyForTesting, NULL);
3006 ProbingParserDummyForTesting, NULL);
3008 ProbingParserDummyForTesting, NULL);
3011 ProbingParserDummyForTesting, NULL);
3013 ProbingParserDummyForTesting, NULL);
3015 ProbingParserDummyForTesting, NULL);
3017 ProbingParserDummyForTesting, NULL);
3020 ProbingParserDummyForTesting, NULL);
3022 ProbingParserDummyForTesting, NULL);
3026 ProbingParserDummyForTesting, NULL);
3030 ProbingParserDummyForTesting, NULL);
3032 ProbingParserDummyForTesting, NULL);
3035 ProbingParserDummyForTesting, NULL);
3037 ProbingParserDummyForTesting, NULL);
3039 ProbingParserDummyForTesting, NULL);
3041 ProbingParserDummyForTesting, NULL);
3043 ProbingParserDummyForTesting, NULL);
3045 ProbingParserDummyForTesting, NULL);
3047 ProbingParserDummyForTesting, NULL);
3049 ProbingParserDummyForTesting, NULL);
3051 ProbingParserDummyForTesting, NULL);
3053 ProbingParserDummyForTesting, NULL);
3065 {
"smb",
ALPROTO_SMB, 80, 5, 6 }, {
"ftp",
ALPROTO_FTP, 80, 7, 10 },
3066 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3078 {
"dcerpc",
ALPROTO_DCERPC, 81, 9, 10 }, {
"jabber",
ALPROTO_JABBER, 0, 12, 23 },
3079 {
"irc",
ALPROTO_IRC, 0, 12, 14 }, {
"tls",
ALPROTO_TLS, 0, 12, 18 },
3092 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3102 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3112 {
"irc",
ALPROTO_IRC, 0, 12, 14 }, {
"tls",
ALPROTO_TLS, 0, 12, 18 },
3141 { 85, 23, 23, element_ts_85, element_tc_85,
3144 { 90, 23, 23, element_ts_90, element_tc_90,
3147 { 0, 23, 23, element_ts_0, element_tc_0,
3176 if (AppLayerProtoDetectPPTestData(alpd_ctx.
ctx_pp, ip_proto,
3190 static int AppLayerProtoDetectTest16(
void)
3195 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3196 "User-Agent: Mozilla/1.0\r\n"
3197 "Cookie: hellocatch\r\n\r\n";
3198 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3213 printf(
"packet setup failed: ");
3217 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3219 printf(
"flow setup failed: ");
3223 f->
proto = IPPROTO_TCP;
3241 "(msg:\"Test content option\"; "
3253 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3258 if (http_state == NULL) {
3259 printf(
"no http state: ");
3267 printf(
"sig 1 didn't alert, but it should: ");
3276 if (det_ctx != NULL)
3287 static int AppLayerProtoDetectTest17(
void)
3292 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3293 "User-Agent: Mozilla/1.0\r\n"
3294 "Cookie: hellocatch\r\n\r\n";
3295 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3310 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3314 f->
proto = IPPROTO_TCP;
3330 "(msg:\"http over non standar port\"; "
3342 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3347 if (http_state == NULL) {
3348 printf(
"no http state: ");
3356 printf(
"sig 1 didn't alert, but it should: ");
3367 if (det_ctx != NULL)
3378 static int AppLayerProtoDetectTest18(
void)
3383 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3384 "User-Agent: Mozilla/1.0\r\n"
3385 "Cookie: hellocatch\r\n\r\n";
3386 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3401 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3405 f->
proto = IPPROTO_TCP;
3421 "(msg:\"Test content option\"; "
3433 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3438 if (http_state == NULL) {
3439 printf(
"no http state: ");
3447 printf(
"sig 1 alerted, but it should not (it's not ftp): ");
3457 if (det_ctx != NULL)
3468 static int AppLayerProtoDetectTest19(
void)
3472 uint8_t http_buf1[] =
"MPUT one\r\n";
3473 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3488 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3492 f->
proto = IPPROTO_TCP;
3508 "(msg:\"http over non standar port\"; "
3518 STREAM_TOSERVER, http_buf1, http_buf1_len);
3520 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3528 printf(
"sig 1 alerted, but it should not (it's ftp): ");
3539 if (det_ctx != NULL)
3552 UtRegisterTest(
"AppLayerProtoDetectTest01", AppLayerProtoDetectTest01);
3553 UtRegisterTest(
"AppLayerProtoDetectTest02", AppLayerProtoDetectTest02);
3554 UtRegisterTest(
"AppLayerProtoDetectTest03", AppLayerProtoDetectTest03);
3555 UtRegisterTest(
"AppLayerProtoDetectTest04", AppLayerProtoDetectTest04);
3556 UtRegisterTest(
"AppLayerProtoDetectTest05", AppLayerProtoDetectTest05);
3557 UtRegisterTest(
"AppLayerProtoDetectTest06", AppLayerProtoDetectTest06);
3558 UtRegisterTest(
"AppLayerProtoDetectTest07", AppLayerProtoDetectTest07);
3559 UtRegisterTest(
"AppLayerProtoDetectTest08", AppLayerProtoDetectTest08);
3560 UtRegisterTest(
"AppLayerProtoDetectTest09", AppLayerProtoDetectTest09);
3561 UtRegisterTest(
"AppLayerProtoDetectTest10", AppLayerProtoDetectTest10);
3562 UtRegisterTest(
"AppLayerProtoDetectTest11", AppLayerProtoDetectTest11);
3563 UtRegisterTest(
"AppLayerProtoDetectTest12", AppLayerProtoDetectTest12);
3564 UtRegisterTest(
"AppLayerProtoDetectTest13", AppLayerProtoDetectTest13);
3565 UtRegisterTest(
"AppLayerProtoDetectTest14", AppLayerProtoDetectTest14);
3566 UtRegisterTest(
"AppLayerProtoDetectTest15", AppLayerProtoDetectTest15);
3567 UtRegisterTest(
"AppLayerProtoDetectTest16", AppLayerProtoDetectTest16);
3568 UtRegisterTest(
"AppLayerProtoDetectTest17", AppLayerProtoDetectTest17);
3569 UtRegisterTest(
"AppLayerProtoDetectTest18", AppLayerProtoDetectTest18);
3570 UtRegisterTest(
"AppLayerProtoDetectTest19", AppLayerProtoDetectTest19);