2018-04-17 18:10:44 +04:30
|
|
|
#!/usr/bin/env python
|
|
|
|
|
import socket
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
from datetime import datetime
|
2018-04-18 16:32:41 +04:30
|
|
|
from ssl import PROTOCOL_TLSv1
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from OpenSSL import SSL
|
|
|
|
|
except ImportError:
|
|
|
|
|
print('Required module does not exist. Install: pip install pyopenssl')
|
|
|
|
|
sys.exit(1)
|
2018-04-17 18:10:44 +04:30
|
|
|
|
|
|
|
|
|
2018-04-17 19:43:46 +04:30
|
|
|
class TextColor:
|
|
|
|
|
"""Text colors."""
|
|
|
|
|
|
|
|
|
|
RED = '\033[31m'
|
|
|
|
|
GREEN = '\033[32m'
|
|
|
|
|
YELLOW = '\033[33m'
|
|
|
|
|
RESET = '\033[39m'
|
|
|
|
|
|
|
|
|
|
|
2018-04-17 20:43:39 +04:30
|
|
|
def get_cert(host, port):
|
2018-04-17 18:10:44 +04:30
|
|
|
"""Connection to the host."""
|
2018-04-18 16:32:41 +04:30
|
|
|
osobj = SSL.Context(PROTOCOL_TLSv1)
|
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2018-04-17 18:10:44 +04:30
|
|
|
|
|
|
|
|
try:
|
2018-04-17 20:43:39 +04:30
|
|
|
sock.connect((host, int(port)))
|
2018-04-17 18:10:44 +04:30
|
|
|
except Exception as e:
|
2018-04-17 19:43:46 +04:30
|
|
|
print('\t{}[-]{} {} failed: {}'.format(TextColor.RED, TextColor.RESET, host, e))
|
2018-04-17 18:10:44 +04:30
|
|
|
return None
|
|
|
|
|
|
2018-04-18 16:32:41 +04:30
|
|
|
oscon = SSL.Connection(osobj, sock)
|
|
|
|
|
oscon.set_tlsext_host_name(host.encode())
|
|
|
|
|
oscon.set_connect_state()
|
|
|
|
|
try:
|
|
|
|
|
oscon.do_handshake()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print('\t{}[-]{} {} failed: {}'.format(TextColor.RED, TextColor.RESET, host, e))
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
print('\t{}[+]{} {}'.format(TextColor.GREEN, TextColor.RESET, host))
|
|
|
|
|
cert = oscon.get_peer_certificate()
|
2018-04-17 18:10:44 +04:30
|
|
|
sock.close()
|
2018-04-18 16:32:41 +04:30
|
|
|
|
2018-04-17 18:10:44 +04:30
|
|
|
return cert
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_cert_info(cert):
|
|
|
|
|
"""Get all the information about cert and create a JSON file."""
|
|
|
|
|
context = {}
|
|
|
|
|
|
2018-04-18 16:32:41 +04:30
|
|
|
context['issuer_c'] = cert.get_issuer().countryName
|
|
|
|
|
context['issuer_o'] = cert.get_issuer().organizationName
|
|
|
|
|
context['issuer_ou'] = cert.get_issuer().organizationalUnitName
|
|
|
|
|
context['issuer_cn'] = cert.get_issuer().commonName
|
|
|
|
|
context['cert_sn'] = cert.get_serial_number()
|
|
|
|
|
context['cert_alg'] = cert.get_signature_algorithm().decode()
|
|
|
|
|
context['cert_ver'] = cert.get_version()
|
|
|
|
|
context['cert_exp'] = cert.has_expired()
|
2018-04-17 18:10:44 +04:30
|
|
|
|
|
|
|
|
# Valid from
|
2018-04-18 16:32:41 +04:30
|
|
|
valid_from = datetime.strptime(cert.get_notBefore().decode('ascii'),
|
|
|
|
|
'%Y%m%d%H%M%SZ')
|
2018-04-17 18:10:44 +04:30
|
|
|
context['valid_from'] = valid_from.strftime('%Y-%m-%d')
|
|
|
|
|
|
2018-04-18 16:32:41 +04:30
|
|
|
# Valid till
|
|
|
|
|
valid_till = datetime.strptime(cert.get_notAfter().decode('ascii'),
|
|
|
|
|
'%Y%m%d%H%M%SZ')
|
2018-04-17 18:10:44 +04:30
|
|
|
context['valid_till'] = valid_till.strftime('%Y-%m-%d')
|
2018-04-18 14:11:46 +04:30
|
|
|
|
|
|
|
|
# Validity days
|
|
|
|
|
context['validity_days'] = (valid_till - valid_from).days
|
|
|
|
|
|
2018-04-17 18:10:44 +04:30
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def show_result(hosts):
|
|
|
|
|
"""Get the context."""
|
2018-04-17 20:43:39 +04:30
|
|
|
context= {}
|
|
|
|
|
failed_cnt, total_cnt = 0, 0
|
2018-04-17 19:43:46 +04:30
|
|
|
print('Analyzing {} hosts:\n'.format(len(hosts)))
|
2018-04-17 18:10:44 +04:30
|
|
|
for host in hosts:
|
2018-04-17 20:43:39 +04:30
|
|
|
host, port = filter_hostname(host)
|
2018-04-18 16:39:40 +04:30
|
|
|
|
|
|
|
|
# Check duplication
|
|
|
|
|
if host in context.keys():
|
|
|
|
|
continue
|
|
|
|
|
|
2018-04-17 20:43:39 +04:30
|
|
|
cert = get_cert(host, port)
|
2018-04-17 18:10:44 +04:30
|
|
|
if cert:
|
|
|
|
|
context[host] = get_cert_info(cert)
|
2018-04-17 19:59:43 +04:30
|
|
|
else:
|
|
|
|
|
failed_cnt += 1
|
|
|
|
|
|
2018-04-18 16:32:41 +04:30
|
|
|
print('\n{} successful and {} failed.\n'.format(len(hosts) - failed_cnt, failed_cnt))
|
2018-04-17 18:10:44 +04:30
|
|
|
|
|
|
|
|
print(context)
|
|
|
|
|
|
|
|
|
|
|
2018-04-17 20:43:39 +04:30
|
|
|
def filter_hostname(host):
|
|
|
|
|
"""Remove unused characters and split by address and port."""
|
|
|
|
|
host = host.replace('http://', '').replace('https://', '').replace('/', '')
|
|
|
|
|
port = 443
|
|
|
|
|
if ':' in host:
|
|
|
|
|
host, port = host.split(':')
|
2018-04-18 14:11:46 +04:30
|
|
|
|
2018-04-17 20:43:39 +04:30
|
|
|
return host, port
|
2018-04-17 18:10:44 +04:30
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
if len(sys.argv) < 2:
|
2018-04-18 09:47:05 +04:30
|
|
|
print('Usage: python {} host1 [host2] [host3] ...'.format(sys.argv[0]))
|
2018-04-17 18:14:40 +04:30
|
|
|
sys.exit(0)
|
2018-04-17 18:10:44 +04:30
|
|
|
|
|
|
|
|
show_result(sys.argv[1:])
|