-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_queries.py
More file actions
75 lines (61 loc) · 2.32 KB
/
generate_queries.py
File metadata and controls
75 lines (61 loc) · 2.32 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
"""
This script allows to quickly collect all flags from the SUASploitable data center machines (except basic).
To do so, you have to put all the flag files in a following tree structure:
├── matrikel
│ ├── cloud
│ │ └── flags.txt
│ ├── cms
│ │ └── flags.txt
│ └── devbox
│ └── flags.txt
├── db_statements.txt
└── generate_queries.py
You can put there as many matrikel directories as you need.
Then run this script. Afterwards you get a file called db_statements.txt.
This file containes the statements to run in the SUASecLab's database to add the flags to the users.
Note 1: When running this script, you need the UUIDs of the students accounts. These must match the matrikel numbers.
Note 2: Besides the matrikel directories, the directory you place this script in is only allowed to have a .git directory.
Having other directories in the root of this script will lead to errors.
"""
import os
# Get all directories
directories = os.listdir()
if ".git" in directories:
directories.remove(".git")
directories.remove("generate_queries.py")
if "db_statements.txt" in directories:
directories.remove("db_statements.txt")
db_statements = []
all_flags = []
for directory in directories:
uuid = input(f"Enter UUID for user with Matrikel {directory}: ")
uuid = uuid.strip()
flags = []
for root, directories, files in os.walk(directory):
for file in files:
if file.endswith("flags.txt"):
with open(os.path.join(root, file)) as flags_file:
for line in flags_file:
flags.append(f"exam-{line.strip()}")
db_statements.append(f"""db.users.updateOne(
{{"uuid": "{uuid}"}},
{{ $push: {{ "availableFlags": {{ $each: {flags}}}}}}}
)
""")
all_flags.extend(flags)
# Collect all flags
all_flags = set(all_flags)
add_flags_statement = "db.ctf.insertMany(["
for flag in all_flags:
add_flags_statement += (f"""
{{
"flag": "{flag}",
"type": "exam",
"description": "Exam flag"
}},""")
add_flags_statement += "\r\n])"
db_statements.append(add_flags_statement)
with open("db_statements.txt", "w") as db_file:
for statement in db_statements:
db_file.write(statement)
db_file.write("\r\n")