-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig-builder.py
82 lines (60 loc) · 2.47 KB
/
config-builder.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
################################################################################
# Configure a box for the general ssh example challenge.
################################################################################
import sys
import os
import subprocess
import re
import zlib
import json
def main():
try:
# Generate password from seed =========================================
seed = os.environ.get("SEED")
if seed == "":
print("Seed was not read from filesystem. Aborting.")
sys.exit(1)
password = hex(zlib.crc32(seed.encode()))
password = password[2:]
with open("/challenge/password.txt", "w") as f:
f.write(password)
# =====================================================================
# Split flag into 3 parts ============================================
flag = os.environ.get("FLAG")
if flag == "":
print("Flag was not read from environment. Aborting.")
sys.exit(-1)
else:
# Get hash part
flag_rand = re.search("{.*}$", flag)
if flag_rand == None:
print("Flag isn't wrapped by curly braces. Aborting.")
sys.exit(-2)
else:
flag_rand = flag_rand.group()
flag_rand = flag_rand[1:-1]
flag_1of3 = "picoCTF{sh311_"
flag_2of3 = "n4v1g4t10n_ftw_"
flag_3of3 = flag_rand + "}"
flag = flag_1of3 + flag_2of3 + flag_3of3
with open("/challenge/1of3.flag.txt", "w") as f:
f.write(flag_1of3)
with open("/challenge/2of3.flag.txt", "w") as f:
f.write(flag_2of3)
with open("/challenge/3of3.flag.txt", "w") as f:
f.write(flag_3of3)
# =====================================================================
# Create and update metadata.json =====================================
metadata = {}
metadata['flag'] = str(flag)
metadata['password'] = str(password)
json_metadata = json.dumps(metadata)
with open("/challenge/metadata.json", "w") as f:
f.write(json_metadata)
# =====================================================================
except subprocess.CalledProcessError:
print("A subprocess has returned an error code")
sys.exit(1)
# =============================================================================
if __name__ == "__main__":
main()