3 # Debian/OpenSSL Weak Key Detector
5 # Written by Florian Weimer <fw@deneb.enyo.de>, with blacklist data
6 # from Kees Cook, Peter Palfrader and James Strandboge.
8 # Patches and comments are welcome.
15 usage: $0 [OPTIONS...] COMMAND [ARGUMENTS...]
19 file: examine files on the command line for weak keys
20 host: examine the specified hosts for weak SSH keys
21 user: examine user SSH keys for weakness; examine all users if no
23 help: show this help screen
27 -c FILE: set the database cache file name (default: dowkd.db)
29 dowkd currently handles the following OpenSSH host and user keys,
30 provided they have been generated on a little-endian architecture
31 (such as i386 or amd64): RSA/1024, RSA/2048 and DSA/1024. (The
32 OpenSSH version in Debian does not support DSA key generation with)
35 OpenVPN shared also detected on little-endian architecture.
37 Note that the blacklist by dowkd may be incomplete; it is only
38 intended as a quick check.
50 my $db_file = 'dowkd.db';
56 $db = tie
%db, 'DB_File', $db_file, O_RDWR
| O_CREAT
, 0777, $DB_BTREE
57 or die "error: could not open database: $!\n";
59 $db{''} = $db_version;
60 while (my $line = <DATA
>) {
61 next if $line =~ /^\**$/;
63 $line =~ /^[0-9a-f]{32}$/ or die "error: invalid data line";
64 $line =~ s/(..)/chr(hex($1))/ge;
73 $db = tie
%db, 'DB_File', $db_file, O_RDONLY
, 0777, $DB_BTREE
74 or die "error: could not open database: $!\n";
75 my $stored_version = $db{''};
76 $stored_version && $stored_version eq $db_version or create_db
;
83 sub safe_backtick
(@
) {
87 or die "error: failed to spawn $args[0]: $!\n";
93 @result = scalar(<$fh>);
96 $?
== 0 or return undef;
105 my $keys_vulnerable = 0;
108 print STDERR
"summary: keys found: $keys_found, weak keys: $keys_vulnerable\n";
111 sub check_hash
($$) {
112 my ($name, $hash) = @_;
114 if (exists $db{$hash}) {
116 print "$name: weak key\n";
120 sub ssh_fprint_file
($) {
122 my $data = safe_backtick qw
/ssh-keygen -l -f/, $name;
123 defined $data or return ();
124 my @data = $data =~ /^(\d+) ([0-9a-f]{2}(?::[0-9a-f]{2}){15})/;
125 return @data if @data == 2;
129 sub ssh_fprint_check
($$$) {
130 my ($name, $length, $hash) = @_;
131 if ($length == 1024 || $length == 2048) {
133 $hash =~ s/(..)/chr(hex($1))/ge;
134 check_hash
$name, $hash;
136 warn "$name: warning: no suitable blacklist\n";
140 sub from_ssh_key_file
($) {
142 my ($length, $hash) = ssh_fprint_file
$name;
143 if ($length && $hash) {
144 ssh_fprint_check
"$name:1", $length, $hash;
146 warn "$name:1: warning: failed to parse SSH key file\n";
152 seek $tmp, 0, 0 or die "seek: $!";
153 truncate $tmp, 0 or die "truncate: $!";
156 sub from_ssh_auth_file
($) {
159 unless (open $auth, '<', $name) {
160 warn "$name:0: error: open failed: $!\n";
163 my $tmp = new File
::Temp
;
164 while (my $line = <$auth>) {
166 next if $line =~ m/^\s*(#|$)/;
169 print $tmp "$line\n" or die "print: $!";
171 my ($length, $hash) = ssh_fprint_file
"$tmp";
172 if ($length && $hash) {
173 ssh_fprint_check
"$name:$lineno", $length, $hash;
175 warn "$name:$lineno: warning: unparsable line\n";
180 sub from_openvpn_key
($) {
183 unless (open $key, '<', $name) {
184 warn "$name:0: open failed: $!\n";
189 while (my $line = <$key>) {
191 if ($line =~ /^-----BEGIN OpenVPN Static key V1-----/) {
194 if ($line =~ /^([0-9a-f]{32})/) {
196 $line =~ s/(..)/chr(hex($1))/ge;
197 check_hash
"$name:$.", $line;
200 warn "$name:$.: warning: illegal OpenVPN file format\n";
207 sub from_ssh_host
(@
) {
210 push @lines, safe_backtick qw
/ssh-keyscan -t rsa/, @names;
211 push @lines, safe_backtick qw
/ssh-keyscan -t dsa/, @names;
213 my $tmp = new File
::Temp
;
214 for my $line (@lines) {
215 next if $line =~ /^#/;
216 my ($host, $data) = $line =~ /^(\S+) (.*)$/;
218 print $tmp "$data\n" or die "print: $!";
220 my ($length, $hash) = ssh_fprint_file
"$tmp";
221 if ($length && $hash) {
222 ssh_fprint_check
"$host", $length, $hash;
224 warn "$host: warning: unparsable line\n";
231 my ($name,$passwd,$uid,$gid,
232 $quota,$comment,$gcos,$dir,$shell,$expire) = getpwnam($user);
233 my $file = "$dir/.ssh/authorized_keys";
234 from_ssh_auth_file
$file if -r
$file;
235 $file = "$dir/.ssh/authorized_keys2";
236 from_ssh_auth_file
$file if -r
$file;
237 $file = "$dir/.ssh/known_hosts";
238 from_ssh_auth_file
$file if -r
$file;
239 $file = "$dir/.ssh/known_hosts2";
240 from_ssh_auth_file
$file if -r
$file;
241 $file = "$dir/.ssh/id_rsa.pub";
242 from_ssh_key_file
$file if -r
$file;
243 $file = "$dir/.ssh/id_dsa.pub";
244 from_ssh_key_file
$file if -r
$file;
247 sub from_user_all
() {
248 # This was one loop initially, but does not work with some Perl
252 while (my $name = getpwent) {
256 from_user
$_ for @names;
259 if (@ARGV && $ARGV[0] eq '-c') {
261 $db_file = shift @ARGV if @ARGV;
265 my $cmd = shift @ARGV;
266 if ($cmd eq 'file') {
267 for my $name (@ARGV) {
268 next if from_openvpn_key
$name;
269 from_ssh_auth_file
$name;
271 } elsif ($cmd eq 'host') {
273 } elsif ($cmd eq 'user') {
275 from_user
$_ for @ARGV;
279 } elsif ($cmd eq 'help') {
283 die "error: invalid command, use \"help\" to get help\n";