-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself-signed-cert-check.py
executable file
·341 lines (243 loc) · 10.2 KB
/
self-signed-cert-check.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python
# Author: Chuck King
# Date: 2013-09-30
# Reason: Take a list of domains and exclusions and actively poll the
# domains (not the exclusions) to harvest their certificate, then use
# OpenSSL to compare the Issuer Organization Name to the Subject
# Organization Name, declaring matches as (non-deterministice)
# self-signed certs. Cache results to speed up the process on
# subsequent runs. This script is only a proof of concept and hasn't
# been completed. It still needs things like:
# - convert to use the python OpenSSL module
# - add default list, exclusion, cache files from home dir if not
# provided on command line
# - expire cache entries after some optional time period
### License ###
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
### License ###
import datetime
import optparse
import os
from os.path import expanduser
import re
import ssl
import subprocess
import sys
import tempfile
global exclusions
exclusions = {}
global cache
cache = {}
class Exclusion(object):
"""Simple container for exlusion entry"""
def __init__(self, domain, port=443, comment=None):
self.domain = domain
self.port = port
self.comment = comment
def __str__(self):
if self.comment:
return "%s|%d#%s" % (self.domain, self.port, self.comment)
else:
return "%s|%d" % (self.domain, self.port)
class Cache(object):
"""Simple container for cache entry"""
def __init__(self, domain, port, poll_date, results_text):
self.domain = domain
self.port = port
self.poll_date = poll_date
self.results_text = results_text
def __str__(self):
return "%s|%d|%s|%s" % (self.domain, self.port, self.poll_date, self.results_text)
def get_server_cert(addr_tuple):
"""Example addr_tuple = ('facebook.com', 443) """
try:
return ssl.get_server_certificate(addr_tuple, ssl_version=ssl.PROTOCOL_SSLv23, ca_certs=None)
except Exception as e:
print "[+]: Could not connect to [%s] using SSL v2 or V3" % addr_tuple[0]
print e
return None
def process_cert(pemcert, addr):
""" Decode with OpenSSL, then check for matching Issuer/Subject O values"""
global cache
msg = ''
f = tempfile.NamedTemporaryFile(delete=False)
cert_file_name = f.name
f.write(pemcert)
f.close()
try:
cert_text = subprocess.check_output(["openssl", "x509", "-text", "-noout",
"-in", cert_file_name])
except Exception as e:
print "[+]: Problem calling openssl for [%s] certificate" % addr_tuple[0]
print e
return
finally:
os.unlink(cert_file_name)
issuer = re.search(r'Issuer:.*(O=[^,]*)', cert_text)
subject = re.search(r'Subject:.*(O=[^,]*)', cert_text)
if not issuer or not subject:
msg = "[SSC]: Domain [%s] presumed self-signed - lack of Issuer/Subject O's" % addr[0]
if issuer and subject:
if issuer.group(1) != subject.group(1):
msg = "[-]: Domain [%s], Issuer: %s, Subject: %s" % (addr[0], issuer.group(1), subject.group(1))
if issuer.group(1) == subject.group(1):
msg = "[SSC]: Domain [%s] matching Issuer & Subject 'O' of %s" % (addr[0], issuer.group(1))
if msg == '':
print "[+]: Problem processing [%s] certificate" % addr_tuple[0]
else:
print msg
# add this record to the cache
mykey = "%s|%d" % (addr[0], addr[1])
mydate = datetime.date.today().isoformat()
cache[mykey] = Cache(domain=addr[0], port=addr[1], poll_date=mydate, results_text=msg)
def processOptions():
""" process commandline options """
usage = """usage: ./%prog [options]
use -h for help / option descriptions
"""
parser = optparse.OptionParser(usage)
parser.add_option("-i", "--in-file", dest="infile", help="""Path to domains-to-process file. Format is domain-name.tld|server-port, one entry per line. Example: yahoo.com|443""")
parser.add_option("-c", "--cache-file", dest="cachefile", help="""Path to processed domains cache file. Format is domain-name|server-port|date-polled|results text. File can be empty to start.""")
parser.add_option("-e", "--exclude-file", dest="excludefile", help="""Path to domain exclusion file. This file includes names that will not be checked and is manually maintained to exclude certain domains. Format is domain-name.tld|port#user comment. Example: google.com|port # google signs their own certs.""")
(options, args) = parser.parse_args()
if not options.infile:
print "[+] -i | --in-file required. Ex: --in-file=domains-to-check.txt"
sys.exit(1)
else:
# make sure the file exists
if not os.path.isfile(options.infile):
print "[+] --in-file is not a file: %s" % (options.infile)
sys.exit(1)
if not options.cachefile:
print "[+] -c | --cache-file required. Ex: --cache-file=self-signed-cert-check.cache"
sys.exit(1)
else:
# make sure the file exists
if not os.path.isfile(options.cachefile):
print "[+] --cache-file is not a file: %s" % (options.cachefile)
sys.exit(1)
if options.excludefile:
# make sure the file exists
if not os.path.isfile(options.excludefile):
print "[+] --exclude-file is not a file: %s" % (options.excludefile)
sys.exit(1)
return (options, args)
def load_exclusions():
# Exclusion format: domain-name.tld|optional port # optional user comment
global exclusions
global options
with open(options.excludefile, 'r') as f:
for line in f:
# track for error reporting
original_line = line
# skip comments and blank lines
if line.startswith('#') or line.strip() == '':
continue
comment = ''
# extract any embedded comments and reset line value
if '#' in line:
line, comment = [x.strip() for x in line.split('#')]
# if there's a port listed
if '|' in line:
domain, port = [x.strip() for x in line.split('|')]
if port == '':
port = 443
else:
port = int(port)
else:
domain = line.strip()
port = 443
if domain == '' or not -1 < port < 65536:
print "[+] can't process exclusion line: %s" % original_line
continue
myexclusion = Exclusion(domain=domain, port=port, comment=comment)
exclusions[domain + '|' + str(port)] = myexclusion
def load_cache():
# Cache format: domain-name.tld|port|poll_date|results_text
global cache
global options
with open(options.cachefile, 'r') as f:
for line in f:
# skip comments and blank lines
if line.startswith('#') or line.strip() == '':
continue
# skip malformed lines
if not '|' in line or len(line.split('|')) < 4:
continue
# extract the fields with strip cleanup
domain, port, poll_date, results_text = [x.strip() for x in line.split('|')]
if domain == '' or port == '' or poll_date == '' or results_text == '':
continue
port = int(port)
# port should be between 0-65535 inclusive
if not -1 < port < 65536:
continue
mycache = Cache(domain=domain, port=port, poll_date=poll_date, results_text=results_text)
cache[domain + '|' + str(port)] = mycache
def dump_cache():
global cache
global options
print "[-] Writing updated cache file"
with open(options.cachefile, 'w') as f:
for mykey in sorted(cache.keys()):
f.write(str(cache[mykey]) + "\n")
def main():
global options, args
global exclusions, cache
print "[-] = INFO, [+] = ERROR, [SSC] = Self Signed Certificate"
(options, args) = processOptions()
load_exclusions()
print "[-] Loaded %d exclusion entries" % len(exclusions)
load_cache()
print "[-] Loaded %d cache entries" % len(cache)
# read in file
with open(options.infile, 'r') as f:
for line in f:
# skip comments and blank lines
if line.startswith('#') or line.strip() == '':
continue
# if there's a port listed
if '|' in line:
# we should only have two elements surrounding one pipe
if len(line.split('|')) != 2:
print "[+] skipping infile line: %s" % line
continue
domain, port = [x.strip() for x in line.split('|')]
if port == '':
port = 443
else:
port = int(port)
else:
domain = line.strip()
port = 443
if domain == '' or port == '':
print "[+] skipping infile line: %s" % line
continue
port = int(port)
mykey = "%s|%d" % (domain, port)
if mykey in exclusions:
continue
if mykey in cache:
print "%s [cached %s]" % (cache[mykey].results_text, cache[mykey].poll_date)
else:
addr = (domain, port)
mycert = get_server_cert(addr)
if mycert:
process_cert(mycert, addr)
# write a new cache file
dump_cache()
if __name__ == "__main__":
main()