Writable /etc/environment for Privilege Escalation
/etc/environment is a system-wide environment file sourced at PAM login: it applies to every user including root, and to every login method including SSH, su, and GUI login. If writable, injecting LD_PRELOAD, PATH, or PYTHONPATH here causes your payload to load the next time root opens a session. Combined with a service restart or admin login, this is a reliable escalation path.
Injection vectors through writable environment files:
LD_PRELOADin/etc/environmentto load a malicious shared libraryPATHmanipulation to hijack commands root calls without absolute pathsPYTHONPATHinjection to hijack Python imports in root scripts- Script injection in
/etc/profile.d/for interactive login shells
Prerequisites Check
Check write access to environment files:
Check the current content to understand what is already set:
Writable /etc/environment Privilege Escalation via LD_PRELOAD
/etc/environment is read by PAM and applied before user shells load. Adding LD_PRELOAD here causes the specified library to be loaded by every dynamically linked program that starts in a new session: including any sudo calls, service restarts, or root login sessions.
Step 1: Compile the malicious shared library:
/tmp/preload.c
#include <stdlib.h>
#include <unistd.h>
void __attribute__((constructor)) init() {
unsetenv("LD_PRELOAD");
setuid(0);
setgid(0);
system("cp /bin/bash /tmp/.rootbash; chmod u+s /tmp/.rootbash");
}
Step 2: Inject into /etc/environment:
Step 3: Wait for root to open a new session or for a privileged service to restart.
Check when services restart or if an admin login is expected:
After root opens a new session or any non-SUID binary starts in that session:
Important: LD_PRELOAD in /etc/environment is typically honoured by PAM-launched processes but ignored by SUID binaries and services launched by systemd (which resets the environment). It is most effective when root opens an interactive login session.
Technique 2: PATH Injection via /etc/environment
Prepend a writable directory to PATH, then plant a malicious binary there with the same name as something root calls:
Plant a malicious binary in /tmp:
For the full PATH hijacking workflow see https://hackindex.io/platforms/linux/privilege-escalation/writable-path-hijacking.
Technique 3: /etc/profile.d/ Script Injection
Scripts in /etc/profile.d/ are sourced by bash and sh for every interactive login. Writing a new script there is equivalent to writing to /etc/profile and affects all users.
Fires the next time any user opens an interactive login shell: including root via SSH or su -.
Technique 4: /etc/bash.bashrc Injection
Sourced by interactive non-login bash shells. Less reliable than /etc/profile.d/ but worth checking:
Cleanup
After escalating, remove your injected lines to avoid detection and system instability:
References
-
pam_env(8) ManualMAN: pam_env(8) Manual, man7.org/linux/man-pages/man8/pam_env.8.html (opens in new tab)
etc/environment sourcing behavior and PAM environment module documentation.
-
Bash Startup FilesDOCS: Bash Startup Files, www.gnu.org/software/bash/manual/bash.html#Bash-Startup-Files (opens in new tab)
GNU Manual Profile, bashrc, and environment file sourcing order for login and interactive shells.