This file is indexed.

/usr/bin/openssl-vulnkey is in openssl-blacklist 0.5-3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/python
#
#    openssl-vulnkey: check a database of sha1'd static key hashes for
#      known vulnerable keys
#    Copyright (C) 2008-2011 Canonical Ltd.
#    Author: Jamie Strandboge <jamie@canonical.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3,
#    as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from optparse import OptionParser
import os
import re
import hashlib
import subprocess
import sys
import tempfile
import shutil

version = "0.5-3"
db_prefix = "/usr/share/openssl-blacklist/blacklist.RSA-"
db_lines = []

parser = OptionParser(usage="%prog FILE [FILE]", \
                      version="%prog: " + version, \
                      description="This program checks if FILEs are known " + \
                                  "vulnerable static keys")
parser.add_option("-q", "--quiet", action="store_true", dest="quiet", \
                  help="be quiet")
parser.add_option("-b", "--bits", dest="bits", \
                  help="number of bits (requires -m)")
parser.add_option("-m", "--modulus", dest="modulus", \
                  help="modulus to check (requires -b)")
(options, args) = parser.parse_args()

if not ((options.modulus and options.bits) or args):
    parser.print_help()
    sys.exit(1)

def cmd(command, input = None, stderr = subprocess.PIPE, stdout = subprocess.PIPE, stdin = None):
    '''Try to execute given command (array) and return its stdout, or return
    a textual error if it failed.'''

    try:
       sp = subprocess.Popen(command, stdin=stdin, stdout=stdout, stderr=stderr, close_fds=True)
    except OSError, e:
        return [127, str(e)]

    out = sp.communicate(input)[0]
    return [sp.returncode,out]

def get_contents(file):
    '''Determine the type of certificate it is. Returns empty string if
       unsupported.'''
    args = ['-modulus', '-text', '-in', file]

    rc, report = cmd(['openssl', 'rsa'] + args)
    if rc == 0:
        return ("rsa", report)

    rc, report = cmd(['openssl', 'x509'] + args)
    if rc == 0:
        return ("x509", report)

    rc, report = cmd(['openssl', 'req'] + args)
    if rc == 0:
        return ("req", report)

    return ("", report)

def get_bits(contents, type):
    '''Find bit length of file. Returns empty string if unsupported.'''
    for line in contents:
        leading = "Private-Key: "
        if type == "x509" or type == "req":
            leading = "RSA Public Key: "
            if leading not in contents:
                # OpenSSL 1.0.0
                leading = "Public-Key: "

        # TODO: don't hardcode these
        if leading + "(512" in contents:
            return "512"
        elif leading + "(1024" in contents:
            return "1024"
        elif leading + "(2048" in contents:
            return "2048"
        elif leading + "(4096" in contents:
            return "4096"
        elif leading + "(8192" in contents:
            return "8192"

    return ""

def get_modulus(contents):
    '''Find modulus of file'''
    for line in contents.split('\n'):
        if re.match(r'^Modulus=', line):
            return line + '\n'

    return ""

def get_exponent(contents):
    '''Find exponent of file. Returns empty string if unsupported.'''
    if "Exponent: 65537 " in contents:
        return "65537"

    return ""

def check_db(bits, last, modulus, name=""):
    '''Check modulus against database'''
    global db_lines
    if last != bits:
        db = db_prefix + bits
        # Read in the database
        try:
            fh = open(db, 'r')
        except:
            try:
                print >> sys.stderr, "WARN: could not open database for %s " \
                                     "bits. Skipped %s" % (bits, name)
            except IOError:
                pass
            return False

        db_lines = fh.read().split('\n')
        fh.close()

    key = hashlib.sha1(modulus).hexdigest()
    #print "bits: %s\nmodulus: %s\nkey: %s\nkey80: %s" % (bits, modulus, key, key[20:])
    if key[20:] in db_lines:
        if not options.quiet:
            print "COMPROMISED: %s %s" % (key, name)
        return True
    else:
        if not options.quiet:
            print "Not blacklisted: %s %s" % (key, name)
        return False


last_bits = ""
found = False
error = False

if options.bits and options.modulus:
    found = check_db(options.bits, last_bits, \
                     "Modulus=%s\n" % (options.modulus))
else:
    # Check each file
    for f in args:
        realname = f

        if f == "-":
            # dump stdin to tmpfile, operate on tmpfile instead
            temp = tempfile.NamedTemporaryFile()
            shutil.copyfileobj(sys.stdin,temp)
            temp.flush()
            f = temp.name

        try:
            file(f).read()
        except IOError, e:
            if not options.quiet:
                print >> sys.stderr, "ERROR: %s: %s" % (realname, e.strerror)
            error = True
            continue

        (type, contents) = get_contents(f)
        if type == "":
            if not options.quiet:
                print >> sys.stderr, "Skipped: '%s' is unsupported type " + \
                                     "(not x509, req or rsa)" % (realname)
            continue

        exp = get_exponent(contents)
        if exp == "":
            if not options.quiet:
                print >> sys.stderr, "Skipped: '%s' has unsupported exponent" % \
                                      (realname)
            continue

        bits = get_bits(contents, type)
        if bits == "":
            if not options.quiet:
                print >> sys.stderr, "Skipped: '%s' has unsupported bit size" % \
                                      (realname)
            continue

        modulus = get_modulus(contents)
        if modulus == "":
            if not options.quiet:
                print >> sys.stderr, "ERROR: %s: problem finding modulus" % \
                                      (realname)
            error = True
            continue

        if check_db(bits, last_bits, modulus, realname):
            found = True
        last_bits = bits

if found:
    sys.exit(1)
elif error:
    sys.exit(2)