88import shutil
99import subprocess
1010import tempfile
11- from typing import Iterable , Iterator , List , Optional , Sequence
11+ from typing import Any , Dict , Iterable , Iterator , List , Optional , Sequence
1212
1313import black
1414import jinja2
1515import pyastyle
16+ import yaml
1617
1718logger = logging .getLogger (__name__ )
1819
1920CHALLENGE_SEED = int (os .environ .get ("CHALLENGE_SEED" , "0" ))
2021
22+ CHALLENGE_CONFIG_DEFAULTS : Dict [str , Any ] = {
23+ "privileged" : False ,
24+ }
25+
26+
27+ def load_challenge_config (challenge_path : pathlib .Path ) -> Dict [str , Any ]:
28+ config = dict (CHALLENGE_CONFIG_DEFAULTS )
29+ config_file = challenge_path / "challenge.yml"
30+ if config_file .is_file ():
31+ logger .debug ("loading challenge config from %s" , config_file )
32+ with open (config_file ) as f :
33+ user_config = yaml .safe_load (f ) or {}
34+ config .update (user_config )
35+ return config
36+
2137
2238class _NoSelfExtendLoader (jinja2 .FileSystemLoader ):
2339 """FileSystemLoader that prevents templates from extending/including themselves.
@@ -136,7 +152,7 @@ def ignore_git_crypt(current, names):
136152
137153@contextlib .contextmanager
138154def run_challenge (
139- challenge_image : str , * , volumes : Optional [Sequence [pathlib .Path ]] = None
155+ challenge_image : str , * , volumes : Optional [Sequence [pathlib .Path ]] = None , privileged : bool = False
140156) -> Iterator [tuple [str , str ]]:
141157 flag = "pwn.college{" + base64 .b64encode (os .urandom (32 )).decode () + "}"
142158 env_options = []
@@ -149,6 +165,8 @@ def run_challenge(
149165 logger .info ("starting container for image %s" , challenge_image )
150166 if volumes :
151167 logger .debug ("mounting volumes: %s" , volumes )
168+ if privileged :
169+ logger .debug ("running container in privileged mode" )
152170 container = (
153171 subprocess .check_output (
154172 [
@@ -159,8 +177,7 @@ def run_challenge(
159177 "--detach" ,
160178 "--init" ,
161179 "--user=0:0" ,
162- "--device=/dev/kvm" ,
163- "--cap-add=SYS_PTRACE" ,
180+ * (["--privileged" ] if privileged else ["--device=/dev/kvm" , "--cap-add=SYS_PTRACE" ]),
164181 * env_options ,
165182 * [f"--volume={ volume } :{ volume } :ro" for volume in (volumes or [])],
166183 challenge_image ,
0 commit comments