CVE-2023-40581
yt-dlp –exec shell command remote code execution
- CVSS 7.8
- CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
- Input Validation and Sanitization
- Remote
yt-dlp is a youtube-dl fork with additional features and fixes. yt-dlp allows the user to provide shell command lines to be executed at various stages in its download steps through the `–exec` flag. This flag allows output template expansion in its argument, so that metadata values may be used in the shell commands. The metadata fields can be combined with the `%q` conversion, which is intended to quote/escape these values so they can be safely passed to the shell. However, the escaping used for `cmd` (the shell used by Python's `subprocess` on Windows) does not properly escape special characters, which can allow for remote code execution if `–exec` is used directly with maliciously crafted remote data. This vulnerability only impacts `yt-dlp` on Windows, and the vulnerability is present regardless of whether `yt-dlp` is run from `cmd` or from `PowerShell`. Support for output template expansion in `–exec`, along with this vulnerable behavior, was added to `yt-dlp` in version 2021.04.11. yt-dlp version 2023.09.24 fixes this issue by properly escaping each special character. `\n` will be replaced by `\r` as no way of escaping it has been found. It is recommended to upgrade yt-dlp to version 2023.09.24 as soon as possible. Also, always be careful when using –exec, because while this specific vulnerability has been patched, using unvalidated input in shell commands is inherently dangerous. For Windows users who are not able to upgrade: 1. Avoid using any output template expansion in –exec other than {} (filepath). 2. If expansion in –exec is needed, verify the fields you are using do not contain ", | or &. 3. Instead of using –exec, write the info json and load the fields from it instead.
- CWE
- CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
- CVSS base score
- 7.8
- Published
- 2023-09-25
- OWASP
- A03 Injection
- Orthogonal defect classification
- Build/Package/Merge
- Code defect classification
- Packaging Issues
- Category
- Input Validation and Sanitization
- Subcategory
- Command Injection
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Fixed by upgrading
- Yes
Solution
Update to yt-dlp version 2023.09.24 or higher
Vulnerable code sample
import subprocess
def modified_exec(command, metadata):
"""Vulnerable function that demonstrates the security issue."""
command_to_run = command.format(**metadata)
subprocess.run(command_to_run, shell=True)
metadata = {
'filepath': 'example.mp4',
'title': 'example title with & special chars',
}
command = 'echo {title} > {filepath}'
modified_exec(command, metadata)Patched code sample
import subprocess
import shlex
def modified_exec(command, metadata):
"""Secure function that fixes the vulnerability."""
sanitized_metadata = {key: value.replace('\n', '\r') for key, value in metadata.items()}
modified_command = command.format(**sanitized_metadata)
modified_command = shlex.quote(modified_command)
subprocess.run(modified_command, shell=True)
metadata = {
'filepath': 'example.mp4',
'title': 'example title with & special chars',
}
command = 'echo {title} > {filepath}'
modified_exec(command, metadata)Cite this entry
@misc{vaitp:cve202340581,
title = {{yt-dlp --exec shell command remote code execution}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2023},
note = {VAITP Python Vulnerability Dataset, entry CVE-2023-40581},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2023-40581/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
