This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate.py
executable file
·54 lines (42 loc) · 1.74 KB
/
generate.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
#!/usr/bin/env python3
import hmac
import json
import os
import os.path
import random
import sys
import subprocess
import tempfile
PREFIX = "ugra_teacher_is_so_proud_of_you_"
SECRET = b"which-pagan-woodlands-scream-victoria"
SALT_SIZE = 12
def get_flag():
user_id = sys.argv[1]
return PREFIX + hmac.new(SECRET, str(user_id).encode(), "sha256").hexdigest()[:SALT_SIZE]
def generate():
if len(sys.argv) < 3:
print("Usage: generate.py user_id target_dir", file=sys.stderr)
sys.exit(1)
user_id = sys.argv[1]
target_dir = sys.argv[2]
random.seed(hmac.new(SECRET, str(user_id).encode(), "sha256").digest())
flag = get_flag()
flag_packed = ""
for c in flag:
t = 0
while t < ord(c):
t0 = min(random.randint(1, 15), ord(c) - t)
flag_packed += f"\\x{t0:0x}"
t += t0
flag_packed += "\\x7f"
with tempfile.TemporaryDirectory() as temp_dir:
open(os.path.join(temp_dir, "history.cpp"), "w").write(
open(os.path.join("private", "history.cpp")).read().replace("+++flag+++", flag_packed)
)
image_name = subprocess.run(["docker", "build", "-q", "-f", "Dockerfile", temp_dir], capture_output=True, check=True).stdout.decode("utf-8").strip()
container_name = subprocess.run(["docker", "create", image_name], capture_output=True, check=True).stdout.decode("utf-8").strip()
subprocess.check_call(["docker", "cp", f"{container_name}:/root/history", os.path.join(target_dir, "history")], stdout=subprocess.DEVNULL)
subprocess.check_call(["docker", "rm", container_name], stdout=subprocess.DEVNULL)
json.dump({"flags": [flag], "substitutions": {}, "urls": []}, sys.stdout)
if __name__ == "__main__":
generate()