-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2_tester.py
More file actions
625 lines (507 loc) · 17.9 KB
/
ex2_tester.py
File metadata and controls
625 lines (507 loc) · 17.9 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import os
import subprocess
import requests
import socket
import time
from prettytable import PrettyTable
# subprocess settings
EXECUTABLE = "proxyServer"
C_FILE = "*.c"
H_FILES = "*.h"
PORT = 3000
# timeout settings
WAIT_TO_INIT = 3
SOCKET_TIMEOUT = 10
DEADLOCK_TIMEOUT = 20
class LocalHostClient:
"""
TCP client that connects to localhost
"""
def __init__(self, port, timeout=10):
self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self._s.connect(('localhost', port))
self._s.settimeout(timeout)
except ConnectionError as e:
self._s.close()
raise e
def recvall(self) -> bytes:
rbytes = b""
while True:
try:
data = self._s.recv(1024)
except BrokenPipeError as e:
self._s.close()
raise e
if not data:
break
rbytes += data
return rbytes
def sendall(self, data: bytes):
try:
self._s.sendall(data)
except BrokenPipeError as e:
self._s.close()
raise e
def die(self):
self._s.close()
def check_errors(file_name: str):
summery = "ERROR SUMMARY: "
with open(file_name, "r") as valgrind_log:
log_output = valgrind_log.read()
summmey_idx = log_output.find(summery)
if summmey_idx == -1:
raise IndexError
errs_loc = log_output[summmey_idx + len(summery):]
num_of_errs = errs_loc.split(' ', 1)[0]
if int(num_of_errs) != 0:
return True
return False
def check_leaks(file_name: str):
leaks = True
with open(file_name, 'r') as valgrind_log:
val_lines = valgrind_log.readlines()
for line in val_lines:
if "no leaks are possible" in line:
leaks = False
if leaks:
print("[-] valgrind found memory leaks, check your code for allocation/freeing errors.")
return True
return False
def valgrind():
print("[!] Valgrind Test")
subprocess_args = ["valgrind", "--leak-check=full", "--tool=memcheck", "--show-leak-kinds=all",
"--track-origins=yes", "--verbose", "--error-exitcode=1", "-v",
"--log-file=valgrind-out.txt", f"./{EXECUTABLE}", str(PORT), "3", "2", "filter.txt"]
proc = subprocess.Popen(subprocess_args, stderr=subprocess.PIPE)
time.sleep(WAIT_TO_INIT)
try:
with open("stdout_valgrind.txt", "wb") as fp:
cli1 = LocalHostClient(PORT, timeout=SOCKET_TIMEOUT)
cli2 = LocalHostClient(PORT, timeout=SOCKET_TIMEOUT)
cli1.sendall(b"GET / HTTP/1.1\r\nHost: octopress.org\r\nConnection: close\r\n\r\n")
cli2.sendall(b"GET / HTTP/1.1\r\nHost: http.badssl.com\r\n\r\n")
res1 = cli1.recvall()
res2 = cli2.recvall()
fp.write(res1 + b"\n" + res2)
proc.communicate(timeout=5)
except (
subprocess.TimeoutExpired, socket.timeout,
ConnectionError, BrokenPipeError
) as e:
if isinstance(e, ConnectionRefusedError):
err = proc.communicate(timeout=3)
if "in use" in str(e):
raise ConnectionRefusedError(e)
proc.terminate()
return False
cli1.die()
cli2.die()
if check_leaks("valgrind-out.txt") is True:
return False
if check_errors("valgrind-out.txt") is True:
return False
res1 = res1.lower()
res2 = res2.lower()
if b"200 OK".lower() not in res1:
return False
if b"403 Forbidden".lower() not in res2:
return False
if proc.returncode > 127:
return False
return True
def deadlock():
print("[!] Deadlock Test")
subprocess_args = [f"./{EXECUTABLE}", str(PORT), "4", "10", "filter.txt"]
proc = subprocess.Popen(subprocess_args, stderr=subprocess.PIPE)
time.sleep(WAIT_TO_INIT)
clients, res = [], []
reqs = [
b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n",
b"GET / HTTP/1.1\r\nHost: pdf995.com\r\nConnection: close\r\n\r\n",
b"GET / HTTP/1.1\r\nHost: clearshinyshininglight.neverssl.com\r\nConnection: close\r\n\r\n",
b"GET / HTTP/1.1\r\nHost: octopress.org\r\nConnection: close\r\n\r\n",
b"GET / HTTP/1.1\r\nHost: info.cern.ch\r\nConnection: close\r\n\r\n",
b"GET /samples/widgets.pdf HTTP/1.1\r\nHost: pdf995.com\r\nConnection: close\r\n\r\n",
b"GET / HTTP/5.6\r\nHost: example.com\r\n\r\n", # bad request
b"UPDATE / HTTP/1.1\r\nHost: example.com\r\n\r\n", # not supported
b"GET /index HTTP/1.1\r\nHost: myunknownhost.com\r\n\r\n", # not found
b"GET / HTTP/1.1\r\nHost: http.badssl.com\r\n\r\n", # forbidden
]
with open("stdout_deadlock.txt", "wb") as fp:
try:
for i in range(5):
clients.append(LocalHostClient(PORT, timeout=DEADLOCK_TIMEOUT))
for i in range(5):
clients[i].sendall(reqs[i])
for i in range(5):
res.append(clients[i].recvall())
for i in range(5):
clients.append(LocalHostClient(PORT, timeout=DEADLOCK_TIMEOUT))
clients[i + 5].sendall(reqs[i + 5])
res.append(clients[i + 5].recvall())
except (socket.timeout, ConnectionError, BrokenPipeError) as e:
print(e)
if isinstance(e, ConnectionRefusedError):
_, err = proc.communicate(timeout=3)
if "in use" in str(err):
raise ConnectionRefusedError(e)
for c in clients:
c.die()
proc.terminate()
return False
fp.write(b"\n".join(res))
res = [r.lower() for r in res]
for c in clients:
c.die()
if sum(r.count(b"200 OK".lower()) for r in res) != 6:
return False
if b"400 Bad Request".lower() not in res[6]:
return False
if b"501 Not supported".lower() not in res[7]:
return False
if b"404 Not Found".lower() not in res[8]:
return False
if b"403 Forbidden".lower() not in res[9]:
return False
try:
proc.communicate(timeout=3)
except subprocess.TimeoutExpired as e:
print(e)
proc.terminate()
return False
if proc.returncode > 127:
return False
return True
def keep_alive():
"""
Send a request with Connection: keep-alive header
"""
print("[!] Keep Alive Test")
subprocess_args = [f"./{EXECUTABLE}", str(PORT), "1", "1", "filter.txt"]
proc = subprocess.Popen(subprocess_args, stderr=subprocess.PIPE)
time.sleep(WAIT_TO_INIT)
try:
with open("stdout_keep_alive.txt", "wb") as fp:
cli = LocalHostClient(PORT, timeout=10)
cli.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: keep-alive\r\n\r\n")
res = cli.recvall()
proc.communicate(timeout=SOCKET_TIMEOUT)
fp.write(res)
except (
subprocess.TimeoutExpired, socket.timeout,
ConnectionError, BrokenPipeError
) as e:
if isinstance(e, ConnectionRefusedError):
_, err = proc.communicate(timeout=3)
if "in use" in str(err):
raise ConnectionRefusedError(e)
proc.terminate()
return False
cli.die()
if b"200 OK" not in res:
return False
if proc.returncode > 127:
return False
return True
def unsigned_char_support():
"""
Request for an image and check whether all bytes have been received
"""
print("[!] Unsigned Char Support Test")
subprocess_args = [f"./{EXECUTABLE}", str(PORT), "1", "1", "filter.txt"]
proc = subprocess.Popen(subprocess_args, stderr=subprocess.PIPE)
time.sleep(WAIT_TO_INIT)
try:
with open("stdout_unsigned_char_support.txt", "wb") as fp:
cli = LocalHostClient(PORT, timeout=SOCKET_TIMEOUT)
req = b"GET /images2015/wa_banner_excellence1024-plain.jpg HTTP/1.1\r\n" \
b"Host: webaward.org\r\n" \
b"Connection: close\r\n\r\n"
cli.sendall(req)
res = cli.recvall()
proc.communicate(timeout=3)
fp.write(res)
req = requests.get("http://webaward.org/images2015/wa_banner_excellence1024-plain.jpg")
if req.status_code != 200:
raise ValueError("invalid request status code")
except (
subprocess.TimeoutExpired, socket.timeout,
ConnectionError, BrokenPipeError
) as e:
print(e)
if isinstance(e, ConnectionRefusedError):
_, err = proc.communicate(timeout=3)
if "in use" in str(err):
raise ConnectionRefusedError(e)
proc.terminate()
return False
cli.die()
if req.content not in res:
return False
if proc.returncode > 127:
return False
return True
def is_read_within_loop():
"""
Slice request and send it in parts
"""
print("[!] Is Read Within Loop Test")
subprocess_args = [f"./{EXECUTABLE}", str(PORT), "1", "1", "filter.txt"]
proc = subprocess.Popen(subprocess_args, stderr=subprocess.PIPE)
time.sleep(WAIT_TO_INIT)
try:
with open("stdout_read_within_loop.txt", "wb") as fp:
cli = LocalHostClient(PORT, timeout=10)
req_slices = [
b"GET ", b"/ ", b"HTTP/1.1\r\n",
b"Host: ", b"example.com\r\n",
b"Connection: ", b"close\r\n\r\n"
]
for rslice in req_slices:
time.sleep(0.5)
cli.sendall(rslice)
res = cli.recvall()
proc.communicate(timeout=3)
fp.write(res)
except (
subprocess.TimeoutExpired, socket.timeout,
ConnectionError, BrokenPipeError
) as e:
if isinstance(e, ConnectionRefusedError):
_, err = proc.communicate(timeout=3)
if "in use" in str(err):
raise ConnectionRefusedError(e)
proc.terminate()
return False
cli.die()
if b"200 OK" not in res:
return False
if proc.returncode > 127:
return False
return True
def validate_response_error(response: bytes, code: int) -> bool:
response = response.lower()
if code == 400:
err = b"400 Bad Request"
description = "Bad Request."
elif code == 403:
err = b"403 Forbidden"
description = "Access denied."
elif code == 404:
err = b"404 Not Found"
description = "File not found."
elif code == 500:
err = b"500 Internal Server Error"
description = "Some server side error."
elif code == 501:
err = b"501 Not supported"
description = "Method is not supported."
else:
raise ValueError("invalid response code")
body1 = f"<HTML><HEAD><TITLE>{err.decode()}</TITLE></HEAD>\r\n" \
f"<BODY><H4>{err.decode()}</H4>\r\n" \
f"{description}\r\n" \
f"</BODY></HTML>\r\n".encode()
body2 = f"<HTML><HEAD><TITLE>{err.decode()}</TITLE></HEAD>\n" \
f"<BODY><H4>{err.decode()}</H4>\n" \
f"{description}\n" \
f"</BODY></HTML>\n".encode()
body3 = f"<HTML><HEAD><TITLE>{err.decode()}</TITLE></HEAD>\r\n" \
f"<BODY><H4>{err.decode()}</H4>\r\n" \
f"{description}\r\n" \
f"</BODY></HTML>".encode()
body4 = f"<HTML><HEAD><TITLE>{err.decode()}</TITLE></HEAD>\n" \
f"<BODY><H4>{err.decode()}</H4>\n" \
f"{description}\n" \
f"</BODY></HTML>".encode()
body5 = f"<HTML><HEAD><TITLE>{err.decode()}</TITLE></HEAD>" \
f"<BODY><H4>{err.decode()}</H4>" \
f"{description}" \
f"</BODY></HTML>".encode()
content_length1 = f"Content-Length: {len(body1)}\r\n".encode()
content_length2 = f"Content-Length: {len(body2)}\r\n".encode()
content_length3 = f"Content-Length: {len(body3)}\r\n".encode()
content_length4 = f"Content-Length: {len(body4)}\r\n".encode()
content_length5 = f"Content-Length: {len(body5)}\r\n".encode()
if (b"HTTP/1.1 " + err + b"\r\n").lower() not in response:
return False
if b"Server: webserver/1.0\r\n".lower() not in response:
return False
if b"Date: ".lower() not in response:
return False
if b"Content-Type: text/html\r\n".lower() not in response:
return False
if b"Connection: close\r\n".lower() not in response:
return False
if content_length1.lower() in response and body1.lower() in response:
pass
elif content_length2.lower() in response and body2.lower() in response:
pass
elif content_length3.lower() in response and body3.lower() in response:
pass
elif content_length4.lower() in response and body4.lower() in response:
pass
elif content_length5.lower() in response and body5.lower() in response:
pass
else:
return False
return True
def invalid_request(stdout_name: str, req: bytes, code: int) -> bool:
subprocess_args = [f"./{EXECUTABLE}", str(PORT), "1", "1", "filter.txt"]
proc = subprocess.Popen(subprocess_args, stderr=subprocess.PIPE)
time.sleep(WAIT_TO_INIT)
try:
with open(stdout_name, "wb") as fp:
cli = LocalHostClient(PORT, timeout=10)
cli.sendall(req)
res = cli.recvall()
proc.communicate(timeout=3)
fp.write(res)
except (
subprocess.TimeoutExpired, socket.timeout,
ConnectionError, BrokenPipeError
) as e:
if isinstance(e, ConnectionRefusedError):
_, err = proc.communicate(timeout=3)
if "in use" in str(err):
raise ConnectionRefusedError(e)
proc.terminate()
return False
cli.die()
if not validate_response_error(res, code):
return False
if proc.returncode > 127:
return False
return True
def forbidden():
"""
HTTP request with forbidden ip
"""
print("[!] Forbidden Test")
return invalid_request(
"stdout_forbidden.txt",
req=b"GET / HTTP/1.1\r\nHost: http.badssl.com\r\n\r\n",
code=403
)
def not_found():
"""
HTTP request with invalid host
"""
print("[!] Not Found Test")
return invalid_request(
"stdout_not_found.txt",
req=b"GET /index HTTP/1.1\r\nHost: myunknownhost.com\r\n\r\n",
code=404
)
def not_supported():
"""
HTTP request with UPDATE method
"""
print("[!] Not Supported Test")
return invalid_request(
"stdout_not_supported.txt",
req=b"UPDATE / HTTP/1.1\r\nHost: example.com\r\n\r\n",
code=501
)
def bad_request():
"""
HTTP request with bad HTTP version
"""
print("[!] Bad Request Test")
return invalid_request(
"stdout_bad_request.txt",
req=b"GET / HTTP/5.6\r\nHost: example.com\r\n\r\n",
code=400
)
def usage(args: str, v: int):
"""
1) Usage Test: ProxyServer
2) Usage Test ProxyServer 3000 2 -1
"""
print(f"[!] Usage{1} Test")
try:
with open(f"stdout_test_usage_{v}.txt", "w") as fp:
proc = subprocess.run(
[f"./{EXECUTABLE}"] + args.split(" "),
stdout=fp,
stderr=fp,
text=True,
timeout=5
)
except subprocess.TimeoutExpired as e:
print(e)
return False
try:
with open(f"stdout_test_usage_{v}.txt") as fp:
res = fp.read().lower().rstrip()
except UnicodeDecodeError as e:
print(e)
return False
usage1 = "Usage: proxyServer <port> <pool-size> <max-number-of-request> <filter>".lower()
if res != usage1 and res != "./" + usage1:
return False
return True
def setup():
if os.path.isfile(EXECUTABLE):
os.remove(EXECUTABLE)
with open("stdout_compilation.txt", 'w') as out_file:
c = subprocess.run(
f'gcc -Wall {C_FILE} {H_FILES} -o {EXECUTABLE} -lpthread',
stderr=out_file,
stdout=out_file,
shell=True,
)
with open("stdout_compilation.txt") as out_file:
res = out_file.read()
return_val = None
if bytes(res, 'utf-8') == b'':
print("Ex. compiled successfully.")
return_val = "Compiled"
if "warning: " in res:
print("Warnings during compilation")
return_val = "Warnings"
if "error: " in res:
print("\nSomething didn't go right when compiling your C source "
"please check stdout_compilation.txt\n")
return_val = "Error"
return return_val
if __name__ == "__main__":
compilation_status = setup()
if compilation_status == "Error":
exit(0)
t_usage1 = usage("", v=1)
PORT = PORT + 1
t_usage2 = usage("3000 1 3", v=2)
PORT = PORT + 1
t_bad_request = bad_request()
PORT = PORT + 1
t_not_found = not_found()
PORT = PORT + 1
t_not_supported = not_supported()
PORT = PORT + 1
t_forbidden = forbidden()
PORT = PORT + 1
t_is_read_within_loop = is_read_within_loop()
PORT = PORT + 1
t_unsigned_char = unsigned_char_support()
PORT = PORT + 1
t_keep_alive = keep_alive()
PORT = PORT + 1
t_deadlock = deadlock()
PORT = PORT + 1
t_valgrind = valgrind()
t = PrettyTable(["Test", "Result"])
t.align['Test'] = 'l'
t.add_row(["Usage1", t_usage1])
t.add_row(["Usage2", t_usage2])
t.add_row(["Bad Request", t_bad_request])
t.add_row(["Not Found", t_not_found])
t.add_row(["Not Supported", t_not_supported])
t.add_row(["Forbidden", t_forbidden])
t.add_row(["Is Read Within Loop", t_is_read_within_loop])
t.add_row(["Is Unsigned Char Supported", t_unsigned_char])
t.add_row(["Keep Alive", t_keep_alive])
t.add_row(["Deadlock", t_deadlock])
t.add_row(["Valgrind", t_valgrind])
print(t)