Courseiva
Question 772 of 185
mediumMultiple ChoiceObjective-mapped

PT0-002 Practice Question: A penetration tester is reviewing a Python script…

A penetration tester is reviewing a Python script that attempts to exploit a command injection vulnerability. The script uses the 'subprocess' module with the 'shell=True' argument. Which of the following code changes would be MOST effective to reduce the risk of unintended consequences when executing system commands?

⚠ Common exam trap

CompTIA often tests the misconception that input sanitization (like quoting) is sufficient to prevent command injection, when in fact the most secure approach is to avoid shell invocation altogether by using a list of arguments with `shell=False`.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Avoid using shell=True and pass the command as a list of arguments

Setting `shell=True` in Python's `subprocess` module causes the command string to be interpreted by the system shell, which introduces command injection risks if any part of the string is user-controlled. By passing the command as a list of arguments (e.g., `['ls', '-l', filename]`) and omitting `shell=True`, the subprocess module directly executes the binary without shell interpretation, eliminating shell metacharacter injection. This is the most effective mitigation as it avoids shell parsing entirely, which is the root cause of the vulnerability.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Replace subprocess with os.system()

    Why it's wrong here

    os.system() is fundamentally insecure because it relies on the system's shell to parse the command string. When user input is concatenated into that string, shell metacharacters like ;, |, or $() are interpreted by the shell, enabling command injection. Unlike subprocess with shell=True, os.system() does not even allow passing arguments as a list, so there is no safe way to separate command from arguments. It also returns only the exit code, making output capture difficult and forcing even more shell usage.

  • Use the 'shlex.quote()' function to sanitize user input before passing to subprocess

    Why it's wrong here

    shlex.quote() can reduce injection risk by escaping shell metacharacters, but it is not a complete defense. The quoting logic has had historical edge cases on different platforms, and when user input is placed in a shell command line, quoting mistakes or unusual shell behaviors can still lead to injection. More importantly, this approach still invokes the shell, meaning the attack surface remains; a single missed quote or a shell-specific expansion can compromise the command. The safest practice is to avoid shell interpretation altogether rather than trying to sanitize input for a shell.

  • Avoid using shell=True and pass the command as a list of arguments

    Why this is correct

    Passing the command as a list of arguments while omitting shell=True is the correct mitigation because subprocess then executes the executable directly via execve or similar system calls without invoking a shell. With shell=False, the shell's metacharacter interpretation is completely bypassed; characters like ;, |, and $ are passed as literal arguments to the command, not interpreted. This eliminates shell injection by design, because user-controlled values are always treated as data, never as code. This is the recommended approach per Python's official subprocess documentation for security-sensitive applications.

  • Use the 'exec()' function to run the command

    Why it's wrong here

    exec() is a Python built-in that compiles and executes a string as Python source code, not as a system command. Passing a shell command string to exec() would raise a SyntaxError unless the input happens to be valid Python, and if user input is interpreted as Python code, it introduces arbitrary code execution vulnerabilities of a different and equally dangerous kind. exec() does not provide any sandboxing or command isolation; it is intended for running dynamic Python code, not for secure subprocess management. Thus, using exec() is neither a valid replacement for subprocess nor a safe way to invoke OS commands.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

This PT0-003 practice question is part of Courseiva's free CompTIA certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PT0-003 exam.