-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdocTestssl.py
183 lines (171 loc) · 8.32 KB
/
docTestssl.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python3
# Import testssl.sh CSV to ELasticSearch
from elasticsearch_dsl import DocType, Object, Date, String, Integer, Short, Boolean
from datetime import datetime
from tzlocal import get_localzone
import csv
import re
import pprint # for debugging purposes only
pp = pprint.PrettyPrinter(indent=4)
tz = get_localzone()
reDefaultFilename = re.compile("(?:^|/)(?P<ip>\d+\.\d+\.\d+\.\d+)(:(?P<port>\d+))?-(?P<datetime>\d{8}-\d{4})\.csv$")
reProtocol = re.compile("^(?:sslv\\d|tls\\d(?:_\\d)?)$")
reCipherTests = re.compile("^std_(.*)$")
reIpHostColumn = re.compile("^(.*)/(.*)$")
reCipherColumnName = re.compile("^cipher_")
reCipherDetails = re.compile("^\\S+\\s+(\\S+)")
reCipherTests = re.compile("^std_(.*)$")
reDefaultProtocol = re.compile("^Default protocol (\\S+)")
reDefaultCipher = re.compile("^Default cipher: (.*?)(?:$|[,\\s])")
reKeySize = re.compile("Server Keys (\\d+) bits")
reSignAlgorithm = re.compile("Signature Algorithm: (.*)\\s\\(")
reFPMD5 = re.compile("MD5 (\\S+)")
reFPSHA1 = re.compile("SHA1 (\\S+)")
reFPSHA256 = re.compile("SHA256 (\\S+)")
reCN = re.compile("^(.*?)[\\s\\(]")
reSAN = re.compile(": (.*)$")
reIssuer = re.compile("'issuer= (.*?)' \\(")
reExpiration = re.compile("--> (.*)\\)")
reOCSPURI = re.compile(" : (?!--)(.*)")
reOffers = re.compile("(?<!not )offered")
reNotOffered = re.compile("not offered")
reOk = re.compile("\\(OK\\)")
reYes = re.compile("yes", re.IGNORECASE)
reVulnerable = re.compile("\\(NOT ok\\)", re.IGNORECASE)
class DocTestSSLResult(DocType):
class Meta:
doc_type = "TestSSLResult"
source = String(fields={'raw': String(index='not_analyzed')})
result = Boolean()
timestamp = Date()
ip = String(index='not_analyzed')
hostname = String(index='not_analyzed')
port = Integer()
svcid = String(index='not_analyzed')
protocols = String(index='not_analyzed', multi=True)
ciphers = String(multi=True, fields={'raw': String(index='not_analyzed')})
ciphertests = String(index='not_analyzed', multi=True)
serverpref = Object(
properties = {
"cipher_order": Boolean(),
"protocol": String(index='not_analyzed'),
"cipher": String(fields={'raw': String(index='not_analyzed')})
})
cert = Object(
properties = {
"keysize": Short(),
"signalgo": String(fields={'raw': String(index='not_analyzed')}),
"md5_fingerprint": String(index='not_analyzed'),
"sha1_fingerprint": String(index='not_analyzed'),
"sha256_fingerprint": String(index='not_analyzed'),
"cn": String(fields={'raw': String(index='not_analyzed')}),
"san": String(multi=True, fields={'raw': String(index='not_analyzed')}),
"issuer": String(fields={'raw': String(index='not_analyzed')}),
"ev": Boolean(),
"expiration": Date(),
"ocsp_uri": String(fields={'raw': String(index='not_analyzed')}),
"ocsp_stapling": Boolean(),
})
vulnerabilities = String(index='not_analyzed', multi=True)
def parseCSVLine(self, line):
if line['id'] == "id":
return
if not self.ip or not self.hostname or not self.port: # host, ip and port
m = reIpHostColumn.search(line['host'])
if m:
self.hostname, self.ip = m.groups()
self.port = int(line['port'])
if reProtocol.search(line['id']) and reOffers.search(line['finding']): # protocols
self.result = True
m = reProtocol.search(line['id'])
if m:
self.protocols.append(line['id'].upper())
elif reCipherColumnName.search(line['id']): # ciphers
m = reCipherDetails.search(line['finding'])
if m:
self.ciphers.append(m.group(1))
elif reCipherTests.search(line['id']) and reVulnerable.search(line['finding']): # cipher tests
m = reCipherTests.search(line['id'])
if m:
self.ciphertests.append(m.group(1))
elif line['id'] == "order": # server prefers cipher
self.serverpref.cipher_order = bool(reOk.search(line['finding']))
elif line['id'] == "order_proto": # preferred protocol
m = reDefaultProtocol.search(line['finding'])
if m:
self.serverpref.protocol = m.group(1)
elif line['id'] == "order_cipher": # preferred cipher
m = reDefaultCipher.search(line['finding'])
if m:
self.serverpref.cipher = m.group(1)
elif line['id'] == "key_size": # certificate key size
m = reKeySize.search(line['finding'])
if m:
self.cert.keysize = int(m.group(1))
elif line['id'] == "algorithm": # certificate sign algorithm
m = reSignAlgorithm.search(line['finding'])
if m:
self.cert.signalgo = m.group(1)
elif line['id'] == "fingerprint": # certificate fingerprints
m = reFPMD5.search(line['finding'])
if m:
self.cert.md5_fingerprint = m.group(1)
m = reFPSHA1.search(line['finding'])
if m:
self.cert.sha1_fingerprint = m.group(1)
m = reFPSHA256.search(line['finding'])
if m:
self.cert.sha256_fingerprint = m.group(1)
elif line['id'] == "cn": # certificate CN
m = reCN.search(line['finding'])
if m:
self.cert.cn = m.group(1)
elif line['id'] == "san": # certificate SAN
m = reSAN.search(line['finding'])
if m:
sans = m.group(1)
for san in sans.split(" "):
if san != "--":
self.cert.san.append(san)
elif line['id'] == "issuer": # certificate issuer
m = reIssuer.search(line['finding'])
if m:
self.cert.issuer = m.group(1)
elif line['id'] == "ev": # certificate extended validation
self.cert.ev = bool(reYes.search(line['finding']))
elif line['id'] == "expiration": # certificate expiration
m = reExpiration.search(line['finding'])
if m:
unparsedDate = m.group(1)
self.cert.expiration = datetime.strptime(unparsedDate, "%Y-%m-%d %H:%M %z")
elif line['id'] == "ocsp_uri": # certificate OCSP URI
m = reOCSPURI.search(line['finding'])
if m:
self.cert.ocsp_uri = m.group(1)
else:
self.cert.ocsp_uri = "-"
elif line['id'] == "ocsp_stapling": # certificate OCSP stapling
self.cert.ocsp_stapling = not bool(reNotOffered.search(line['finding']))
elif line['id'] in ("heartbleed", "ccs", "secure_renego", "sec_client_renego", "crime", "breach", "poodle_ssl", "fallback_scsv", "freak", "DROWN", "logjam", "beast", "rc4") and reVulnerable.search(line['finding']):
self.vulnerabilities.append(line['id'].upper())
def parseCSV(self, csvfile):
if self.source:
m = reDefaultFilename.search(self.source)
if m:
self.ip = m.group('ip')
self.port = int(m.group('port') or 0)
self.timestamp = datetime.strptime(m.group('datetime'), "%Y%m%d-%H%M")
csvReader = csv.DictReader(csvfile, fieldnames=("id", "host", "port", "severity", "finding"), delimiter=',', quotechar='"')
for line in csvReader:
self.parseCSVLine(line)
def save(self, **kwargs):
if not self.timestamp:
self.timestamp = datetime.now(tz)
if not self.port:
raise ValueError("Empty scan result")
self.svcid = "%s:%d" % (self.ip, int(self.port) or 0)
if not self.result:
self.result = False
if 'debug' in kwargs and kwargs['debug']:
pp.pprint(self.to_dict())
return super().save()