gmsmeghana/Linux-Permissions-Privilege-Escalation

GitHub: gmsmeghana/Linux-Permissions-Privilege-Escalation

Stars: 0 | Forks: 0

# Linux File Permissions & Privilege Escalation — SetUID, umask & Capability Leaking A deep-dive into **Linux file permission management** and **privilege escalation techniques** including SetUID exploitation, PATH hijacking, and capability leaking. Covers both the mechanics of Linux access controls and how misconfigurations can be exploited to gain elevated privileges. ## Tools & Environment | Tool | Purpose | |------|---------| | Ubuntu Linux | Lab environment | | chmod / chown | Permission management | | umask | Default permission control | | SetUID binaries | Privilege escalation vector | | GCC | Compiling exploit code | ## Part A — Linux File Permissions ### Step 1 — File Ownership & Basic Permissions Created files and directories under multiple user accounts to observe ownership and permission inheritance: ![screenshot-01](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/d14e4fd43d001938.png) ![screenshot-02](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/7508212df9001939.png) ![screenshot-03](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/2afb1b8dde001939.png) **Ownership analysis:** - `Hello world` — owned by `user1`, group `users` - After `chown` — ownership transferred to `user2`, group `users` ### Step 2 — Directory Permission Analysis Examined permissions across home directories for multiple users: ![screenshot-04](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/024cdb9b2c001940.png) ![screenshot-05](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/dbcfd1168e001941.png) | Directory | Permissions | Meaning | |-----------|-------------|---------| | `/home/user1` | `drwxr-xr-x` | Owner: full access; Group/Others: read + execute | | `/home/user2` | `drwxrwx---` | Owner + Group: full access; Others: no access | | `/home/test` | `drwxr-xr-x` | Owner: full access; Group/Others: read + execute | ### Step 3 — Cross-User Access Testing Tested whether `user1` could access `user2`'s home directory under different permission settings: ![screenshot-06](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/ebcd897e4f001942.png) ![screenshot-07](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/0d27acfae5001943.png) ![screenshot-08](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/aed58515ba001943.png) ![screenshot-09](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/591aba33f7001944.png) ![screenshot-10](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/5c74fa11b6001945.png) **Findings:** - `user1` could `cd` into `/home/user2` when group permissions allowed it - After `chmod 700 /home/user2`, access was correctly denied - After modifying permissions to `755`, `user1` could list contents and create files ### Step 4 — File Write & Read Permission Testing Tested read/write access between users on shared files: ![screenshot-11](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/f64883ee86001946.png) ![screenshot-12](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/7e7e724f79001947.png) ![screenshot-13](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/13608cdc22001948.png) ![screenshot-14](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/8fab82a9b9001949.png) **Result:** `user1` could not modify `file1` without write permissions. `user2` (the owner) retained full access. ### Step 5 — Symbolic Links Created and analyzed symbolic links — confirming that symlinks inherit the permissions of the target file rather than having their own: lrwxrwxrwx -> /test/temp/hello.txt The actual permissions of `hello.txt` remain `-rw-rw-r--` regardless of the symlink's displayed permissions. ![screenshot-15](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/b4b8086f92001949.png) ![screenshot-16](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/855d0561ec001950.png) ![screenshot-17](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/d9a16a5bd4001951.png) ### Step 6 — umask — Default Permission Control Tested the effect of different `umask` values on newly created files and directories: ![screenshot-18](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/0fa20726c9001951.png) | umask | New File Permissions | New Directory Permissions | |-------|---------------------|--------------------------| | `0002` (default) | `664` (rw-rw-r--) | `775` (rwxrwxr-x) | | `0077` (restrictive) | `600` (rw-------) | `700` (rwx------) | | `0000` (open) | `666` (rw-rw-rw-) | `777` (rwxrwxrwx) | **Security risk of `umask 0000`:** All users can read and modify any new file — a critical misconfiguration in multi-user environments. Best practice is `umask 0022` or `0077` for sensitive files. ![screenshot-19](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/bc2eb3998d001952.png) ![screenshot-20](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/eb08acaa41001953.png) ![screenshot-21](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/03035d6dd5001954.png) ## Part B — Privilege Escalation ### Step 1 — SetUID Binaries **SetUID** allows a program to run with the privileges of the file owner rather than the executing user. Key system binaries that require SetUID: | Binary | Why SetUID is needed | |--------|---------------------| | `passwd` | Must write to `/etc/shadow` (root-only) | | `chsh` | Must edit `/etc/passwd` (root-only) | | `su` | Must authenticate against privileged resources | | `sudo` | Must check `/etc/sudoers` for elevated permissions | Without SetUID, none of these operations would be possible for regular users. ![screenshot-22](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/23ba660465001956.png) ![screenshot-23](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/667061645a001956.png) ![screenshot-24](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/99d57cf6bb001957.png) ![screenshot-25](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/0804c86d4e001958.png) ![screenshot-26](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/1ae13b7567001959.png) ![screenshot-27](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/ef58571037002000.png) **Result:** Bash detects when it is running in a SetUID environment and drops elevated privileges — a built-in protection preventing direct root shell access this way. ### Step 2 — PATH Hijacking via SetUID Exploited a SetUID program that calls `ls` without an absolute path. By modifying the `PATH` variable to point to a malicious binary named `ls` (actually a shell), elevated privileges were obtained: ![screenshot-28](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/697b42f280002001.png) ![screenshot-29](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/28558810dd002002.png) ![screenshot-30](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/278f3df530002002.png) ![screenshot-31](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/c8b5687ea6002003.png) ![screenshot-32](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/a38e0a45bc002004.png) **Result:** The program executed the malicious `ls` binary instead of the real one, spawning a root shell. This demonstrates why SetUID programs must always use **absolute paths** for system calls. ![screenshot-33](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/3690a7ce94002005.png) ### Step 3 — Capability Leaking Analyzed a vulnerable SetUID C program (`hack2.c`) that leaks file descriptor access after dropping privileges: ![screenshot-34](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/fd306eb5ed002006.png) ![screenshot-35](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/d15d9a5ad1002007.png) ![screenshot-36](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/cffc96a210002008.png) **Vulnerability explained:** 1. Program opens `/etc/zzz` (root-owned) with read/write access 2. Drops root privileges using `setuid(getuid())` 3. Forks a child process — which **inherits the open file descriptor** 4. The child process (now running as a normal user) writes `"Malicious Data"` to `/etc/zzz` **Root cause:** The program failed to close the file descriptor before relinquishing privileges. The child process retains access to the privileged resource even after the parent drops its elevated rights — a classic **capability leaking** vulnerability. ## Key Takeaways - File permission misconfigurations are one of the most common Linux privilege escalation vectors - SetUID binaries must always use absolute paths to prevent PATH hijacking attacks - `umask 0000` is a critical security risk — always use restrictive defaults - File descriptors must be explicitly closed before dropping privileges to prevent capability leaking - Understanding how Linux access controls work is fundamental to both system hardening and penetration testing ## Disclaimer All privilege escalation techniques were performed in a controlled, isolated lab environment. These techniques must only be used on systems you own or have explicit authorization to test.