T09 · Insecure Skill Coding Practices
Error
- Location
- prng.md:94
- Finding
- Arbitrary Code Execution Through Unsafe Evaluation of External Data<![CDATA[ ## Vulnerability Details **File Location**: `prng.md:94-98` **Vulnerability Type**: Unsafe deserialization and dynamic code evaluation **Risk Level**: High ### Vulnerable Code ```python import random, gzip, hashlib # Load precomputed GF(2) magic matrix (from github.com/fx5/not_random) f = gzip.GzipFile("magic_data", "r") magic = eval(f.read()) f.close() ``` ### Technical Analysis The example passes decompressed file content directly to Python's `eval`. Unlike a data parser, `eval` accepts arbitrary Python expressions and executes them with the privileges of the current Python process. The `magic_data` file is described as originating from an external GitHub project, but the example does not authenticate its source, verify a checksum, validate its structure, or constrain evaluation. A malicious or replaced file could therefore contain expressions that import modules, execute commands, read local files, or initiate network connections. Although this is documentation code rather than automatically executed Skill code, users or agents following the example would expose their environment to arbitrary code execution. ### Attack Path 1. An attacker compromises or impersonates the source from which `magic_data` is obtained, or places a malicious file with that name in the working directory. 2. A user or agent follows the documented example. 3. `gzip.GzipFile` decompresses the attacker-controlled content. 4. `eval(f.read())` interprets that content as Python code rather than inert matrix data. 5. The payload executes with the Python process's filesystem, network, and operating-system permissions. For example, an expression using `__import__` could invoke operating-system functionality while still returning a value that appears compatible with the expected data. ### Impact Assessment Successful exploitation provides arbitrary code execution under the account running the example. The attacker could: - Read challenge data, source files, environment ...[truncated 448 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Replace `eval` with a non-executable serialization format such as JSON, MessagePack, or a documented binary matrix format. - If the upstream file is necessarily represented as Python literals, use `ast.literal_eval` instead: ```python import ast import gzip with gzip.open("magic_data", "rt", encoding="utf-8") as f: magic = ast.literal_eval(f.read()) ``` - Validate the parsed object before use, including: - Expected top-level container type. - Exact dimensions. - Permitted element types. - Numeric bounds and maximum input size. - Publish and verify a cryptographic checksum for the expected data file. - Obtain the file only from a pinned, reviewed upstream revision. - Run CTF tooling in a non-privileged container or isolated virtual machine with no unrelated credentials mounted. ]]>
