diff --git a/etc/schema.json b/etc/schema.json index fd0b6d2..f10c92a 100644 --- a/etc/schema.json +++ b/etc/schema.json @@ -6807,6 +6807,12 @@ "ipv6_in_ipv6_wrong_version": { "type": "integer" }, + "nonzero_padn": { + "type": "integer" + }, + "padding_too_long": { + "type": "integer" + }, "pkt_too_small": { "type": "integer" }, diff --git a/rules/decoder-events.rules b/rules/decoder-events.rules index bd239ae..4210f99 100644 --- a/rules/decoder-events.rules +++ b/rules/decoder-events.rules @@ -39,6 +39,10 @@ alert pkthdr any any -> any any (msg:"SURICATA IPv6 DSTOPTS only padding"; decod alert ipv6 any any -> any any (msg:"SURICATA RH Type 0"; decode-event:ipv6.rh_type_0; classtype:protocol-command-decode; sid:2200093; rev:2;) # padN option with zero length field. This is not uncommon, so disabled by default. #alert ipv6 any any -> any any (msg:"SURICATA zero length padN option"; decode-event:ipv6.zero_len_padn; classtype:protocol-command-decode; sid:2200094; rev:2;) +# PadN option with non-zero data (RFC 8200 section 4.2); possible covert channel +alert ipv6 any any -> any any (msg:"SURICATA IPv6 PadN option with non-zero data"; decode-event:ipv6.nonzero_padn; classtype:protocol-command-decode; sid:2200131; rev:1;) +# more than 7 consecutive padding octets in Hop-by-Hop or Destination Options +alert ipv6 any any -> any any (msg:"SURICATA IPv6 padding longer than 7 octets"; decode-event:ipv6.padding_too_long; classtype:protocol-command-decode; sid:2200132; rev:1;) # Frag Header 'length' field is reserved and should be 0 alert ipv6 any any -> any any (msg:"SURICATA reserved field in Frag Header not zero"; decode-event:ipv6.fh_non_zero_reserved_field; classtype:protocol-command-decode; sid:2200095; rev:2;) # Data after the 'none' header (59) is suspicious. diff --git a/src/decode-events.c b/src/decode-events.c index b22f9df..2491b6a 100644 --- a/src/decode-events.c +++ b/src/decode-events.c @@ -221,6 +221,14 @@ const struct DecodeEvents_ DEvents[] = { "decoder.ipv6.zero_len_padn", IPV6_EXTHDR_ZERO_LEN_PADN, }, + { + "decoder.ipv6.nonzero_padn", + IPV6_EXTHDR_NONZERO_PADN, + }, + { + "decoder.ipv6.padding_too_long", + IPV6_EXTHDR_PADDING_TOO_LONG, + }, { "decoder.ipv6.fh_non_zero_reserved_field", IPV6_FH_NON_ZERO_RES_FIELD, diff --git a/src/decode-events.h b/src/decode-events.h index 89f8d18..583194b 100644 --- a/src/decode-events.h +++ b/src/decode-events.h @@ -87,6 +87,8 @@ enum { IPV6_EXTHDR_RH_TYPE_0, /**< RH 0 is deprecated as per rfc5095 */ IPV6_EXTHDR_ZERO_LEN_PADN, /**< padN w/o data (0 len) */ + IPV6_EXTHDR_NONZERO_PADN, /**< padN data not all zero (rfc 8200 4.2) */ + IPV6_EXTHDR_PADDING_TOO_LONG, /**< padding run longer than 7 octets */ IPV6_FH_NON_ZERO_RES_FIELD, /**< reserved field not zero */ IPV6_DATA_AFTER_NONE_HEADER, /**< data after 'none' (59) header */ diff --git a/src/decode-ipv6.c b/src/decode-ipv6.c index 73f7413..accb2b1 100644 --- a/src/decode-ipv6.c +++ b/src/decode-ipv6.c @@ -286,6 +286,8 @@ static void DecodeIPV6ExtHdrs(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint16_t padn_cnt = 0; uint16_t other_cnt = 0; uint16_t offset = 0; + uint16_t pad_run = 0; /* consecutive padding octets */ + bool pad_run_set = false; while(offset < optslen) { if (*ptr == IPV6OPT_PAD1) @@ -293,6 +295,11 @@ static void DecodeIPV6ExtHdrs(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, padn_cnt++; offset++; ptr++; + pad_run++; + if (pad_run > 7 && !pad_run_set) { + ENGINE_SET_EVENT(p, IPV6_EXTHDR_PADDING_TOO_LONG); + pad_run_set = true; + } continue; } @@ -320,6 +327,20 @@ static void DecodeIPV6ExtHdrs(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, /* a zero padN len would be weird */ if (ip6_optlen == 0) ENGINE_SET_EVENT(p, IPV6_EXTHDR_ZERO_LEN_PADN); + + /* rfc 8200 4.2: PadN option data consists of zero-valued + * octets. Non-zero data can carry a covert channel. */ + for (uint8_t i = 0; i < ip6_optlen; i++) { + if (*(ptr + 2 + i) != 0) { + ENGINE_SET_EVENT(p, IPV6_EXTHDR_NONZERO_PADN); + break; + } + } + pad_run += 2 + ip6_optlen; + if (pad_run > 7 && !pad_run_set) { + ENGINE_SET_EVENT(p, IPV6_EXTHDR_PADDING_TOO_LONG); + pad_run_set = true; + } } else if (*ptr == IPV6OPT_RA) /* RA */ { @@ -327,8 +348,11 @@ static void DecodeIPV6ExtHdrs(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, ra->ip6ra_len = ip6_optlen; if (ip6_optlen < sizeof(ra->ip6ra_value)) { + /* the option still fits the header: flag it, skip its + * value, and keep scanning the remaining options */ ENGINE_SET_INVALID_EVENT(p, IPV6_EXTHDR_INVALID_OPTLEN); - break; + other_cnt++; + goto next_opt; } memcpy(&ra->ip6ra_value, (ptr + 2), sizeof(ra->ip6ra_value)); @@ -343,8 +367,10 @@ static void DecodeIPV6ExtHdrs(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, jumbo->ip6j_len = ip6_optlen; if (ip6_optlen < sizeof(jumbo->ip6j_payload_len)) { + /* the option still fits the header: flag it, skip its + * value, and keep scanning the remaining options */ ENGINE_SET_INVALID_EVENT(p, IPV6_EXTHDR_INVALID_OPTLEN); - break; + goto next_opt; } memcpy(&jumbo->ip6j_payload_len, (ptr+2), sizeof(jumbo->ip6j_payload_len)); @@ -358,8 +384,11 @@ static void DecodeIPV6ExtHdrs(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, hao->ip6hao_len = ip6_optlen; if (ip6_optlen < sizeof(hao->ip6hao_hoa)) { + /* the option still fits the header: flag it, skip its + * value, and keep scanning the remaining options */ ENGINE_SET_INVALID_EVENT(p, IPV6_EXTHDR_INVALID_OPTLEN); - break; + other_cnt++; + goto next_opt; } memcpy(&hao->ip6hao_hoa, (ptr+2), sizeof(hao->ip6hao_hoa)); @@ -378,6 +407,9 @@ static void DecodeIPV6ExtHdrs(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, other_cnt++; } + next_opt: + if (*ptr != IPV6OPT_PADN) + pad_run = 0; uint16_t optlen = (*(ptr + 1) + 2); ptr += optlen; /* +2 for opt type and opt len fields */ offset += optlen; @@ -895,6 +927,83 @@ static int DecodeIPV6HopTest01 (void) PASS; } +/** + * \test non-zero PadN data and over-long padding in a Destination Options header + */ +static int DecodeIPV6PadNTest01(void) +{ + /* IPv6, nh=60 (DestOpt), DestOpt: nh=17, len=0, PadN len 4 data 41 42 43 44; UDP */ + uint8_t raw_nz[] = { 0x60, 0x00, 0x00, 0x00, 0x00, 0x10, 0x3c, 0x40, 0xfd, 0x00, 0x00, 0x5a, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfd, 0x00, 0x00, + 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x11, 0x00, + 0x01, 0x04, 0x41, 0x42, 0x43, 0x44, 0x03, 0xe8, 0x13, 0x88, 0x00, 0x08, 0x00, 0x00 }; + /* same with zero PadN data */ + uint8_t raw_z[sizeof(raw_nz)]; + memcpy(raw_z, raw_nz, sizeof(raw_nz)); + memset(raw_z + 44, 0, 4); + /* DestOpt len=1 (16 octets): PadN len 12 zero data; UDP */ + uint8_t raw_long[] = { 0x60, 0x00, 0x00, 0x00, 0x00, 0x18, 0x3c, 0x40, 0xfd, 0x00, 0x00, 0x5a, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfd, 0x00, 0x00, + 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x11, 0x01, + 0x01, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, + 0xe8, 0x13, 0x88, 0x00, 0x08, 0x00, 0x00 }; + ThreadVars tv; + DecodeThreadVars dtv; + + FlowInitConfig(FLOW_QUIET); + memset(&tv, 0, sizeof(ThreadVars)); + memset(&dtv, 0, sizeof(DecodeThreadVars)); + + Packet *p1 = PacketGetFromAlloc(); + FAIL_IF_NULL(p1); + PacketCopyData(p1, raw_nz, sizeof(raw_nz)); + DecodeIPV6(&tv, &dtv, p1, GET_PKT_DATA(p1), GET_PKT_LEN(p1)); + FAIL_IF_NOT(ENGINE_ISSET_EVENT(p1, IPV6_EXTHDR_NONZERO_PADN)); + FAIL_IF(ENGINE_ISSET_EVENT(p1, IPV6_EXTHDR_PADDING_TOO_LONG)); + PacketRecycle(p1); + SCFree(p1); + + Packet *p2 = PacketGetFromAlloc(); + FAIL_IF_NULL(p2); + PacketCopyData(p2, raw_z, sizeof(raw_z)); + DecodeIPV6(&tv, &dtv, p2, GET_PKT_DATA(p2), GET_PKT_LEN(p2)); + FAIL_IF(ENGINE_ISSET_EVENT(p2, IPV6_EXTHDR_NONZERO_PADN)); + FAIL_IF(ENGINE_ISSET_EVENT(p2, IPV6_EXTHDR_PADDING_TOO_LONG)); + PacketRecycle(p2); + SCFree(p2); + + Packet *p3 = PacketGetFromAlloc(); + FAIL_IF_NULL(p3); + PacketCopyData(p3, raw_long, sizeof(raw_long)); + DecodeIPV6(&tv, &dtv, p3, GET_PKT_DATA(p3), GET_PKT_LEN(p3)); + FAIL_IF(ENGINE_ISSET_EVENT(p3, IPV6_EXTHDR_NONZERO_PADN)); + FAIL_IF_NOT(ENGINE_ISSET_EVENT(p3, IPV6_EXTHDR_PADDING_TOO_LONG)); + PacketRecycle(p3); + SCFree(p3); + + /* HAO option with a short length (0) followed by non-zero PadN: the + * decoder flags the short option and still inspects the PadN */ + uint8_t raw_hao[sizeof(raw_nz)]; + memcpy(raw_hao, raw_nz, sizeof(raw_nz)); + raw_hao[42] = 0xc9; + raw_hao[43] = 0x00; + raw_hao[44] = 0x01; + raw_hao[45] = 0x02; + raw_hao[46] = 0x46; + raw_hao[47] = 0x30; + Packet *p4 = PacketGetFromAlloc(); + FAIL_IF_NULL(p4); + PacketCopyData(p4, raw_hao, sizeof(raw_hao)); + DecodeIPV6(&tv, &dtv, p4, GET_PKT_DATA(p4), GET_PKT_LEN(p4)); + FAIL_IF_NOT(ENGINE_ISSET_EVENT(p4, IPV6_EXTHDR_INVALID_OPTLEN)); + FAIL_IF_NOT(ENGINE_ISSET_EVENT(p4, IPV6_EXTHDR_NONZERO_PADN)); + PacketRecycle(p4); + SCFree(p4); + + FlowShutdown(); + PASS; +} + #endif /* UNITTESTS */ /** @@ -907,6 +1016,7 @@ void DecodeIPV6RegisterTests(void) UtRegisterTest("DecodeIPV6FragTest01", DecodeIPV6FragTest01); UtRegisterTest("DecodeIPV6RouteTest01", DecodeIPV6RouteTest01); UtRegisterTest("DecodeIPV6HopTest01", DecodeIPV6HopTest01); + UtRegisterTest("DecodeIPV6PadNTest01", DecodeIPV6PadNTest01); #endif /* UNITTESTS */ }