T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/image_finder.py:126
- Finding
- Predictable Temporary Screenshot Permits Symlink Attacks and Cross-Process Interference<![CDATA[ ## Vulnerability Details **File Location**: `scripts/image_finder.py`, lines 126–137; additional affected paths at lines 151–158, 179–190, 467–474, and 503–512 **Vulnerability Type**: Predictable temporary file and unsafe file replacement **Risk Level**: Medium ### Vulnerable Code ```python screenshot = pyautogui.screenshot() temp_path = ".temp_screenshot.png" screenshot.save(temp_path) print("正在初始化OCR引擎...") ocr = RapidOCR() result, elapse = ocr(temp_path) if os.path.exists(temp_path): os.remove(temp_path) ``` The same predictable path is also used by other OCR and image-marking operations: ```python screenshot = pyautogui.screenshot() temp_path = ".temp_screenshot.png" screenshot.save(temp_path) draw_matches_on_image(temp_path, args.mark_on_image, result) os.remove(temp_path) ``` ### Technical Analysis The script creates a screenshot under the fixed name `.temp_screenshot.png` in the current working directory. It does not create the file atomically, verify that the destination is a regular file, reject symbolic links, or assign a name unique to the running process. If the program is launched in a directory writable by another local user or untrusted process, an attacker can create `.temp_screenshot.png` as a symbolic link to another file. `screenshot.save()` may then follow that link and overwrite the linked target using the privileges of the process running the Skill. Concurrent invocations also share the same filename. One process may replace or delete another process's screenshot, causing incorrect OCR results, disclosure of captured screen data, failed operations, or deletion of an unexpected temporary entry. Cleanup is performed manually rather than through a `finally` block. An interruption or exception before deletion can therefore leave a screenshot containing potentially sensitive on-screen information in the working directory. ### Attack Path 1. The attacker identifies a working directory in which the victim will run `imag ...[truncated 1431 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Replace the fixed filename with a securely generated temporary file: ```python import os import tempfile temp_path = None try: with tempfile.NamedTemporaryFile( prefix="image_finder_", suffix=".png", delete=False ) as temp_file: temp_path = temp_file.name screenshot.save(temp_path) result, elapse = ocr(temp_path) finally: if temp_path: try: os.unlink(temp_path) except FileNotFoundError: pass ``` 2. Prefer a private `tempfile.TemporaryDirectory()` when several intermediate files are required. 3. Ensure temporary files are created with permissions that prevent access by other users. 4. Do not create temporary files in the caller-controlled current working directory. 5. Keep cleanup in a `finally` block so it runs after both successful and failed processing. 6. Apply the same remediation to every `.temp_screenshot.png` occurrence. 7. Where supported, verify with `os.lstat()` that paths are not symbolic links before processing. Secure atomic creation should remain the primary defense. 8. Avoid running GUI automation with administrative or root privileges. ]]>
