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");
769 AppLayerProtoDetectProbingParserElementFree(e);
776 AppLayerProtoDetectProbingParserElementFree(e);
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) {
1920 const char *ipproto,
const char *alproto,
bool default_enabled)
1924 BUG_ON(ipproto == NULL || alproto == NULL);
1929 bool g_enabled =
false;
1930 bool i_enabled =
false;
1935 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1937 default_enabled =
true;
1940 r = snprintf(param,
sizeof(param),
"%s%s%s%s%s",
"app-layer.protocols.", alproto,
".", ipproto,
1944 }
else if (r > (
int)
sizeof(param)) {
1945 FatalError(
"buffer not big enough to write param.");
1950 if (i_proto && i_proto->
val) {
1955 }
else if (strcasecmp(i_proto->
val,
"detection-only") == 0) {
1958 FatalError(
"Invalid value found for %s.", param);
1962 r = snprintf(param,
sizeof(param),
"%s%s%s",
"app-layer.protocols.", alproto,
".enabled");
1965 }
else if (r > (
int)
sizeof(param)) {
1966 FatalError(
"buffer not big enough to write param.");
1971 if (g_proto && g_proto->
val) {
1976 }
else if (strcasecmp(g_proto->
val,
"detection-only") == 0) {
1979 FatalError(
"Invalid value found for %s", param);
1983 if ((i_proto && g_proto) && (i_enabled ^ g_enabled)) {
1984 SCLogWarning(
"Inconsistent global (%s) and respective ipproto (%s) settings found for "
1985 "alproto %s and ipproto %s",
1986 g_enabled ?
"TRUE" :
"FALSE", i_enabled ?
"TRUE" :
"FALSE", alproto, ipproto);
1995 if (!default_enabled) {
2018 for (j = 0; j < 2; j++) {
2019 if (max_pat_id == 0) {
2039 for (j = 0; j < 2; j++) {
2066 int ipproto_map, dir;
2069 for (dir = 0; dir < 2; dir++) {
2098 AppLayerProtoDetectPMGetIpprotos(alproto, ipprotos);
2099 AppLayerProtoDetectPPGetIpprotos(alproto, ipprotos);
2100 AppLayerProtoDetectPEGetIpprotos(alproto, ipprotos);
2111 while (cur_alias != NULL) {
2112 if (strcasecmp(alproto_name, cur_alias->
proto_alias) == 0) {
2116 cur_alias = cur_alias->
next;
2122 if (alpd_ctx.
alproto_names[a] != NULL && AppProtoEquals(b, a)) {
2158 alprotos[alproto] = 1;
2164 static void AppLayerProtoDetectPEGetIpprotos(
AppProto alproto,
2171 ipprotos[IPPROTO_TCP / 8] |= 1 << (IPPROTO_TCP % 8);
2174 ipprotos[IPPROTO_UDP / 8] |= 1 << (IPPROTO_UDP % 8);
2182 SCLogError(
"Expectation on 2 IP protocols are not supported");
2200 alpd_ctx_ut = alpd_ctx;
2201 memset(&alpd_ctx, 0,
sizeof(alpd_ctx));
2208 alpd_ctx = alpd_ctx_ut;
2209 memset(&alpd_ctx_ut, 0,
sizeof(alpd_ctx_ut));
2213 static int AppLayerProtoDetectTest01(
void)
2218 const char *buf =
"HTTP";
2234 static int AppLayerProtoDetectTest02(
void)
2239 const char *buf =
"HTTP";
2260 static int AppLayerProtoDetectTest03(
void)
2265 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2267 memset(pm_results, 0,
sizeof(pm_results));
2269 memset(&f, 0x00,
sizeof(f));
2273 const char *buf =
"HTTP";
2293 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2294 &f, l7data,
sizeof(l7data),
2296 pm_results, &rflow);
2306 static int AppLayerProtoDetectTest04(
void)
2311 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2313 memset(&f, 0x00,
sizeof(f));
2315 memset(pm_results, 0,
sizeof(pm_results));
2318 const char *buf =
"200 ";
2335 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2336 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2347 static int AppLayerProtoDetectTest05(
void)
2352 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n<HTML><BODY>Blahblah</BODY></HTML>";
2354 memset(pm_results, 0,
sizeof(pm_results));
2356 memset(&f, 0x00,
sizeof(f));
2359 const char *buf =
"HTTP";
2379 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2380 &f, l7data,
sizeof(l7data),
2392 static int AppLayerProtoDetectTest06(
void)
2397 uint8_t l7data[] =
"220 Welcome to the OISF FTP server\r\n";
2399 memset(pm_results, 0,
sizeof(pm_results));
2401 memset(&f, 0x00,
sizeof(f));
2404 const char *buf =
"HTTP";
2424 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2425 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2436 static int AppLayerProtoDetectTest07(
void)
2441 uint8_t l7data[] =
"220 Welcome to the OISF HTTP/FTP server\r\n";
2443 memset(&f, 0x00,
sizeof(f));
2446 memset(pm_results, 0,
sizeof(pm_results));
2448 const char *buf =
"HTTP";
2464 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2465 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2475 static int AppLayerProtoDetectTest08(
void)
2480 uint8_t l7data[] = {
2481 0x00, 0x00, 0x00, 0x85, 0xff, 0x53, 0x4d, 0x42,
2482 0x72, 0x00, 0x00, 0x00, 0x00, 0x18, 0x53, 0xc8,
2483 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2484 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfe,
2485 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00, 0x02,
2486 0x50, 0x43, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f,
2487 0x52, 0x4b, 0x20, 0x50, 0x52, 0x4f, 0x47, 0x52,
2488 0x41, 0x4d, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02,
2489 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x31, 0x2e,
2490 0x30, 0x00, 0x02, 0x57, 0x69, 0x6e, 0x64, 0x6f,
2491 0x77, 0x73, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x57,
2492 0x6f, 0x72, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x70,
2493 0x73, 0x20, 0x33, 0x2e, 0x31, 0x61, 0x00, 0x02,
2494 0x4c, 0x4d, 0x31, 0x2e, 0x32, 0x58, 0x30, 0x30,
2495 0x32, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41,
2496 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x4e, 0x54,
2497 0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32,
2501 memset(pm_results, 0,
sizeof(pm_results));
2503 memset(&f, 0x00,
sizeof(f));
2506 const char *buf =
"|ff|SMB";
2522 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2523 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2534 static int AppLayerProtoDetectTest09(
void)
2539 uint8_t l7data[] = {
2540 0x00, 0x00, 0x00, 0x66, 0xfe, 0x53, 0x4d, 0x42,
2541 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2542 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00,
2543 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2544 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2545 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2546 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2547 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2548 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x01, 0x00,
2549 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2550 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2551 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2552 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2556 memset(pm_results, 0,
sizeof(pm_results));
2558 memset(&f, 0x00,
sizeof(f));
2561 const char *buf =
"|fe|SMB";
2577 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2578 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2589 static int AppLayerProtoDetectTest10(
void)
2594 uint8_t l7data[] = {
2595 0x05, 0x00, 0x0b, 0x03, 0x10, 0x00, 0x00, 0x00,
2596 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2597 0xd0, 0x16, 0xd0, 0x16, 0x00, 0x00, 0x00, 0x00,
2598 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
2599 0xb8, 0x4a, 0x9f, 0x4d, 0x1c, 0x7d, 0xcf, 0x11,
2600 0x86, 0x1e, 0x00, 0x20, 0xaf, 0x6e, 0x7c, 0x57,
2601 0x00, 0x00, 0x00, 0x00, 0x04, 0x5d, 0x88, 0x8a,
2602 0xeb, 0x1c, 0xc9, 0x11, 0x9f, 0xe8, 0x08, 0x00,
2603 0x2b, 0x10, 0x48, 0x60, 0x02, 0x00, 0x00, 0x00
2606 memset(pm_results, 0,
sizeof(pm_results));
2608 memset(&f, 0x00,
sizeof(f));
2611 const char *buf =
"|05 00|";
2628 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2629 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2644 static int AppLayerProtoDetectTest11(
void)
2649 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2650 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2652 memset(pm_results, 0,
sizeof(pm_results));
2654 memset(&f, 0x00,
sizeof(f));
2666 IPPROTO_TCP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2668 IPPROTO_TCP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2670 IPPROTO_TCP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2695 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2696 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2701 memset(pm_results, 0,
sizeof(pm_results));
2703 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2717 static int AppLayerProtoDetectTest12(
void)
2729 printf(
"failure 1\n");
2735 printf(
"failure 2\n");
2741 printf(
"failure 3\n");
2745 printf(
"failure 4\n");
2749 printf(
"failure 5\n");
2753 printf(
"failure 6\n");
2769 static int AppLayerProtoDetectTest13(
void)
2774 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2775 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2779 memset(&f, 0x00,
sizeof(f));
2791 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2793 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2795 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2816 memset(pm_results, 0,
sizeof(pm_results));
2818 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2819 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2823 memset(pm_results, 0,
sizeof(pm_results));
2825 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2840 static int AppLayerProtoDetectTest14(
void)
2845 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2846 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2850 memset(&f, 0x00,
sizeof(f));
2862 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2864 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2866 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2888 memset(pm_results, 0,
sizeof(pm_results));
2891 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2896 memset(pm_results, 0,
sizeof(pm_results));
2898 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2941 int i = -1, j = -1 , k = -1;
2945 for (i = 0; i < no_of_ip_proto; i++, pp = pp->
next) {
2946 if (pp->ipproto != ip_proto[i].
ipproto)
2950 for (k = 0; k < ip_proto[i].
no_of_port; k++, pp_port = pp_port->
next) {
2951 if (pp_port->port != ip_proto[i].
port[k].
port)
2963 j++, pp_element = pp_element->
next) {
2975 if (pp_element != NULL)
2978 pp_element = pp_port->sp;
2993 if (pp_element != NULL)
2996 if (pp_port != NULL)
3005 printf(
"i = %d, k = %d, j = %d(%s)\n", i, k, j, (dir == 0) ?
"ts" :
"tc");
3010 static uint16_t ProbingParserDummyForTesting(
3011 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t input_len, uint8_t *rdir)
3016 static int AppLayerProtoDetectTest15(
void)
3024 ProbingParserDummyForTesting, NULL);
3026 ProbingParserDummyForTesting, NULL);
3028 ProbingParserDummyForTesting, NULL);
3031 ProbingParserDummyForTesting, NULL);
3033 ProbingParserDummyForTesting, NULL);
3035 ProbingParserDummyForTesting, NULL);
3037 ProbingParserDummyForTesting, NULL);
3040 ProbingParserDummyForTesting, NULL);
3042 ProbingParserDummyForTesting, NULL);
3046 ProbingParserDummyForTesting, NULL);
3050 ProbingParserDummyForTesting, NULL);
3052 ProbingParserDummyForTesting, NULL);
3055 ProbingParserDummyForTesting, NULL);
3057 ProbingParserDummyForTesting, NULL);
3059 ProbingParserDummyForTesting, NULL);
3061 ProbingParserDummyForTesting, NULL);
3063 ProbingParserDummyForTesting, NULL);
3065 ProbingParserDummyForTesting, NULL);
3067 ProbingParserDummyForTesting, NULL);
3069 ProbingParserDummyForTesting, NULL);
3071 ProbingParserDummyForTesting, NULL);
3073 ProbingParserDummyForTesting, NULL);
3085 {
"smb",
ALPROTO_SMB, 80, 5, 6 }, {
"ftp",
ALPROTO_FTP, 80, 7, 10 },
3086 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3098 {
"dcerpc",
ALPROTO_DCERPC, 81, 9, 10 }, {
"jabber",
ALPROTO_JABBER, 0, 12, 23 },
3099 {
"irc",
ALPROTO_IRC, 0, 12, 14 }, {
"tls",
ALPROTO_TLS, 0, 12, 18 },
3112 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3122 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3132 {
"irc",
ALPROTO_IRC, 0, 12, 14 }, {
"tls",
ALPROTO_TLS, 0, 12, 18 },
3161 { 85, 23, 23, element_ts_85, element_tc_85,
3164 { 90, 23, 23, element_ts_90, element_tc_90,
3167 { 0, 23, 23, element_ts_0, element_tc_0,
3196 if (AppLayerProtoDetectPPTestData(alpd_ctx.
ctx_pp, ip_proto,
3210 static int AppLayerProtoDetectTest16(
void)
3215 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3216 "User-Agent: Mozilla/1.0\r\n"
3217 "Cookie: hellocatch\r\n\r\n";
3218 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3233 printf(
"packet setup failed: ");
3237 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3239 printf(
"flow setup failed: ");
3243 f->
proto = IPPROTO_TCP;
3261 "(msg:\"Test content option\"; "
3273 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3278 if (http_state == NULL) {
3279 printf(
"no http state: ");
3287 printf(
"sig 1 didn't alert, but it should: ");
3296 if (det_ctx != NULL)
3307 static int AppLayerProtoDetectTest17(
void)
3312 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3313 "User-Agent: Mozilla/1.0\r\n"
3314 "Cookie: hellocatch\r\n\r\n";
3315 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3330 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3334 f->
proto = IPPROTO_TCP;
3350 "(msg:\"http over non standar port\"; "
3362 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3367 if (http_state == NULL) {
3368 printf(
"no http state: ");
3376 printf(
"sig 1 didn't alert, but it should: ");
3387 if (det_ctx != NULL)
3398 static int AppLayerProtoDetectTest18(
void)
3403 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3404 "User-Agent: Mozilla/1.0\r\n"
3405 "Cookie: hellocatch\r\n\r\n";
3406 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3421 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3425 f->
proto = IPPROTO_TCP;
3441 "(msg:\"Test content option\"; "
3453 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3458 if (http_state == NULL) {
3459 printf(
"no http state: ");
3467 printf(
"sig 1 alerted, but it should not (it's not ftp): ");
3477 if (det_ctx != NULL)
3488 static int AppLayerProtoDetectTest19(
void)
3492 uint8_t http_buf1[] =
"MPUT one\r\n";
3493 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3508 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3512 f->
proto = IPPROTO_TCP;
3528 "(msg:\"http over non standar port\"; "
3538 STREAM_TOSERVER, http_buf1, http_buf1_len);
3540 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3548 printf(
"sig 1 alerted, but it should not (it's ftp): ");
3559 if (det_ctx != NULL)
3572 UtRegisterTest(
"AppLayerProtoDetectTest01", AppLayerProtoDetectTest01);
3573 UtRegisterTest(
"AppLayerProtoDetectTest02", AppLayerProtoDetectTest02);
3574 UtRegisterTest(
"AppLayerProtoDetectTest03", AppLayerProtoDetectTest03);
3575 UtRegisterTest(
"AppLayerProtoDetectTest04", AppLayerProtoDetectTest04);
3576 UtRegisterTest(
"AppLayerProtoDetectTest05", AppLayerProtoDetectTest05);
3577 UtRegisterTest(
"AppLayerProtoDetectTest06", AppLayerProtoDetectTest06);
3578 UtRegisterTest(
"AppLayerProtoDetectTest07", AppLayerProtoDetectTest07);
3579 UtRegisterTest(
"AppLayerProtoDetectTest08", AppLayerProtoDetectTest08);
3580 UtRegisterTest(
"AppLayerProtoDetectTest09", AppLayerProtoDetectTest09);
3581 UtRegisterTest(
"AppLayerProtoDetectTest10", AppLayerProtoDetectTest10);
3582 UtRegisterTest(
"AppLayerProtoDetectTest11", AppLayerProtoDetectTest11);
3583 UtRegisterTest(
"AppLayerProtoDetectTest12", AppLayerProtoDetectTest12);
3584 UtRegisterTest(
"AppLayerProtoDetectTest13", AppLayerProtoDetectTest13);
3585 UtRegisterTest(
"AppLayerProtoDetectTest14", AppLayerProtoDetectTest14);
3586 UtRegisterTest(
"AppLayerProtoDetectTest15", AppLayerProtoDetectTest15);
3587 UtRegisterTest(
"AppLayerProtoDetectTest16", AppLayerProtoDetectTest16);
3588 UtRegisterTest(
"AppLayerProtoDetectTest17", AppLayerProtoDetectTest17);
3589 UtRegisterTest(
"AppLayerProtoDetectTest18", AppLayerProtoDetectTest18);
3590 UtRegisterTest(
"AppLayerProtoDetectTest19", AppLayerProtoDetectTest19);