forked from Moopinger/CLZero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clzero.py
342 lines (262 loc) · 13.3 KB
/
clzero.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
342
import socket
import ssl
from urllib.parse import urlparse
import argparse
from colorama import Fore, Back, Style
import random, string
import os
#These are later defined by the user config file. no touchy
smuggle_request = ""
smuggle_request_body = ""
regular_request = ""
clzero_headers = {}
def color_code(code):
global NOCOLOR
if NOCOLOR:
return code
if code == "200":
msg = Fore.BLACK + Back.GREEN + code + Style.RESET_ALL
elif code == "301" or code == "302":
msg = Fore.BLACK + Back.YELLOW + code + Style.RESET_ALL
elif code == "404":
msg = Fore.WHITE + Back.BLUE + code + Style.RESET_ALL
else:
msg = Fore.WHITE + Back.RED + code + Style.RESET_ALL
return msg
#cache buster
def randomword(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
def send_request(url, request_type = "regular", modified_cl = "none"):
# Parse the URL
parsed_url = urlparse(url)
host = parsed_url.hostname
port = parsed_url.port or (443 if parsed_url.scheme == 'https' else 80)
path = parsed_url.path if parsed_url.path else '/'
#Parse the config file values for the regular request
regular_request_temp = regular_request.replace("__PATH__", path).replace("__HOST__", host).replace("__CACHE_BUSTER__", cache_buster)
#Parse the config file values for the smuggle request
smuggle_content_len = str(len(smuggle_request_body))
smuggle_request_temp = smuggle_request.replace("__HOST__", host).replace("__PATH__", path).replace("__CACHE_BUSTER__", path).replace("__METHOD__", request_method).replace("__MOD_CL__", modified_cl)
smuggle_request_temp = smuggle_request_temp.replace("__CL__", smuggle_content_len) + smuggle_request_body
if request_type == "regular":
http_request = regular_request_temp
elif request_type == "smuggle":
http_request = smuggle_request_temp
elif request_type == "generate":
http_request = smuggle_request_temp
return http_request
else:
http_request = regular_request
if DEBUG:
print("\r\n========================================")
print(http_request)
print("========================================\r\n")
if LAST_BYTE_SYNC and request_type == "smuggle":
#We will withold the last byte of the probe request and send the smuggle. Once the last byte of the smuggle is sent, the last byte for the probe is immediately sent.
last_byte = regular_request_temp[-1]
regular_request_missing_byte = regular_request_temp[:-1]
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
try:
sock.settimeout(5)
if parsed_url.scheme == 'https':
context = ssl._create_unverified_context()
with context.wrap_socket(sock, server_hostname=host) as ssock:
ssock.connect((host, port))
ssock.send(regular_request_missing_byte.encode())
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock2:
try:
with context.wrap_socket(sock2, server_hostname=host) as ssock2:
ssock2.connect((host, port))
ssock2.send(http_request.encode())
#Bounce without reading response for maximum speeed
except:
return "LASTBYTE_FAIL", "0"
#complete the probe (regular_request) request
ssock.send(last_byte.encode())
response = ssock.recv(4096)
try:
response = response.decode()
except:
return "DECODE_ERROR", "0"
else:
sock.connect((host, port))
sock.send(regular_request_missing_byte.encode())
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock2:
sock2.settimeout(5)
sock2.connect((host, port))
sock2.send(http_request.encode())
sock.send(last_byte.encode())
response = sock.recv(4096).decode()
except socket.timeout:
return "TIMEOUT", "0"
except socket.error as e:
string_error = str(e)
if "Connection reset by peer" in string_error:
return "PEER_RESET", "0"
else:
return "ERROR", "0"
# Standard request method begins
else:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
try:
sock.settimeout(5)
if parsed_url.scheme == 'https':
context = ssl._create_unverified_context()
with context.wrap_socket(sock, server_hostname=host) as ssock:
ssock.connect((host, port))
ssock.send(http_request.encode())
if request_type == "smuggle" and SKIP_R:
return "Skip", "0"
response = ssock.recv(4096)
try:
response = response.decode()
except:
return "DECODE_ERROR", "0"
else:
sock.connect((host, port))
sock.send(http_request.encode())
if request_type == "smuggle" and SKIP_R:
return "Skip", "0"
response = sock.recv(4096).decode()
except socket.timeout:
return "TIMEOUT", "0"
except socket.error as e:
string_error = str(e)
if "Connection reset by peer" in string_error:
return "PEER_RESET", "0"
else:
return "ERROR", "0"
#basic error checks below
if response == "":
return "EMPTY", "0"
response_lines = response.split('\r\n')
status_line = response_lines[0]
content_length = next((line for line in response_lines if line.lower().startswith('content-length:')), None)
#likely chunked if status is 200 and CL headers is 0, will just return 0 for now, has no affect
if not content_length:
content_length = "0"
else:
content_length = content_length.split(" ")[1]
if len(status_line.split(" ")) > 1:
status_code = status_line.split(" ")[1]
return status_code, content_length
else:
return "NO_STATUS", "0"
def overwrite_file(value, filename):
with open(filename, 'w') as file:
file.write(value)
def write_value_to_file(value, filename):
with open(filename, 'a') as f:
f.write(str(value) + '\n')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='CLZero by Moopinger - Thanks: Smuggler - @Defparam. @Albinowax. D3d - @deadvolvo')
parser.add_argument('-url', type=str, help='(-u), Single target URL.')
parser.add_argument('-file', type=str, help='(-f), Files containing multiple targets.')
parser.add_argument('-index', type=int, default=0, help="(-i), Index start point when using a file list.")
parser.add_argument('-verbose', action='store_true', help="(-v), Enable verbose output.")
parser.add_argument('-no-color', action='store_true', help="Disable colors in HTTP Status")
parser.add_argument('-resume', action='store_true', help="Resume scan from last index place.")
parser.add_argument('-skipread', action='store_true', help="Skip the read response on smuggle requests, recommended. This will save a lot of time between requests. Ideal for targets with standard HTTP traffic.")
parser.add_argument('-quiet', action='store_true', help="(-q), Disable output. Only successful payloads will be written to ./payloads/ ")
parser.add_argument('-lb', action='store_true', help="Last byte sync method for least request latency. Due to the nature of the request, it cannot guarantee that the smuggle request will be processed first. Ideal for targets with a high amount of traffic, and you do not mind sending multiple requests.")
parser.add_argument('-config', type=str, default="./configs/default.py", help="(-c) Config file to load, see ./configs/ to create custom payloads")
parser.add_argument('-method', type=str, default="POST", help="(-m) Method to use when sending the smuggle request. Default: POST")
args = parser.parse_args()
LAST_BYTE_SYNC = args.lb
DEBUG = args.verbose
NOCOLOR = args.no_color
QUIET = args.quiet
SKIP_R = args.skipread
config_file = args.config
request_method = args.method
slice = args.index
targets = []
if args.url:
targets.append(args.url)
elif args.file:
my_file = open(args.file, "r")
data = my_file.read()
hosts = data.split("\n")
targets = targets + hosts
if args.url or args.file:
if not QUIET:
print("CLZero.py - CL.0 Finder and Fuzzer -=Moopinger=-")
else:
exit(parser.print_help())
if args.resume and args.file:
try:
session_file = open('./resume.config', "r")
data = session_file.read().lower().split(":::")
data[1] = data[1].strip()
#data[0] = string: index position
#data[1] = string: targetname
target_at_index = targets[int(data[0])]
if data[1] == target_at_index:
slice = int(data[0])
else:
print(f"[-] Could not resume. list is different")
except Exception as e:
print(f"[-] Could not read resume.config {e}")
pass
#load our config file
if (config_file[1] != '/'):
config_file = os.path.dirname(os.path.realpath(__file__)) + "/" + config_file
try:
f = open(config_file)
except Exception as e:
exit(f"[-] Could not open config file: {e}")
script = f.read()
f.close()
exec(script)
total_urls = len(targets)
count = 0
cache_buster = randomword(10)
if slice > 0:
targets = targets[slice:]
count = slice
for target in targets:
#dont want to overwrite valid resume.config with 0 when running single url scan
if args.file:
try:
overwrite_file(str(count) + ":::" + target, "./resume.config")
except:
print("Cannot write session info")
pass
count = count + 1
if target.lower().strip()[0:4] != "http":
target = "https://" + target
target = target.rstrip()
#base request
first_status, first_length = send_request(target, "regular")
if first_status != "ERROR" and first_status != "TIMEOUT":
if not QUIET:
print("[+] Target URL: " + target + " [" + str(count) + "/" + str(total_urls) + "]")
print("[+] Base Response: "+ color_code(first_status) + " [" + first_length + "]" )
for smuggle_name, modified_header in clzero_headers.items():
if LAST_BYTE_SYNC:
reg_status, reg_length = send_request(target, "smuggle", modified_header)
if not QUIET:
print("--> [" + smuggle_name + "] [LastByte-Sync] : \t" + color_code(reg_status) + " [" + reg_length + "] ")
#Slower way below
else:
smuggle_status, smuggle_length = send_request(target, "smuggle", modified_header)
if not QUIET:
print("\r--> [" + smuggle_name + "] : \t" + color_code(smuggle_status) + " [" + smuggle_length + "] ", end="")
reg_status, reg_length = send_request(target, "regular")
if not QUIET:
print("\t[Probe]: "+ color_code(reg_status) + " [" + reg_length + "]")
# 429 - "HTTP code: Too many requests", this will only cause FPs and heartbreak, so we ignore here
if reg_status != first_status and reg_status != "429" and reg_status != "LASTBYTE_FAIL" and reg_status != "ERROR":
request_payload = send_request(target, "generate", modified_header)
payload_filename = target.replace("http://", "").replace("https://", "").replace("/", "")
payload_filename = payload_filename.replace(".", "_") + smuggle_name.replace("-","_") + ".txt"
try:
overwrite_file(request_payload , "./payloads/" + payload_filename)
except Exception as e:
print(f"[-]Could not save payload: {e}")
if not QUIET:
print(f"[+] {target} may be vulnerable. \r\n[+] Smuggle type: {smuggle_name} \r\n[+]Payload saved to ./payloads/{payload_filename}" )
else:
if not QUIET:
print(f"[-] Something went wrong at: {target} - moving to next host" )