2018-04-17 18:10:44 +04:30
|
|
|
#!/usr/bin/env python
|
|
|
|
|
import socket
|
|
|
|
|
import sys
|
|
|
|
|
|
2018-04-18 14:11:46 +04:30
|
|
|
from ssl import create_default_context
|
2018-04-17 18:10:44 +04:30
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
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 14:11:46 +04:30
|
|
|
sslctx = create_default_context()
|
|
|
|
|
sock = sslctx.wrap_socket(socket.socket(), server_hostname=host)
|
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 19:59:43 +04:30
|
|
|
print('\t{}[+]{} {}'.format(TextColor.GREEN, TextColor.RESET, host))
|
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 14:11:46 +04:30
|
|
|
cert = sock.getpeercert()
|
2018-04-17 18:10:44 +04:30
|
|
|
sock.close()
|
|
|
|
|
return cert
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_cert_info(cert):
|
|
|
|
|
"""Get all the information about cert and create a JSON file."""
|
|
|
|
|
context = {}
|
|
|
|
|
|
2018-04-18 14:11:46 +04:30
|
|
|
issued_to = dict(x[0] for x in cert['subject'])
|
|
|
|
|
issued_by = dict(x[0] for x in cert['issuer'])
|
|
|
|
|
|
|
|
|
|
context['issuer_c'] = issued_by['countryName']
|
|
|
|
|
context['issuer_o'] = issued_by['organizationName']
|
|
|
|
|
context['issuer_cn'] = issued_by['commonName']
|
|
|
|
|
context['issued_to'] = issued_to['commonName']
|
|
|
|
|
context['cert_sn'] = cert['serialNumber']
|
|
|
|
|
context['cert_ver'] = cert['version']
|
2018-04-17 18:10:44 +04:30
|
|
|
|
|
|
|
|
# Valid from
|
2018-04-18 14:11:46 +04:30
|
|
|
valid_from = datetime.strptime(cert['notBefore'], '%b %d %H:%M:%S %Y %Z')
|
2018-04-17 18:10:44 +04:30
|
|
|
context['valid_from'] = valid_from.strftime('%Y-%m-%d')
|
|
|
|
|
|
|
|
|
|
# Vali till
|
2018-04-18 14:11:46 +04:30
|
|
|
valid_till = datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z')
|
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
|
|
|
|
|
|
|
|
|
|
# Expiry check
|
|
|
|
|
context['expired'] = False if valid_till >= datetime.now() else True
|
|
|
|
|
|
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)
|
|
|
|
|
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 14:18:10 +04:30
|
|
|
print('\n{} successful and {} failed.'.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:])
|