-
Notifications
You must be signed in to change notification settings - Fork 40
treewide: add challenge config for privileged #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ dependencies = [ | |
| "click", | ||
| "jinja2", | ||
| "pyastyle", | ||
| "pyyaml", | ||
| "rich", | ||
| ] | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,16 +8,32 @@ | |
| import shutil | ||
| import subprocess | ||
| import tempfile | ||
| from typing import Iterable, Iterator, List, Optional, Sequence | ||
| from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence | ||
|
|
||
| import black | ||
| import jinja2 | ||
| import pyastyle | ||
| import yaml | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| CHALLENGE_SEED = int(os.environ.get("CHALLENGE_SEED", "0")) | ||
|
|
||
| CHALLENGE_CONFIG_DEFAULTS: Dict[str, Any] = { | ||
| "privileged": False, | ||
| } | ||
|
|
||
|
|
||
| def load_challenge_config(challenge_path: pathlib.Path) -> Dict[str, Any]: | ||
| config = dict(CHALLENGE_CONFIG_DEFAULTS) | ||
| config_file = challenge_path / "challenge.yml" | ||
| if config_file.is_file(): | ||
| logger.debug("loading challenge config from %s", config_file) | ||
| with open(config_file) as f: | ||
| user_config = yaml.safe_load(f) or {} | ||
| config.update(user_config) | ||
| return config | ||
|
Comment on lines
+27
to
+35
|
||
|
|
||
|
|
||
| class _NoSelfExtendLoader(jinja2.FileSystemLoader): | ||
| """FileSystemLoader that prevents templates from extending/including themselves. | ||
|
|
@@ -136,7 +152,7 @@ def ignore_git_crypt(current, names): | |
|
|
||
| @contextlib.contextmanager | ||
| def run_challenge( | ||
| challenge_image: str, *, volumes: Optional[Sequence[pathlib.Path]] = None | ||
| challenge_image: str, *, volumes: Optional[Sequence[pathlib.Path]] = None, privileged: bool = False | ||
| ) -> Iterator[tuple[str, str]]: | ||
| flag = "pwn.college{" + base64.b64encode(os.urandom(32)).decode() + "}" | ||
| env_options = [] | ||
|
|
@@ -149,6 +165,8 @@ def run_challenge( | |
| logger.info("starting container for image %s", challenge_image) | ||
| if volumes: | ||
| logger.debug("mounting volumes: %s", volumes) | ||
| if privileged: | ||
| logger.debug("running container in privileged mode") | ||
| container = ( | ||
| subprocess.check_output( | ||
| [ | ||
|
|
@@ -159,8 +177,7 @@ def run_challenge( | |
| "--detach", | ||
| "--init", | ||
| "--user=0:0", | ||
| "--device=/dev/kvm", | ||
| "--cap-add=SYS_PTRACE", | ||
| *(["--privileged"] if privileged else ["--device=/dev/kvm", "--cap-add=SYS_PTRACE"]), | ||
|
Comment on lines
154
to
+180
|
||
| *env_options, | ||
| *[f"--volume={volume}:{volume}:ro" for volume in (volumes or [])], | ||
| challenge_image, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reads
challenge.ymlwithopen(config_file)using the platform default encoding. To avoid locale-dependent failures, preferconfig_file.open(encoding="utf-8")(or an explicit encoding consistent with the rest of the file loader logic).