Refactor Code

This commit is contained in:
Narbeh 2018-04-19 11:20:32 +04:30
parent 7aee72ac09
commit e013620694

View File

@ -25,23 +25,11 @@ def get_cert(host, port):
"""Connection to the host.""" """Connection to the host."""
osobj = SSL.Context(PROTOCOL_TLSv1) osobj = SSL.Context(PROTOCOL_TLSv1)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, int(port))) sock.connect((host, int(port)))
except Exception as e:
print('\t{}[-]{} {:<20s} failed: {}'.format(Clr.RED, Clr.RST, host, e))
return None
oscon = SSL.Connection(osobj, sock) oscon = SSL.Connection(osobj, sock)
oscon.set_tlsext_host_name(host.encode()) oscon.set_tlsext_host_name(host.encode())
oscon.set_connect_state() oscon.set_connect_state()
try:
oscon.do_handshake() oscon.do_handshake()
except Exception as e:
print('\t{}[-]{} {:<20s} failed: {}'.format(Clr.RED, Clr.RST, host, e))
return None
print('\t{}[+]{} {}'.format(Clr.GREEN, Clr.RST, host))
cert = oscon.get_peer_certificate() cert = oscon.get_peer_certificate()
sock.close() sock.close()
@ -79,8 +67,8 @@ def get_cert_info(cert):
def show_result(hosts): def show_result(hosts):
"""Get the context.""" """Get the context."""
context= {} context = {}
failed_cnt, total_cnt = 0, 0 failed_cnt = 0
print('Analyzing {} hosts:\n'.format(len(hosts))) print('Analyzing {} hosts:\n'.format(len(hosts)))
for host in hosts: for host in hosts:
host, port = filter_hostname(host) host, port = filter_hostname(host)
@ -89,13 +77,16 @@ def show_result(hosts):
if host in context.keys(): if host in context.keys():
continue continue
try:
cert = get_cert(host, port) cert = get_cert(host, port)
if cert:
context[host] = get_cert_info(cert) context[host] = get_cert_info(cert)
else: print('\t{}[+]{} {}'.format(Clr.GREEN, Clr.RST, host))
except Exception as error:
print('\t{}[-]{} {:<20s} failed: {}'.format(Clr.RED, Clr.RST, host, error))
failed_cnt += 1 failed_cnt += 1
print('\n{} successful and {} failed.\n'.format(len(hosts) - failed_cnt, failed_cnt))
print('\n{} successful and {} failed\n'.format(len(hosts) - failed_cnt, failed_cnt))
#print(context) #print(context)