audit-backup-credentials.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env bash
  2. # Audit a database backup (snapshot, WAL segment, or LMDB file) for
  3. # PLAINTEXT credentials.
  4. #
  5. # WHY THIS EXISTS, AND IT IS NOT THE OBVIOUS SCRIPT. The obvious version is
  6. # `grep -F` for a few credential names, and on these files it returns 0 for
  7. # EVERYTHING - including strings that are certainly present. Measured
  8. # 2026-10-19: `grep -c -F novitax` answered 0 on a WAL segment where
  9. # `strings -a | grep -c -F novitax` answered 5957, because the files carry
  10. # custom framing (CALWAL01 / CALSNAP1) with embedded NULs. They are not
  11. # compressed; gzip still halves them. So the text is right there and grep
  12. # will not surface it.
  13. #
  14. # That makes the obvious script a check that CANNOT FAIL: it reports a clean
  15. # bill of health on a credential audit while being incapable of reporting an
  16. # exposure. A clean report is the most dangerous possible output for this
  17. # check, so every absence here is gated behind a POSITIVE CONTROL: unless a
  18. # string known to be in the file is found, the script refuses to draw any
  19. # conclusion and says so.
  20. #
  21. # The patterns are DERIVED FROM THE SOURCE that defines the credentials
  22. # rather than listed here, so the list cannot drift from the thing it checks.
  23. # (Suggested by a consumer who found the same shape in their own bundle
  24. # audit, where one pattern happened to be present and was an ACCIDENTAL
  25. # positive control - accidental being the problem.)
  26. #
  27. # Never prints a credential value. Counts and masked shapes only.
  28. set -uo pipefail
  29. # NOTE: never `strings ... | grep -q` in this script. `grep -q` exits on the
  30. # FIRST match, which SIGPIPEs `strings`, and under `pipefail` that makes the
  31. # whole pipeline non-zero - so a control that MATCHED reports as failed. That
  32. # bug made every real file report CANNOT DETERMINE on the first run of this
  33. # script. Use `grep -c` (which consumes all input) and compare the count.
  34. SRC_DIR="${SRC_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/src}"
  35. die() { printf 'audit: %s\n' "$*" >&2; exit 2; }
  36. [ $# -ge 1 ] || die "usage: $0 <backup-file-or-directory>..."
  37. # --- derive the patterns from the stores that define the credentials --------
  38. derive_patterns() {
  39. # Credential-bearing collection names, from the collection constants.
  40. grep -rhno 'vectorapi_\(keys\|settings\|secrets\)' "$SRC_DIR" 2>/dev/null \
  41. | cut -d: -f2- | sort -u
  42. # Credential VALUE field names, from the stores' own serialisation.
  43. grep -rhno '"\(openai_api_key\|api_key\|apiKey\|password\|secret\|token\|credentials\)"' \
  44. "$SRC_DIR" 2>/dev/null | cut -d: -f2- | tr -d '"' | sort -u
  45. }
  46. mapfile -t PATTERNS < <(derive_patterns)
  47. [ "${#PATTERNS[@]}" -gt 0 ] || die "derived NO patterns from $SRC_DIR - refusing to report anything"
  48. rc=0
  49. printf 'audit: %d pattern(s) derived from %s\n\n' "${#PATTERNS[@]}" "$SRC_DIR"
  50. audit_one() {
  51. local f="$1"
  52. printf '=== %s ===\n' "$f"
  53. # One `strings` pass per file, reused for every pattern. These files run to
  54. # hundreds of MB and re-reading per pattern made the audit minutes long.
  55. local tok
  56. tok=$(mktemp) || die "mktemp failed"
  57. strings -a "$f" 2>/dev/null > "$tok"
  58. # --- the positive control, before any absence is believed ---------------
  59. # The file's own magic is a string we KNOW is at offset 0. If `strings`
  60. # cannot surface that, it cannot surface anything, and no zero below means
  61. # a thing.
  62. local magic
  63. magic=$(head -c 8 "$f" 2>/dev/null | tr -dc 'A-Za-z0-9')
  64. if [ "${#magic}" -lt 4 ]; then
  65. printf ' CONTROL: INCONCLUSIVE - no printable magic; falling back to token yield\n'
  66. local tokens
  67. tokens=$(head -2000 "$tok" | wc -l)
  68. if [ "$tokens" -lt 10 ]; then
  69. printf ' RESULT: CANNOT DETERMINE - only %d printable token(s). NO ABSENCE BELOW MEANS ANYTHING.\n\n' "$tokens"
  70. rc=3; rm -f "$tok"; return
  71. fi
  72. printf ' CONTROL: PASS via token yield (%d tokens)\n' "$tokens"
  73. elif [ "$(grep -c -F "$magic" "$tok")" -eq 0 ]; then
  74. printf ' RESULT: CANNOT DETERMINE - the control string %s is in the file but not findable.\n' "$magic"
  75. printf ' NO ABSENCE BELOW MEANS ANYTHING. Do not read this as clean.\n\n'
  76. rc=3; rm -f "$tok"; return
  77. else
  78. printf ' CONTROL: PASS - %s findable, so this file IS searchable\n' "$magic"
  79. fi
  80. # --- positive evidence of encryption ------------------------------------
  81. local enc encj
  82. enc=$(grep -c -F '$ENC$' "$tok")
  83. encj=$(grep -c -F '$ENCJ$' "$tok")
  84. printf ' ciphertext markers: $ENC$=%s $ENCJ$=%s\n' "$enc" "$encj"
  85. # --- the actual question -------------------------------------------------
  86. local found=0 p n
  87. for p in "${PATTERNS[@]}"; do
  88. n=$(grep -c -F "$p" "$tok")
  89. [ "$n" -gt 0 ] && { printf ' PRESENT: %-22s %s hit(s)\n' "$p" "$n"; found=1; }
  90. done
  91. # A key-SHAPED token is the thing that matters, not a field name: a field
  92. # name can appear in tenant content. Anchored at a token start, because
  93. # `sk-` occurs mid-blob in ciphertext by chance (seen 2026-10-19).
  94. local shaped
  95. shaped=$(grep -c -E '(^|[^A-Za-z0-9_-])sk-[A-Za-z0-9_-]{20,}' "$tok")
  96. if [ "$shaped" -gt 0 ]; then
  97. printf ' *** EXPOSED: %s key-shaped token(s) (sk- at a token start, 20+ chars)\n' "$shaped"
  98. rc=1
  99. elif [ "$found" -eq 0 ]; then
  100. printf ' RESULT: CLEAN - control passed and no credential pattern present\n'
  101. else
  102. printf ' RESULT: REVIEW - credential field/collection names present but no key-shaped token\n'
  103. [ "$rc" -eq 0 ] && rc=4
  104. fi
  105. rm -f "$tok"
  106. printf '\n'
  107. }
  108. for target in "$@"; do
  109. [ -e "$target" ] || die "no such path: $target (refusing to report clean for a path that does not exist)"
  110. if [ -d "$target" ]; then
  111. while IFS= read -r -d '' f; do audit_one "$f"; done \
  112. < <(find "$target" -type f ! -name 'SHA256SUMS' ! -name '*.md' ! -name '*.txt' -print0)
  113. else
  114. audit_one "$target"
  115. fi
  116. done
  117. case $rc in
  118. 0) printf 'audit: CLEAN (every file had a passing control)\n' ;;
  119. 1) printf 'audit: EXPOSED - plaintext key-shaped material found\n' ;;
  120. 3) printf 'audit: INCONCLUSIVE for at least one file - absences there mean nothing\n' ;;
  121. 4) printf 'audit: REVIEW needed\n' ;;
  122. esac
  123. exit $rc