generated from C4T-BuT-S4D/ad-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.py
executable file
·658 lines (532 loc) · 19.8 KB
/
check.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
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#!/usr/bin/env python3
import argparse
import json
import os
import random
import secrets
import string
import subprocess
import time
import traceback
from collections import defaultdict
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from enum import Enum
from pathlib import Path
from threading import Lock, current_thread
from typing import List, Tuple
import yaml
from dockerfile_parse import DockerfileParser
BASE_DIR = Path(__file__).resolve().absolute().parent
SERVICES_PATH = BASE_DIR / "services"
CHECKERS_PATH = BASE_DIR / "checkers"
MAX_THREADS = int(os.getenv("MAX_THREADS", default=2 * os.cpu_count()))
RUNS = int(os.getenv("RUNS", default=10))
HOST = os.getenv("HOST", default="127.0.0.1")
OUT_LOCK = Lock()
DISABLE_LOG = False
DC_REQUIRED_OPTIONS = ["services"]
DC_ALLOWED_OPTIONS = DC_REQUIRED_OPTIONS + ["volumes", "version"]
CONTAINER_REQUIRED_OPTIONS = ["restart"]
CONTAINER_ALLOWED_OPTIONS = CONTAINER_REQUIRED_OPTIONS + [
"pids_limit",
"mem_limit",
"cpus",
"build",
"image",
"ports",
"volumes",
"environment",
"env_file",
"healthcheck",
"depends_on",
"sysctls",
"privileged",
"security_opt",
"command",
]
SERVICE_REQUIRED_OPTIONS = ["pids_limit", "mem_limit", "cpus"]
SERVICE_ALLOWED_OPTIONS = CONTAINER_ALLOWED_OPTIONS
DATABASES = [
"redis",
"postgres",
"mysql",
"mariadb",
"mongo",
"mssql",
"clickhouse",
"tarantool",
]
PROXIES = ["nginx", "envoy"]
CLEANERS = ["dedcleaner"]
VALIDATE_DIRS = ["checkers", "services", "internal", "sploits"]
ALLOWED_CHECKER_PATTERNS = [
"import requests",
"requests.exceptions",
"s: requests.Session",
"sess: requests.Session",
"session: requests.Session",
"r: requests.Response",
"resp: requests.Response",
"Got requests connection error",
]
FORBIDDEN_CHECKER_PATTERNS = ["requests"]
ALLOWED_YAML_FILES = [
"buf.yaml",
"buf.gen.yaml",
"application.yaml",
]
class ColorType(Enum):
INFO = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
BOLD = "\033[1m"
ENDC = "\033[0m"
def __str__(self):
return self.value
def generate_flag(name):
alph = string.ascii_uppercase + string.digits
return name[0].upper() + "".join(random.choices(alph, k=30)) + "="
def colored_log(*messages, color: ColorType = ColorType.INFO):
ts = datetime.utcnow().isoformat(sep=" ", timespec="milliseconds")
print(
f"{color}{color.name} [{current_thread().name} {ts}]{ColorType.ENDC}", *messages
)
class BaseValidator:
def _log(self, message: str):
with OUT_LOCK:
if not DISABLE_LOG:
colored_log(f"{self}: {message}")
def _fatal(self, cond, message):
global DISABLE_LOG
with OUT_LOCK:
if not cond:
if not DISABLE_LOG:
colored_log(f"{self}: {message}", color=ColorType.FAIL)
DISABLE_LOG = True
raise AssertionError
def _warning(self, cond: bool, message: str) -> bool:
with OUT_LOCK:
if not cond and not DISABLE_LOG:
colored_log(f"{self}: {message}", color=ColorType.WARNING)
return not cond
def _error(self, cond, message) -> bool:
with OUT_LOCK:
if not cond and not DISABLE_LOG:
colored_log(f"{self}: {message}", color=ColorType.FAIL)
return not cond
class Checker(BaseValidator):
def __init__(self, name: str):
self._name = name
self._exe_path = CHECKERS_PATH / self._name / "checker.py"
self._fatal(
os.access(self._exe_path, os.X_OK),
f"{self._exe_path.relative_to(BASE_DIR)} must be executable",
)
self._timeout = 3
self._get_info()
def _get_info(self):
self._log("running info action")
cmd = [str(self._exe_path), "info", HOST]
out, _ = self._run_command(cmd)
info = json.loads(out)
self._log(f"got info: {info}")
self._vulns = int(info["vulns"])
self._timeout = int(info["timeout"])
self._attack_data = bool(info["attack_data"])
self._fatal(
60 > self._timeout > 0,
f"invalid timeout: {self._timeout}",
)
@property
def info(self):
return {
"vulns": self._vulns,
"timeout": self._timeout,
"attack_data": self._attack_data,
}
def _run_command(self, command: List[str], env=None) -> Tuple[str, str]:
action = command[1].upper()
cmd = ["timeout", str(self._timeout)] + command
if env is None:
env = os.environ
env["PYTHONUNBUFFERED"] = "1"
env["PWNLIB_NOTERM"] = "1"
start = time.monotonic()
p = subprocess.run(cmd, capture_output=True, check=False, env=env)
elapsed = time.monotonic() - start
out = p.stdout.decode()
err = p.stderr.decode()
out_s = out.rstrip("\n")
err_s = err.rstrip("\n")
self._log(
f"action: {action}\ntime: {elapsed:.2f}s\nstdout:\n{out_s}\nstderr:\n{err_s}"
)
self._fatal(
p.returncode != 124,
f"action {action}: bad return code: 124, probably {ColorType.BOLD}timeout{ColorType.ENDC}",
)
self._fatal(
p.returncode == 101, f"action {action}: bad return code: {p.returncode}"
)
return out, err
def check(self):
self._log("running CHECK")
cmd = [str(self._exe_path), "check", HOST]
self._run_command(cmd)
def put(self, flag: str, flag_id: str, vuln: int):
self._log(f"running PUT, flag={flag} flag_id={flag_id} vuln={vuln}")
cmd = [str(self._exe_path), "put", HOST, flag_id, flag, str(vuln)]
out, err = self._run_command(cmd)
self._fatal(len(out) <= 1024, "returned stdout is longer than 1024 characters")
self._fatal(len(err) <= 1024, "returned stderr is longer than 1024 characters")
if self._attack_data:
self._fatal(out, "stdout is empty")
self._fatal(err, "stderr is empty")
self._fatal(flag not in out, "flag is leaked in public data")
# new flag ID is in stderr for attack_data checkers
return err
self._fatal(out, "stdout is empty")
# new flag ID is in stdout for checkers without attack_data
return out
def get(self, flag: str, flag_id: str, vuln: int):
self._log(f"running GET, flag={flag} flag_id={flag_id} vuln={vuln}")
cmd = [str(self._exe_path), "get", HOST, flag_id, flag, str(vuln)]
self._run_command(cmd)
def run_all(self, step: int):
self._log(f"running all actions (run {step} of {RUNS})")
self.check()
for vuln in range(1, self._vulns + 1):
flag = generate_flag(self._name)
flag_id = self.put(flag=flag, flag_id=secrets.token_hex(16), vuln=vuln)
flag_id = flag_id.strip()
self.get(flag, flag_id, vuln)
def __str__(self):
return f"checker {self._name}"
class Service(BaseValidator):
def __init__(self, name: str):
self._name = name
self._path = SERVICES_PATH / self._name
self._dc_path = self._path / "docker-compose.yml"
self._fatal(
self._dc_path.exists(),
f"{self._dc_path.relative_to(BASE_DIR)} missing",
)
self._checker = Checker(self._name)
@property
def name(self):
return self._name
@property
def checker_info(self):
return self._checker.info
def _run_dc(self, *args):
cmd = ["docker", "compose", "-f", str(self._dc_path)] + list(args)
subprocess.run(cmd, check=True)
def up(self):
self._log("starting")
self._run_dc("up", "--build", "-d")
def logs(self):
self._log("printing logs")
self._run_dc("logs", "--tail", "2000")
def down(self):
self._log("stopping")
self._run_dc("down", "-v")
def validate_checker(self):
self._log("validating checker")
cnt_threads = max(1, min(MAX_THREADS, RUNS // 10))
self._log(f"starting {cnt_threads} checker threads")
with ThreadPoolExecutor(
max_workers=cnt_threads,
thread_name_prefix="Executor",
) as executor:
for _ in executor.map(self._checker.run_all, range(1, RUNS + 1)):
pass
def __str__(self):
return f"service {self._name}"
class StructureValidator(BaseValidator):
def __init__(self, d: Path, service: Service):
self._dir = d
self._was_error = False
self._service = service
def _error(self, cond, message):
err = super()._error(cond, message)
self._was_error |= err
return err
def validate(self):
for d in VALIDATE_DIRS:
self.validate_dir(self._dir / d / self._service.name)
return not self._was_error
def validate_dir(self, d: Path):
if not d.exists():
return
for f in d.iterdir():
if f.is_file():
self.validate_file(f)
elif f.name[0] != ".":
self.validate_dir(f)
def validate_file(self, f: Path):
path = f.relative_to(BASE_DIR)
if f.name not in ALLOWED_YAML_FILES:
self._error(f.suffix != ".yaml", f"file {path} has .yaml extension")
self._error(f.name != ".gitkeep", f"{path} found, should be named .keep")
if f.name == "docker-compose.yml":
with f.open() as file:
dc = yaml.safe_load(file)
if self._error(isinstance(dc, dict), f"{path} is not dict"):
return
for opt in DC_REQUIRED_OPTIONS:
if self._error(opt in dc, f"required option {opt} not in {path}"):
return
if "version" in dc:
if self._error(
isinstance(dc["version"], str),
f"version option in {path} is not string",
):
return
try:
dc_version = float(dc["version"])
except ValueError:
self._error(False, f"version option in {path} is not float")
return
self._error(
2.4 <= dc_version < 3,
f"invalid version in {path}, need >=2.4 and <3 (or no version at all), got {dc_version}",
)
for opt in dc:
self._error(
opt in DC_ALLOWED_OPTIONS,
f"option {opt} in {path} is not allowed",
)
services = []
databases = []
proxies = []
dependencies = defaultdict(list)
if self._error(
isinstance(dc["services"], dict),
f"services option in {path} is not dict",
):
return
for container, container_conf in dc["services"].items():
if self._error(
isinstance(container_conf, dict),
f"config in {path} for container {container} is not dict",
):
continue
for opt in CONTAINER_REQUIRED_OPTIONS:
self._error(
opt in container_conf,
f"required option {opt} not in {path} for container {container}",
)
self._error(
"restart" in container_conf
and container_conf["restart"] == "unless-stopped",
f'restart option in {path} for container {container} must be equal to "unless-stopped"',
)
for opt in container_conf:
self._error(
opt in CONTAINER_ALLOWED_OPTIONS
or (opt == "command" and container == "redis")
or (
container == "goldarn"
and opt in ["network_mode", "tmpfs", "healthcheck"]
),
f"option {opt} in {path} is not allowed for container {container}",
)
if self._error(
"image" not in container_conf or "build" not in container_conf,
f"both image and build options in {path} for container {container}",
):
continue
if self._error(
"image" in container_conf or "build" in container_conf,
f"both image and build options not in {path} for container {container}",
):
continue
if "image" in container_conf:
image = container_conf["image"]
else:
build = container_conf["build"]
if isinstance(build, str):
dockerfile = f.parent / build / "Dockerfile"
else:
context = build["context"]
if "dockerfile" in build:
dockerfile = f.parent / context / build["dockerfile"]
else:
dockerfile = f.parent / context / "Dockerfile"
if self._error(
dockerfile.exists(), f"no dockerfile found in {dockerfile}"
):
continue
with dockerfile.open() as file:
dfp = DockerfileParser(fileobj=file)
image = dfp.baseimage
if self._error(
image is not None, f"no image option in {dockerfile}"
):
continue
if "depends_on" in container_conf:
for dependency in container_conf["depends_on"]:
dependencies[container].append(dependency)
is_service = True
for database in DATABASES:
if database in image:
databases.append(container)
is_service = False
for proxy in PROXIES:
if proxy in image:
proxies.append(container)
is_service = False
for cleaner in CLEANERS:
if cleaner in image:
is_service = False
if is_service:
services.append(container)
for opt in SERVICE_REQUIRED_OPTIONS:
self._error(
opt in container_conf,
f"required option {opt} not in {path} for service {container}",
)
for opt in container_conf:
self._error(
opt in SERVICE_ALLOWED_OPTIONS
or (
container == "goldarn"
and opt in ["network_mode", "tmpfs", "healthcheck"]
),
f"option {opt} in {path} is not allowed for service {container}",
)
for service in services:
for database in databases:
self._warning(
service in dependencies and database in dependencies[service],
f"service {service} may need to depends_on database {database}",
)
for proxy in proxies:
for service in services:
self._warning(
proxy in dependencies and service in dependencies[proxy],
f"proxy {proxy} may need to depends_on service {service}",
)
elif BASE_DIR / "checkers" in f.parents and f.suffix == ".py":
checker_code = f.read_text()
for p in ALLOWED_CHECKER_PATTERNS:
checker_code = checker_code.replace(p, "")
for p in FORBIDDEN_CHECKER_PATTERNS:
self._error(p not in checker_code, f'forbidden pattern "{p}" in {path}')
def __str__(self):
return f"Structure validator for {self._service.name}"
def get_services() -> List[Service]:
if os.getenv("SERVICE") in ["all", None]:
result = list(
Service(service_path.name)
for service_path in SERVICES_PATH.iterdir()
if service_path.name[0] != "." and service_path.is_dir()
)
else:
result = [Service(os.environ["SERVICE"])]
with OUT_LOCK:
colored_log("Got services:", ", ".join(map(str, result)))
return result
def list_services(_args):
services = get_services()
if outfile := os.getenv("GITHUB_OUTPUT"):
data = {
"include": [{"service": service.name} for service in services],
}
with open(outfile, "a") as f:
f.write(f"matrix={json.dumps(data)}")
def start_services(_args):
for service in get_services():
service.up()
def stop_services(_args):
for service in get_services():
service.down()
def logs_services(_args):
for service in get_services():
service.logs()
def validate_checkers(_args):
for service in get_services():
service.validate_checker()
def validate_structure(_args):
was_error = False
for service in get_services():
validator = StructureValidator(BASE_DIR, service)
if not validator.validate():
was_error = True
if was_error:
with OUT_LOCK:
colored_log("Structure validator: failed", color=ColorType.FAIL)
raise AssertionError
def dump_tasks(_args):
result = {"tasks": []}
for service in get_services():
info = service.checker_info
checker_type = "gevent"
if info["attack_data"]:
checker_type += "_pfr"
result["tasks"].append(
{
"name": service.name,
"checker": f"{service.name}/checker.py",
"checker_timeout": info["timeout"],
"checker_type": checker_type,
"places": info["vulns"],
"puts": 1,
"gets": 1,
}
)
colored_log("\n" + yaml.safe_dump(result))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Validate checkers for A&D. "
"Host & number of runs are passed with HOST and RUNS env vars"
)
subparsers = parser.add_subparsers()
list_parser = subparsers.add_parser(
"list",
help="List services to test",
)
list_parser.set_defaults(func=list_services)
up_parser = subparsers.add_parser(
"up",
help="Start services",
)
up_parser.set_defaults(func=start_services)
down_parser = subparsers.add_parser(
"down",
help="Stop services",
)
down_parser.set_defaults(func=stop_services)
logs_parser = subparsers.add_parser(
"logs",
help="Print logs for services",
)
logs_parser.set_defaults(func=logs_services)
check_parser = subparsers.add_parser(
"check",
help="Run checkers validation",
)
check_parser.set_defaults(func=validate_checkers)
validate_parser = subparsers.add_parser(
"validate",
help="Run structure validation",
)
validate_parser.set_defaults(func=validate_structure)
dump_parser = subparsers.add_parser(
"dump_tasks",
help="Dump tasks in YAML for ForcAD",
)
dump_parser.set_defaults(func=dump_tasks)
parsed = parser.parse_args()
if "func" not in parsed:
print("Type -h")
exit(1)
try:
parsed.func(parsed)
except AssertionError:
exit(1)
except Exception as e:
tb = traceback.format_exc()
print("Got exception, report it:", e, tb)
exit(1)