3 # This cheak is based on code from the Debian/OpenSSL Weak Key Detector
4 # written by Florian Weimer <fw@deneb.enyo.de>.
5 # The code has been modified and enhanced by Alexander Wirt
6 # <formorer@debian.org> to use it as a nagios check.
8 # Copyright (c) 2008, Florian Weimer <fw@deneb.enyo.de> for the original
9 # Debian/OpenSSL Weak Key Detector
10 # (http://security.debian.org/project/extra/dowkd/dowkd.pl.gz)
12 # Copyright (c) 2008, Alexander Wirt <formorer@debian.org> for check_weakkeys
14 # Permission to use, copy, modify, and/or distribute this software for any
15 # purpose with or without fee is hereby granted, provided that the above
16 # copyright notice and this permission notice appear in all copies.
18 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 B<check_weakkeys> - checks system for weak ssh keys
37 B<check_weakkeys> [options]
43 B<check_weakkeys> checks for all users if there id_rsa, id_dsa or
44 authorized_key files if they contain weak ssh keys created by a Debian with a
45 broken libssl (see DSA-1571 for more informations). Optionally <check_weakkeys>
46 can spit out a warning of there are any DSA keys left in key or authorized_key
47 files. To work it needs a database of precomputed hashes of known weak keys.
48 This file is expected as an bdb database with the hash (like
49 03:a2:f0:46:7f:13:9f:5f:96:71:a9:b8:a0:1c:01:05) as key. See <gen_fprdb> for
50 such a database generator. <check_weakkeys> outputs his data to STDOUT or to a
51 file. It meaned to be picked up by an nagios check like B<dsa-check-statusfile>
62 Prints out a brief help
64 =item B<-s, --statusfile> "statusfile"
66 Use 'F<statusfile>' instead of 'F<STDOUT>'.
68 =item B<-f, --fprdb> "database" (default: /var/lib/dsa/ssh-weak-keys.db)
70 Use 'F<database>' instead of 'F</var/lib/dsa/ssh-weak-keys.db>'
71 as fingerprint database.
73 =item B<-n, --dsa_nowarn>
75 Don't warn for DSA keys
90 my $fprdb_fname = "/var/lib/dsa/ssh-weak-keys.db" ;
94 GetOptions( 'help|h' => \$help, #Help function
95 'statusfile|s=s' => \$outfile,
96 'fprdb|f=s' => \$fprdb_fname,
97 'n|dsa_nowarn' => \$dsa_nowarn,
100 pod2usage(1) if $help;
104 open ($fh, '>', $outfile)
105 or die "Could not open statusfile '$outfile' for writing: $!";
111 tie %fpr_hash, 'BerkeleyDB::Btree',
112 -Filename => $fprdb_fname,
114 or die "Cannot open fingerprint db $fprdb_fname: $! $BerkeleyDB::Error\n";
117 my ($weak_keys,$checked_keys) = 0;
125 &from_ssh_host(qw(localhost));
129 $status = "CRITICAL";
130 } elsif ($dsa_keys && ! $dsa_nowarn) {
134 print $fh "$status\n";
135 print $fh "Checked $checked_keys keys - $weak_keys weak - $dsa_keys dsa keys\n";
137 foreach my $size (sort(keys(%key_sizes))) {
138 print $fh "$size:$key_sizes{$size} ";
142 print $fh "$text" if $text;
146 sub safe_backtick (@) {
149 my ($wtr, $fh, $err);
151 open3($wtr,$fh,$err, @args)
152 or die "error: failed to spawn $args[0]: $!\n";
158 @result = scalar(<$fh>);
161 $? == 0 or return undef;
169 sub ssh_fprint_file ($) {
171 my $data = safe_backtick qw/ssh-keygen -l -f/, $name;
172 defined $data or return ();
173 my @data = $data =~ /^(\d+) ([0-9a-f]{2}(?::[0-9a-f]{2}){15})/;
174 return @data if @data == 2;
178 sub ssh_fprint_check ($$$) {
179 my ($name, $length, $hash) = @_;
180 if (exists $key_sizes{$length}) {
181 $key_sizes{$length}++;
183 $key_sizes{$length}=1;
186 if (exists $fpr_hash{$hash}) {
188 $text .= "$name weak ($hash)\n";
193 sub from_ssh_key_file ($) {
195 if (open (my $FH, '<', $name)) {
197 if ($key =~ m/^ssh-dss/) {
199 $text .= "$name is a DSA key\n";
202 $text .= "Could not open $name: $!";
204 my ($length, $hash) = ssh_fprint_file $name;
205 if ($length && $hash) {
206 ssh_fprint_check "$name:1", $length, $hash;
208 $text .= "$name:1: warning: failed to parse SSH key file\n";
214 seek $tmp, 0, 0 or die "seek: $!";
215 truncate $tmp, 0 or die "truncate: $!";
218 sub from_ssh_auth_file ($) {
221 unless (open $auth, '<', $name) {
222 warn "$name:0: error: open failed: $!\n";
225 my $tmp = new File::Temp;
226 while (my $line = <$auth>) {
230 next if $line =~ m/^$/; # ignore empty lines
231 next if $line =~ m/^#/; # ignore comments
232 if ($line =~ m/^ssh-dss/) {
234 $text .= "$name:$lineno is a DSA key\n";
236 print $tmp "$line\n" or die "print: $!";
238 my ($length, $hash) = ssh_fprint_file "$tmp";
239 if ($length && $hash) {
240 ssh_fprint_check "$name:$lineno", $length, $hash;
242 $text .= "$name:$lineno: warning: unparsable line\n";
247 sub from_ssh_host (@) {
250 push @lines, safe_backtick qw|ssh-keyscan -t rsa|, @names;
251 push @lines, safe_backtick qw|ssh-keyscan -t dsa|, @names;
253 my $tmp = new File::Temp;
254 for my $line (@lines) {
255 next if $line =~ /^#/;
256 my ($host, $data) = $line =~ /^(\S+) (.*)$/;
258 print $tmp "$data\n" or die "print: $!";
260 my ($length, $hash) = ssh_fprint_file "$tmp";
261 if ($length && $hash) {
262 ssh_fprint_check "$host", $length, $hash;
264 $text .= "$host: warning: unparsable line\n";
271 my ($name,$passwd,$uid,$gid,
272 $quota,$comment,$gcos,$dir,$shell,$expire) = getpwnam($user);
273 my $file = "$dir/.ssh/authorized_keys";
274 from_ssh_auth_file $file if -r $file;
275 $file = "$dir/.ssh/authorized_keys2";
276 from_ssh_auth_file $file if -r $file;
277 $file = "$dir/.ssh/id_rsa.pub";
278 from_ssh_key_file $file if -r $file;
279 $file = "$dir/.ssh/id_dsa.pub";
280 from_ssh_key_file $file if -r $file;
283 sub from_user_all () {
285 while (my $name = getpwent) {