2014-10-09 03:53:05 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
#
|
|
|
|
# Contributor: Julien Vehent jvehent@mozilla.com [:ulfr]
|
|
|
|
|
2015-05-30 15:46:26 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-08-23 00:33:28 +02:00
|
|
|
import sys, os, json, subprocess, logging, argparse, platform, urllib2, re
|
2014-10-09 15:35:59 +02:00
|
|
|
from collections import namedtuple
|
2014-10-12 01:37:08 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from copy import deepcopy
|
2014-10-09 03:53:05 +02:00
|
|
|
|
2015-08-23 17:31:04 +02:00
|
|
|
def str_compat(data):
|
|
|
|
if sys.version_info >= (3,0):
|
|
|
|
data = str(data, 'utf-8')
|
|
|
|
return data
|
|
|
|
|
2014-10-17 17:58:19 +02:00
|
|
|
# has_good_pfs compares a given PFS configuration with a target
|
|
|
|
# dh parameter a target elliptic curve, and return true if good
|
|
|
|
# if `must_match` is True, the exact values are expected, if not
|
|
|
|
# larger pfs values than the targets are acceptable
|
|
|
|
def has_good_pfs(pfs, target_dh, target_ecc, must_match=False):
|
2016-02-24 16:48:28 +01:00
|
|
|
if target_ecc and 'ECDH,' in pfs:
|
2014-10-17 17:58:19 +02:00
|
|
|
# split string, expected format is 'ECDH,P-256,256bits'
|
|
|
|
ecc = pfs.split(',')[2].split('b')[0]
|
|
|
|
if int(ecc) < target_ecc:
|
|
|
|
return False
|
|
|
|
if must_match and int(ecc) != target_ecc:
|
|
|
|
return False
|
2016-02-24 16:48:28 +01:00
|
|
|
elif target_dh and 'DH,' in pfs:
|
2014-10-17 17:58:19 +02:00
|
|
|
dhparam = pfs.split(',')[1].split('b')[0]
|
|
|
|
if int(dhparam) < target_dh:
|
|
|
|
return False
|
|
|
|
if must_match and int(dhparam) != target_dh:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2014-10-09 15:35:59 +02:00
|
|
|
# is_fubar assumes that a configuration is not completely messed up
|
|
|
|
# and looks for reasons to think otherwise. it will return True if
|
|
|
|
# it finds one of these reason
|
2014-10-09 03:53:05 +02:00
|
|
|
def is_fubar(results):
|
2016-02-24 16:48:28 +01:00
|
|
|
logging.debug('entering fubar evaluation')
|
2014-10-11 01:07:31 +02:00
|
|
|
lvl = 'fubar'
|
2016-08-21 14:16:54 +02:00
|
|
|
|
2014-10-09 15:35:59 +02:00
|
|
|
fubar = False
|
2014-10-11 01:07:31 +02:00
|
|
|
has_ssl2 = False
|
|
|
|
has_wrong_pubkey = False
|
2016-08-21 14:16:54 +02:00
|
|
|
has_wrong_ec_pubkey = False
|
2016-02-24 16:48:28 +01:00
|
|
|
has_bad_sig = False
|
2014-10-11 01:07:31 +02:00
|
|
|
has_untrust_cert = False
|
2014-10-17 17:58:19 +02:00
|
|
|
has_wrong_pfs = False
|
2016-08-21 14:16:54 +02:00
|
|
|
|
2014-10-09 03:53:05 +02:00
|
|
|
for conn in results['ciphersuite']:
|
2016-02-24 16:48:28 +01:00
|
|
|
logging.debug('testing connection %s' % conn)
|
2016-08-21 14:16:54 +02:00
|
|
|
pubkey_bits = int(conn['pubkey'][0])
|
2016-08-23 00:33:28 +02:00
|
|
|
ec_kex = re.match(r"(ECDHE|EECDH|ECDH)-", conn['cipher'])
|
2016-08-21 14:16:54 +02:00
|
|
|
|
2016-02-24 16:48:28 +01:00
|
|
|
if conn['cipher'] not in (set(old["ciphersuites"]) | set(inter["ciphersuites"]) | set(modern["ciphersuites"])):
|
2014-10-11 01:07:31 +02:00
|
|
|
failures[lvl].append("remove cipher " + conn['cipher'])
|
2014-10-09 15:35:59 +02:00
|
|
|
logging.debug(conn['cipher'] + ' is in the list of fubar ciphers')
|
|
|
|
fubar = True
|
2014-10-09 03:53:05 +02:00
|
|
|
if 'SSLv2' in conn['protocols']:
|
2014-10-11 01:07:31 +02:00
|
|
|
has_ssl2 = True
|
2014-10-09 15:35:59 +02:00
|
|
|
logging.debug('SSLv2 is in the list of fubar protocols')
|
|
|
|
fubar = True
|
2016-08-21 14:16:54 +02:00
|
|
|
if not ec_kex and pubkey_bits < 2048:
|
2014-10-11 01:07:31 +02:00
|
|
|
has_wrong_pubkey = True
|
2015-08-14 16:55:54 +02:00
|
|
|
logging.debug(conn['pubkey'][0] + ' is a fubar pubkey size')
|
2014-10-09 15:35:59 +02:00
|
|
|
fubar = True
|
2016-08-25 01:40:39 +02:00
|
|
|
if ec_kex and pubkey_bits < 256:
|
2016-08-21 14:16:54 +02:00
|
|
|
has_wrong_ec_pubkey = True
|
|
|
|
logging.debug(conn['pubkey'][0] + ' is a fubar EC pubkey size')
|
|
|
|
fubar = True
|
2014-10-17 17:20:25 +02:00
|
|
|
if conn['pfs'] != 'None':
|
2014-10-18 13:23:24 +02:00
|
|
|
if not has_good_pfs(conn['pfs'], 1024, 160):
|
2014-10-17 17:58:19 +02:00
|
|
|
logging.debug(conn['pfs']+ ' is a fubar PFS parameters')
|
2014-10-17 17:20:25 +02:00
|
|
|
fubar = True
|
2014-10-17 17:58:19 +02:00
|
|
|
has_wrong_pfs = True
|
2016-02-24 16:48:28 +01:00
|
|
|
for sigalg in conn['sigalg']:
|
|
|
|
if sigalg not in (set(old["certificate_signatures"]) | set(inter["certificate_signatures"]) | set(modern["certificate_signatures"])):
|
|
|
|
logging.debug(sigalg + ' is a fubar cert signature')
|
|
|
|
fubar = True
|
2014-10-09 15:35:59 +02:00
|
|
|
if conn['trusted'] == 'False':
|
2014-10-11 01:07:31 +02:00
|
|
|
has_untrust_cert = True
|
2014-10-09 15:35:59 +02:00
|
|
|
logging.debug('The certificate is not trusted, which is quite fubar')
|
|
|
|
fubar = True
|
2014-10-11 01:07:31 +02:00
|
|
|
if has_ssl2:
|
|
|
|
failures[lvl].append("disable SSLv2")
|
2016-02-24 16:48:28 +01:00
|
|
|
if has_bad_sig:
|
|
|
|
failures[lvl].append("don't use a cert with a bad signature algorithm")
|
2014-10-11 01:07:31 +02:00
|
|
|
if has_wrong_pubkey:
|
|
|
|
failures[lvl].append("don't use a public key smaller than 2048 bits")
|
2016-08-21 14:16:54 +02:00
|
|
|
if has_wrong_ec_pubkey:
|
2016-08-25 01:40:39 +02:00
|
|
|
failures[lvl].append("don't use an EC key smaller than 256 bits")
|
2014-10-11 01:07:31 +02:00
|
|
|
if has_untrust_cert:
|
|
|
|
failures[lvl].append("don't use an untrusted or self-signed certificate")
|
2014-10-17 17:58:19 +02:00
|
|
|
if has_wrong_pfs:
|
2014-10-18 13:23:24 +02:00
|
|
|
failures[lvl].append("don't use DHE smaller than 1024bits or ECC smaller than 160bits")
|
2014-10-09 15:35:59 +02:00
|
|
|
return fubar
|
2014-10-09 03:53:05 +02:00
|
|
|
|
2014-10-09 15:35:59 +02:00
|
|
|
# is_old assumes a configuration *is* old, and will return False if
|
|
|
|
# the parameters of an old configuration are not found. Those parameters
|
|
|
|
# are defined in https://wiki.mozilla.org/Security/Server_Side_TLS#Old_backward_compatibility
|
2014-10-09 03:53:05 +02:00
|
|
|
def is_old(results):
|
2016-02-24 16:48:28 +01:00
|
|
|
logging.debug('entering old evaluation')
|
2014-10-09 15:35:59 +02:00
|
|
|
lvl = 'old'
|
2016-02-24 16:48:28 +01:00
|
|
|
isold = True
|
2014-10-09 15:35:59 +02:00
|
|
|
has_3des = False
|
|
|
|
has_sha1 = True
|
2014-10-17 17:58:19 +02:00
|
|
|
has_pfs = True
|
2014-10-09 15:35:59 +02:00
|
|
|
has_ocsp = True
|
|
|
|
all_proto = []
|
2014-10-09 03:53:05 +02:00
|
|
|
for conn in results['ciphersuite']:
|
2016-02-24 16:48:28 +01:00
|
|
|
logging.debug('testing connection %s' % conn)
|
2014-10-09 15:35:59 +02:00
|
|
|
# flag unwanted ciphers
|
2016-02-24 16:48:28 +01:00
|
|
|
if conn['cipher'] not in old["ciphersuites"]:
|
2014-10-09 15:35:59 +02:00
|
|
|
logging.debug(conn['cipher'] + ' is not in the list of old ciphers')
|
|
|
|
failures[lvl].append("remove cipher " + conn['cipher'])
|
2016-02-24 16:48:28 +01:00
|
|
|
isold = False
|
2014-10-09 15:35:59 +02:00
|
|
|
# verify required 3des cipher is present
|
|
|
|
if conn['cipher'] == 'DES-CBC3-SHA':
|
|
|
|
has_3des = True
|
|
|
|
for proto in conn['protocols']:
|
|
|
|
if proto not in all_proto:
|
|
|
|
all_proto.append(proto)
|
|
|
|
# verify required sha1 signature is used
|
2014-10-09 03:53:05 +02:00
|
|
|
if 'sha1WithRSAEncryption' not in conn['sigalg']:
|
2014-10-09 15:35:59 +02:00
|
|
|
logging.debug(conn['sigalg'][0] + ' is a not an old signature')
|
|
|
|
has_sha1 = False
|
|
|
|
# verify required pfs parameter is used
|
2014-10-17 17:20:25 +02:00
|
|
|
if conn['pfs'] != 'None':
|
2016-02-24 16:48:28 +01:00
|
|
|
if not has_good_pfs(conn['pfs'], old["dh_param_size"], old["ecdh_param_size"], True):
|
2014-10-17 17:58:19 +02:00
|
|
|
logging.debug(conn['pfs']+ ' is not a good PFS parameter for the old configuration')
|
|
|
|
has_pfs = False
|
2014-10-09 15:35:59 +02:00
|
|
|
if conn['ocsp_stapling'] == 'False':
|
|
|
|
has_ocsp = False
|
2016-02-24 16:48:28 +01:00
|
|
|
extra_proto = set(all_proto) - set(old["tls_versions"])
|
2014-10-09 15:35:59 +02:00
|
|
|
for proto in extra_proto:
|
|
|
|
logging.debug("found protocol not wanted in the old configuration:" + proto)
|
|
|
|
failures[lvl].append('disable ' + proto)
|
2016-02-24 16:48:28 +01:00
|
|
|
isold = False
|
|
|
|
missing_proto = set(old["tls_versions"]) - set(all_proto)
|
2014-10-09 15:35:59 +02:00
|
|
|
for proto in missing_proto:
|
|
|
|
logging.debug("missing protocol wanted in the old configuration:" + proto)
|
2016-02-24 16:48:28 +01:00
|
|
|
failures[lvl].append('enable ' + proto)
|
|
|
|
isold = False
|
2014-10-09 15:35:59 +02:00
|
|
|
if not has_3des:
|
|
|
|
logging.debug("DES-CBC3-SHA is not supported and required by the old configuration")
|
|
|
|
failures[lvl].append("add cipher DES-CBC3-SHA")
|
2016-02-24 16:48:28 +01:00
|
|
|
isold = False
|
2014-10-09 15:35:59 +02:00
|
|
|
if not has_sha1:
|
|
|
|
failures[lvl].append("use a certificate with sha1WithRSAEncryption signature")
|
2016-02-24 16:48:28 +01:00
|
|
|
isold = False
|
2014-10-17 17:58:19 +02:00
|
|
|
if not has_pfs:
|
2016-02-24 16:48:28 +01:00
|
|
|
failures[lvl].append("use DHE of {dhe}bits and ECC of {ecdhe}bits".format(
|
|
|
|
dhe=old["dh_param_size"], ecdhe=old["ecdh_param_size"]))
|
|
|
|
isold = False
|
2014-10-09 15:35:59 +02:00
|
|
|
if not has_ocsp:
|
2014-10-09 15:57:40 +02:00
|
|
|
failures[lvl].append("consider enabling OCSP Stapling")
|
2014-10-11 06:34:07 +02:00
|
|
|
if results['serverside'] != 'True':
|
|
|
|
failures[lvl].append("enforce server side ordering")
|
2016-02-24 16:48:28 +01:00
|
|
|
return isold
|
2014-10-09 03:53:05 +02:00
|
|
|
|
2014-10-09 15:35:59 +02:00
|
|
|
# is_intermediate is similar to is_old but for intermediate configuration from
|
|
|
|
# https://wiki.mozilla.org/Security/Server_Side_TLS#Intermediate_compatibility_.28default.29
|
2014-10-09 03:53:05 +02:00
|
|
|
def is_intermediate(results):
|
2016-02-24 16:48:28 +01:00
|
|
|
logging.debug('entering intermediate evaluation')
|
2014-10-09 15:35:59 +02:00
|
|
|
lvl = 'intermediate'
|
2016-02-24 16:48:28 +01:00
|
|
|
isinter = True
|
2014-10-09 15:35:59 +02:00
|
|
|
has_tls1 = False
|
|
|
|
has_aes = False
|
2014-10-17 17:58:19 +02:00
|
|
|
has_pfs = True
|
2016-02-24 16:48:28 +01:00
|
|
|
has_sigalg = True
|
2014-10-09 15:35:59 +02:00
|
|
|
has_ocsp = True
|
|
|
|
all_proto = []
|
2014-10-09 03:53:05 +02:00
|
|
|
for conn in results['ciphersuite']:
|
2016-02-24 16:48:28 +01:00
|
|
|
logging.debug('testing connection %s' % conn)
|
|
|
|
if conn['cipher'] not in inter["ciphersuites"]:
|
2014-10-09 15:35:59 +02:00
|
|
|
logging.debug(conn['cipher'] + ' is not in the list of intermediate ciphers')
|
|
|
|
failures[lvl].append("remove cipher " + conn['cipher'])
|
2016-02-24 16:48:28 +01:00
|
|
|
isinter = False
|
2014-10-09 15:35:59 +02:00
|
|
|
if conn['cipher'] == 'AES128-SHA':
|
|
|
|
has_aes = True
|
|
|
|
for proto in conn['protocols']:
|
|
|
|
if proto not in all_proto:
|
|
|
|
all_proto.append(proto)
|
|
|
|
if 'TLSv1' in conn['protocols']:
|
|
|
|
has_tls1 = True
|
2016-02-24 16:48:28 +01:00
|
|
|
if conn['sigalg'][0] not in inter["certificate_signatures"]:
|
2014-10-09 15:35:59 +02:00
|
|
|
logging.debug(conn['sigalg'][0] + ' is a not an intermediate signature')
|
2016-02-24 16:48:28 +01:00
|
|
|
has_sigalg = False
|
2014-10-17 17:20:25 +02:00
|
|
|
if conn['pfs'] != 'None':
|
2016-02-24 16:48:28 +01:00
|
|
|
if not has_good_pfs(conn['pfs'], inter["dh_param_size"], inter["ecdh_param_size"], True):
|
2014-10-17 17:58:19 +02:00
|
|
|
logging.debug(conn['pfs']+ ' is not a good PFS parameter for the intermediate configuration')
|
|
|
|
has_pfs = False
|
2014-10-09 15:35:59 +02:00
|
|
|
if conn['ocsp_stapling'] == 'False':
|
|
|
|
has_ocsp = False
|
2016-02-24 16:48:28 +01:00
|
|
|
extra_proto = set(all_proto) - set(inter["tls_versions"])
|
2014-10-09 15:35:59 +02:00
|
|
|
for proto in extra_proto:
|
|
|
|
logging.debug("found protocol not wanted in the intermediate configuration:" + proto)
|
|
|
|
failures[lvl].append('disable ' + proto)
|
2016-02-24 16:48:28 +01:00
|
|
|
isinter = False
|
|
|
|
missing_proto = set(inter["tls_versions"]) - set(all_proto)
|
2014-10-09 15:35:59 +02:00
|
|
|
for proto in missing_proto:
|
|
|
|
logging.debug("missing protocol wanted in the intermediate configuration:" + proto)
|
2014-10-09 15:57:40 +02:00
|
|
|
failures[lvl].append('consider enabling ' + proto)
|
2014-10-09 15:35:59 +02:00
|
|
|
if not has_tls1:
|
|
|
|
logging.debug("TLSv1 is not supported and required by the old configuration")
|
2016-02-24 16:48:28 +01:00
|
|
|
isinter = False
|
2014-10-09 15:35:59 +02:00
|
|
|
if not has_aes:
|
|
|
|
logging.debug("AES128-SHA is not supported and required by the intermediate configuration")
|
|
|
|
failures[lvl].append("add cipher AES128-SHA")
|
2016-02-24 16:48:28 +01:00
|
|
|
isinter = False
|
|
|
|
if not has_sigalg:
|
|
|
|
failures[lvl].append("use a certificate signed with %s" % " or ".join(inter["certificate_signatures"]))
|
|
|
|
isinter = False
|
2014-10-17 17:58:19 +02:00
|
|
|
if not has_pfs:
|
2018-05-20 14:31:09 +02:00
|
|
|
failures[lvl].append("consider using DHE of at least 2048bits and ECC of 256bits and greater")
|
2014-10-09 15:35:59 +02:00
|
|
|
if not has_ocsp:
|
2014-10-09 15:57:40 +02:00
|
|
|
failures[lvl].append("consider enabling OCSP Stapling")
|
2014-10-11 06:34:07 +02:00
|
|
|
if results['serverside'] != 'True':
|
|
|
|
failures[lvl].append("enforce server side ordering")
|
2016-02-24 16:48:28 +01:00
|
|
|
return isinter
|
2014-10-09 03:53:05 +02:00
|
|
|
|
2014-10-09 15:35:59 +02:00
|
|
|
# is_modern is similar to is_old but for modern configuration from
|
|
|
|
# https://wiki.mozilla.org/Security/Server_Side_TLS#Modern_compatibility
|
2014-10-09 03:53:05 +02:00
|
|
|
def is_modern(results):
|
2016-02-24 16:48:28 +01:00
|
|
|
logging.debug('entering modern evaluation')
|
2014-10-09 15:35:59 +02:00
|
|
|
lvl = 'modern'
|
2016-02-24 16:48:28 +01:00
|
|
|
ismodern = True
|
2014-10-17 17:58:19 +02:00
|
|
|
has_pfs = True
|
2016-02-24 16:48:28 +01:00
|
|
|
has_sigalg = True
|
2014-10-09 15:35:59 +02:00
|
|
|
has_ocsp = True
|
|
|
|
all_proto = []
|
2014-10-09 03:53:05 +02:00
|
|
|
for conn in results['ciphersuite']:
|
2016-02-24 16:48:28 +01:00
|
|
|
logging.debug('testing connection %s' % conn)
|
|
|
|
if conn['cipher'] not in modern["ciphersuites"]:
|
2014-10-09 15:35:59 +02:00
|
|
|
logging.debug(conn['cipher'] + ' is not in the list of modern ciphers')
|
|
|
|
failures[lvl].append("remove cipher " + conn['cipher'])
|
2016-02-24 16:48:28 +01:00
|
|
|
ismodern = False
|
2014-10-09 15:35:59 +02:00
|
|
|
for proto in conn['protocols']:
|
|
|
|
if proto not in all_proto:
|
|
|
|
all_proto.append(proto)
|
2016-02-24 16:48:28 +01:00
|
|
|
if conn['sigalg'][0] not in modern["certificate_signatures"]:
|
2014-10-17 17:08:32 +02:00
|
|
|
logging.debug(conn['sigalg'][0] + ' is a not an modern signature')
|
2016-02-24 16:48:28 +01:00
|
|
|
has_sigalg = False
|
2014-10-17 17:20:25 +02:00
|
|
|
if conn['pfs'] != 'None':
|
2016-02-24 16:48:28 +01:00
|
|
|
if not has_good_pfs(conn['pfs'], modern["dh_param_size"], modern["ecdh_param_size"], True):
|
2014-10-17 17:58:19 +02:00
|
|
|
logging.debug(conn['pfs']+ ' is not a good PFS parameter for the modern configuration')
|
2016-02-24 16:48:28 +01:00
|
|
|
ismodern = False
|
2014-10-17 17:58:19 +02:00
|
|
|
has_pfs = False
|
2014-10-09 15:35:59 +02:00
|
|
|
if conn['ocsp_stapling'] == 'False':
|
|
|
|
has_ocsp = False
|
2016-02-24 16:48:28 +01:00
|
|
|
extra_proto = set(all_proto) - set(modern["tls_versions"])
|
2014-10-09 15:35:59 +02:00
|
|
|
for proto in extra_proto:
|
|
|
|
logging.debug("found protocol not wanted in the modern configuration:" + proto)
|
|
|
|
failures[lvl].append('disable ' + proto)
|
2016-02-24 16:48:28 +01:00
|
|
|
ismodern = False
|
|
|
|
missing_proto = set(modern["tls_versions"]) - set(all_proto)
|
2014-10-09 15:35:59 +02:00
|
|
|
for proto in missing_proto:
|
|
|
|
logging.debug("missing protocol wanted in the modern configuration:" + proto)
|
2014-10-09 15:57:40 +02:00
|
|
|
failures[lvl].append('consider enabling ' + proto)
|
2016-02-24 16:48:28 +01:00
|
|
|
if not has_sigalg:
|
|
|
|
failures[lvl].append("use a certificate signed with %s" % " or ".join(modern["certificate_signatures"]))
|
|
|
|
ismodern = False
|
2014-10-17 17:58:19 +02:00
|
|
|
if not has_pfs:
|
2018-05-20 14:31:09 +02:00
|
|
|
failures[lvl].append("use DHE of at least 2048bits and ECC of at 256bits and greater")
|
2016-02-24 16:48:28 +01:00
|
|
|
ismodern = False
|
2014-10-09 15:35:59 +02:00
|
|
|
if not has_ocsp:
|
2014-10-09 15:57:40 +02:00
|
|
|
failures[lvl].append("consider enabling OCSP Stapling")
|
2014-10-11 06:34:07 +02:00
|
|
|
if results['serverside'] != 'True':
|
|
|
|
failures[lvl].append("enforce server side ordering")
|
2016-02-24 16:48:28 +01:00
|
|
|
return ismodern
|
2014-10-09 03:53:05 +02:00
|
|
|
|
2014-10-11 02:30:27 +02:00
|
|
|
def is_ordered(results, ref_ciphersuite, lvl):
|
|
|
|
ordered = True
|
|
|
|
previous_pos = 0
|
|
|
|
# iterate through the list of ciphers returned by the target
|
|
|
|
for conn in results['ciphersuite']:
|
|
|
|
pos = 0
|
|
|
|
# compare against each cipher of the reference ciphersuite
|
|
|
|
for ref_cipher in ref_ciphersuite:
|
|
|
|
# if the target cipher matches the reference ciphersuite,
|
|
|
|
# look for its position against the reference and flag cipher
|
|
|
|
# that violate the reference ordering
|
|
|
|
if conn['cipher'] == ref_cipher:
|
|
|
|
logging.debug("{0} found in reference ciphersuite at position {1}".format(conn['cipher'], pos))
|
|
|
|
if pos < previous_pos:
|
|
|
|
failures[lvl].append("increase priority of {0} over {1}".format(conn['cipher'], ref_ciphersuite[previous_pos]))
|
|
|
|
ordered = False
|
|
|
|
# save current position
|
|
|
|
previous_pos = pos
|
|
|
|
pos += 1
|
|
|
|
if not ordered:
|
|
|
|
failures[lvl].append("fix ciphersuite ordering, use recommended " + lvl + " ciphersuite")
|
|
|
|
return ordered
|
2014-10-09 03:53:05 +02:00
|
|
|
|
2014-10-09 15:35:59 +02:00
|
|
|
def evaluate_all(results):
|
2014-10-12 01:37:08 +02:00
|
|
|
status = "obscure or unknown"
|
2014-10-09 03:53:05 +02:00
|
|
|
|
|
|
|
if len(results['ciphersuite']) == 0:
|
2014-10-12 01:37:08 +02:00
|
|
|
return "no"
|
2014-10-09 03:53:05 +02:00
|
|
|
|
2014-10-12 02:45:14 +02:00
|
|
|
if is_old(results):
|
|
|
|
status = "old"
|
2016-02-24 16:48:28 +01:00
|
|
|
if not is_ordered(results, old["ciphersuites"], "old"):
|
2014-10-12 02:45:14 +02:00
|
|
|
status = "old with bad ordering"
|
2014-10-09 03:53:05 +02:00
|
|
|
|
|
|
|
if is_intermediate(results):
|
2014-10-12 01:37:08 +02:00
|
|
|
status = "intermediate"
|
2016-02-24 16:48:28 +01:00
|
|
|
if not is_ordered(results, inter["ciphersuites"], "intermediate"):
|
2014-10-12 02:45:14 +02:00
|
|
|
status = "intermediate with bad ordering"
|
2014-10-09 03:53:05 +02:00
|
|
|
|
2014-10-12 02:45:14 +02:00
|
|
|
if is_modern(results):
|
|
|
|
status = "modern"
|
2016-02-24 16:48:28 +01:00
|
|
|
if not is_ordered(results, modern["ciphersuites"], "modern"):
|
2014-10-12 02:45:14 +02:00
|
|
|
status = "modern with bad ordering"
|
2014-10-09 03:53:05 +02:00
|
|
|
|
|
|
|
if is_fubar(results):
|
2014-10-12 01:37:08 +02:00
|
|
|
status = "bad"
|
2014-10-09 03:53:05 +02:00
|
|
|
|
|
|
|
return status
|
|
|
|
|
2014-12-23 14:51:50 +01:00
|
|
|
def process_results(data, level=None, do_json=False, do_nagios=False):
|
2016-02-24 16:48:28 +01:00
|
|
|
logging.debug('processing results on %s' % data)
|
2014-12-23 14:51:50 +01:00
|
|
|
exit_status = 0
|
2014-10-09 03:53:05 +02:00
|
|
|
results = dict()
|
2014-10-09 15:35:59 +02:00
|
|
|
# initialize the failures struct
|
|
|
|
global failures
|
2014-10-12 01:37:08 +02:00
|
|
|
json_output = dict()
|
2014-10-09 15:35:59 +02:00
|
|
|
failures = dict()
|
2014-10-11 01:07:31 +02:00
|
|
|
failures['fubar'] = []
|
2014-10-09 15:35:59 +02:00
|
|
|
failures['old'] = []
|
|
|
|
failures['intermediate'] = []
|
|
|
|
failures['modern'] = []
|
2014-10-12 01:37:08 +02:00
|
|
|
if not level:
|
|
|
|
level='none'
|
2014-10-09 03:53:05 +02:00
|
|
|
try:
|
|
|
|
results = json.loads(data)
|
2015-05-30 15:46:26 +02:00
|
|
|
except ValueError as e:
|
|
|
|
print("invalid json data: " + str(e))
|
2014-10-09 03:53:05 +02:00
|
|
|
try:
|
|
|
|
if results:
|
2014-10-12 01:37:08 +02:00
|
|
|
if do_json:
|
|
|
|
json_output['target'] = results['target']
|
|
|
|
d = datetime.utcnow()
|
|
|
|
json_output['utctimestamp'] = d.isoformat("T") + "Z"
|
|
|
|
json_output['level'] = evaluate_all(results)
|
|
|
|
json_output['target_level'] = level
|
|
|
|
json_output['compliance'] = False
|
2014-10-12 05:08:35 +02:00
|
|
|
if json_output['target_level'] in json_output['level']:
|
2014-10-12 01:37:08 +02:00
|
|
|
json_output['compliance'] = True
|
2014-10-12 02:52:18 +02:00
|
|
|
if operator:
|
|
|
|
json_output['operator'] = operator
|
2014-10-12 01:37:08 +02:00
|
|
|
else:
|
2014-10-17 17:58:05 +02:00
|
|
|
measured_lvl = evaluate_all(results)
|
|
|
|
print(results['target'] + " has " + measured_lvl + " ssl/tls")
|
|
|
|
if level != 'none':
|
|
|
|
if level in measured_lvl:
|
|
|
|
print("and complies with the '" + level + "' level")
|
|
|
|
else:
|
|
|
|
print("and DOES NOT comply with the '" + level + "' level")
|
2015-05-30 15:46:26 +02:00
|
|
|
except TypeError as e:
|
|
|
|
print("Error processing data: " + str(e))
|
2014-10-12 01:37:08 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
if do_json:
|
|
|
|
json_output['failures'] = deepcopy(failures)
|
2015-05-30 15:46:26 +02:00
|
|
|
print(json.dumps(json_output))
|
2014-10-12 01:37:08 +02:00
|
|
|
return True
|
2014-10-09 03:53:05 +02:00
|
|
|
|
2014-10-11 01:07:31 +02:00
|
|
|
if len(failures['fubar']) > 0:
|
2014-10-18 14:20:00 +02:00
|
|
|
print("\nThings that are bad:")
|
2014-10-11 01:07:31 +02:00
|
|
|
for failure in failures['fubar']:
|
|
|
|
print("* " + failure)
|
2014-12-23 14:51:50 +01:00
|
|
|
if do_nagios:
|
|
|
|
exit_status = 2
|
2014-10-11 01:07:31 +02:00
|
|
|
|
2014-10-09 15:35:59 +02:00
|
|
|
# print failures
|
2014-10-12 01:37:08 +02:00
|
|
|
if level != 'none':
|
2014-10-09 15:35:59 +02:00
|
|
|
if len(failures[level]) > 0:
|
2014-10-09 16:09:44 +02:00
|
|
|
print("\nChanges needed to match the " + level + " level:")
|
2014-10-09 15:35:59 +02:00
|
|
|
for failure in failures[level]:
|
|
|
|
print("* " + failure)
|
2014-12-23 14:51:50 +01:00
|
|
|
if do_nagios and exit_status < 2:
|
|
|
|
exit_status = 1
|
2014-10-09 15:35:59 +02:00
|
|
|
else:
|
|
|
|
for lvl in ['old', 'intermediate', 'modern']:
|
|
|
|
if len(failures[lvl]) > 0:
|
2014-10-09 16:09:44 +02:00
|
|
|
print("\nChanges needed to match the " + lvl + " level:")
|
2014-10-09 15:35:59 +02:00
|
|
|
for failure in failures[lvl]:
|
|
|
|
print("* " + failure)
|
2014-12-23 14:51:50 +01:00
|
|
|
if do_nagios and exit_status < 2:
|
|
|
|
exit_status = 1
|
|
|
|
return exit_status
|
2014-10-09 15:35:59 +02:00
|
|
|
|
2016-02-24 16:48:28 +01:00
|
|
|
def build_ciphers_lists():
|
|
|
|
sstlsurl = "https://statics.tls.security.mozilla.org/server-side-tls-conf.json"
|
|
|
|
conf = dict()
|
|
|
|
try:
|
|
|
|
raw = urllib2.urlopen(sstlsurl).read()
|
|
|
|
conf = json.loads(raw)
|
|
|
|
logging.debug('retrieving online server side tls recommendations from %s' % sstlsurl)
|
2016-02-29 14:21:04 +01:00
|
|
|
except urllib2.URLError:
|
|
|
|
with open('server-side-tls-conf.json', 'r') as f:
|
|
|
|
conf = json.load(f)
|
|
|
|
logging.debug('Error connecting to %s; using local archive of server side tls recommendations' % sstlsurl)
|
2016-02-24 16:48:28 +01:00
|
|
|
except:
|
|
|
|
print("failed to retrieve JSON configurations from %s" % sstlsurl)
|
|
|
|
sys.exit(23)
|
2014-10-11 00:00:10 +02:00
|
|
|
|
2016-02-24 16:48:28 +01:00
|
|
|
global old, inter, modern
|
|
|
|
old = conf["configurations"]["old"]
|
|
|
|
inter = conf["configurations"]["intermediate"]
|
|
|
|
modern = conf["configurations"]["modern"]
|
2014-10-09 15:35:59 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Analyze cipherscan results and provides guidelines to improve configuration.',
|
|
|
|
usage='\n* Analyze a single target, invokes cipherscan: $ ./analyze.py -t [target]' \
|
2016-02-24 16:52:18 +01:00
|
|
|
'\n* Evaluate json results passed through stdin: $ python analyze.py target_results.json' \
|
2016-02-24 16:48:28 +01:00
|
|
|
'\nexample: ./analyze.py -t mozilla.org',
|
2014-10-09 15:35:59 +02:00
|
|
|
epilog='Julien Vehent [:ulfr] - 2014')
|
|
|
|
parser.add_argument('-d', dest='debug', action='store_true',
|
|
|
|
help='debug output')
|
|
|
|
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
|
|
|
|
default=sys.stdin, help='cipherscan json results')
|
|
|
|
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
|
|
|
|
default=sys.stdout, help='json formatted analysis')
|
|
|
|
parser.add_argument('-l', dest='level',
|
|
|
|
help='target configuration level [old, intermediate, modern]')
|
|
|
|
parser.add_argument('-t', dest='target',
|
|
|
|
help='analyze a <target>, invokes cipherscan')
|
2014-10-11 00:56:44 +02:00
|
|
|
parser.add_argument('-o', dest='openssl',
|
|
|
|
help='path to openssl binary, if you don\'t like the default')
|
2014-10-12 01:37:08 +02:00
|
|
|
parser.add_argument('-j', dest='json', action='store_true',
|
|
|
|
help='output results in json format')
|
2014-10-12 02:52:18 +02:00
|
|
|
parser.add_argument('--ops', dest='operator',
|
|
|
|
help='optional name of the operator\'s team added into the JSON output (for database insertion)')
|
2014-12-23 14:51:50 +01:00
|
|
|
parser.add_argument('--nagios', dest='nagios', action='store_true',
|
|
|
|
help='use nagios-conformant exit codes')
|
2014-10-09 15:35:59 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2014-12-26 09:49:52 +01:00
|
|
|
global mypath
|
2014-12-24 11:26:24 +01:00
|
|
|
mypath = os.path.dirname(os.path.realpath(sys.argv[0]))
|
|
|
|
|
2014-10-09 15:35:59 +02:00
|
|
|
if args.debug:
|
|
|
|
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
|
|
|
|
else:
|
|
|
|
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
|
|
|
|
|
2014-10-12 02:52:18 +02:00
|
|
|
global operator
|
|
|
|
operator=''
|
|
|
|
if args.operator:
|
|
|
|
operator=args.operator
|
|
|
|
|
2016-02-24 16:48:28 +01:00
|
|
|
build_ciphers_lists()
|
2014-10-09 15:35:59 +02:00
|
|
|
|
|
|
|
if args.target:
|
2014-10-09 03:53:05 +02:00
|
|
|
# evaluate target specified as argument
|
2014-10-09 15:35:59 +02:00
|
|
|
logging.debug('Invoking cipherscan with target: ' + args.target)
|
2014-10-11 00:56:44 +02:00
|
|
|
data=''
|
|
|
|
if args.openssl:
|
2014-12-24 11:26:24 +01:00
|
|
|
data = subprocess.check_output([mypath + '/cipherscan', '-o', args.openssl, '-j', args.target])
|
2014-10-11 00:56:44 +02:00
|
|
|
else:
|
2014-12-24 11:26:24 +01:00
|
|
|
data = subprocess.check_output([mypath + '/cipherscan', '-j', args.target])
|
2015-08-23 17:31:04 +02:00
|
|
|
data = str_compat(data)
|
2015-05-30 15:46:26 +02:00
|
|
|
exit_status=process_results(str(data), args.level, args.json, args.nagios)
|
2014-10-09 03:53:05 +02:00
|
|
|
else:
|
2014-10-09 15:35:59 +02:00
|
|
|
if os.fstat(args.infile.fileno()).st_size < 2:
|
|
|
|
logging.error("invalid input file")
|
|
|
|
parser.print_help()
|
2014-12-23 14:51:50 +01:00
|
|
|
if args.nagios:
|
|
|
|
sys.exit(3)
|
|
|
|
else:
|
|
|
|
sys.exit(1)
|
2014-10-09 15:35:59 +02:00
|
|
|
data = args.infile.readline()
|
|
|
|
logging.debug('Evaluating results from stdin: ' + data)
|
2014-12-23 14:51:50 +01:00
|
|
|
exit_status=process_results(data, args.level, args.json, args.nagios)
|
|
|
|
sys.exit(exit_status)
|
2014-10-09 03:53:05 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|