Environment variable exploitation

Understanding Environment Variable Exploitation

What Makes Environment Variables Dangerous

Environment variables can be exploited when privileged programs or scripts read user-controlled environment variables without proper validation. Attackers can manipulate these variables to alter program behavior, inject malicious commands, or escalate privileges by controlling how applications execute or load resources.

The Attack Principle: Exploit scenarios where:

  • SUID/SGID binaries read environment variables for configuration

  • Scripts use environment variables in command execution

  • Programs use variables like LD_PRELOAD for library loading

  • Applications trust user-controlled environment data without validation

Why This Works: Many programs inherit the environment from their parent process. If an attacker controls environment variables and a privileged program reads them, malicious values can alter program execution flow.

Environment Variable Discovery and Enumeration

Finding Environment Variable Usage

Basic Environment Discovery:

# Display current environment
env
printenv

# Check specific important variables
echo $PATH
echo $LD_PRELOAD

# Find which variables are set
env | grep -E "(PATH|LD_|LIBRARY|PRELOAD|SHELL)"

Analyzing Binary Environment Usage:

Script Environment Analysis:

High-Value Environment Variable Exploits

LD_PRELOAD Exploitation

Why LD_PRELOAD is Critical: Forces programs to load specified shared libraries before others, allowing function hijacking.

Basic LD_PRELOAD Exploit:

Advanced LD_PRELOAD Function Hijacking:

PATH Variable Exploitation

PATH Manipulation for Environment Exploitation:

SHELL Variable Exploitation

Shell Environment Manipulation:

Script-Based Environment Exploitation

Shell Script Variable Injection

Identifying Vulnerable Scripts:

Command Injection via Environment:

Python Environment Exploitation

Python Path Injection:

Python Startup File Exploitation:

Cron and Service Environment Exploitation

Cron Environment Variables

Cron Job Environment Injection:

Service Environment Manipulation

Systemd Service Environment:

Real-World Exploitation Examples

Example 1: LD_PRELOAD SUID Bypass

Discovery:

Exploitation:

Example 2: Script Environment Injection

Discovery:

Exploitation:

Example 3: Python Environment Exploitation

Discovery:

Exploitation:

Key Operational Considerations

Success Indicators

  • Environment variables accepted by privileged programs

  • Library preloading successful with LD_PRELOAD

  • Command injection achieved through variable expansion

  • Privilege escalation confirmed through environment manipulation

Common Failure Points

  • Environment stripping by security-aware programs

  • LD_PRELOAD disabled for SUID binaries (modern systems)

  • Variable validation preventing injection attacks

  • Restricted environments (containers, chroot) limiting exploitation

Exploitation Notes

  • Custom applications more likely to trust environment variables

  • Development systems often have relaxed environment security

  • LD_PRELOAD most powerful but often restricted on modern systems

  • Script-based services frequently vulnerable to environment injection

Environment variable exploitation remains effective against applications that trust user-controlled environment data, particularly in development environments and custom applications where security hardening may be incomplete.

Last updated

Was this helpful?