Add argparse and Remove pprint
This commit is contained in:
parent
1dee91b82c
commit
5bbf5e7172
30
README.md
30
README.md
@ -13,14 +13,32 @@ You only need to installl pyOpenSSL:
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
`python ssl_checker.py host1[:port] [host2:port] [host3:port]...`
|
```bash
|
||||||
|
./ssl_checker.py -h
|
||||||
|
usage: ssl_checker.py -H [HOSTS [HOSTS ...]] [-j] [-h]
|
||||||
|
|
||||||
|
optional arguments:
|
||||||
|
-H [HOSTS [HOSTS ...]], --host [HOSTS [HOSTS ...]]
|
||||||
|
Hosts as input separated by space
|
||||||
|
-j, --json Enable JSON in the output
|
||||||
|
-h, --help Show this help message and exit
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Port is optional here. The script will use 443 if not specified.
|
Port is optional here. The script will use 443 if not specified.
|
||||||
|
|
||||||
|
`-j, --json` Use this if you want to only have the result in JSON
|
||||||
|
|
||||||
|
`-H, --host` Enter the hosts separated by space
|
||||||
|
|
||||||
|
`-h, --help` Shows the help and exit
|
||||||
|
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
narbeh@narbeh-xps:~/ssl-checker$ python ssl_checker.py test.com narbeh.org:443 archive.org facebook.com:443 twitter.com github.com google.com
|
narbeh@narbeh-xps:~/ssl-checker$ ./ssl_checker.py -H test.com narbeh.org:443 archive.org facebook.com:443 twitter.com github.com google.com
|
||||||
Analyzing 7 hosts:
|
Analyzing 7 hosts:
|
||||||
|
|
||||||
[+] test.com Expired: False
|
[+] test.com Expired: False
|
||||||
@ -33,3 +51,11 @@ Analyzing 7 hosts:
|
|||||||
|
|
||||||
5 successful and 2 failed
|
5 successful and 2 failed
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Example only with the `-j` argument which show the JSON only. Perfect for piping to another tool.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
narbeh@narbeh-xps:~/ssl-checker$ ./ssl_checker.py -j -H test.com narbeh.org:443
|
||||||
|
{'test.com': {'valid_till': '2020-01-24', 'valid_from': '2017-01-15', 'cert_alg': u'sha256WithRSAEncryption', 'cert_ver': 2, 'cert_sn': 73932709062103623902948514363737041075L, 'cert_exp': False, 'issuer_c': u'US', 'issuer_cn': u'Network Solutions DV Server CA 2', 'issuer_o': u'Network Solutions L.L.C.', 'validity_days': 1104, 'issuer_ou': None}, 'narbeh.org': {'valid_till': '2018-05-18', 'valid_from': '2018-02-17', 'cert_alg': u'sha256WithRSAEncryption', 'cert_ver': 2, 'cert_sn': 319510066429286596971677345373584681421772L, 'cert_exp': False, 'issuer_c': u'US', 'issuer_cn': u"Let's Encrypt Authority X3", 'issuer_o': u"Let's Encrypt", 'validity_days': 90, 'issuer_ou': None}}
|
||||||
|
```
|
||||||
43
ssl_checker.py
Normal file → Executable file
43
ssl_checker.py
Normal file → Executable file
@ -2,7 +2,7 @@
|
|||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from pprint import pprint
|
from argparse import ArgumentParser, SUPPRESS
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from ssl import PROTOCOL_TLSv1
|
from ssl import PROTOCOL_TLSv1
|
||||||
|
|
||||||
@ -66,11 +66,15 @@ def get_cert_info(cert):
|
|||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
def show_result(hosts):
|
def show_result(user_args):
|
||||||
"""Get the context."""
|
"""Get the context."""
|
||||||
context = {}
|
context = {}
|
||||||
failed_cnt = 0
|
failed_cnt = 0
|
||||||
|
hosts = user_args.hosts
|
||||||
|
|
||||||
|
if not user_args.json_true:
|
||||||
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)
|
||||||
|
|
||||||
@ -81,14 +85,20 @@ def show_result(hosts):
|
|||||||
try:
|
try:
|
||||||
cert = get_cert(host, port)
|
cert = get_cert(host, port)
|
||||||
context[host] = get_cert_info(cert)
|
context[host] = get_cert_info(cert)
|
||||||
|
if not user_args.json_true:
|
||||||
print('\t{}[+]{} {:<20s} Expired: {}'.format(Clr.GREEN, Clr.RST, host, context[host]['cert_exp']))
|
print('\t{}[+]{} {:<20s} Expired: {}'.format(Clr.GREEN, Clr.RST, host, context[host]['cert_exp']))
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
|
if not user_args.json_true:
|
||||||
print('\t{}[-]{} {:<20s} Failed: {}'.format(Clr.RED, Clr.RST, host, error))
|
print('\t{}[-]{} {:<20s} Failed: {}'.format(Clr.RED, Clr.RST, host, error))
|
||||||
|
|
||||||
failed_cnt += 1
|
failed_cnt += 1
|
||||||
|
|
||||||
|
if not user_args.json_true:
|
||||||
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))
|
||||||
|
|
||||||
pprint(context)
|
# Enable JSON output if -j argument specified
|
||||||
|
if user_args.json_true:
|
||||||
|
print(context)
|
||||||
|
|
||||||
|
|
||||||
def filter_hostname(host):
|
def filter_hostname(host):
|
||||||
@ -101,9 +111,28 @@ def filter_hostname(host):
|
|||||||
return host, port
|
return host, port
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def get_args():
|
||||||
if len(sys.argv) < 2:
|
"""Set argparse options."""
|
||||||
print('Usage: python {} host1 [host2] [host3] ...'.format(sys.argv[0]))
|
parser = ArgumentParser(prog='ssl_checker.py', add_help=False)
|
||||||
|
parser.add_argument("-H", "--host", dest="hosts", nargs='*', required=True,
|
||||||
|
help="Hosts as input separated by space")
|
||||||
|
parser.add_argument("-j", "--json", dest="json_true",
|
||||||
|
action="store_true", default=False,
|
||||||
|
help="Enable JSON in the output")
|
||||||
|
parser.add_argument("-h", "--help", default=SUPPRESS,
|
||||||
|
action='help',
|
||||||
|
help='Show this help message and exit')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# Checks hosts list
|
||||||
|
if isinstance(args.hosts, list):
|
||||||
|
if len(args.hosts) == 0:
|
||||||
|
parser.print_help()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
show_result(sys.argv[1:])
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
show_result(get_args())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user