177 static void AppLayerProtoDetectPEGetIpprotos(
AppProto alproto,
188 uint32_t buflen, uint16_t searchlen,
bool *rflow)
193 SCLogDebug(
"s->co->offset (%"PRIu16
") > searchlen (%"PRIu16
")",
197 if (s->
cd->
depth > searchlen) {
198 SCLogDebug(
"s->co->depth (%"PRIu16
") > searchlen (%"PRIu16
")",
203 const uint8_t *sbuf = buf + s->
cd->
offset;
205 SCLogDebug(
"s->co->offset (%"PRIu16
") s->cd->depth (%"PRIu16
")",
214 uint8_t direction = (
flags & (STREAM_TOSERVER | STREAM_TOCLIENT));
215 SCLogDebug(
"matching, s->direction %s, our dir %s",
216 (s->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
217 (
flags & STREAM_TOSERVER) ?
"toserver" :
"toclient");
222 SCLogDebug(
"direction is wrong, rflow = true");
228 SCLogDebug(
"PP can't be run yet as pp_min_depth %u > buflen %u",
236 SCLogDebug(
"found %s/%u, rdir %02x reverse_flow? %s",
238 (rdir && direction != rdir) ?
"true" :
"false");
239 *rflow = (rdir && direction != rdir);
246 SCLogDebug(
"depth reached and answer inconclusive: fail");
261 uint32_t buflen, uint8_t
flags,
AppProto *pm_results,
bool *rflow)
267 SCLogDebug(
"searchlen %u buflen %u", searchlen, buflen);
273 if (search_cnt == 0) {
281 memset(pm_results_bf, 0,
sizeof(pm_results_bf));
290 s, tctx,
f,
flags, buf, buflen, searchlen, rflow);
293 if (AppProtoIsValid(
proto) &&
294 !(pm_results_bf[
proto / 8] & (1 << (
proto % 8))) )
296 pm_results[pm_matches++] =
proto;
302 if (pm_matches == 0 && buflen >= pm_ctx->
pp_max_len) {
314 const uint8_t *buf, uint32_t buflen, uint8_t
flags,
AppProto *pm_results,
bool *rflow)
329 if (
flags & STREAM_TOSERVER) {
337 m = PMGetProtoInspect(tctx, pm_ctx, mpm_tctx,
f, buf, buflen,
flags, pm_results, rflow);
358 if (
flags & STREAM_TOSERVER) {
365 SCLogDebug(
"no matches and in midstream mode, lets try the "
366 "*patterns for the other side");
370 om = PMGetProtoInspect(
371 tctx, pm_ctx, mpm_tctx,
f, buf, buflen,
flags, pm_results, rflow);
379 }
else if (om < 0 &&
m &&
m < 0) {
384 }
else if (om == 0 ||
m == 0) {
406 while (pp_port != NULL) {
407 if (pp_port->
dp != NULL && pp_port->
dp->
alproto == alproto) {
408 pp_elem = pp_port->
dp;
411 if (pp_port->
sp != NULL && pp_port->
sp->
alproto == alproto) {
412 pp_elem = pp_port->
sp;
415 pp_port = pp_port->
next;
418 SCReturnPtr(pp_elem,
"AppLayerProtoDetectProbingParserElement *");
438 while (pp_port != NULL) {
443 pp_port = pp_port->
next;
447 SCReturnPtr(pp_port,
"AppLayerProtoDetectProbingParserPort *");
453 AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, port);
483 uint8_t
flags,
const uint8_t *buf, uint32_t buflen, uint32_t *alproto_masks, uint8_t *rdir,
488 if (alproto_masks != NULL) {
490 if (buflen < pe->min_depth || (alproto_masks[0] &
BIT_U32(*nb_tried))) {
493 *nb_tried = *nb_tried + 1;
496 }
else if (buflen < pe->min_depth) {
507 if (AppProtoIsValid(alproto)) {
510 if (alproto_masks != NULL) {
513 alproto_masks[0] |=
BIT_U32(*nb_tried);
515 *nb_tried = *nb_tried + 1;
530 static AppProto AppLayerProtoDetectPPGetProto(
Flow *
f,
const uint8_t *buf, uint32_t buflen,
531 uint8_t ipproto,
const uint8_t
flags,
bool *reverse_flow)
551 uint8_t nb_tried = 0;
552 uint32_t *alproto_masks = NULL;
553 uint8_t idir = (
flags & (STREAM_TOSERVER | STREAM_TOCLIENT));
557 bool probe_is_found =
false;
564 (dir == STREAM_TOSERVER) ?
"toserver" :
"toclient");
566 if (dir == STREAM_TOSERVER) {
568 pp_port_dp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, dp);
570 if (pp_port_dp != NULL) {
571 SCLogDebug(
"toserver - Probing parser found for destination port %"PRIu16, dp);
574 pe1 = pp_port_dp->
dp;
576 SCLogDebug(
"toserver - No probing parser registered for dest port %"PRIu16, dp);
579 pp_port_sp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, sp);
580 if (pp_port_sp != NULL) {
581 SCLogDebug(
"toserver - Probing parser found for source port %"PRIu16, sp);
584 pe2 = pp_port_sp->
sp;
586 SCLogDebug(
"toserver - No probing parser registered for source port %"PRIu16, sp);
590 pp_port_dp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, dp);
596 if (pp_port_dp != NULL) {
597 SCLogDebug(
"toclient - Probing parser found for destination port %"PRIu16, dp);
600 pe1 = pp_port_dp->
dp;
602 SCLogDebug(
"toclient - No probing parser registered for dest port %"PRIu16, dp);
605 pp_port_sp = AppLayerProtoDetectGetProbingParsers(alpd_ctx.
ctx_pp, ipproto, sp);
606 if (pp_port_sp != NULL) {
607 SCLogDebug(
"toclient - Probing parser found for source port %"PRIu16, sp);
609 pe2 = pp_port_sp->
sp;
611 SCLogDebug(
"toclient - No probing parser registered for source port %"PRIu16, sp);
619 pe0 = AppLayerProtoDetectGetProbingParser(alpd_ctx.
ctx_pp, ipproto,
f->
alproto_tc);
621 pe0 = AppLayerProtoDetectGetProbingParser(alpd_ctx.
ctx_pp, ipproto,
f->
alproto_ts);
624 if (pe1 == NULL && pe2 == NULL && pe0 == NULL) {
625 SCLogDebug(
"%s - No probing parsers found for either port",
626 (dir == STREAM_TOSERVER) ?
"toserver":
"toclient");
629 probe_is_found =
true;
635 alproto = PPGetProto(pe0,
f,
flags, buf, buflen, NULL, &rdir, NULL);
636 if (AppProtoIsValid(alproto))
638 alproto = PPGetProto(pe1,
f,
flags, buf, buflen, alproto_masks, &rdir, &nb_tried);
639 if (AppProtoIsValid(alproto))
641 alproto = PPGetProto(pe2,
f,
flags, buf, buflen, alproto_masks, &rdir, &nb_tried);
642 if (AppProtoIsValid(alproto))
649 if (alproto_masks[0] ==
BIT_U32(nb_tried) - 1) {
651 SCLogDebug(
"%s, mask is now %08x, needed %08x, so done",
652 (dir == STREAM_TOSERVER) ?
"toserver" :
"toclient", alproto_masks[0],
656 (dir == STREAM_TOSERVER) ?
"toserver" :
"toclient", alproto_masks[0],
663 if (idir == STREAM_TOSERVER) {
664 dir = STREAM_TOCLIENT;
666 dir = STREAM_TOSERVER;
668 SCLogDebug(
"no match + midstream, retry the other direction %s",
669 (dir == STREAM_TOSERVER) ?
"toserver" :
"toclient");
670 goto again_midstream;
671 }
else if (!probe_is_found) {
676 if (AppProtoIsValid(alproto) && rdir != 0 && rdir != idir) {
677 SCLogDebug(
"PP found %u, is reverse flow", alproto);
678 *reverse_flow =
true;
682 (idir == STREAM_TOSERVER) ?
"toserver":
"toclient", alproto_masks[0]);
688 static void AppLayerProtoDetectPPGetIpprotos(
AppProto alproto,
697 for (pp = alpd_ctx.
ctx_pp; pp != NULL; pp = pp->
next) {
698 for (pp_port = pp->
port; pp_port != NULL; pp_port = pp_port->
next) {
699 for (pp_pe = pp_port->
dp; pp_pe != NULL; pp_pe = pp_pe->
next) {
703 for (pp_pe = pp_port->
sp; pp_pe != NULL; pp_pe = pp_pe->
next) {
723 SCReturnPtr(
p,
"AppLayerProtoDetectProbingParserElement");
756 AppLayerProtoDetectProbingParserElementFree(e);
763 AppLayerProtoDetectProbingParserElementFree(e);
791 AppLayerProtoDetectProbingParserPortFree(pt);
801 AppProto alproto, uint16_t min_depth, uint16_t max_depth)
810 if (max_depth != 0 && min_depth >= max_depth) {
812 "register the probing parser. min_depth >= max_depth");
815 if (alproto <= ALPROTO_UNKNOWN || alproto >=
g_alproto_max) {
816 SCLogError(
"Invalid arguments sent to register "
817 "the probing parser. Invalid alproto - %d",
822 SCReturnPtr(pe,
"AppLayerProtoDetectProbingParserElement");
824 AppLayerProtoDetectProbingParserElementFree(pe);
825 SCReturnPtr(NULL,
"AppLayerProtoDetectProbingParserElement");
842 SCReturnPtr(new_pe,
"AppLayerProtoDetectProbingParserElement");
853 printf(
"\nProtocol Detection Configuration\n");
855 for ( ; pp != NULL; pp = pp->
next) {
857 if (pp->
ipproto == IPPROTO_TCP)
858 printf(
"IPProto: TCP\n");
859 else if (pp->
ipproto == IPPROTO_UDP)
860 printf(
"IPProto: UDP\n");
862 printf(
"IPProto: %"PRIu8
"\n", pp->
ipproto);
865 for ( ; pp_port != NULL; pp_port = pp_port->
next) {
866 if (pp_port->
dp != NULL) {
867 printf(
" Port: %"PRIu16
"\n", pp_port->
port);
869 printf(
" Destination port: (max-depth: %" PRIu16
")\n",
872 for ( ; pp_pe != NULL; pp_pe = pp_pe->
next) {
875 printf(
" min_depth: %"PRIu32
"\n", pp_pe->
min_depth);
876 printf(
" max_depth: %"PRIu32
"\n", pp_pe->
max_depth);
882 if (pp_port->
sp == NULL) {
886 printf(
" Source port: (max-depth: %" PRIu16
")\n", pp_port->
sp_max_depth);
888 for ( ; pp_pe != NULL; pp_pe = pp_pe->
next) {
891 printf(
" min_depth: %"PRIu32
"\n", pp_pe->
min_depth);
892 printf(
" max_depth: %"PRIu32
"\n", pp_pe->
max_depth);
908 if (*head_pe == NULL) {
914 while (temp_pe->
next != NULL)
915 temp_pe = temp_pe->
next;
916 temp_pe->
next = new_pe;
926 if (*head_pp == NULL) {
932 while (temp_pp->
next != NULL)
933 temp_pp = temp_pp->
next;
934 temp_pp->
next = new_pp;
945 if (*head_port == NULL) {
946 *head_port = new_port;
951 if ((*head_port)->port == 0 && (*head_port)->use_ports) {
952 new_port->
next = *head_port;
953 *head_port = new_port;
956 while (temp_port->
next != NULL &&
958 temp_port = temp_port->
next;
961 temp_port->
next = new_port;
969 uint8_t ipproto,
bool use_ports, uint16_t port,
AppProto alproto, uint16_t min_depth,
977 while (curr_pp != NULL) {
978 if (curr_pp->
ipproto == ipproto)
980 curr_pp = curr_pp->
next;
982 if (curr_pp == NULL) {
985 AppLayerProtoDetectProbingParserAppend(pp, new_pp);
991 while (curr_port != NULL) {
993 if (curr_port->
port == port && use_ports)
995 curr_port = curr_port->
next;
997 if (curr_port == NULL) {
999 new_port->
port = port;
1001 AppLayerProtoDetectProbingParserPortAppend(&curr_pp->
port, new_port);
1002 curr_port = new_port;
1003 if (direction & STREAM_TOSERVER) {
1011 zero_port = curr_pp->
port;
1013 while (zero_port != NULL && !(zero_port->
port == 0 && zero_port->
use_ports)) {
1014 zero_port = zero_port->
next;
1016 if (zero_port != NULL) {
1019 zero_pe = zero_port->
dp;
1020 for ( ; zero_pe != NULL; zero_pe = zero_pe->
next) {
1021 if (curr_port->
dp == NULL)
1031 AppLayerProtoDetectProbingParserElementDuplicate(zero_pe);
1032 AppLayerProtoDetectProbingParserElementAppend(&curr_port->
dp, dup_pe);
1035 zero_pe = zero_port->
sp;
1036 for ( ; zero_pe != NULL; zero_pe = zero_pe->
next) {
1037 if (curr_port->
sp == NULL)
1047 AppLayerProtoDetectProbingParserElementDuplicate(zero_pe);
1048 AppLayerProtoDetectProbingParserElementAppend(&curr_port->
sp, dup_pe);
1055 if (direction & STREAM_TOSERVER)
1056 curr_pe = curr_port->
dp;
1058 curr_pe = curr_port->
sp;
1059 while (curr_pe != NULL) {
1060 if (curr_pe->
alproto == alproto) {
1062 "ipproto - %" PRIu8
" Port - %" PRIu16
" "
1063 "App Protocol - NULL, App Protocol(ID) - "
1064 "%" PRIu16
" min_depth - %" PRIu16
" "
1065 "max_dept - %" PRIu16
".",
1066 ipproto, port, alproto, min_depth, max_depth);
1069 curr_pe = curr_pe->
next;
1073 AppLayerProtoDetectProbingParserElementCreate(alproto, min_depth, max_depth);
1078 if (direction & STREAM_TOSERVER) {
1081 if (curr_port->
dp == NULL)
1089 head_pe = &curr_port->
dp;
1093 if (curr_port->
sp == NULL)
1101 head_pe = &curr_port->
sp;
1103 AppLayerProtoDetectProbingParserElementAppend(head_pe, new_pe);
1108 while (temp_port != NULL && !(temp_port->
port == 0 && temp_port->
use_ports)) {
1109 if (direction & STREAM_TOSERVER) {
1110 if (temp_port->
dp == NULL)
1118 AppLayerProtoDetectProbingParserElementAppend(
1119 &temp_port->
dp, AppLayerProtoDetectProbingParserElementDuplicate(curr_pe));
1121 if (temp_port->
sp == NULL)
1129 AppLayerProtoDetectProbingParserElementAppend(
1130 &temp_port->
sp, AppLayerProtoDetectProbingParserElementDuplicate(curr_pe));
1132 temp_port = temp_port->
next;
1142 static void AppLayerProtoDetectPMGetIpprotos(
AppProto alproto,
1149 for (
int j = 0; j < 2; j++) {
1155 ipprotos[ipproto / 8] |= 1 << (ipproto % 8);
1167 typedef struct TempContainer_ {
1169 uint16_t content_len;
1174 uint32_t struct_total_size = 0;
1175 uint32_t content_total_size = 0;
1177 uint8_t *ahb = NULL;
1178 uint8_t *content = NULL;
1179 uint16_t content_len = 0;
1181 TempContainer *struct_offset = NULL;
1182 uint8_t *content_offset = NULL;
1185 if (
ctx->head == NULL)
1188 for (s =
ctx->head; s != NULL; s = s->
next) {
1189 struct_total_size +=
sizeof(TempContainer);
1194 ahb =
SCMalloc(
sizeof(uint8_t) * (struct_total_size + content_total_size));
1198 struct_offset = (TempContainer *)ahb;
1199 content_offset = ahb + struct_total_size;
1200 for (s =
ctx->head; s != NULL; s = s->
next) {
1201 TempContainer *tcdup = (TempContainer *)ahb;
1205 for (; tcdup != struct_offset; tcdup++) {
1206 if (tcdup->content_len != content_len ||
1207 SCMemcmp(tcdup->content, content, tcdup->content_len) != 0)
1214 if (tcdup != struct_offset) {
1215 s->
cd->
id = tcdup->id;
1219 struct_offset->content_len = content_len;
1220 struct_offset->content = content_offset;
1221 content_offset += content_len;
1222 memcpy(struct_offset->content, content, content_len);
1223 struct_offset->id = max_id++;
1224 s->
cd->
id = struct_offset->id;
1229 ctx->max_pat_id = max_id;
1250 if (
ctx->map == NULL)
1254 for (s =
ctx->head; s != NULL; ) {
1273 ctx->map[s->
id] = s;
1315 AppProto alproto, uint8_t direction,
1317 uint16_t pp_min_depth, uint16_t pp_max_depth)
1339 static int AppLayerProtoDetectPMRegisterPattern(uint8_t ipproto,
AppProto alproto,
1340 const char *pattern,
1341 uint16_t depth, uint16_t
offset,
1345 uint16_t pp_min_depth, uint16_t pp_max_depth)
1369 if (depth < cd->content_len)
1372 if (direction & STREAM_TOSERVER)
1379 if (depth < ctx_pm->min_len)
1383 AppLayerProtoDetectPMAddSignature(ctx_pm, cd, alproto, direction,
1384 PPFunc, pp_min_depth, pp_max_depth);
1397 const uint8_t *buf, uint32_t buflen, uint8_t ipproto, uint8_t
flags,
bool *reverse_flow)
1400 SCLogDebug(
"buflen %u for %s direction", buflen,
1401 (
flags & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1407 uint16_t pm_matches = AppLayerProtoDetectPMGetProto(
1408 tctx,
f, buf, buflen,
flags, pm_results, reverse_flow);
1409 if (pm_matches > 0) {
1411 alproto = pm_results[0];
1414 uint8_t reverse_dir = (
flags & STREAM_TOSERVER) ? STREAM_TOCLIENT : STREAM_TOSERVER;
1427 alproto = AppLayerProtoDetectPPGetProto(
f, buf, buflen, ipproto,
flags, reverse_flow);
1428 if (AppProtoIsValid(alproto)) {
1436 alproto = AppLayerProtoDetectPEGetProto(
f,
flags);
1451 while (pp != NULL) {
1453 AppLayerProtoDetectProbingParserFree(pp);
1461 static void AppLayerProtoDetectFreeAliases(
void)
1466 if (cur_alias == NULL)
1470 while (cur_alias != NULL) {
1471 next_alias = cur_alias->
next;
1473 cur_alias = next_alias;
1493 for (j = 0; j < 2; j++) {
1496 if (AppLayerProtoDetectPMSetContentIDs(ctx_pm) < 0)
1502 if (AppLayerProtoDetectPMMapSignatures(ctx_pm) < 0)
1504 if (AppLayerProtoDetectPMPrepareMpm(ctx_pm) < 0)
1511 AppLayerProtoDetectPrintProbingParsers(alpd_ctx.
ctx_pp);
1529 uint16_t min_depth, uint16_t max_depth, uint8_t direction,
ProbingParserFPtr ProbingParser1,
1535 if (portstr == NULL) {
1538 AppLayerProtoDetectInsertNewProbingParser(&alpd_ctx.
ctx_pp, ipproto,
false, 0, alproto,
1539 min_depth, max_depth, direction, ProbingParser1, ProbingParser2);
1544 while (temp_dp != NULL) {
1545 uint16_t port = temp_dp->
port;
1546 if (port == 0 && temp_dp->
port2 != 0)
1549 AppLayerProtoDetectInsertNewProbingParser(&alpd_ctx.
ctx_pp, ipproto,
true, port,
1550 alproto, min_depth, max_depth, direction, ProbingParser1, ProbingParser2);
1551 if (port == temp_dp->
port2) {
1557 temp_dp = temp_dp->
next;
1565 const char *alproto_name,
AppProto alproto, uint16_t min_depth, uint16_t max_depth,
1576 r = snprintf(param,
sizeof(param),
"%s%s%s",
"app-layer.protocols.",
1577 alproto_name,
".detection-ports");
1580 }
else if (r > (
int)
sizeof(param)) {
1581 FatalError(
"buffer not big enough to write param.");
1585 SCLogDebug(
"Entry for %s not found.", param);
1586 r = snprintf(param,
sizeof(param),
"%s%s%s%s%s",
"app-layer.protocols.",
1587 alproto_name,
".", ipproto_name,
".detection-ports");
1590 }
else if (r > (
int)
sizeof(param)) {
1591 FatalError(
"buffer not big enough to write param.");
1600 if (port_node == NULL)
1603 if (port_node != NULL && port_node->
val != NULL) {
1606 ProbingParserTs, ProbingParserTc);
1611 if (port_node == NULL)
1614 if (port_node != NULL && port_node->
val != NULL) {
1617 ProbingParserTc, ProbingParserTs);
1628 uint16_t depth, uint16_t
offset, uint8_t direction)
1631 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1639 const char *pattern, uint16_t depth, uint16_t
offset, uint8_t direction,
1643 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1646 PPFunc, pp_min_depth, pp_max_depth);
1651 const char *pattern, uint16_t depth, uint16_t
offset, uint8_t direction,
1655 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto, pattern, depth,
offset,
1656 direction, 0 , PPFunc, pp_min_depth, pp_max_depth);
1661 uint16_t depth, uint16_t
offset, uint8_t direction)
1664 int r = AppLayerProtoDetectPMRegisterPattern(ipproto, alproto,
1679 memset(&alpd_ctx, 0,
sizeof(alpd_ctx));
1686 FatalError(
"Unable to alloc SpmGlobalThreadCtx.");
1690 for (j = 0; j < 2; j++) {
1697 FatalError(
"Unable to alloc alproto_names.");
1703 FatalError(
"Unable to alloc expectation_proto.");
1734 int ipproto_map = 0;
1741 for (dir = 0; dir < 2; dir++) {
1744 for (
id = 0;
id < pm_ctx->
max_sig_id;
id++) {
1745 sig = pm_ctx->
map[id];
1746 AppLayerProtoDetectPMFreeSignature(sig);
1762 AppLayerProtoDetectFreeAliases();
1764 AppLayerProtoDetectFreeProbingParsers(alpd_ctx.
ctx_pp);
1776 FatalError(
"Unable to realloc alproto_names.");
1800 new_alias->
next = NULL;
1802 if (alpda_ctx == NULL) {
1803 alpda_ctx = new_alias;
1806 while (cur_alias->
next != NULL) {
1807 cur_alias = cur_alias->
next;
1809 cur_alias->
next = new_alias;
1907 const char *ipproto,
const char *alproto,
bool default_enabled)
1911 BUG_ON(ipproto == NULL || alproto == NULL);
1916 bool g_enabled =
false;
1917 bool i_enabled =
false;
1922 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1924 default_enabled =
true;
1927 r = snprintf(param,
sizeof(param),
"%s%s%s%s%s",
"app-layer.protocols.", alproto,
".", ipproto,
1931 }
else if (r > (
int)
sizeof(param)) {
1932 FatalError(
"buffer not big enough to write param.");
1937 if (i_proto && i_proto->
val) {
1942 }
else if (strcasecmp(i_proto->
val,
"detection-only") == 0) {
1945 FatalError(
"Invalid value found for %s.", param);
1949 r = snprintf(param,
sizeof(param),
"%s%s%s",
"app-layer.protocols.", alproto,
".enabled");
1952 }
else if (r > (
int)
sizeof(param)) {
1953 FatalError(
"buffer not big enough to write param.");
1958 if (g_proto && g_proto->
val) {
1963 }
else if (strcasecmp(g_proto->
val,
"detection-only") == 0) {
1966 FatalError(
"Invalid value found for %s", param);
1970 if ((i_proto && g_proto) && (i_enabled ^ g_enabled)) {
1971 SCLogWarning(
"Inconsistent global (%s) and respective ipproto (%s) settings found for "
1972 "alproto %s and ipproto %s",
1973 g_enabled ?
"TRUE" :
"FALSE", i_enabled ?
"TRUE" :
"FALSE", alproto, ipproto);
1982 if (!default_enabled) {
2005 for (j = 0; j < 2; j++) {
2006 if (max_pat_id == 0) {
2026 for (j = 0; j < 2; j++) {
2053 int ipproto_map, dir;
2056 for (dir = 0; dir < 2; dir++) {
2085 AppLayerProtoDetectPMGetIpprotos(alproto, ipprotos);
2086 AppLayerProtoDetectPPGetIpprotos(alproto, ipprotos);
2087 AppLayerProtoDetectPEGetIpprotos(alproto, ipprotos);
2098 while (cur_alias != NULL) {
2099 if (strcasecmp(alproto_name, cur_alias->
proto_alias) == 0) {
2103 cur_alias = cur_alias->
next;
2109 if (alpd_ctx.
alproto_names[a] != NULL && AppProtoEquals(b, a)) {
2145 alprotos[alproto] = 1;
2151 static void AppLayerProtoDetectPEGetIpprotos(
AppProto alproto,
2158 ipprotos[IPPROTO_TCP / 8] |= 1 << (IPPROTO_TCP % 8);
2161 ipprotos[IPPROTO_UDP / 8] |= 1 << (IPPROTO_UDP % 8);
2169 SCLogError(
"Expectation on 2 IP protocols are not supported");
2194 alpd_ctx_ut = alpd_ctx;
2195 memset(&alpd_ctx, 0,
sizeof(alpd_ctx));
2202 alpd_ctx = alpd_ctx_ut;
2203 memset(&alpd_ctx_ut, 0,
sizeof(alpd_ctx_ut));
2207 static int AppLayerProtoDetectTest01(
void)
2212 const char *buf =
"HTTP";
2228 static int AppLayerProtoDetectTest02(
void)
2233 const char *buf =
"HTTP";
2254 static int AppLayerProtoDetectTest03(
void)
2259 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2261 memset(pm_results, 0,
sizeof(pm_results));
2263 memset(&
f, 0x00,
sizeof(
f));
2267 const char *buf =
"HTTP";
2287 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2288 &
f, l7data,
sizeof(l7data),
2290 pm_results, &rflow);
2300 static int AppLayerProtoDetectTest04(
void)
2305 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n";
2307 memset(&
f, 0x00,
sizeof(
f));
2309 memset(pm_results, 0,
sizeof(pm_results));
2312 const char *buf =
"200 ";
2329 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2330 &
f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2341 static int AppLayerProtoDetectTest05(
void)
2346 uint8_t l7data[] =
"HTTP/1.1 200 OK\r\nServer: Apache/1.0\r\n\r\n<HTML><BODY>Blahblah</BODY></HTML>";
2348 memset(pm_results, 0,
sizeof(pm_results));
2350 memset(&
f, 0x00,
sizeof(
f));
2353 const char *buf =
"HTTP";
2373 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2374 &
f, l7data,
sizeof(l7data),
2386 static int AppLayerProtoDetectTest06(
void)
2391 uint8_t l7data[] =
"220 Welcome to the OISF FTP server\r\n";
2393 memset(pm_results, 0,
sizeof(pm_results));
2395 memset(&
f, 0x00,
sizeof(
f));
2398 const char *buf =
"HTTP";
2418 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2419 &
f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2430 static int AppLayerProtoDetectTest07(
void)
2435 uint8_t l7data[] =
"220 Welcome to the OISF HTTP/FTP server\r\n";
2437 memset(&
f, 0x00,
sizeof(
f));
2440 memset(pm_results, 0,
sizeof(pm_results));
2442 const char *buf =
"HTTP";
2458 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2459 &
f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2469 static int AppLayerProtoDetectTest08(
void)
2474 uint8_t l7data[] = {
2475 0x00, 0x00, 0x00, 0x85, 0xff, 0x53, 0x4d, 0x42,
2476 0x72, 0x00, 0x00, 0x00, 0x00, 0x18, 0x53, 0xc8,
2477 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2478 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfe,
2479 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00, 0x02,
2480 0x50, 0x43, 0x20, 0x4e, 0x45, 0x54, 0x57, 0x4f,
2481 0x52, 0x4b, 0x20, 0x50, 0x52, 0x4f, 0x47, 0x52,
2482 0x41, 0x4d, 0x20, 0x31, 0x2e, 0x30, 0x00, 0x02,
2483 0x4c, 0x41, 0x4e, 0x4d, 0x41, 0x4e, 0x31, 0x2e,
2484 0x30, 0x00, 0x02, 0x57, 0x69, 0x6e, 0x64, 0x6f,
2485 0x77, 0x73, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x57,
2486 0x6f, 0x72, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x70,
2487 0x73, 0x20, 0x33, 0x2e, 0x31, 0x61, 0x00, 0x02,
2488 0x4c, 0x4d, 0x31, 0x2e, 0x32, 0x58, 0x30, 0x30,
2489 0x32, 0x00, 0x02, 0x4c, 0x41, 0x4e, 0x4d, 0x41,
2490 0x4e, 0x32, 0x2e, 0x31, 0x00, 0x02, 0x4e, 0x54,
2491 0x20, 0x4c, 0x4d, 0x20, 0x30, 0x2e, 0x31, 0x32,
2495 memset(pm_results, 0,
sizeof(pm_results));
2497 memset(&
f, 0x00,
sizeof(
f));
2500 const char *buf =
"|ff|SMB";
2516 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2517 &
f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2528 static int AppLayerProtoDetectTest09(
void)
2533 uint8_t l7data[] = {
2534 0x00, 0x00, 0x00, 0x66, 0xfe, 0x53, 0x4d, 0x42,
2535 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2536 0x00, 0x00, 0x3f, 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, 0x00, 0x00, 0x00, 0x00,
2542 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x01, 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,
2550 memset(pm_results, 0,
sizeof(pm_results));
2552 memset(&
f, 0x00,
sizeof(
f));
2555 const char *buf =
"|fe|SMB";
2571 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2572 &
f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2583 static int AppLayerProtoDetectTest10(
void)
2588 uint8_t l7data[] = {
2589 0x05, 0x00, 0x0b, 0x03, 0x10, 0x00, 0x00, 0x00,
2590 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2591 0xd0, 0x16, 0xd0, 0x16, 0x00, 0x00, 0x00, 0x00,
2592 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
2593 0xb8, 0x4a, 0x9f, 0x4d, 0x1c, 0x7d, 0xcf, 0x11,
2594 0x86, 0x1e, 0x00, 0x20, 0xaf, 0x6e, 0x7c, 0x57,
2595 0x00, 0x00, 0x00, 0x00, 0x04, 0x5d, 0x88, 0x8a,
2596 0xeb, 0x1c, 0xc9, 0x11, 0x9f, 0xe8, 0x08, 0x00,
2597 0x2b, 0x10, 0x48, 0x60, 0x02, 0x00, 0x00, 0x00
2600 memset(pm_results, 0,
sizeof(pm_results));
2602 memset(&
f, 0x00,
sizeof(
f));
2605 const char *buf =
"|05 00|";
2622 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2623 &
f, l7data,
sizeof(l7data), STREAM_TOCLIENT,
2638 static int AppLayerProtoDetectTest11(
void)
2643 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2644 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2646 memset(pm_results, 0,
sizeof(pm_results));
2648 memset(&
f, 0x00,
sizeof(
f));
2660 IPPROTO_TCP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2662 IPPROTO_TCP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2664 IPPROTO_TCP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2689 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2690 &
f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2695 memset(pm_results, 0,
sizeof(pm_results));
2697 &
f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2711 static int AppLayerProtoDetectTest12(
void)
2723 printf(
"failure 1\n");
2729 printf(
"failure 2\n");
2735 printf(
"failure 3\n");
2739 printf(
"failure 4\n");
2743 printf(
"failure 5\n");
2747 printf(
"failure 6\n");
2763 static int AppLayerProtoDetectTest13(
void)
2768 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2769 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2773 memset(&
f, 0x00,
sizeof(
f));
2785 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2787 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2789 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2810 memset(pm_results, 0,
sizeof(pm_results));
2812 uint32_t
cnt = AppLayerProtoDetectPMGetProto(
alpd_tctx,
2813 &
f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2817 memset(pm_results, 0,
sizeof(pm_results));
2819 &
f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2834 static int AppLayerProtoDetectTest14(
void)
2839 uint8_t l7data[] =
"CONNECT www.ssllabs.com:443 HTTP/1.0\r\n";
2840 uint8_t l7data_resp[] =
"HTTP/1.1 405 Method Not Allowed\r\n";
2844 memset(&
f, 0x00,
sizeof(
f));
2856 IPPROTO_UDP,
ALPROTO_HTTP1,
"TRACE", 5, 0, STREAM_TOSERVER);
2858 IPPROTO_UDP,
ALPROTO_HTTP1,
"OPTIONS", 7, 0, STREAM_TOSERVER);
2860 IPPROTO_UDP,
ALPROTO_HTTP1,
"CONNECT", 7, 0, STREAM_TOSERVER);
2882 memset(pm_results, 0,
sizeof(pm_results));
2885 &
f, l7data,
sizeof(l7data), STREAM_TOSERVER,
2890 memset(pm_results, 0,
sizeof(pm_results));
2892 &
f, l7data_resp,
sizeof(l7data_resp), STREAM_TOCLIENT,
2935 int i = -1, j = -1 , k = -1;
2939 for (i = 0; i < no_of_ip_proto; i++, pp = pp->
next) {
2940 if (pp->ipproto != ip_proto[i].
ipproto)
2944 for (k = 0; k < ip_proto[i].
no_of_port; k++, pp_port = pp_port->
next) {
2945 if (pp_port->port != ip_proto[i].
port[k].
port)
2957 j++, pp_element = pp_element->
next) {
2969 if (pp_element != NULL)
2972 pp_element = pp_port->sp;
2987 if (pp_element != NULL)
2990 if (pp_port != NULL)
2999 printf(
"i = %d, k = %d, j = %d(%s)\n", i, k, j, (dir == 0) ?
"ts" :
"tc");
3004 static uint16_t ProbingParserDummyForTesting(
3005 const Flow *
f, uint8_t direction,
const uint8_t *input, uint32_t input_len, uint8_t *rdir)
3010 static int AppLayerProtoDetectTest15(
void)
3018 ProbingParserDummyForTesting, NULL);
3020 ProbingParserDummyForTesting, NULL);
3022 ProbingParserDummyForTesting, NULL);
3025 ProbingParserDummyForTesting, NULL);
3027 ProbingParserDummyForTesting, NULL);
3029 ProbingParserDummyForTesting, NULL);
3031 ProbingParserDummyForTesting, NULL);
3034 ProbingParserDummyForTesting, NULL);
3036 ProbingParserDummyForTesting, NULL);
3040 ProbingParserDummyForTesting, NULL);
3044 ProbingParserDummyForTesting, NULL);
3046 ProbingParserDummyForTesting, NULL);
3049 ProbingParserDummyForTesting, NULL);
3051 ProbingParserDummyForTesting, NULL);
3053 ProbingParserDummyForTesting, NULL);
3055 ProbingParserDummyForTesting, NULL);
3057 ProbingParserDummyForTesting, NULL);
3059 ProbingParserDummyForTesting, NULL);
3061 ProbingParserDummyForTesting, NULL);
3063 ProbingParserDummyForTesting, NULL);
3065 ProbingParserDummyForTesting, NULL);
3067 ProbingParserDummyForTesting, NULL);
3079 {
"smb",
ALPROTO_SMB, 80, 5, 6 }, {
"ftp",
ALPROTO_FTP, 80, 7, 10 },
3080 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3092 {
"dcerpc",
ALPROTO_DCERPC, 81, 9, 10 }, {
"jabber",
ALPROTO_JABBER, 0, 12, 23 },
3093 {
"irc",
ALPROTO_IRC, 0, 12, 14 }, {
"tls",
ALPROTO_TLS, 0, 12, 18 },
3106 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3116 {
"jabber",
ALPROTO_JABBER, 0, 12, 23 }, {
"irc",
ALPROTO_IRC, 0, 12, 14 },
3126 {
"irc",
ALPROTO_IRC, 0, 12, 14 }, {
"tls",
ALPROTO_TLS, 0, 12, 18 },
3155 { 85, 23, 23, element_ts_85, element_tc_85,
3158 { 90, 23, 23, element_ts_90, element_tc_90,
3161 { 0, 23, 23, element_ts_0, element_tc_0,
3190 if (AppLayerProtoDetectPPTestData(alpd_ctx.
ctx_pp, ip_proto,
3204 static int AppLayerProtoDetectTest16(
void)
3209 uint8_t
http_buf1[] =
"POST /one HTTP/1.0\r\n"
3210 "User-Agent: Mozilla/1.0\r\n"
3211 "Cookie: hellocatch\r\n\r\n";
3212 uint32_t http_buf1_len =
sizeof(
http_buf1) - 1;
3227 printf(
"packet setup failed: ");
3233 printf(
"flow setup failed: ");
3255 "(msg:\"Test content option\"; "
3267 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3272 if (http_state == NULL) {
3273 printf(
"no http state: ");
3281 printf(
"sig 1 didn't alert, but it should: ");
3290 if (det_ctx != NULL)
3301 static int AppLayerProtoDetectTest17(
void)
3306 uint8_t
http_buf1[] =
"POST /one HTTP/1.0\r\n"
3307 "User-Agent: Mozilla/1.0\r\n"
3308 "Cookie: hellocatch\r\n\r\n";
3309 uint32_t http_buf1_len =
sizeof(
http_buf1) - 1;
3344 "(msg:\"http over non standar port\"; "
3356 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3361 if (http_state == NULL) {
3362 printf(
"no http state: ");
3370 printf(
"sig 1 didn't alert, but it should: ");
3381 if (det_ctx != NULL)
3392 static int AppLayerProtoDetectTest18(
void)
3397 uint8_t
http_buf1[] =
"POST /one HTTP/1.0\r\n"
3398 "User-Agent: Mozilla/1.0\r\n"
3399 "Cookie: hellocatch\r\n\r\n";
3400 uint32_t http_buf1_len =
sizeof(
http_buf1) - 1;
3435 "(msg:\"Test content option\"; "
3447 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3452 if (http_state == NULL) {
3453 printf(
"no http state: ");
3461 printf(
"sig 1 alerted, but it should not (it's not ftp): ");
3471 if (det_ctx != NULL)
3482 static int AppLayerProtoDetectTest19(
void)
3487 uint32_t http_buf1_len =
sizeof(
http_buf1) - 1;
3522 "(msg:\"http over non standar port\"; "
3532 STREAM_TOSERVER,
http_buf1, http_buf1_len);
3534 printf(
"toserver chunk 1 returned %" PRId32
", expected 0: ", r);
3542 printf(
"sig 1 alerted, but it should not (it's ftp): ");
3553 if (det_ctx != NULL)
3566 UtRegisterTest(
"AppLayerProtoDetectTest01", AppLayerProtoDetectTest01);
3567 UtRegisterTest(
"AppLayerProtoDetectTest02", AppLayerProtoDetectTest02);
3568 UtRegisterTest(
"AppLayerProtoDetectTest03", AppLayerProtoDetectTest03);
3569 UtRegisterTest(
"AppLayerProtoDetectTest04", AppLayerProtoDetectTest04);
3570 UtRegisterTest(
"AppLayerProtoDetectTest05", AppLayerProtoDetectTest05);
3571 UtRegisterTest(
"AppLayerProtoDetectTest06", AppLayerProtoDetectTest06);
3572 UtRegisterTest(
"AppLayerProtoDetectTest07", AppLayerProtoDetectTest07);
3573 UtRegisterTest(
"AppLayerProtoDetectTest08", AppLayerProtoDetectTest08);
3574 UtRegisterTest(
"AppLayerProtoDetectTest09", AppLayerProtoDetectTest09);
3575 UtRegisterTest(
"AppLayerProtoDetectTest10", AppLayerProtoDetectTest10);
3576 UtRegisterTest(
"AppLayerProtoDetectTest11", AppLayerProtoDetectTest11);
3577 UtRegisterTest(
"AppLayerProtoDetectTest12", AppLayerProtoDetectTest12);
3578 UtRegisterTest(
"AppLayerProtoDetectTest13", AppLayerProtoDetectTest13);
3579 UtRegisterTest(
"AppLayerProtoDetectTest14", AppLayerProtoDetectTest14);
3580 UtRegisterTest(
"AppLayerProtoDetectTest15", AppLayerProtoDetectTest15);
3581 UtRegisterTest(
"AppLayerProtoDetectTest16", AppLayerProtoDetectTest16);
3582 UtRegisterTest(
"AppLayerProtoDetectTest17", AppLayerProtoDetectTest17);
3583 UtRegisterTest(
"AppLayerProtoDetectTest18", AppLayerProtoDetectTest18);
3584 UtRegisterTest(
"AppLayerProtoDetectTest19", AppLayerProtoDetectTest19);