-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathssltools-ip
executable file
·96 lines (79 loc) · 2.93 KB
/
ssltools-ip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
###########
# IMPORTS #
###########
import os
import sys
import re
import argparse
import asyncio
import aiohttp
from aiohttp import ClientSession, TCPConnector
from pypeln.task import TaskPool
####################
# GLOBAL VARIABLES #
####################
global key
#############
# FUNCTIONS #
#############
async def fetch(url, address, session):
try:
async with session.get(url + address) as response:
if response.status == 200:
html = await response.text()
san = re.search('<br>Subject Alternative Names :(.*?)<br>', html)
cn = re.search('<br>Common Name :(.*?)<br>', html)
hostnames = list()
if san:
hostnames.append(san.group(1))
if cn:
hostnames.append(cn.group(1))
for hostname in hostnames:
sys.stdout.write('%s,%s\n' % (address, hostname))
sys.stdout.flush()
except Exception as e:
sys.stderr.write(f"{url} => Unexpected exception: {e}\n")
async def run(addresses):
url = "https://www.ssltools.com/certificate_lookup/"
async with ClientSession(connector=TCPConnector(limit=None)) as session, TaskPool(1) as tasks:
for address in addresses:
await tasks.put(fetch(url, address, session))
########
# MAIN #
########
if __name__ == '__main__':
desc = 'Obtain observed Certificate SANs and CNs from SslTools.com for the supplied IP addresses and output results in CSV format.'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('file',
nargs='?',
type=argparse.FileType('r'),
action='store',
help='file containing a list of IP addresses split by a newline, otherwise read from STDIN',
metavar='FILE',
default=sys.stdin)
args = parser.parse_args()
# Check for DNSDB env
try:
key = os.environ['VIRUSTOTAL_KEY']
if not key:
sys.stderr.write("Error: VIRUSTOTAL_KEY environment variable is empty, unable to obtain server url, please set accordingly.\n")
exit(1)
except KeyError:
sys.stderr.write("Error: VIRUSTOTAL_KEY environment variable not set, unable to obtain server url, please set accordingly.\n")
exit(1)
try:
addresses = [line.strip() for line in args.file if len(line.strip())>0 and line[0] != '#']
except KeyboardInterrupt:
exit()
# remove duplicates and sort
addresses = sorted(set(addresses))
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(run(addresses))
except KeyboardInterrupt:
sys.stderr.write("\nCaught keyboard interrupt, cleaning up...\n")
asyncio.gather(*asyncio.Task.all_tasks()).cancel()
loop.stop()
finally:
loop.close()