From 97787a9dd8b136c8dc327fab42aedf2aa1109ec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Tue, 10 Oct 2023 10:44:24 +0200 Subject: Add dnssec --- flakes/flake.lock | 22 +-- flakes/private/monitoring/myplugins.nix | 23 +++ flakes/private/monitoring/plugins/check_dnssec | 195 +++++++++++++++++++++++++ 3 files changed, 229 insertions(+), 11 deletions(-) create mode 100755 flakes/private/monitoring/plugins/check_dnssec (limited to 'flakes') diff --git a/flakes/flake.lock b/flakes/flake.lock index 751316c..090ef48 100644 --- a/flakes/flake.lock +++ b/flakes/flake.lock @@ -2815,7 +2815,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-rybO4c9UB9a34Xgoh+ToYz36Dz2OM1sgYxi3m00+W+E=", + "narHash": "sha256-DN3hgnw6hXCrSGXep4mumwksWSggsuyyaKXuKvswXl8=", "path": "../../flakes/private/monitoring", "type": "path" }, @@ -2833,7 +2833,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-rybO4c9UB9a34Xgoh+ToYz36Dz2OM1sgYxi3m00+W+E=", + "narHash": "sha256-DN3hgnw6hXCrSGXep4mumwksWSggsuyyaKXuKvswXl8=", "path": "../../flakes/private/monitoring", "type": "path" }, @@ -2851,7 +2851,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-rybO4c9UB9a34Xgoh+ToYz36Dz2OM1sgYxi3m00+W+E=", + "narHash": "sha256-DN3hgnw6hXCrSGXep4mumwksWSggsuyyaKXuKvswXl8=", "path": "../../flakes/private/monitoring", "type": "path" }, @@ -2869,7 +2869,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-rybO4c9UB9a34Xgoh+ToYz36Dz2OM1sgYxi3m00+W+E=", + "narHash": "sha256-DN3hgnw6hXCrSGXep4mumwksWSggsuyyaKXuKvswXl8=", "path": "../../flakes/private/monitoring", "type": "path" }, @@ -3753,7 +3753,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-yqURiQf32DNTy5zfAIatoWwFTqvsGDQd+221BoSfsCY=", + "narHash": "sha256-KR4/Na/SqEfg9PNnBLk17lTn4LUU7irZGrgvw7TEUYQ=", "path": "../systems/backup-2", "type": "path" }, @@ -3776,7 +3776,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-2Q1QywPMmeYtlrSNE869LwUJQjtbRUXbDhNFT4WBRJE=", + "narHash": "sha256-7B/UHUhGyJRBRjEms+zI8ZhBAN1vE365GZw2ciJVg1M=", "path": "../systems/dilion", "type": "path" }, @@ -3824,7 +3824,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-E88xTYPerBoKGo+EB6RThKwM1AxuhPWhs583WxwD8cA=", + "narHash": "sha256-q1+zzXLioBDjua4Omke9ki0hUaW2rtqTMRUXZ/+uHwU=", "path": "../systems/eldiron", "type": "path" }, @@ -3850,7 +3850,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-Ejc4fEaRV8u1yWV+u4z6F2SAGDBYEubbgRoG7tE3ctM=", + "narHash": "sha256-tsZO/C4md/8qRfxIsvVgeMkB0iAEl4IJC5/i8t/li2I=", "path": "../systems/monitoring-1", "type": "path" }, @@ -3875,7 +3875,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-6hR+IuCejk0AIiwggSgrvCQXiRzbF5IiMFr3YqbBwZI=", + "narHash": "sha256-UrrTxZeyqV2cFsC3XKVrJoay7LdnE6OTZnBJfimPle4=", "path": "../systems/quatresaisons", "type": "path" }, @@ -7384,7 +7384,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-rybO4c9UB9a34Xgoh+ToYz36Dz2OM1sgYxi3m00+W+E=", + "narHash": "sha256-DN3hgnw6hXCrSGXep4mumwksWSggsuyyaKXuKvswXl8=", "path": "../../flakes/private/monitoring", "type": "path" }, @@ -8294,7 +8294,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-rybO4c9UB9a34Xgoh+ToYz36Dz2OM1sgYxi3m00+W+E=", + "narHash": "sha256-DN3hgnw6hXCrSGXep4mumwksWSggsuyyaKXuKvswXl8=", "path": "./private/monitoring", "type": "path" }, diff --git a/flakes/private/monitoring/myplugins.nix b/flakes/private/monitoring/myplugins.nix index 35730bb..f76f2c1 100644 --- a/flakes/private/monitoring/myplugins.nix +++ b/flakes/private/monitoring/myplugins.nix @@ -69,8 +69,31 @@ in dns = { commands = { check_dns = "$USER1$/check_dns -H $ARG1$ -s $HOSTADDRESS$ $ARG2$"; + check_dns_soa = "$USER2$/check_dns_soa -H $ARG1$ -z $ARG2$ -M $ARG3$"; + check_dnssec = "$USER2$/check_dnssec -z $ARG1$"; check_external_dns = "$USER1$/check_dns -H $ARG2$ -s $ARG1$ $ARG3$"; }; + chunk = let + soa_plugin = pkgs.fetchurl { + name = "check_dns_soa"; + url = "https://exchange.nagios.org/components/com_mtree/attachment.php?link_id=1429&cf_id=24"; + sha256 = "sha256-Yy4XO19Fb7WdHZZmhUfyyAGBnxJyFWwc7U3HiWyE8wc="; + }; + in '' + cp ${./plugins}/check_dnssec $out/ + patchShebangs $out/check_dnssec + wrapProgram $out/check_dnssec --prefix PATH : ${lib.makeBinPath [ + pkgs.bind.dnsutils pkgs.gnugrep pkgs.gawk pkgs.which pkgs.coreutils + ]} + + cp ${soa_plugin} $out/check_dns_soa + chmod +xw $out/check_dns_soa + patchShebangs $out/check_dns_soa + sed -i -e 's/^use utils qw.*$/my %ERRORS = ("OK" => 0, "WARNING" => 1, "CRITICAL" => 2, "UNKNOWN" => 3);my $TIMEOUT = 10;/' -e '/^use lib /d' $out/check_dns_soa + wrapProgram $out/check_dns_soa --prefix PERL5LIB : ${pkgs.perlPackages.makePerlPath [ + pkgs.perlPackages.NetDNS + ]} + ''; }; mdadm = { commands = { diff --git a/flakes/private/monitoring/plugins/check_dnssec b/flakes/private/monitoring/plugins/check_dnssec new file mode 100755 index 0000000..a6e408d --- /dev/null +++ b/flakes/private/monitoring/plugins/check_dnssec @@ -0,0 +1,195 @@ +#!/usr/bin/env bash + +# check_dnssec_expiry.sh +# +# Copyright 2017 by Mario Rimann +# Licensed under the permissive MIT license, see LICENSE.md +# +# Development of this script was partially sponsored by my +# employer internezzo, see http://www.internezzo.ch +# +# If this script helps you to make your work easier, please consider +# to give feedback or do something good, see https://rimann.org/support + +usage() { + cat - >&2 << _EOT_ +usage $0 -z [-w ] [-c ] [-r ] [-f ] + + -z + specify zone to check + -w + warning time left percentage + -c + critical time left percentage + -r + specify which resolver to use. + -f + specify a domain that will always fail DNSSEC. + used to test if DNSSEC is supported in used tools. + -t + specify a DNS record type for calculating the remaining lifetime. + For example SOA, A, etc. +_EOT_ + exit 255 +} + +# Parse the input options +while getopts ":z:w:c:r:f:h:t:" opt; do + case $opt in + z) + zone=$OPTARG + ;; + w) + warning=$OPTARG + ;; + c) + critical=$OPTARG + ;; + r) + resolver=$OPTARG + ;; + f) + alwaysFailingDomain=$OPTARG + ;; + t) + recordType=$OPTARG + ;; + h) + usage ;; + esac +done + + +# Check if dig is available at all - fail hard if not +pathToDig=$( which dig ) +if [[ ! -e $pathToDig ]]; then + echo "No executable of dig found, cannot proceed without dig. Sorry!" + exit 1 +fi + +# Check if we got a zone to validate - fail hard if not +if [[ -z $zone ]]; then + echo "Missing zone to test - please provide a zone via the -z parameter." + usage + exit 3 +fi + +# Check if we got warning/critical percentage values, use defaults if not +if [[ -z $warning ]]; then + warning=20 +fi +if [[ -z $critical ]]; then + critical=10 +fi + + +# Use Google's 8.8.8.8 resolver as fallback if none is provided +if [[ -z $resolver ]]; then + resolver="8.8.8.8" +fi + +if [[ -z $alwaysFailingDomain ]]; then + alwaysFailingDomain="dnssec-failed.org" +fi + +# Use SOA record type as fallback +if [[ -z $recordType ]]; then + recordType="SOA" +fi + +# Check the resolver to properly validate DNSSEC at all (if he doesn't, every further test is futile and a waste of bandwith) +checkResolverDoesDnssecValidation=$(dig +nocmd +nostats +noquestion $alwaysFailingDomain @${resolver} | grep "opcode: QUERY" | grep "status: SERVFAIL") +if [[ -z $checkResolverDoesDnssecValidation ]]; then + echo "WARNING: Resolver seems to not validate DNSSEC signatures - going further seems hopeless right now." + exit 1 +fi + +# Check if the resolver delivers an answer for the domain to test +checkDomainResolvableWithDnssecEnabledResolver=$(dig +short @${resolver} SOA $zone) +if [[ -z $checkDomainResolvableWithDnssecEnabledResolver ]]; then + + checkDomainResolvableWithDnssecValidationExplicitelyDisabled=$(dig +short @${resolver} SOA $zone +cd) + + if [[ ! -z $checkDomainResolvableWithDnssecValidationExplicitelyDisabled ]]; then + echo "CRITICAL: The domain $zone can be validated without DNSSEC validation - but will fail on resolvers that do validate DNSSEC." + exit 2 + else + echo "CRITICAL: The domain $zone cannot be resolved via $resolver as resolver while DNSSEC validation is active." + exit 2 + fi +fi + +# Check if the domain is DNSSEC signed at all +# (and emerge a WARNING in that case, since this check is about testing DNSSEC being "present" and valid which is not the case for an unsigned zone) +checkZoneItselfIsSignedAtAll=$( dig $zone @$resolver DS +short ) +if [[ -z $checkZoneItselfIsSignedAtAll ]]; then + echo "WARNING: Zone $zone seems to be unsigned itself (= resolvable, but no DNSSEC involved at all)" + exit 1 +fi + + +# Check if there are multiple RRSIG responses and check them one after the other +now=$(date +"%s") +rrsigEntries=$( dig @$resolver $recordType $zone +dnssec | grep RRSIG ) +if [[ -z $rrsigEntries ]]; then + echo "CRITICAL: There is no RRSIG for the SOA of your zone." + exit 2 +else + while read -r rrsig; do + # Get the RRSIG entry and extract the date out of it + expiryDateOfSignature=$( echo $rrsig | awk '{print $9}') + checkValidityOfExpirationTimestamp=$( echo $expiryDateOfSignature | egrep '[0-9]{14}') + if [[ -z $checkValidityOfExpirationTimestamp ]]; then + echo "UNKNOWN: Something went wrong while checking the expiration of the RRSIG entry - investigate please". + exit 3 + fi + + inceptionDateOfSignature=$( echo $rrsig | awk '{print $10}') + checkValidityOfInceptionTimestamp=$( echo $inceptionDateOfSignature | egrep '[0-9]{14}') + if [[ -z $checkValidityOfInceptionTimestamp ]]; then + echo "UNKNOWN: Something went wrong while checking the inception date of the RRSIG entry - investigate please". + exit 3 + fi + + # Fiddle out the expiry and inceptiondate of the signature to have a base to do some calculations afterwards + expiryDateAsString="${expiryDateOfSignature:0:4}-${expiryDateOfSignature:4:2}-${expiryDateOfSignature:6:2} ${expiryDateOfSignature:8:2}:${expiryDateOfSignature:10:2}:00" + expiryDateOfSignatureAsUnixTime=$( date -u -d "$expiryDateAsString" +"%s" 2>/dev/null ) + if [[ $? -ne 0 ]]; then + # if we come to this place, something must have gone wrong converting the date-string. This can happen as e.g. MacOS X and Linux don't behave the same way in this topic... + expiryDateOfSignatureAsUnixTime=$( date -j -u -f "%Y-%m-%d %T" "$expiryDateAsString" +"%s" ) + fi + inceptionDateAsString="${inceptionDateOfSignature:0:4}-${inceptionDateOfSignature:4:2}-${inceptionDateOfSignature:6:2} ${inceptionDateOfSignature:8:2}:${inceptionDateOfSignature:10:2}:00" + inceptionDateOfSignatureAsUnixTime=$( date -u -d "$inceptionDateAsString" +"%s" 2>/dev/null ) + if [[ $? -ne 0 ]]; then + # if we come to this place, something must have gone wrong converting the date-string. This can happen as e.g. MacOS X and Linux don't behave the same way in this topic... + inceptionDateOfSignatureAsUnixTime=$( date -j -u -f "%Y-%m-%d %T" "$inceptionDateAsString" +"%s" ) + fi + + + # calculate the remaining lifetime of the signature + totalLifetime=$( expr $expiryDateOfSignatureAsUnixTime - $inceptionDateOfSignatureAsUnixTime) + remainingLifetimeOfSignature=$( expr $expiryDateOfSignatureAsUnixTime - $now) + remainingPercentage=$( expr "100" \* $remainingLifetimeOfSignature / $totalLifetime) + + # store the result of this single RRSIG's check + if [[ -z $maxRemainingLifetime || $remainingLifetimeOfSignature -gt $maxRemainingLifetime ]]; then + maxRemainingLifetime=$remainingLifetimeOfSignature + maxRemainingPercentage=$remainingPercentage + fi + done <<< "$rrsigEntries" +fi + + + + +# determine if we need to alert, and if so, how loud to cry, depending on warning/critial threshholds provided +if [[ $maxRemainingPercentage -lt $critical ]]; then + echo "CRITICAL: DNSSEC signature for $zone is very short before expiration! ($maxRemainingPercentage% remaining) | sig_lifetime=$maxRemainingLifetime sig_lifetime_percentage=$remainingPercentage%;$warning;$critical" + exit 2 +elif [[ $remainingPercentage -lt $warning ]]; then + echo "WARNING: DNSSEC signature for $zone is short before expiration! ($maxRemainingPercentage% remaining) | sig_lifetime=$maxRemainingLifetime sig_lifetime_percentage=$remainingPercentage%;$warning;$critical" + exit 1 +else + echo "OK: DNSSEC signatures for $zone seem to be valid and not expired ($maxRemainingPercentage% remaining) | sig_lifetime=$maxRemainingLifetime sig_lifetime_percentage=$remainingPercentage%;$warning;$critical" + exit 0 +fi -- cgit v1.2.3