Level03 introduces privilege escalation through PATH environment variable manipulation. This challenge demonstrates how SUID binaries that use relative paths instead of absolute paths can be exploited to gain elevated privileges. The level emphasizes understanding special file permissions, environment variables, and how Unix systems search for executables.
As with every level, I started by examining the home directory to identify any useful files or clues:
ls -laThe output revealed several files:
-r-x------ 1 level03 level03 220 Apr 3 2012 .bash_logout*
-r-x------ 1 level03 level03 3518 Aug 30 2015 .bashrc*
-rwsr-sr-x 1 flag03 level03 8627 Mar 5 2016 level03*
-r-x------ 1 level03 level03 675 Apr 3 2012 .profile*
The file level03 immediately stood out due to its unusual permissions: -rwsr-sr-x. The presence of the s character in both the owner and group permission sections indicated that this was a SUID and SGID binary.
I executed the binary to see what it did:
./level03The output was simply: Exploit me
Before proceeding, it's important to understand what the special permissions on this file mean. The s in the file permissions represents the SUID (Set User ID) and SGID (Set Group ID) bits, which allow users to run an executable with the permissions of the file owner or group, rather than their own permissions.
In this case:
- The file is owned by
flag03 - When I (as
level03) execute this file, it runs withflag03's permissions - This is a common mechanism for allowing users to perform specific privileged tasks
To understand what the binary was doing internally, I used the strings command to extract readable text from the executable:
strings level03Among the output, I found a critical line:
/usr/bin/env echo Exploit me
This revealed that the program was calling echo through /usr/bin/env rather than using an absolute path like /bin/echo. This is a significant security vulnerability.
The /usr/bin/env command is used to run programs in a modified environment. When a program is executed through env without an absolute path, the system searches for the executable in the directories listed in the PATH environment variable, checking each directory in order until it finds a match.
The vulnerability in this binary stems from its use of relative paths. When the program calls:
system("/usr/bin/env echo Exploit me");The /usr/bin/env searches for echo in the directories listed in the PATH environment variable sequentially, and if an attacker places a malicious program named echo in a directory that appears earlier in PATH, that malicious version will be executed instead of the legitimate one.
This is known as PATH hijacking or PATH interception, and it's a common privilege escalation technique.
To exploit this vulnerability, I needed to create a fake echo program that would execute getflag instead of the normal echo functionality. I chose to work in the /tmp directory since I had write permissions there.
First, I created the exploit code in C:
#include <stdlib.h>
int main()
{
system("/bin/getflag");
return 0;
}This simple program calls getflag using its absolute path to ensure we're executing the correct binary.
Next, I compiled it with a specific name:
cd /tmp
cc -o echo exploit.cThe key here is naming the compiled binary echo instead of something like exploit or a.out. This ensures that when /usr/bin/env searches for echo, it will find our malicious version.
The PATH environment variable determines where the system looks for executables and in what order. I checked the current PATH:
echo $PATHThe output showed:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
To ensure my fake echo binary would be found, I needed to modify the PATH variable to include /tmp. I did this with:
export PATH=/tmpNow when any program searches for echo, it will check /tmp, find our malicious binary, and execute it.
With everything in place, I returned to the home directory and executed the vulnerable binary:
cd ~
./level03Here's what happened behind the scenes:
- The
level03binary executed with flag03's SUID privileges - It called
system("/usr/bin/env echo Exploit me") /usr/bin/envsearched forechoin the PATH directories- It found
/tmp/echo - Our fake
echoexecuted with flag03's privileges - Our program called
/bin/getflag getflagreturned the password for level04
The flag was successfully retrieved: qi0maab88jeaj46qoumi7maus
Level03 provided an excellent introduction to privilege escalation through environment variable manipulation. This challenge demonstrated several critical security concepts that are fundamental to understanding Unix-like system security.
The vulnerability in this level exists because the program uses a relative path (echo) instead of an absolute path (/bin/echo). When SUID/SGID binaries make calls to programs using relative paths instead of absolute paths, attackers can hijack these calls by manipulating the PATH environment variable to make the binary execute a program controlled by the attacker. This is a common mistake in system programming and has been exploited in real-world scenarios for decades.
The SUID mechanism itself is not inherently insecure, it's a necessary feature that allows specific programs to perform privileged operations. However, developers must design and implement SUID programs carefully to avoid security vulnerabilities, including ensuring proper input validation and using absolute paths for all system calls.
Flag: qi0maab88jeaj46qoumi7maus
Next Level: Level04