forked from mtusi2026/mtusi2026lab7
65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
import argparse
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def private_values():
|
|
result = []
|
|
|
|
def collect(value):
|
|
if isinstance(value, dict):
|
|
for item in value.values():
|
|
collect(item)
|
|
elif isinstance(value, list):
|
|
for item in value:
|
|
collect(item)
|
|
elif isinstance(value, str) and len(value) >= 20:
|
|
result.append(value)
|
|
|
|
for path in (ROOT / "secrets/operator").glob("*.json"):
|
|
collect(json.loads(path.read_text()))
|
|
for path in (ROOT / "secrets/operator").glob("*.txt"):
|
|
value = path.read_text().strip()
|
|
if len(value) >= 20:
|
|
result.append(value)
|
|
return result
|
|
|
|
|
|
def run(name, command):
|
|
started = datetime.now(timezone.utc).isoformat()
|
|
before = time.monotonic()
|
|
output = subprocess.run(command, cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
text = output.stdout.decode("utf-8", errors="replace").replace("\r\n", "\n")
|
|
if any(value in text for value in private_values()):
|
|
raise RuntimeError("Private value detected in command output; evidence withheld")
|
|
report = (
|
|
"Command: "
|
|
+ json.dumps(command, ensure_ascii=False)
|
|
+ "\nUTC: "
|
|
+ started
|
|
+ "\nExit code: "
|
|
+ str(output.returncode)
|
|
+ "\nElapsed seconds: "
|
|
+ str(round(time.monotonic() - before, 3))
|
|
+ "\n\n"
|
|
+ text
|
|
)
|
|
(ROOT / "evidence" / (name + ".txt")).write_text(report, encoding="utf-8", newline="\n")
|
|
print(text, end="", flush=True)
|
|
if output.returncode:
|
|
raise SystemExit(output.returncode)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("name")
|
|
parser.add_argument("command", nargs=argparse.REMAINDER)
|
|
args = parser.parse_args()
|
|
run(args.name, args.command)
|