forked from chenjj/CORScanner
/
corscheck.py
262 lines (196 loc) · 8.5 KB
/
corscheck.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
import gevent.monkey
gevent.monkey.patch_all()
import requests, json, os, inspect, tldextract
from future.utils import iteritems
try:
from urllib.parse import urlparse
except Exception as e:
from urlparse import urlparse
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class CORSCheck:
"""docstring for CORSCheck"""
url = None
cfg = None
headers = None
result = {}
def __init__(self, url, cfg):
self.url = url
self.cfg = cfg
if cfg["headers"] != None:
self.headers = cfg["headers"]
def send_req(self, url, origin):
try:
headers = {
'Origin':
origin,
'Cache-Control':
'no-cache',
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
}
if self.headers != None:
headers.update(self.headers)
# self-signed cert OK, follow redirections
resp = requests.get(self.url, timeout=5, headers=headers, verify=False, allow_redirects=True)
# remove cross-domain redirections, which may cause false results
first_domain =tldextract.extract(url).registered_domain
last_domain = tldextract.extract(resp.url).registered_domain
if(first_domain.lower() != last_domain.lower()):
resp = None
except Exception as e:
resp = None
return resp
def get_resp_headers(self, resp):
if resp == None:
return None
resp_headers = dict(
(k.lower(), v) for k, v in iteritems(resp.headers))
return resp_headers
def check_cors_policy(self, test_module_name,test_origin,test_url):
resp = self.send_req(self.url, test_origin)
resp_headers = self.get_resp_headers(resp)
status_code = resp.status_code if resp is not None else None
if resp_headers == None:
return -1
"""----以下是我加的分割线----"""
if resp_headers.get("access-control-allow-origin") == "*":
credentials = "false"
if resp_headers.get("access-control-allow-credentials") == "true":
credentials = "true"
msg = {
"url": test_url,
"type": "test_allow_any_origin",
"credentials": "false",
"origin": test_origin,
"status_code" : status_code
}
return msg
"""----以上是我加的分割线----"""
parsed = urlparse(str(resp_headers.get("access-control-allow-origin")))
resp_origin = parsed.scheme + "://" + parsed.netloc.split(':')[0]
msg = None
# test_origin does not have to be case sensitive
if test_origin.lower() == resp_origin:
credentials = "false"
if resp_headers.get("access-control-allow-credentials") == "true":
credentials = "true"
# Set the msg
msg = {
"url": test_url,
"type": test_module_name,
"credentials": credentials,
"origin": test_origin,
"status_code" : status_code
}
return msg
def is_cors_permissive(self,test_module_name,test_origin,test_url):
msg = self.check_cors_policy(test_module_name,test_origin,test_url)
if msg != None:
self.cfg["logger"].warning(msg)
self.result = msg
return True
self.cfg["logger"].info("nothing found for {url: %s, origin: %s, type: %s}" % (test_url, test_origin, test_module_name))
return False
def test_reflect_origin(self):
module_name = inspect.stack()[0][3].replace('test_','');
test_url = self.url
parsed = urlparse(test_url)
test_origin = parsed.scheme + "://" + "evil.com"
self.cfg["logger"].info(
"Start checking %s for %s" % (module_name,test_url))
return self.is_cors_permissive(module_name,test_origin,test_url)
def test_prefix_match(self):
module_name = inspect.stack()[0][3].replace('test_','');
test_url = self.url
parsed = urlparse(test_url)
test_origin = parsed.scheme + "://" + parsed.netloc.split(':')[0] + ".evil.com"
self.cfg["logger"].info(
"Start checking %s for %s" % (module_name,test_url))
return self.is_cors_permissive(module_name,test_origin,test_url)
def test_suffix_match(self):
module_name = inspect.stack()[0][3].replace('test_','');
test_url = self.url
parsed = urlparse(test_url)
sld = tldextract.extract(test_url.strip()).registered_domain
test_origin = parsed.scheme + "://" + "evil" + sld
self.cfg["logger"].info(
"Start checking %s for %s" % (module_name,test_url))
return self.is_cors_permissive(module_name,test_origin,test_url)
def test_trust_null(self):
module_name = inspect.stack()[0][3].replace('test_','');
test_url = self.url
test_origin = "null"
self.cfg["logger"].info(
"Start checking %s for %s" % (module_name,test_url))
return self.is_cors_permissive(module_name,test_origin,test_url)
def test_include_match(self):
module_name = inspect.stack()[0][3].replace('test_','');
test_url = self.url
parsed = urlparse(test_url)
sld = tldextract.extract(test_url.strip()).registered_domain
test_origin = parsed.scheme + "://" + sld[1:]
self.cfg["logger"].info(
"Start checking %s for %s" % (module_name,test_url))
return self.is_cors_permissive(module_name,test_origin,test_url)
def test_not_escape_dot(self):
module_name = inspect.stack()[0][3].replace('test_','');
test_url = self.url
parsed = urlparse(test_url)
sld = tldextract.extract(test_url.strip()).registered_domain
domain = parsed.netloc.split(':')[0]
test_origin = parsed.scheme + "://" + domain[::-1].replace(
'.', 'a', 1)[::-1]
self.cfg["logger"].info(
"Start checking %s for %s" % (module_name,test_url))
return self.is_cors_permissive(module_name,test_origin,test_url)
def test_trust_any_subdomain(self):
module_name = inspect.stack()[0][3].replace('test_','');
test_url = self.url
parsed = urlparse(test_url)
test_origin = parsed.scheme + "://" + "evil." + parsed.netloc.split(':')[0]
self.cfg["logger"].info(
"Start checking %s for %s" % (module_name,test_url))
return self.is_cors_permissive(module_name,test_origin,test_url)
def test_https_trust_http(self):
module_name = inspect.stack()[0][3].replace('test_','');
test_url = self.url
parsed = urlparse(test_url)
if parsed.scheme != "https":
return
test_origin = "http://" + parsed.netloc.split(':')[0]
self.cfg["logger"].info(
"Start checking %s for %s" % (module_name,test_url))
return self.is_cors_permissive(module_name,test_origin,test_url)
def test_custom_third_parties(self):
module_name = inspect.stack()[0][3].replace('test_','');
test_url = self.url
parsed = urlparse(test_url)
sld = tldextract.extract(test_url.strip()).registered_domain
domain = parsed.netloc.split(':')[0]
self.cfg["logger"].info(
"Start checking %s for %s" % (module_name,test_url))
is_cors_perm = False
# Opening origins file
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)),'..%sorigins.json' % os.sep)) as origins_file:
origins = json.load(origins_file)['origins']
for test_origin in origins:
is_cors_perm = self.is_cors_permissive(module_name,test_origin,test_url)
if is_cors_perm: break
return is_cors_perm
def check_one_by_one(self):
functions = [
'test_reflect_origin',
'test_prefix_match',
'test_suffix_match',
'test_trust_null',
'test_include_match',
'test_not_escape_dot',
'test_custom_third_parties',
'test_trust_any_subdomain',
'test_https_trust_http',
]
for fname in functions:
func = getattr(self,fname)
if(func()): break
return self.result