-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDesyncCL0.py
executable file
·218 lines (195 loc) · 8.56 KB
/
DesyncCL0.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
#!/usr/bin/env python3
__version__ = '0.0.1'
import sys
import base64
import argparse
import socket
import ssl
import random
from urllib.parse import urlparse, parse_qsl
from http.client import HTTPResponse
from io import BytesIO
class FakeSocket():
def __init__(self, response_bytes):
self._file = BytesIO(response_bytes)
def makefile(self, *args, **kwargs):
return self._file
def send_request(sock, request):
sock.sendall(request)
response = b''
while True:
try:
chunk = sock.recv(4096)
if not chunk:
break
else:
response = response + chunk
source = FakeSocket(response)
httpresponse = HTTPResponse(source)
try:
httpresponse.begin()
if httpresponse.getheader('Content-Length'):
CL = int(httpresponse.getheader('Content-Length'))
body = httpresponse.read(CL)
if CL == len(body):
break
else:
continue
elif httpresponse.getheader('Transfer-Encoding'):
body = httpresponse.read(len(response))
if b'0\r\n\r\n' in chunk:
break
else:
continue
except:
continue
except socket.error as err:
print('ERROR! Raw Response:', response)
print(err)
exit(1)
if response == b'':
print('ERROR! Got a blank response from the server.')
exit(1)
elif 'body' not in locals():
body = b''
return httpresponse, body
def cl0_check(URL, SRL,user_agent, timeout, debug):
hostname = URL.netloc
if URL.path == '':
path = '/'
else:
path = URL.path + ('?' + URL.query if URL.query else '') + ('#' + URL.fragment if URL.fragment else '')
# >>>>> request404
requestSmuggled = SRL + '\r\n'
requestSmuggled = requestSmuggled + 'Foo: x'
requestRoot = 'GET / HTTP/1.1\r\n'
requestRoot = requestRoot + 'Host: ' + hostname + '\r\n'
requestRoot = requestRoot + 'User-Agent: ' + user_agent + '\r\n'
requestRoot = requestRoot + 'Connection: close\r\n'
requestRoot = requestRoot + '\r\n'
request404 = requestSmuggled + requestRoot
if debug:
print(">>>>> request404")
print(request404)
print(">>>>> request404")
sock = connect(URL, timeout)
httpresponse404, body404 = send_request(sock, request404.encode('utf-8'))
sock.close()
if debug:
print(">>>>> httpresponse404")
print("status404:", httpresponse404.status)
print("headers404:", httpresponse404.getheaders())
print("body404:", body404)
print("<<<<< httpresponse404")
# <<<<< request404
# >>>>> requestRoot
if debug:
print(">>>>> requestRoot")
print(requestRoot)
print("<<<<< requestRoot")
sock = connect(URL, timeout)
httpresponseRoot, bodyRoot = send_request(sock, requestRoot.encode('utf-8'))
sock.close()
if debug:
print(">>>>> httpresponseRoot")
print("statusRoot:", httpresponseRoot.status)
print("headersRoot:", httpresponseRoot.getheaders())
print("bodyRoot:", bodyRoot)
print("<<<<< httpresponseRoot")
# <<<<< requestRoot
# >>>>> requestDesync
requestDesync = 'POST ' + path + ' HTTP/1.1\r\n'
requestDesync = requestDesync + 'Host: ' + hostname + '\r\n'
requestDesync = requestDesync + 'User-Agent: ' + user_agent + '\r\n'
requestDesync = requestDesync + 'Content-Length: ' + str(len(requestSmuggled)) + '\r\n'
requestDesync = requestDesync + 'Connection: keep-alive\r\n'
requestDesync = requestDesync + 'Content-Type: application/x-www-form-urlencoded\r\n'
requestDesync = requestDesync + '\r\n'
requestDesync = requestDesync + requestSmuggled
if debug:
print(">>>>> requestDesync")
print(requestDesync)
print("<<<<< requestDesync")
sock = connect(URL, timeout)
httpresponseDesync, bodyDesync = send_request(sock, requestDesync.encode('utf-8'))
if debug:
print(">>>>> httpresponseDesync")
print("statusDesync:", httpresponseDesync.status)
print("headersDesync:", httpresponseDesync.getheaders())
print("bodyDesync:", bodyDesync)
print("<<<<< httpresponseDesync")
# <<<<< requestDesync
# >>>>> requestRootSmuggled
requestRootSmuggled = requestRoot
if debug:
print(">>>>> requestRootSmuggled")
print(requestRootSmuggled)
print("<<<<< requestRootSmuggled")
httpresponseRootSmuggled, bodyRootSmuggled = send_request(sock, requestRootSmuggled.encode('utf-8'))
sock.close()
if debug:
print(">>>>> httpresponseRootSmuggled")
print("statusRootSmuggled:", httpresponseRootSmuggled.status)
print("headersRootSmuggled:", httpresponseRootSmuggled.getheaders())
print("bodyRootSmuggled:", bodyRootSmuggled)
print("<<<<< httpresponseRootSmuggled")
# <<<<< requestRootSmuggled
if httpresponseRootSmuggled.status == httpresponse404.status and httpresponseRootSmuggled.status != httpresponseRoot.status:
print('WARNING! Back-end server interpreted the body of the POST request as the start of another request.')
elif httpresponseRootSmuggled.status == httpresponseRoot.status and httpresponseRootSmuggled.status == httpresponse404.status and str(httpresponseRootSmuggled.status).startswith('3') and httpresponseRootSmuggled.getheader('Location') != httpresponseRoot.getheader('Location'):
print('WARNING! Probably vulnerable due different redirects.')
print('httpresponse404', httpresponse404.getheader('Location'))
print('httpresponseRoot', httpresponseRoot.getheader('Location'))
print('httpresponseRootSmuggled', httpresponseRootSmuggled.getheader('Location'))
if 'hopefully404' in httpresponseRootSmuggled.getheader('Location'):
print('httpresponseRootSmuggled contains hopefully404')
elif httpresponseRootSmuggled.status == httpresponseRoot.status and httpresponseRootSmuggled.status == httpresponse404.status and str(httpresponseRootSmuggled.status).startswith('3') and httpresponseRootSmuggled.getheader('Location') == httpresponseRoot.getheader('Location'):
print('WARNING! All responses are redirects to the same location.', httpresponseRootSmuggled.getheaders())
print('Try to debug with an invalid or HEAD method on the smuggled request line.')
else:
print('Not vulnerable.')
def connect(URL, timeout):
hostname = URL.netloc.split(':')[0]
if URL.scheme == 'https':
port = 443 if URL.port is None else URL.port
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
sock = socket.create_connection((hostname, port), timeout)
ssock = context.wrap_socket(sock, server_hostname=hostname)
return ssock
elif URL.scheme == 'http':
port = 80 if URL.port is None else URL.port
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
sock.connect((hostname, port))
return sock
def check_url(url):
url_checked = urlparse(url)
if ((url_checked.scheme != 'http') & (url_checked.scheme != 'https')) | (url_checked.netloc == ''):
raise argparse.ArgumentTypeError('Invalid %s URL (example: https://www.example.com/path).' % url)
return url_checked
def Desync():
if sys.version_info < (3, 9):
print("Error: requires Python 3.9.")
sys.exit(1)
banner = 'ICAgIF9fX18gICAgICAgICAgICAgICAgICAgICAgICAgICAgIF9fX19fX19fICAgIF9fX18gCiAgIC8gX18gXF9fXyAgX19fX19fXyAgX19fX19fICBfX19fXy8gX19fXy8gLyAgIC8gX18gXAogIC8gLyAvIC8gXyBcLyBfX18vIC8gLyAvIF9fIFwvIF9fXy8gLyAgIC8gLyAgIC8gLyAvIC8KIC8gL18vIC8gIF9fKF9fICApIC9fLyAvIC8gLyAvIC9fXy8gL19fXy8gL19fXy8gL18vIC8gCi9fX19fXy9cX19fL19fX18vXF9fLCAvXy8gL18vXF9fXy9cX19fXy9fX19fXy9cX19fXy8gIAogICAgICAgICAgICAgICAgL19fX18vICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA='
print(base64.b64decode(banner).decode('UTF-8'))
print('version ' + __version__)
parser = argparse.ArgumentParser(prog='DesyncCL0', description='Detects HTTP desync CL.0 vulnerabilities.')
parser.add_argument('URL', type=check_url, help='The URL to be checked.')
parser.add_argument('-s', '--smuggledrequestline', default='GET /hopefully404 HTTP/1.1', help='Set the smuggled request line (default "GET /hopefully404 HTTP/1.1").')
parser.add_argument('-t', '--timeout', type=int, default=5, help='Set connection timeout for desync test (default 5).')
parser.add_argument('-u', '--user_agent', default='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36', help='Set default User-Agent request header (default "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36").')
parser.add_argument('-d', '--debug', action=argparse.BooleanOptionalAction, default=False, help='Print debug data.')
args = parser.parse_args()
URL = args.URL
SRL = args.smuggledrequestline
timeout = args.timeout
user_agent = args.user_agent
debug = args.debug
print('Testing URL: ' + URL.scheme + '://' + URL.netloc + URL.path + ('?' + URL.query if URL.query else '') + ('#' + URL.fragment if URL.fragment else ''))
print('Testing for CL.0 vulnerability...')
cl0_check(URL, SRL, user_agent, timeout, debug)
if __name__ == '__main__':
Desync()