We need to look for two locations, not two directives
[mirror/dsa-nagios.git] / dsa-nagios-checks / share / weak-ssh-keys-check
1 #!/usr/bin/perl
2
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. 
7 #
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)
11 #
12 # Copyright (c) 2008, Alexander Wirt <formorer@debian.org> for check_weakkeys
13 #
14 # Copyright (c) 2008 Peter Palfrader <peter@palfrader.org>
15 #
16 # Permission to use, copy, modify, and/or distribute this software for any
17 # purpose with or without fee is hereby granted, provided that the above
18 # copyright notice and this permission notice appear in all copies.
19 #
20 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
21 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
22 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
23 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
25 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
26 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #
28
29 =pod
30
31 =head1 NAME
32
33 B<check_weakkeys> - checks system for weak ssh keys 
34
35 =cut
36
37 =head1 SYNOPSIS
38
39 B<check_weakkeys> [options]
40
41 =cut
42
43 =head1 DESCRIPTION
44
45 B<check_weakkeys> checks for all users if there id_rsa, id_dsa or
46 authorized_key files if they contain weak ssh keys created by a Debian with a
47 broken libssl (see DSA-1571 for more informations). Optionally <check_weakkeys>
48 can spit out a warning of there are any DSA keys left in key or authorized_key
49 files. To work it needs a database of precomputed hashes of known weak keys.
50 This file is expected as an bdb database with the hash (like
51 03:a2:f0:46:7f:13:9f:5f:96:71:a9:b8:a0:1c:01:05) as key. See <gen_fprdb> for
52 such a database generator.  <check_weakkeys> outputs his data to STDOUT or to a
53 file. It meaned to be picked up by an nagios check like B<dsa-check-statusfile>
54 from Peter Palfrader. 
55
56 =cut
57
58 =head1 OPTIONS
59
60 =over 4
61
62 =item B<-h, --help>
63
64 Prints out a brief help
65
66 =item B<-s, --statusfile> "statusfile"
67
68 Use 'F<statusfile>' instead of 'F<STDOUT>'. 
69
70 =item B<-f, --fprdb> "database" (default: /var/lib/dsa/ssh-weak-keys.db)
71
72 Use 'F<database>' instead of 'F</var/lib/dsa/ssh-weak-keys.db>'
73 as fingerprint database. 
74
75 =item B<-n, --dsa_nowarn> 
76
77 Don't warn for DSA keys
78
79 =back 
80
81 =cut
82
83 use strict;
84 use warnings;
85
86 use File::Temp;
87 use BerkeleyDB;
88 use Pod::Usage;
89 use Getopt::Long;
90 use IPC::Open3;
91
92 my $fprdb_fname = "/var/lib/dsa/ssh-weak-keys.db" ;
93 my ($outfile, $help);
94 my $dsa_nowarn = 0;
95 my $debian_org = 1;
96
97 GetOptions(     'help|h' => \$help, #Help function
98                 'statusfile|s=s' => \$outfile, 
99                 'fprdb|f=s' => \$fprdb_fname,
100                 'n|dsa_nowarn' => \$dsa_nowarn,
101                 'd|debian-org!' => \$debian_org,
102 );
103
104 pod2usage(1) if $help;
105
106 my $fh; 
107 if ($outfile) {
108         open ($fh, '>', $outfile) 
109                 or die "Could not open statusfile '$outfile' for writing: $!";
110 } else {
111         $fh = *STDOUT; 
112 }
113
114 my %fpr_hash;
115 tie %fpr_hash, 'BerkeleyDB::Btree',
116         -Filename   => $fprdb_fname,
117         -Flags      => DB_RDONLY
118                 or die "Cannot open fingerprint db $fprdb_fname: $! $BerkeleyDB::Error\n";
119
120
121 my ($weak_keys,$checked_keys) = 0;
122 my $dsa_keys = 0;
123 my $weird_keyfiles = 0;
124 my $text = '';
125 my %key_sizes;
126
127
128 if ($debian_org) {
129         &from_debianorg_places;
130 } else {
131         &from_user_all;
132 }
133 &from_ssh_host(qw(localhost));
134
135 my $status="OK";
136 if ($weak_keys) {
137         $status = "CRITICAL";
138 } elsif ($dsa_keys && ! $dsa_nowarn  ||  $weird_keyfiles) {
139         $status = "WARNING";
140 }
141
142 print $fh "$status\n";
143 print $fh "Checked $checked_keys keys - $weak_keys weak - $dsa_keys dsa keys\n";
144 print $fh "Sizes: ";
145 foreach my $size (sort(keys(%key_sizes))) {
146         print $fh "$size:$key_sizes{$size} ";
147 }
148
149 print $fh "\n";
150 print $fh "$text" if $text;
151
152
153
154 sub safe_backtick (@) {
155     my @args = @_;
156
157     my ($wtr, $fh, $err);
158
159     open3($wtr,$fh,$err, @args)
160         or die "error: failed to spawn $args[0]: $!\n";
161     my @result;
162     if (wantarray) {
163         @result = <$fh>;
164     } else {
165         local $/;
166         @result = scalar(<$fh>);
167     }
168     close $fh;
169     $? == 0 or return undef;
170     if (wantarray) {
171         return @result;
172     } else {
173         return $result[0];
174     }
175 }
176
177 sub ssh_fprint_file ($) {
178     my $name = shift;
179     my $data = safe_backtick qw/ssh-keygen -l -f/, $name;
180     defined $data or return ();
181     my @data = $data =~ /^(\d+) ([0-9a-f]{2}(?::[0-9a-f]{2}){15})/;
182     return @data if @data == 2;
183     return ();
184 }
185
186 sub ssh_fprint_check ($$$) {
187     my ($name, $length, $hash) = @_;
188     if (exists $key_sizes{$length}) {
189             $key_sizes{$length}++;
190     } else {
191             $key_sizes{$length}=1;
192     }
193     $checked_keys++;
194     if (exists $fpr_hash{$hash}) {
195         $weak_keys++;
196         $text .= "$name weak ($hash)\n";
197     }
198 }
199
200
201 sub from_ssh_key_file ($) {
202     my $name = shift;
203     if (open (my $FH, '<', $name)) {
204         my $key = <$FH>; 
205         close($FH);
206         if (! defined $key) {
207                 $weird_keyfiles++;
208                 $text .= "cannot read $name properly - empty?\n";
209         } elsif ($key =~ m/ssh-dss/) {
210                 $dsa_keys++;
211                 $text .= "$name is a DSA key\n";
212         }
213     } else {
214         $text .= "Could not open $name: $!";
215     }
216     my ($length, $hash) = ssh_fprint_file $name;
217     if ($length && $hash) {
218         ssh_fprint_check "$name:1", $length, $hash;
219     } else {
220         $text .= "$name:1: warning: failed to parse SSH key file\n";
221     }
222 }
223
224 sub clear_tmp ($) {
225     my $tmp = shift;
226     seek $tmp, 0, 0 or die "seek: $!";
227     truncate $tmp, 0 or die "truncate: $!";
228 }
229
230 sub from_ssh_auth_file ($) {
231     my $name = shift;
232     my $auth;
233     unless (open $auth, '<', $name) {
234         warn "$name:0: error: open failed: $!\n";
235         return;
236     }
237     my $tmp = new File::Temp;
238     while (my $line = <$auth>) {
239         chomp $line;
240         my $lineno = $.;
241         clear_tmp $tmp;
242         next if $line =~ m/^$/; # ignore empty lines
243         next if $line =~ m/^#/; # ignore comments
244         if ($line =~ m/ssh-dss/) {
245                 $dsa_keys++;
246                 $text .= "$name:$lineno is a DSA key\n";
247         }
248         print $tmp "$line\n" or die "print: $!";
249         $tmp->flush;
250         my ($length, $hash) = ssh_fprint_file "$tmp";
251         if ($length && $hash) {
252             ssh_fprint_check "$name:$lineno", $length, $hash;
253         } else {
254             $text .= "$name:$lineno: warning: unparsable line\n";
255         }
256     }
257 }
258
259 sub from_ssh_host (@) {
260     my @names = @_;
261     my @lines;
262     push @lines, safe_backtick qw|ssh-keyscan -t rsa|, @names;
263     push @lines, safe_backtick qw|ssh-keyscan -t dsa|, @names;
264
265     my $tmp = new File::Temp;
266     for my $line (@lines) {
267         next if $line =~ /^#/;
268         next if $line =~ /^no hostkey alg/;
269         my ($host, $data) = $line =~ /^(\S+) (.*)$/;
270         clear_tmp $tmp;
271         print $tmp "$data\n" or die "print: $!";
272         $tmp->flush;
273         my ($length, $hash) = ssh_fprint_file "$tmp";
274         if ($length && $hash) {
275             ssh_fprint_check "$host", $length, $hash;
276         } else {
277             $text .= "$host: warning: unparsable line\n";
278         }
279     }
280 }
281
282 sub from_user ($) {
283     my $user = shift;
284     my ($name,$passwd,$uid,$gid,
285         $quota,$comment,$gcos,$dir,$shell,$expire) = getpwnam($user);
286     my $file = "$dir/.ssh/authorized_keys";
287     from_ssh_auth_file $file if -r $file;
288     $file = "$dir/.ssh/authorized_keys2";
289     from_ssh_auth_file $file if -r $file;
290     $file = "$dir/.ssh/id_rsa.pub";
291     from_ssh_key_file $file if -r $file;
292     $file = "$dir/.ssh/id_dsa.pub";
293     from_ssh_key_file $file if -r $file;
294 }
295
296 sub from_user_all () {
297     setpwent;
298     while (my $name = getpwent) {
299         from_user $name;
300     }
301     endpwent;
302 }
303
304
305 sub from_debianorg_places () {
306     open(F, "/etc/ssh/sshd_config") or die ("Cannot open /etc/ssh/sshd_config: $!\n");
307     my @lines = <F>;
308     close(F);
309
310     my @ak = grep { /^AuthorizedKeysFile\s/i } @lines;
311     my @ak2 = grep { /^AuthorizedKeysFile2\s/i } @lines;
312     my @ak_files;
313
314     for my $line ((@ak, @ak2)) {
315             my @file_locations = split /\s+/, $line;
316             shift @file_locations;
317             push @ak_files, @file_locations;
318     }
319
320     if (scalar @ak_files != 2) {
321         print $fh "UNKNOWN\n";
322         print $fh "There should be two locations for User AuthorizedKeysFile defined in sshd_config\n";
323         exit
324     }
325
326     unless (grep { m#^/etc/ssh/userkeys/%u$# } @ak_files) {
327         print $fh "UNKNOWN\n";
328         print $fh "The AuthorizedKeysFile definition has an unexpected value.  Should be /etc/ssh/userkeys/%u\n";
329         exit
330     }
331     unless (grep { m#^/var/lib/misc/userkeys/%u$# } @ak_files) {
332         print $fh "UNKNOWN\n";
333         print $fh "The AuthorizedKeysFile2 definition has an unexpected value.  Should be /var/lib/misc/userkeys/%u\n";
334         exit
335     }
336
337     for my $d (qw{/etc/ssh/userkeys /var/lib/misc/userkeys}) {
338         next unless (-d $d);
339         opendir(D, $d) or die "Cannot opendir $d: $!\n";
340         for my $file (grep { ! -d $d.'/'.$_ } readdir(D)) {
341             next if ($file eq 'README-DSA-BUILDD');
342             my $f = $d.'/'.$file;
343             from_ssh_auth_file $f if -r $f;
344         };
345     };
346 }
347
348