-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
234 lines (208 loc) · 7.04 KB
/
scan.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
#!/usr/bin/python
import os
import sys
if os.name == 'nt':
GOODSTART = ""
GOODEND = ""
BADSTART = "*"
BADEND = "*"
WARNINGSTART = '-'
WARNINGEND = '-'
else:
GOODSTART = "\033[0;32m"
GOODEND = "\033[0m"
BADSTART = "\033[0;31m"
BADEND = "\033[0m"
WARNINGSTART = '\033[0;33m'
WARNINGEND = '\033[0m'
try:
#TRACE checker--------------------
import httplib
import urlparse
bad =[400,401,402,403,405,406,501]
def resolve_http_redirect(url, method, depth=0):
if depth > 10:
raise Exception("Redirected "+depth+" times, giving up.")
o = urlparse.urlparse(url,allow_fragments=True)
if(o.scheme == 'https'):
conn = httplib.HTTPSConnection(o.netloc)
else:
conn = httplib.HTTPConnection(o.netloc)
path = o.path
if o.query:
path +='?'+o.query
conn.request(method, path)
res = conn.getresponse()
headers = dict(res.getheaders())
if headers.has_key('location') and headers['location'] != url:
print('redirect to '+headers['location'])
try:
return resolve_http_redirect(headers['location'], method, depth+1)
except:
print 'error following redirect'
return url
else:
return url
TRACE = False
try:
commandline = True
address = sys.argv[1]
except:
commandline = False
address = raw_input("server ip or domain? ")
if('://' not in address):
address = 'http://'+address
url = resolve_http_redirect(address, 'TRACE')
o = urlparse.urlparse(url,allow_fragments=True)
if(o.scheme == 'https'):
con = httplib.HTTPSConnection(o.netloc)
else:
con = httplib.HTTPConnection(o.netloc)
con.request('TRACE',o.path)
resp = con.getresponse()
if(resp.status not in bad ):
print BADSTART+ 'TRACE request responded with:'+BADEND
print resp.read()
TRACE = True
else:
print GOODSTART+'TRACE says '+resp.reason+GOODEND
url = resolve_http_redirect(address, 'OPTIONS')
o = urlparse.urlparse(url,allow_fragments=True)
if(o.scheme == 'https'):
con = httplib.HTTPSConnection(o.netloc)
else:
con = httplib.HTTPConnection(o.netloc)
con.request('OPTIONS',o.path)
resp = con.getresponse()
if(resp.status in bad ):
print GOODSTART+'and OPTIONS says '+resp.reason+GOODEND
elif(resp.status != 200):
print GOODSTART+'checking OPTIONS failed'+GOODEND
else:
print
print WARNINGSTART+'These are the reported OPTIONS'+WARNINGEND
print str(resp.status)+' '+resp.reason
print resp.msg
netloc = o.netloc
except Exception, e:
print e
netloc = address
#SSL eyes processes --------------------
import subprocess
import xml.etree.ElementTree as et
def checkCert(certinfo):
try:
return certinfo[0].attrib['hasMatchingHostname'] == 'True', certinfo[0].attrib['isTrustedByMozillaCAStore'] == 'True'
except Exception, e:
print WARNINGSTART+'cert check '+str(e)
print 'failed to insecure'+WARNINGEND
return False,False
def renegotiation(reneg):
try:
return reneg[0].attrib['canBeClientInitiated'] == 'True', reneg[0].attrib['isSecure'] == 'True'
except Exception, e:
print WARNINGSTART+'renegotiation '+str(e)
print 'failed to insecure'+WARNINGEND
return True, False
def resumption(resum):
try:
return resum[0].attrib['isSupported'] == 'True', resum[1].attrib['isSupported'] == 'True'
except Exception, e:
print WARNINGSTART+'Resumption: '+str(e)
print 'failed to insecure'+WARNINGEND
return False,False
def cipher_check(ciphers, used):
try:
fin = {}
suites = {}
for suite in used:
try:
test = (len(suite[2]) != 0)
suites[suite.attrib['title'].split()[0]] = test
for cipher in suite[2]:
fin[cipher.attrib['name']] = ciphers[cipher.attrib['name']]
except:
suites[suite.attrib['exception'].split()[2]] = "False"
return fin, suites
except Exception, e:
print WARNINGSTART+'ciphers '+str(e)
print 'some information may have been lost'+WARNINGEND
try:
return fin, suites
except:
return [], []
tags = ['HIGH', 'MEDIUM', 'LOW', 'aNULL', 'EXP', 'MD5']
ciphers = {}
if os.name == 'nt':
f = open('ciphers')
ciphers = eval(f.read())
f.close()
os.chdir('windows')
subprocess.check_output(['python','sslyze.py','--regular',netloc, '--xml_out=out.xml'])
else:
try:
for tag in tags:
osslciphers = subprocess.check_output(['openssl','ciphers',tag]).split(':')
for cipher in osslciphers:
cipher = cipher.strip()
ciphers[cipher] = tag
f = open('ciphers','w')
f.write(str(ciphers))
f.close()
except:
print('openssl is not working, reverting to backup')
f = open('ciphers')
ciphers = eval(f.read())
f.close()
os.chdir('not')
subprocess.check_output(['python','sslyze.py','--regular',netloc, '--xml_out=out.xml'])
findings = et.parse('out.xml').getroot()[0][0]
certinfo = findings[0]
compress = findings[1]
reneg = findings[2]
resum = findings[3]
ciphersUsed = findings[4:]
def TrueFalseColor(value):
if value == "False":
return BADSTART+value+BADEND
return GOODSTART+value+GOODEND
def TrueFalseColorInverse(value):
if value == "False":
return GOODSTART+value+GOODEND
return BADSTART+value+BADEND
def StrengthColor(value):
if value == 'HIGH':
return GOODSTART+value+GOODEND
if value == 'MEDIUM':
return WARNINGSTART+value+WARNINGEND
return BADSTART+value+BADEND
certHost, certTrust = checkCert(certinfo)
renegStatus, renegSecure = renegotiation(reneg)
resumSessionID, resumTLS = resumption(resum)
checkedCiphers, checkedSuites = cipher_check(ciphers,ciphersUsed)
print
print 'CERT'
print 'trusted '+TrueFalseColor(str(certTrust))
print 'for correct host '+TrueFalseColor(str(certHost))
print
print 'Ciphers used'
for cipher in checkedCiphers:
print StrengthColor(checkedCiphers[cipher])+'\t'+cipher
print
print 'Suites'
for suite in checkedSuites:
if suite in ["SSLV2","SSLv2"]:
print suite+'\t'+TrueFalseColorInverse(str(checkedSuites[suite]))
else:
print suite+'\t'+TrueFalseColor(str(checkedSuites[suite]))
print
if(renegStatus):
print 'renegotiation: '+WARNINGSTART+str(renegStatus)+WARNINGEND
print 'renegotiation secure: '+TrueFalseColor(str(renegSecure))
else:
print 'renegotiation: '+GOODSTART+str(renegStatus)+GOODEND
print 'resuming with Session IDs: '+str(resumSessionID)
print 'resuming with TLS: '+str(resumTLS)
os.remove('out.xml')
if(not commandline):
raw_input('press enter to finish')