Add HTML Export

This commit is contained in:
Narbeh Arakil 2020-07-20 18:53:36 +04:30
parent b2c9f196b5
commit bca0bcb2e1
2 changed files with 20 additions and 10 deletions

View File

@ -1 +1,2 @@
pyopenssl pyopenssl
json2html

View File

@ -1,18 +1,19 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import socket import socket
import sys import sys
import json
from argparse import ArgumentParser, SUPPRESS from argparse import ArgumentParser, SUPPRESS
from datetime import datetime from datetime import datetime
from ssl import PROTOCOL_TLSv1 from ssl import PROTOCOL_TLSv1
from time import sleep from time import sleep
from csv import DictWriter from csv import DictWriter
import json
try: try:
from OpenSSL import SSL from OpenSSL import SSL
from json2html import *
except ImportError: except ImportError:
print('Required module does not exist. Install: pip install pyopenssl') print('Please install required modules: pip install -r requirements.txt')
sys.exit(1) sys.exit(1)
@ -60,7 +61,6 @@ class SSLChecker:
return cert return cert
def border_msg(self, message): def border_msg(self, message):
"""Print the message in the box.""" """Print the message in the box."""
row = len(message) row = len(message)
@ -68,7 +68,6 @@ class SSLChecker:
result = h + '\n' "|" + message + "|"'\n' + h result = h + '\n' "|" + message + "|"'\n' + h
print(result) print(result)
def analyze_ssl(self, host, context, user_args): def analyze_ssl(self, host, context, user_args):
"""Analyze the security of the SSL certificate.""" """Analyze the security of the SSL certificate."""
try: try:
@ -114,7 +113,6 @@ class SSLChecker:
return context return context
def get_cert_sans(self, x509cert): def get_cert_sans(self, x509cert):
""" """
Get Subject Alt Names from Certificate. Shameless taken from stack overflow: Get Subject Alt Names from Certificate. Shameless taken from stack overflow:
@ -130,7 +128,6 @@ class SSLChecker:
san = san.replace(',', ';') san = san.replace(',', ';')
return san return san
def get_cert_info(self, host, cert): def get_cert_info(self, host, cert):
"""Get all the information about cert and create a JSON file.""" """Get all the information about cert and create a JSON file."""
context = {} context = {}
@ -184,7 +181,6 @@ class SSLChecker:
return context return context
def print_status(self, host, context, analyze=False): def print_status(self, host, context, analyze=False):
"""Print all the usefull info about host.""" """Print all the usefull info about host."""
print('\t{}[+]{} {}\n\t{}'.format(Clr.GREEN, Clr.RST, host, '-' * (len(host) + 5))) print('\t{}[+]{} {}\n\t{}'.format(Clr.GREEN, Clr.RST, host, '-' * (len(host) + 5)))
@ -217,7 +213,6 @@ class SSLChecker:
print('\n') print('\n')
def show_result(self, user_args): def show_result(self, user_args):
"""Get the context.""" """Get the context."""
context = {} context = {}
@ -275,6 +270,10 @@ class SSLChecker:
if user_args.csv_enabled: if user_args.csv_enabled:
self.export_csv(context, user_args.csv_enabled, user_args) self.export_csv(context, user_args.csv_enabled, user_args)
# HTML export if -x/--html is specified
if user_args.html_true:
self.export_html(context)
# Enable JSON output if -j/--json argument specified # Enable JSON output if -j/--json argument specified
if user_args.json_true: if user_args.json_true:
print(json.dumps(context)) print(json.dumps(context))
@ -284,7 +283,6 @@ class SSLChecker:
with open(host + '.json', 'w', encoding='UTF-8') as fp: with open(host + '.json', 'w', encoding='UTF-8') as fp:
fp.write(json.dumps(context[host])) fp.write(json.dumps(context[host]))
def export_csv(self, context, filename, user_args): def export_csv(self, context, filename, user_args):
"""Export all context results to CSV file.""" """Export all context results to CSV file."""
# prepend dict keys to write column headers # prepend dict keys to write column headers
@ -297,6 +295,14 @@ class SSLChecker:
for host in context.keys(): for host in context.keys():
csv_writer.writerow(context[host]) csv_writer.writerow(context[host])
def export_html(self, context):
"""Export JSON to HTML."""
html = json2html.convert(json=context)
file_name = datetime.strftime(datetime.now(), '%Y_%m_%d_%H_%M_%S')
with open('{}.html'.format(file_name), 'w') as html_file:
html_file.write(html)
return
def filter_hostname(self, host): def filter_hostname(self, host):
"""Remove unused characters and split by address and port.""" """Remove unused characters and split by address and port."""
@ -307,7 +313,6 @@ class SSLChecker:
return host, port return host, port
def get_args(self): def get_args(self):
"""Set argparse options.""" """Set argparse options."""
parser = ArgumentParser(prog='ssl_checker.py', add_help=False, parser = ArgumentParser(prog='ssl_checker.py', add_help=False,
@ -330,6 +335,9 @@ class SSLChecker:
parser.add_argument('-S', '--summary', dest='summary_true', parser.add_argument('-S', '--summary', dest='summary_true',
action='store_true', default=False, action='store_true', default=False,
help='Enable summary output only') help='Enable summary output only')
parser.add_argument('-x', '--html', dest='html_true',
action='store_true', default=False,
help='Enable HTML file export')
parser.add_argument('-J', '--json-save', dest='json_save_true', parser.add_argument('-J', '--json-save', dest='json_save_true',
action='store_true', default=False, action='store_true', default=False,
help='Enable JSON export individually per host') help='Enable JSON export individually per host')
@ -358,6 +366,7 @@ class SSLChecker:
return args return args
if __name__ == '__main__': if __name__ == '__main__':
SSLCheckerObject = SSLChecker() SSLCheckerObject = SSLChecker()
SSLCheckerObject.show_result(SSLCheckerObject.get_args()) SSLCheckerObject.show_result(SSLCheckerObject.get_args())