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);
1922 bool g_enabled =
false;
1923 bool i_enabled =
false;
1928 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1930 default_enabled =
true;
1933 r = snprintf(param,
sizeof(param),
"%s%s%s%s%s",
"app-layer.protocols.", alproto,
".", ipproto,
1937 }
else if (r > (
int)
sizeof(param)) {
1938 FatalError(
"buffer not big enough to write param.");
1943 if (i_proto && i_proto->
val) {
1948 }
else if (strcasecmp(i_proto->
val,
"detection-only") == 0) {
1951 FatalError(
"Invalid value found for %s.", param);
1955 r = snprintf(param,
sizeof(param),
"%s%s%s",
"app-layer.protocols.", alproto,
".enabled");
1958 }
else if (r > (
int)
sizeof(param)) {
1959 FatalError(
"buffer not big enough to write param.");
1964 if (g_proto && g_proto->
val) {
1969 }
else if (strcasecmp(g_proto->
val,
"detection-only") == 0) {
1972 FatalError(
"Invalid value found for %s", param);
1976 if ((i_proto && g_proto) && (i_enabled ^ g_enabled)) {
1977 SCLogWarning(
"Inconsistent global (%s) and respective ipproto (%s) settings found for "
1978 "alproto %s and ipproto %s",
1979 g_enabled ?
"TRUE" :
"FALSE", i_enabled ?
"TRUE" :
"FALSE", alproto, ipproto);
1988 if (!default_enabled) {
2011 for (j = 0; j < 2; j++) {
2012 if (max_pat_id == 0) {
2032 for (j = 0; j < 2; j++) {
2059 int ipproto_map, dir;
2062 for (dir = 0; dir < 2; dir++) {
2091 AppLayerProtoDetectPMGetIpprotos(alproto, ipprotos);
2092 AppLayerProtoDetectPPGetIpprotos(alproto, ipprotos);
2093 AppLayerProtoDetectPEGetIpprotos(alproto, ipprotos);
2104 while (cur_alias != NULL) {
2105 if (strcasecmp(alproto_name, cur_alias->
proto_alias) == 0) {
2109 cur_alias = cur_alias->
next;
2115 if (alpd_ctx.
alproto_names[a] != NULL && AppProtoEquals(b, a)) {
2151 alprotos[alproto] = 1;
2157 static void AppLayerProtoDetectPEGetIpprotos(
AppProto alproto,
2164 ipprotos[IPPROTO_TCP / 8] |= 1 << (IPPROTO_TCP % 8);
2167 ipprotos[IPPROTO_UDP / 8] |= 1 << (IPPROTO_UDP % 8);
2175 SCLogError(
"Expectation on 2 IP protocols are not supported");
2193 alpd_ctx_ut = alpd_ctx;
2194 memset(&alpd_ctx, 0,
sizeof(alpd_ctx));
2201 alpd_ctx = alpd_ctx_ut;
2202 memset(&alpd_ctx_ut, 0,
sizeof(alpd_ctx_ut));
2206 static int AppLayerProtoDetectTest01(
void)
2211 const char *buf =
"HTTP";
2227 static int AppLayerProtoDetectTest02(
void)
2232 const char *buf =
"HTTP";
2253 static int AppLayerProtoDetectTest03(
void)
2258 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2260 memset(pm_results, 0,
sizeof(pm_results));
2262 memset(&f, 0x00,
sizeof(f));
2266 const char *buf =
"HTTP";
2286 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2287 &f, l7data,
sizeof(l7data),
2289 pm_results, &rflow);
2299 static int AppLayerProtoDetectTest04(
void)
2304 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2306 memset(&f, 0x00,
sizeof(f));
2308 memset(pm_results, 0,
sizeof(pm_results));
2311 const char *buf =
"200 ";
2328 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2329 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2340 static int AppLayerProtoDetectTest05(
void)
2345 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n<HTML><BODY>Blahblah</BODY></HTML>";
2347 memset(pm_results, 0,
sizeof(pm_results));
2349 memset(&f, 0x00,
sizeof(f));
2352 const char *buf =
"HTTP";
2372 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2373 &f, l7data,
sizeof(l7data),
2385 static int AppLayerProtoDetectTest06(
void)
2390 uint8_t l7data[] =
"220 Welcome to the OISF FTP server\r\n";
2392 memset(pm_results, 0,
sizeof(pm_results));
2394 memset(&f, 0x00,
sizeof(f));
2397 const char *buf =
"HTTP";
2417 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2418 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2429 static int AppLayerProtoDetectTest07(
void)
2434 uint8_t l7data[] =
"220 Welcome to the OISF HTTP/FTP server\r\n";
2436 memset(&f, 0x00,
sizeof(f));
2439 memset(pm_results, 0,
sizeof(pm_results));
2441 const char *buf =
"HTTP";
2457 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2458 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2468 static int AppLayerProtoDetectTest08(
void)
2473 uint8_t l7data[] = {
2474 0x00, 0x00, 0x00, 0x85, 0xff, 0x53, 0x4d, 0x42,
2475 0x72, 0x00, 0x00, 0x00, 0x00, 0x18, 0x53, 0xc8,
2476 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2477 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfe,
2478 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00, 0x02,
2479 0x50, 0x43, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f,
2480 0x52, 0x4b, 0x20, 0x50, 0x52, 0x4f, 0x47, 0x52,
2481 0x41, 0x4d, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02,
2482 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x31, 0x2e,
2483 0x30, 0x00, 0x02, 0x57, 0x69, 0x6e, 0x64, 0x6f,
2484 0x77, 0x73, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x57,
2485 0x6f, 0x72, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x70,
2486 0x73, 0x20, 0x33, 0x2e, 0x31, 0x61, 0x00, 0x02,
2487 0x4c, 0x4d, 0x31, 0x2e, 0x32, 0x58, 0x30, 0x30,
2488 0x32, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41,
2489 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x4e, 0x54,
2490 0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32,
2494 memset(pm_results, 0,
sizeof(pm_results));
2496 memset(&f, 0x00,
sizeof(f));
2499 const char *buf =
"|ff|SMB";
2515 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2516 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2527 static int AppLayerProtoDetectTest09(
void)
2532 uint8_t l7data[] = {
2533 0x00, 0x00, 0x00, 0x66, 0xfe, 0x53, 0x4d, 0x42,
2534 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2535 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00,
2536 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2537 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2538 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2539 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2540 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2541 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x01, 0x00,
2542 0x00, 0x00, 0x00, 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,
2549 memset(pm_results, 0,
sizeof(pm_results));
2551 memset(&f, 0x00,
sizeof(f));
2554 const char *buf =
"|fe|SMB";
2570 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2571 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2582 static int AppLayerProtoDetectTest10(
void)
2587 uint8_t l7data[] = {
2588 0x05, 0x00, 0x0b, 0x03, 0x10, 0x00, 0x00, 0x00,
2589 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2590 0xd0, 0x16, 0xd0, 0x16, 0x00, 0x00, 0x00, 0x00,
2591 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
2592 0xb8, 0x4a, 0x9f, 0x4d, 0x1c, 0x7d, 0xcf, 0x11,
2593 0x86, 0x1e, 0x00, 0x20, 0xaf, 0x6e, 0x7c, 0x57,
2594 0x00, 0x00, 0x00, 0x00, 0x04, 0x5d, 0x88, 0x8a,
2595 0xeb, 0x1c, 0xc9, 0x11, 0x9f, 0xe8, 0x08, 0x00,
2596 0x2b, 0x10, 0x48, 0x60, 0x02, 0x00, 0x00, 0x00
2599 memset(pm_results, 0,
sizeof(pm_results));
2601 memset(&f, 0x00,
sizeof(f));
2604 const char *buf =
"|05 00|";
2621 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2622 &f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2637 static int AppLayerProtoDetectTest11(
void)
2642 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2643 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2645 memset(pm_results, 0,
sizeof(pm_results));
2647 memset(&f, 0x00,
sizeof(f));
2659 IPPROTO_TCP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2661 IPPROTO_TCP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2663 IPPROTO_TCP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2688 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2689 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2694 memset(pm_results, 0,
sizeof(pm_results));
2696 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2710 static int AppLayerProtoDetectTest12(
void)
2722 printf(
"failure 1\n");
2728 printf(
"failure 2\n");
2734 printf(
"failure 3\n");
2738 printf(
"failure 4\n");
2742 printf(
"failure 5\n");
2746 printf(
"failure 6\n");
2762 static int AppLayerProtoDetectTest13(
void)
2767 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2768 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2772 memset(&f, 0x00,
sizeof(f));
2784 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2786 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2788 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2809 memset(pm_results, 0,
sizeof(pm_results));
2811 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2812 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2816 memset(pm_results, 0,
sizeof(pm_results));
2818 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2833 static int AppLayerProtoDetectTest14(
void)
2838 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2839 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2843 memset(&f, 0x00,
sizeof(f));
2855 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2857 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2859 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2881 memset(pm_results, 0,
sizeof(pm_results));
2884 &f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2889 memset(pm_results, 0,
sizeof(pm_results));
2891 &f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2934 int i = -1, j = -1 , k = -1;
2938 for (i = 0; i < no_of_ip_proto; i++, pp = pp->
next) {
2939 if (pp->ipproto != ip_proto[i].
ipproto)
2943 for (k = 0; k < ip_proto[i].
no_of_port; k++, pp_port = pp_port->
next) {
2944 if (pp_port->port != ip_proto[i].
port[k].
port)
2956 j++, pp_element = pp_element->
next) {
2968 if (pp_element != NULL)
2971 pp_element = pp_port->sp;
2986 if (pp_element != NULL)
2989 if (pp_port != NULL)
2998 printf(
"i = %d, k = %d, j = %d(%s)\n", i, k, j, (dir == 0) ?
"ts" :
"tc");
3003 static uint16_t ProbingParserDummyForTesting(
3004 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t input_len, uint8_t *rdir)
3009 static int AppLayerProtoDetectTest15(
void)
3017 ProbingParserDummyForTesting, NULL);
3019 ProbingParserDummyForTesting, NULL);
3021 ProbingParserDummyForTesting, NULL);
3024 ProbingParserDummyForTesting, NULL);
3026 ProbingParserDummyForTesting, NULL);
3028 ProbingParserDummyForTesting, NULL);
3030 ProbingParserDummyForTesting, NULL);
3033 ProbingParserDummyForTesting, NULL);
3035 ProbingParserDummyForTesting, NULL);
3039 ProbingParserDummyForTesting, NULL);
3043 ProbingParserDummyForTesting, NULL);
3045 ProbingParserDummyForTesting, NULL);
3048 ProbingParserDummyForTesting, NULL);
3050 ProbingParserDummyForTesting, NULL);
3052 ProbingParserDummyForTesting, NULL);
3054 ProbingParserDummyForTesting, NULL);
3056 ProbingParserDummyForTesting, NULL);
3058 ProbingParserDummyForTesting, NULL);
3060 ProbingParserDummyForTesting, NULL);
3062 ProbingParserDummyForTesting, NULL);
3064 ProbingParserDummyForTesting, NULL);
3066 ProbingParserDummyForTesting, NULL);
3078 {
"smb",
ALPROTO_SMB, 80, 5, 6 }, {
"ftp",
ALPROTO_FTP, 80, 7, 10 },
3079 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3091 {
"dcerpc",
ALPROTO_DCERPC, 81, 9, 10 }, {
"jabber",
ALPROTO_JABBER, 0, 12, 23 },
3092 {
"irc",
ALPROTO_IRC, 0, 12, 14 }, {
"tls",
ALPROTO_TLS, 0, 12, 18 },
3105 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3115 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3125 {
"irc",
ALPROTO_IRC, 0, 12, 14 }, {
"tls",
ALPROTO_TLS, 0, 12, 18 },
3154 { 85, 23, 23, element_ts_85, element_tc_85,
3157 { 90, 23, 23, element_ts_90, element_tc_90,
3160 { 0, 23, 23, element_ts_0, element_tc_0,
3189 if (AppLayerProtoDetectPPTestData(alpd_ctx.
ctx_pp, ip_proto,
3203 static int AppLayerProtoDetectTest16(
void)
3208 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3209 "User-Agent: Mozilla/1.0\r\n"
3210 "Cookie: hellocatch\r\n\r\n";
3211 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3226 printf(
"packet setup failed: ");
3230 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3232 printf(
"flow setup failed: ");
3236 f->
proto = IPPROTO_TCP;
3254 "(msg:\"Test content option\"; "
3266 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3271 if (http_state == NULL) {
3272 printf(
"no http state: ");
3280 printf(
"sig 1 didn't alert, but it should: ");
3289 if (det_ctx != NULL)
3300 static int AppLayerProtoDetectTest17(
void)
3305 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3306 "User-Agent: Mozilla/1.0\r\n"
3307 "Cookie: hellocatch\r\n\r\n";
3308 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3323 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3327 f->
proto = IPPROTO_TCP;
3343 "(msg:\"http over non standar port\"; "
3355 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3360 if (http_state == NULL) {
3361 printf(
"no http state: ");
3369 printf(
"sig 1 didn't alert, but it should: ");
3380 if (det_ctx != NULL)
3391 static int AppLayerProtoDetectTest18(
void)
3396 uint8_t http_buf1[] =
"POST /one HTTP/1.0\r\n"
3397 "User-Agent: Mozilla/1.0\r\n"
3398 "Cookie: hellocatch\r\n\r\n";
3399 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3414 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3418 f->
proto = IPPROTO_TCP;
3434 "(msg:\"Test content option\"; "
3446 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3451 if (http_state == NULL) {
3452 printf(
"no http state: ");
3460 printf(
"sig 1 alerted, but it should not (it's not ftp): ");
3470 if (det_ctx != NULL)
3481 static int AppLayerProtoDetectTest19(
void)
3485 uint8_t http_buf1[] =
"MPUT one\r\n";
3486 uint32_t http_buf1_len =
sizeof(http_buf1) - 1;
3501 f =
UTHBuildFlow(AF_INET,
"1.1.1.1",
"2.2.2.2", 1024, 80);
3505 f->
proto = IPPROTO_TCP;
3521 "(msg:\"http over non standar port\"; "
3531 STREAM_TOSERVER, http_buf1, http_buf1_len);
3533 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3541 printf(
"sig 1 alerted, but it should not (it's ftp): ");
3552 if (det_ctx != NULL)
3565 UtRegisterTest(
"AppLayerProtoDetectTest01", AppLayerProtoDetectTest01);
3566 UtRegisterTest(
"AppLayerProtoDetectTest02", AppLayerProtoDetectTest02);
3567 UtRegisterTest(
"AppLayerProtoDetectTest03", AppLayerProtoDetectTest03);
3568 UtRegisterTest(
"AppLayerProtoDetectTest04", AppLayerProtoDetectTest04);
3569 UtRegisterTest(
"AppLayerProtoDetectTest05", AppLayerProtoDetectTest05);
3570 UtRegisterTest(
"AppLayerProtoDetectTest06", AppLayerProtoDetectTest06);
3571 UtRegisterTest(
"AppLayerProtoDetectTest07", AppLayerProtoDetectTest07);
3572 UtRegisterTest(
"AppLayerProtoDetectTest08", AppLayerProtoDetectTest08);
3573 UtRegisterTest(
"AppLayerProtoDetectTest09", AppLayerProtoDetectTest09);
3574 UtRegisterTest(
"AppLayerProtoDetectTest10", AppLayerProtoDetectTest10);
3575 UtRegisterTest(
"AppLayerProtoDetectTest11", AppLayerProtoDetectTest11);
3576 UtRegisterTest(
"AppLayerProtoDetectTest12", AppLayerProtoDetectTest12);
3577 UtRegisterTest(
"AppLayerProtoDetectTest13", AppLayerProtoDetectTest13);
3578 UtRegisterTest(
"AppLayerProtoDetectTest14", AppLayerProtoDetectTest14);
3579 UtRegisterTest(
"AppLayerProtoDetectTest15", AppLayerProtoDetectTest15);
3580 UtRegisterTest(
"AppLayerProtoDetectTest16", AppLayerProtoDetectTest16);
3581 UtRegisterTest(
"AppLayerProtoDetectTest17", AppLayerProtoDetectTest17);
3582 UtRegisterTest(
"AppLayerProtoDetectTest18", AppLayerProtoDetectTest18);
3583 UtRegisterTest(
"AppLayerProtoDetectTest19", AppLayerProtoDetectTest19);