-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProtocolVersions.py
executable file
·341 lines (303 loc) · 13.2 KB
/
ProtocolVersions.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
#!/usr/bin/python
# Copyright (C) 2018 Stephen Farrell, [email protected]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# check the protocol versions being used
# This goes back to records.fresh as I don't keep the versioning info
# in the fingerprint structure (TODO: add version stuff to metadata
# in FP structures)
# we'll count versions per-port overall, and for IPs that are, and
# are not, within a cluster
# for that last, an initial step is to select the IPs from the
# dodgy.json which are those not within clusters
import re, os, sys, argparse, tempfile, gc
import json
import jsonpickle # install via "$ sudo pip install -U jsonpickle"
import time, datetime
from dateutil import parser as dparser # for parsing time from comand line and certs
import pytz # for adding back TZ info to allow comparisons
# our own stuff
from SurveyFuncs import *
# counters, per port, per version
counters={}
counters['o']={}
counters['c']={}
counters['nc']={}
for pstr in portstrings:
# overall counters
# in-cluster counters
# not in-cluster counters
counters['o'][pstr]={}
counters['c'][pstr]={}
counters['nc'][pstr]={}
# counter updater
def counterupdate(inout,pstr,ver):
if ver in counters['o'][pstr]:
counters['o'][pstr][ver]=counters['o'][pstr][ver]+1
else:
counters['o'][pstr][ver]=1
if inout:
if ver in counters['c'][pstr]:
counters['c'][pstr][ver]=counters['c'][pstr][ver]+1
else:
counters['c'][pstr][ver]=1
else:
if ver in counters['nc'][pstr]:
counters['nc'][pstr][ver]=counters['nc'][pstr][ver]+1
else:
counters['nc'][pstr][ver]=1
# figure out runname
dirname=os.getcwd()
#print dirname
fullrunname=dirname.split('/')[-1]
runname=fullrunname.split('-')[0] + '-' + fullrunname.split('-')[1]
print >>sys.stderr, "Doing " + runname
# default values
infile="records.fresh"
outfile="versions.tex"
# if this file exists, read it to determine if IP is (not) in some cluster
dodgyfile="dodgy.json"
collfile="collisions.json"
# command line arg handling
argparser=argparse.ArgumentParser(description='Count protocol versions in a run')
argparser.add_argument('-i','--input',
dest='infile',
help='file containing output from zgrab')
argparser.add_argument('-o','--output_file',
dest='outfile',
help='file in which to put latex table')
argparser.add_argument('-d','--dodgy_file',
dest='dodgy',
help='file in which we find non-clustered IPs')
argparser.add_argument('-c','--collisions',
dest='collisions',
help='file in which we find clustered IPs')
args=argparser.parse_args()
if args.infile is not None:
infile=args.infile
if args.outfile is not None:
outfile=args.outfile
if args.dodgy is not None:
dodgyfile=args.dodgy
if args.collisions is not None:
collfile=args.collisions
# we won't fully decode this, just grep out the IP addresses
# do: grep '^ "ip":' dodgyfile
dodgycount=0
clusterips=[]
ooccount=0
oocips=[]
thatip=''
df = open(dodgyfile,"r")
for line in df:
if re.search('^ "ip"', line):
thatip=line.split()[1][1:-2]
dodgycount+=1
if dodgycount % 100 == 0:
print >>sys.stderr, "Reading dodgies, did: " + str(dodgycount)
if re.search('"wrong_country"',line):
ooccount+=1
oocips.append(thatip)
print >>sys.stderr, "Done reading dodgies, did: " + str(dodgycount)
print >>sys.stderr, "Number of ooc IPs: " + str(len(oocips))
cf = open(collfile,"r")
fp=getnextfprint(cf)
clcount=0
while fp:
clusterips.append(fp.ip)
clcount+=1
if clcount % 100 == 0:
print >>sys.stderr, "Reading cluster IPs, did: " + str(clcount)
fp=getnextfprint(cf)
cf.close()
print >>sys.stderr, "Number of non-cluster IPs: " + str(len(clusterips))
# encoder options
jsonpickle.set_encoder_options('json', sort_keys=True, indent=2)
jsonpickle.set_encoder_options('simplejson', sort_keys=True, indent=2)
# keep track of how long this is taking per ip
peripaverage=0
overallcount=0
somekeycount=0
# initialise to versions we know about - others that we don't
# will be added as we find 'em - that may screw up the latex
# output but that'll be visible and can be fixed as we see 'em
sshversions=["1.99", "2.0"]
tlsversions=["SSLv3","TLSv1.0","TLSv1.1","TLSv1.2"]
# init counters
for ctype in counters:
for pstr in portstrings:
if pstr=="p22":
for ver in sshversions:
counters[ctype][pstr][ver]=0
else:
for ver in tlsversions:
counters[ctype][pstr][ver]=0
# count our fp exceptions
sshfpes=0
tlsfpes=0
with open(infile,'r') as f:
for line in f:
ipstart=time.time()
badrec=False
j_content = json.loads(line)
somekey=False
thisip=j_content['ip'].strip()
# ignore if not in-country
if thisip in oocips:
print >>sys.stderr, "IP : " + thisip + " is out of country"
continue
incluster=False
if thisip in clusterips:
incluster=True
#print >>sys.stderr, "IP : " + thisip + " incluster: " + str(incluster)
for pstr in portstrings:
if pstr=='p22':
try:
sshver=j_content['p22']['data']['xssh']['server_id']['version']
# make sure we got an FP for that - sometimes we get protocol versions
# but don't get an FP, which skews the numbers. That can happen
# e.g. if something doesn't decode or whatever
# attempting to get this should cause an exception if it's not there
try:
fp=j_content['p22']['data']['xssh']['key_exchange']['server_host_key']['fingerprint_sha256']
#print >>sys.stderr, "IP2 : " + thisip + " incluster: " + str(incluster)
counterupdate(incluster,'p22',sshver)
somekey=True
if sshver not in sshversions:
sshversions.append(sshver)
except Exception as e:
sshfpes+=1
#print >> sys.stderr, "p22 ver/FP exception #" + str(sshfpes) + " " + str(e) + " ip:" + thisip
except Exception as e:
#print >> sys.stderr, "p22 exception " + str(e) + " ip:" + thisone.ip
pass
elif pstr=='p443':
try:
tls=j_content['p443']['data']['http']['response']['request']['tls_handshake']
ver=tls['server_hello']['version']['name']
# make sure we got an FP for that - sometimes we get protocol versions
# but don't get an FP, which skews the numbers. That can happen
# e.g. if something doesn't decode or whatever
# attempting to get this should cause an exception if it's not there
try:
cert=tls['server_certificates']['certificate']
fp=cert['parsed']['subject_key_info']['fingerprint_sha256']
#print >>sys.stderr, "IP3 : " + thisip + " incluster: " + str(incluster)
counterupdate(incluster,pstr,ver)
somekey=True
if ver not in tlsversions:
tlsversions.append(ver)
except Exception as e:
tlsfpes+=1
#print >> sys.stderr, pstr + "ver/FP exception #" + str(tlsfpes) + " " + str(e) + " ip:" + thisip
except Exception as e:
#print >> sys.stderr, pstr + "exception for:" + thisip + ":" + str(e)
pass
else:
try:
tls=j_content[pstr]['data']['tls']
ver=tls['server_hello']['version']['name']
# make sure we got an FP for that - sometimes we get protocol versions
# but don't get an FP, which skews the numbers. That can happen
# e.g. if something doesn't decode or whatever
# attempting to get this should cause an exception if it's not there
try:
cert=tls['server_certificates']['certificate']
fp=cert['parsed']['subject_key_info']['fingerprint_sha256']
#print >>sys.stderr, "IP4 : " + thisip + " incluster: " + str(incluster)
counterupdate(incluster,pstr,ver)
if ver not in tlsversions:
tlsversions.append(ver)
somekey=True
except Exception as e:
tlsfpes+=1
#print >> sys.stderr, pstr + "ver/FP exception #" + str(tlsfpes) + " " + str(e) + " ip:" + thisip
except Exception as e:
#print >> sys.stderr, pstr + "exception for:" + thisip + ":" + str(e)
pass
if somekey:
somekeycount+=1
#print >>sys.stderr, "IP4 : " + thisip + " incluster: " + str(incluster) + " somekey: " + str(somekey)
overallcount += 1
# update average
ipend=time.time()
thistime=ipend-ipstart
peripaverage=((overallcount*peripaverage)+thistime)/(overallcount+1)
if overallcount % 100 == 0:
print >> sys.stderr, "Reading versions, did: " + str(overallcount) + \
" number with keys " + str(somekeycount) + \
" ssh FP exceptions: " + str(sshfpes) + \
" tls FP exceptions: " + str(tlsfpes) + \
" most recent ip " + thisip + \
" average time/ip: " + str(peripaverage) \
+ " last time: " + str(thistime)
del j_content
f.close()
gc.collect()
print >> sys.stderr, "Done reading versions, did: " + str(overallcount) + \
" number with keys " + str(somekeycount) + \
" ssh FP exceptions: " + str(sshfpes) + \
" tls FP exceptions: " + str(tlsfpes) + \
" most recent ip " + thisip + \
" average time/ip: " + str(peripaverage) \
+ " last time: " + str(thistime)
bstr=jsonpickle.encode(counters)
print >>sys.stderr, "Counters:\n" + bstr
ocounters={}
# count tls versions overall
for pstr in portstrings:
if pstr=='p22':
continue
else:
for ver in counters['o'][pstr]:
if ver not in ocounters:
ocounters[ver]=counters['o'][pstr][ver]
else:
ocounters[ver]=ocounters[ver]+counters['o'][pstr][ver]
bstr=jsonpickle.encode(ocounters)
print >>sys.stderr, "Overall TLS Counters:\n" + bstr
# produce some latex table entry lines, for tls
eotl=' \\\\ \\hline'
print runname + eotl
lineout= 'port '
for ver in sorted(tlsversions):
lineout+= ' & ' + ver
print lineout + ' & Total ' + eotl
coltotal=0
for pstr in portstrings:
if pstr=='p22':
continue
lineout= pstr
linetotal=0
for ver in sorted(tlsversions):
if ver not in counters['o'][pstr]:
lineout+= ' & 0 '
else:
lineout+= ' & ' + str(counters['o'][pstr][ver])
linetotal+= counters['o'][pstr][ver]
print lineout + ' & ' + str(linetotal) + eotl
coltotal+=linetotal
lineout='Total '
linetotal=0
for ver in sorted(tlsversions):
lineout+= ' & ' + str(ocounters[ver])
linetotal += ocounters[ver]
if linetotal != coltotal:
print >>sys.stderr, "Totals mismatch!!!, cols (" + str(coltotal) + ") != last line (" + str(linetotal) + ")"
print lineout + ' & ' + str(linetotal) + eotl