Question 394 of 509
Attacks and ExploitshardMultiple ChoiceObjective-mapped

Quick Answer

The answer is writing a malicious Python script to /opt/scripts/ that spawns a root shell. This technique is effective because the sudo wildcard privilege escalation with Python script exploits a misconfigured sudoers entry: the wildcard `*.py` allows execution of any `.py` file in a directory the tester owns, so by creating a script that calls `os.setuid(0)` and `os.system('/bin/bash')`, the tester gains an immediate root shell without needing to escape arguments or modify existing files. On the CompTIA PenTest+ PT0-002 exam, this scenario tests your ability to recognize dangerous wildcard patterns in sudo rules—a common trap is assuming you must exploit the wildcard via command injection, but the real vulnerability is the writable directory combined with the wildcard. Remember the memory tip: “Own the dir, own the wildcard—write your own root card.”

PT0-002 Attacks and Exploits Practice Question

This PT0-002 practice question tests your understanding of attacks and exploits. This is a configuration task: choose the command set that satisfies every stated requirement. Small differences — like 'secret' vs 'password' or 'transport input ssh' vs 'all' — change whether the answer is correct. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

A penetration tester has gained low-privilege shell access on a Linux server. The tester runs `sudo -l` and sees the following entry: `(root) NOPASSWD: /usr/bin/python3 /opt/scripts/*.py` The `/opt/scripts/` directory is owned by the tester's current user. Which technique is most effective for escalating privileges to root?

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

Write a malicious Python script to /opt/scripts/ that spawns a root shell

Option B is correct because the tester's user owns `/opt/scripts/` and can write arbitrary files there. The sudo rule allows executing any `.py` file in that directory as root without a password. By writing a Python script that calls `os.setuid(0); os.system('/bin/bash')` or similar, the tester can spawn a root shell, directly leveraging the misconfigured sudoers entry.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

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

  • Create a symbolic link from a Python script to a system file like /etc/shadow

    Why it's wrong here

    The sudo rule executes Python scripts, not arbitrary files. A symlink to /etc/shadow would not be executed by Python correctly and would not grant a shell.

  • Write a malicious Python script to /opt/scripts/ that spawns a root shell

    Why this is correct

    Since the user owns the directory, they can write a Python script that executes `/bin/bash` or similar, then run it via sudo to gain a root shell.

    Related concept

    Read the scenario before looking for a memorised answer.

  • Exploit a kernel vulnerability to overwrite the sudo binary

    Why it's wrong here

    Kernel exploits require additional conditions and are risky; also overwriting sudo is not necessary when a sudo rule can be directly abused.

  • Overwrite an existing Python script in /usr/bin/ with a malicious payload

    Why it's wrong here

    The user likely does not have write access to /usr/bin/, and the sudo rule is limited to /opt/scripts/.

Common exam traps

Common exam trap: answer the scenario, not the keyword

The trap here is that candidates may think symbolic links or overwriting system files are viable, but the key is that the sudo rule specifically executes Python scripts from a writable directory, making a crafted script the simplest and most direct escalation path.

Detailed technical explanation

How to think about this question

The sudoers entry `(root) NOPASSWD: /usr/bin/python3 /opt/scripts/*.py` uses a wildcard that expands to any `.py` file in that directory. Since the directory is writable by the tester, they can create a script that imports `pty` or uses `os.exec*` to spawn a shell. A common payload is: `import os; os.setuid(0); os.system('/bin/bash')`. This technique is a classic example of exploiting writable script directories in sudo rules, often found in CTFs and real-world misconfigurations.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A security team runs a vulnerability scan on a web application and discovers an unpatched SQL injection flaw. The team prioritises remediation by CVSS score — critical flaws are patched within 24 hours, high within 7 days. Questions like this test whether you understand vulnerability management processes, scanning tools, and remediation prioritisation.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related PT0-002 practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free PT0-002 practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this PT0-002 question test?

Attacks and Exploits — This question tests Attacks and Exploits — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: Write a malicious Python script to /opt/scripts/ that spawns a root shell — Option B is correct because the tester's user owns `/opt/scripts/` and can write arbitrary files there. The sudo rule allows executing any `.py` file in that directory as root without a password. By writing a Python script that calls `os.setuid(0); os.system('/bin/bash')` or similar, the tester can spawn a root shell, directly leveraging the misconfigured sudoers entry.

What should I do if I get this PT0-002 question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

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

Keep practising

More PT0-002 practice questions

Last reviewed: Jun 11, 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-002 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-002 exam.