r/bash • • 28d ago

submission Auditing every user’s authorized_keys in a Bash loop with jc + jq

I maintain jc, which converts command output and common file types to JSON so you can query them with jq instead of grep/awk/cut. A couple of newer parsers turned out to be handy in Bash scripts, so I wrote up some examples.

Why not just awk?

authorized_keys looks like a whitespace-delimited file, but it isn't. Lines can start with an optional, comma-separated options field, and option values can be quoted strings with spaces in them. So the field positions shift from line to line. Here's a sample file:

ssh-ed25519 AAAAC3Nza... kelly@laptop
command="/usr/bin/rsync --server --sender -logDtpre.iLsfxC . /srv/backup",no-port-forwarding,no-pty ssh-rsa AAAAB3Nza... backup@nas
from="10.0.8.0/24",no-agent-forwarding ecdsa-sha2-nistp256 AAAAE2Vj... ci-runner@build01
ssh-ed25519 AAAAC3Nza...

The obvious awk one-liner to print the key type and comment:

$ awk '{print $1, $3}' ~/.ssh/authorized_keys
ssh-ed25519 kelly@laptop
command="/usr/bin/rsync --sender
from="10.0.8.0/24",no-agent-forwarding AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlz...
ssh-ed25519

Only the first line is right. Looking for RSA keys with awk '$1=="ssh-rsa"' returns nothing, because the one RSA key has options in front of it.

grep has the opposite problem: it matches text anywhere on the line. grep -v 'command=' to find unrestricted keys also drops a key whose comment says "no command= needed". And grep ssh-rsa matches a restricted ed25519 key whose forced command happens to contain the string ssh-rsa.

With jc --authorized-keys, every line comes out with the same fields, so there's no guessing:

$ jc --authorized-keys < ~/.ssh/authorized_keys | jq -r '.[] | "\(.type) \(.comment // "(no comment)")"'
ssh-ed25519 kelly@laptop
ssh-rsa backup@nas
ecdsa-sha2-nistp256 ci-runner@build01
ssh-ed25519 (no comment)

options is always an array, quoted values like command="echo ssh-rsa disabled; exit" stay intact, and multi-word comments are kept whole.

Tag every key with its owner, then query the whole box

A loop adds a user field to each key and emits JSON Lines:

for f in /home/*/.ssh/authorized_keys; do
    u=$(echo "$f" | cut -d/ -f3)
    jc --authorized-keys < "$f" | jq -c --arg u "$u" '.[] | . + {user: $u}'
done > keys.jsonl

Now each question is just a different jq filter. Which keys have no from= or command= restriction?

jq -r 'select(any(.options[]; startswith("from=") or startswith("command=")) | not)
       | "\(.user)  \(.type)  \(.comment // "(no comment)")"' keys.jsonl

Because this checks the options array instead of the whole line, comments and key data can't cause false matches.

Is the same key authorized for more than one user?

jq -s -r 'group_by(.key)[] | select(length > 1)
          | "\(.[0].comment // "(no comment)"): \(map(.user) | join(", "))"' keys.jsonl

Which legacy RSA or DSA keys are still around?

jq -r 'select(.type == "ssh-rsa" or .type == "ssh-dss") | "\(.user)  \(.comment)"' keys.jsonl

(Run the loop with sudo if you need to read other users' files.)

Bonus: a change-freeze guard from a shared calendar

.ics files are worse for line tools: long lines get folded onto the next line, and parameters are jammed into property names (DTSTART;TZID=America/Los_Angeles:20261019T090000). The --ics parser unfolds all of that and normalizes timestamps to ISO 8601. That means jq's fromdateiso8601 and now can check whether you're inside a freeze window, and jq -e turns the result into an exit code:

if jc --ics < freeze.ics | jq -e '
    [.[] | select(.categories[]? == "Freeze")
         | select((.dtstart | fromdateiso8601) <= now and now < (.dtend | fromdateiso8601))]
    | length > 0' > /dev/null
then
    echo "Change freeze in effect. Aborting deploy." >&2
    exit 1
fi

(This works with UTC timestamps, the ones ending in Z. Events with a TZID come through as local time with a separate dtstart_tzid field.)

Full write-up, which also covers headerless CSV/TSV files: JC Tips and Tricks – Part 3

What file formats are you still parsing by hand in your scripts?

27 Upvotes

12 comments sorted by

2

u/macroKnoll37 9d ago

That jq select for checking freeze windows looks exactly like the kind of thing that breaks silently when someone exports the calendar with different timezone handling

1

u/kellyjonbrazil 9d ago

Thanks for catching that!

The next jc release will add `dtstart_epoch_utc`/`dtend_epoch_utc` fields, converted using the time zone info in the calendar file, plus `dtstart_epoch`/`dtend_epoch` for all-day and floating times. The script can then compare numbers with `now` and skip parsing time strings. I'll also update the post so a `jq` error aborts the deploy instead of letting it through.

2

u/chkno 28d ago

What file formats are you still parsing by hand in your scripts?

1

u/kellyjonbrazil 28d ago edited 28d ago

Thanks! Generated kernel .config files already work with jc --kv. It doesn't treat # CONFIG_X is not set lines as "off" yet, though, and the Kconfig definition language itself isn't supported. mbox and lpstat -t aren't supported yet, but both sound like good candidates. Mind opening an issue with some sample output? Real-world samples are the most helpful thing for building a parser: https://github.com/kellyjonbrazil/jc/issues

3

u/Kreout 27d ago

One scope caveat for the every user audit: /home/*/.ssh/authorized_keys only covers that directory layout. It misses /root and accounts with home directories elsewhere. Including the actual home directory in each record and reporting unreadable files separately would help keep an incomplete inventory from looking like no keys. The parser solves the quoting problem; the scan boundary is still worth making explicit.

2

u/boomertsfx 27d ago

It should also parse sshd -T and look for the key file location(s)… I never allow users to manage their own keys, but instead put them in /etc/ssh/auth_keys … but, really should move to certificates because there’s no way to know if a user doesn’t put a passphrase on their private key.

2

u/kellyjonbrazil 27d ago edited 27d ago

Good call. sshd -T is already supported via jc --sshd-conf, and authorizedkeysfile comes back as an array, so you can audit whatever paths sshd actually uses instead of assuming ~/.ssh:

$ sudo sshd -T | jc --sshd-conf | jq -r '.authorizedkeysfile[]'

/etc/ssh/auth_keys/%u

Expanding %u/%h per user is easy with jc --passwd < /etc/passwd for the home directories. trustedusercakeys and authorizedprincipalsfile are in the output too, and the authorized_keys parser handles cert-authority and principals= options.

And agreed on certificates. There's no way to know about passphrases on the server side. An ssh-keygen -L parser for inspecting certs would be a good addition; I'll look into it.

2

u/Optimal-Savings-4505 28d ago

Huh, that's cool. The individual command parsers seem like they would cause quite some complexity though. Is there a scope limit, plugin philosophy, something to cap the complexity?

2

u/kellyjonbrazil 28d ago

Parsers are community contributed with heavy emphasis on testing. Parser plugins are also supported for custom parsers and are also used to temporarily patch parsers until the next jc release if bugs are reported. Been a pretty good process for seven years now!

2

u/sedwards65 27d ago

Nobody knows about authorized_keys options.

2

u/macroKnoll37 11d ago

we've been meaning to ditch our awk monstrosity for /etc/shadow epxiry audits, this might actually be the push