Courseiva
mediumMultiple ChoiceObjective-mapped

PT0-002 Practice Question: A penetration tester gained low-privileged access…

A penetration tester gained low-privileged access to a Linux server and found that the user can run a custom script located at /opt/tool/backup.sh with setuid root. The script begins with a hashbang #!/bin/bash and uses an internal variable defined as BASEDIR=$(dirname $0) to determine paths. Which technique is most likely to allow privilege escalation?

⚠ Common exam trap

The trap here is that candidates may focus on modifying `$0` (Option A) or overwriting the script (Option C), but the actual vulnerability lies in the insecure use of a relative command (`dirname`) within a setuid script, which allows PATH hijacking.

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

Create a malicious executable named 'dirname' in a directory earlier in the PATH

The script uses `BASEDIR=$(dirname $0)` to resolve paths. If the user can place a malicious executable named `dirname` earlier in the PATH than the legitimate `/usr/bin/dirname`, then when the script runs with setuid root, the shell will execute the attacker's `dirname` binary instead, allowing arbitrary code execution as root.

Answer analysis

Option-by-option breakdown

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

  • Modify the $0 variable during execution

    Why it's wrong here

    In Bash, $0 is a special read-only positional parameter that reflects the argv[0] passed by the kernel or calling process. It is not an environment variable, so an external attacker cannot alter it via env or command-line tricks; attempting to change it inside the script (e.g., with set --) only changes positional parameters $1, $2, etc., not $0. Thus, even if the script uses $(dirname $0), the attacker cannot preconfigure or redirect $0 from outside to point to a malicious directory or value, making this an ineffective escalation technique.

  • Create a malicious executable named 'dirname' in a directory earlier in the PATH

    Why this is correct

    Since the script uses $(dirname $0) without an absolute path, the system searches PATH for 'dirname'. If the attacker puts a malicious 'dirname' script in a writable directory earlier in PATH, it will be executed as root.

  • Overwrite /opt/tool/backup.sh with a reverse shell

    Why it's wrong here

    Overwriting /opt/tool/backup.sh with a reverse shell requires write permission on the script file itself or on its containing directory. A low-privileged user typically has no write access to /opt/tool, and the file is likely owned by root with mode 755 or 700, preventing modification. Additionally, this approach is not a subtle command injection; it directly replaces the target script, which would require an existing writable configuration error that is not implied by the scenario. Since the question specifically highlights the use of $(dirname $0), a PATH-based hijack is the intended attack vector, not a file-overwrite escalation.

  • Exploit a buffer overflow in the Bash interpreter

    Why it's wrong here

    Exploiting a buffer overflow in the Bash interpreter is a memory-corruption attack that would require a specific vulnerable version of Bash, a way to deliver a malicious input to the interpreter's parser, and root privileges on the target process. The script `backup.sh` simply executes `dirname` with a normalized argument (`$0`), which does not overflow any buffer or involve unmarshalling attacker-controlled binary data. Moreover, the flaw described in the question is a logical command-hijacking vulnerability (PATH resolution), not a low-level interpreter bug; memory-safety exploits are irrelevant and anachronistic here. The attack is a straightforward substitution of an executable found via PATH, not a code-execution exploit against the shell binary.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

About these practice questions

One of 185 original PT0-003 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

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.