[project @ peter@palfrader.org-20080519135700-ekglreng7ttedbqd]
[mirror/dsa-nagios.git] / dsa-nagios-nrpe-config / 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 # 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.
17 #
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.
25 #
26
27 =pod
28
29 =head1 NAME
30
31 B<check_weakkeys> - checks system for weak ssh keys 
32
33 =cut
34
35 =head1 SYNOPSIS
36
37 B<check_weakkeys> [options]
38
39 =cut
40
41 =head1 DESCRIPTION
42
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>
52 from Peter Palfrader. 
53
54 =cut
55
56 =head1 OPTIONS
57
58 =over 4
59
60 =item B<-h, --help>
61
62 Prints out a brief help
63
64 =item B<-s, --statusfile> "statusfile"
65
66 Use 'F<statusfile>' instead of 'F<STDOUT>'. 
67
68 =item B<-f, --fprdb> "database" (default: /var/lib/dsa/ssh-weak-keys.db)
69
70 Use 'F<database>' instead of 'F</var/lib/dsa/ssh-weak-keys.db>'
71 as fingerprint database. 
72
73 =item B<-n, --dsa_nowarn> 
74
75 Don't warn for DSA keys
76
77 =back 
78
79 =cut
80
81 use strict;
82 use warnings;
83
84 use File::Temp;
85 use BerkeleyDB;
86 use Pod::Usage;
87 use Getopt::Long;
88 use IPC::Open3;
89
90 my $fprdb_fname = "/var/lib/dsa/ssh-weak-keys.db" ;
91 my ($outfile, $help);
92 my $dsa_nowarn = 0;
93
94 GetOptions(     'help|h' => \$help, #Help function
95                 'statusfile|s=s' => \$outfile, 
96                 'fprdb|f=s' => \$fprdb_fname,
97                 'n|dsa_nowarn' => \$dsa_nowarn,  
98 );
99
100 pod2usage(1) if $help;
101
102 my $fh; 
103 if ($outfile) {
104         open ($fh, '>', $outfile) 
105                 or die "Could not open statusfile '$outfile' for writing: $!";
106 } else {
107         $fh = *STDOUT; 
108 }
109
110 my %fpr_hash;
111 tie %fpr_hash, 'BerkeleyDB::Btree',
112         -Filename   => $fprdb_fname,
113         -Flags      => DB_RDONLY
114                 or die "Cannot open fingerprint db $fprdb_fname: $! $BerkeleyDB::Error\n";
115
116
117 my ($weak_keys,$checked_keys) = 0;
118 my $dsa_keys = 0;
119 my $text = '';
120 my %key_sizes;
121
122
123
124 &from_user_all;
125 &from_ssh_host(qw(localhost));
126
127 my $status="OK";
128 if ($weak_keys) {
129         $status = "CRITICAL";
130 } elsif ($dsa_keys && ! $dsa_nowarn) {
131         $status = "WARNING";
132 }
133
134 print $fh "$status\n";
135 print $fh "Checked $checked_keys keys - $weak_keys weak - $dsa_keys dsa keys\n";
136 print $fh "Sizes: ";
137 foreach my $size (sort(keys(%key_sizes))) {
138         print $fh "$size:$key_sizes{$size} ";
139 }
140
141 print $fh "\n";
142 print $fh "$text" if $text;
143
144
145
146 sub safe_backtick (@) {
147     my @args = @_;
148
149     my ($wtr, $fh, $err);
150
151     open3($wtr,$fh,$err, @args)
152         or die "error: failed to spawn $args[0]: $!\n";
153     my @result;
154     if (wantarray) {
155         @result = <$fh>;
156     } else {
157         local $/;
158         @result = scalar(<$fh>);
159     }
160     close $fh;
161     $? == 0 or return undef;
162     if (wantarray) {
163         return @result;
164     } else {
165         return $result[0];
166     }
167 }
168
169 sub ssh_fprint_file ($) {
170     my $name = shift;
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;
175     return ();
176 }
177
178 sub ssh_fprint_check ($$$) {
179     my ($name, $length, $hash) = @_;
180     if (exists $key_sizes{$length}) {
181             $key_sizes{$length}++;
182     } else {
183             $key_sizes{$length}=1;
184     }
185     $checked_keys++;
186     if (exists $fpr_hash{$hash}) {
187         $weak_keys++;
188         $text .= "$name weak ($hash)\n";
189     }
190 }
191
192
193 sub from_ssh_key_file ($) {
194     my $name = shift;
195     if (open (my $FH, '<', $name)) {
196         my $key = <$FH>; 
197         if ($key =~ m/^ssh-dss/) {
198                 $dsa_keys++;
199                 $text .= "$name is a DSA key\n";
200         }
201     } else {
202         $text .= "Could not open $name: $!";
203     }
204     my ($length, $hash) = ssh_fprint_file $name;
205     if ($length && $hash) {
206         ssh_fprint_check "$name:1", $length, $hash;
207     } else {
208         $text .= "$name:1: warning: failed to parse SSH key file\n";
209     }
210 }
211
212 sub clear_tmp ($) {
213     my $tmp = shift;
214     seek $tmp, 0, 0 or die "seek: $!";
215     truncate $tmp, 0 or die "truncate: $!";
216 }
217
218 sub from_ssh_auth_file ($) {
219     my $name = shift;
220     my $auth;
221     unless (open $auth, '<', $name) {
222         warn "$name:0: error: open failed: $!\n";
223         return;
224     }
225     my $tmp = new File::Temp;
226     while (my $line = <$auth>) {
227         chomp $line;
228         my $lineno = $.;
229         clear_tmp $tmp;
230         next if $line =~ m/^#/; # ignore comments
231         if ($line =~ m/^ssh-dss/) {
232                 $dsa_keys++;
233                 $text .= "$name:$lineno is a DSA key\n";
234         }
235         print $tmp "$line\n" or die "print: $!";
236         $tmp->flush;
237         my ($length, $hash) = ssh_fprint_file "$tmp";
238         if ($length && $hash) {
239             ssh_fprint_check "$name:$lineno", $length, $hash;
240         } else {
241             $text .= "$name:$lineno: warning: unparsable line\n";
242         }
243     }
244 }
245
246 sub from_ssh_host (@) {
247     my @names = @_;
248     my @lines;
249     push @lines, safe_backtick qw|ssh-keyscan -t rsa|, @names;
250     push @lines, safe_backtick qw|ssh-keyscan -t dsa|, @names;
251
252     my $tmp = new File::Temp;
253     for my $line (@lines) {
254         next if $line =~ /^#/;
255         my ($host, $data) = $line =~ /^(\S+) (.*)$/;
256         clear_tmp $tmp;
257         print $tmp "$data\n" or die "print: $!";
258         $tmp->flush;
259         my ($length, $hash) = ssh_fprint_file "$tmp";
260         if ($length && $hash) {
261             ssh_fprint_check "$host", $length, $hash;
262         } else {
263             $text .= "$host: warning: unparsable line\n";
264         }
265     }
266 }
267
268 sub from_user ($) {
269     my $user = shift;
270     my ($name,$passwd,$uid,$gid,
271         $quota,$comment,$gcos,$dir,$shell,$expire) = getpwnam($user);
272     my $file = "$dir/.ssh/authorized_keys";
273     from_ssh_auth_file $file if -r $file;
274     $file = "$dir/.ssh/authorized_keys2";
275     from_ssh_auth_file $file if -r $file;
276     $file = "$dir/.ssh/id_rsa.pub";
277     from_ssh_key_file $file if -r $file;
278     $file = "$dir/.ssh/id_dsa.pub";
279     from_ssh_key_file $file if -r $file;
280 }
281
282 sub from_user_all () {
283     setpwent;
284     while (my $name = getpwent) {
285         from_user $name;
286     }
287     endpwent;
288 }
289
290