🐧NIX Shells

Overview

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

  • Services: FTP, SSH, HTTP/HTTPS, MySQL, RPC

  • Function: Web server hosting web application

  • SSL Configuration: Self-signed certificate present

Step 2: Web Application Discovery

Initial Web Reconnaissance:

  • Navigate to HTTP/HTTPS endpoints

  • Identify hosted applications

  • Check for version information

  • Look for default credentials

Example: rConfig Discovery

  • Application: rConfig Configuration Management Tool

  • Purpose: Network device configuration automation

  • Version: 3.9.6 (visible on login page)

  • Critical Risk: Admin access to network infrastructure

rConfig Significance:

  • Automates network appliance configuration

  • Remote interface configuration capabilities

  • Potential access to routers, switches, firewalls

  • High-value target for network compromise

  • Could lead to complete network infrastructure control

Step 3: Vulnerability Research

Research Methodology:

  1. Version-specific searches: "rConfig 3.9.6 vulnerability"

  2. CVE databases: Check NIST, MITRE, ExploitDB

  3. Security advisories: Vendor bulletins, security researchers

  4. Proof of concepts: GitHub, security blogs

  5. Metasploit modules: Built-in exploit framework

Search Results for rConfig 3.9.6:

  • CVE-2019-16662: Arbitrary file upload to RCE

  • CVE-2019-16663: Authentication bypass

  • Multiple vulnerabilities: Configuration disclosure, SQL injection

Step 4: Metasploit Module Discovery

Search for Exploits:

Available Modules:

Module Selection Criteria:

  • Target specificity: Matches exact version

  • Reliability rank: Good to excellent ranking

  • Functionality: Provides shell access

  • Prerequisites: Authentication requirements

Step 5: Advanced Exploit Research

GitHub Repository Search:

Manual Module Installation:

Metasploit Updates:

Exploiting rConfig - Practical Example

Step 1: Module Selection and Configuration

Load the Exploit:

View Module Options:

Required Configuration:

Step 2: Exploit Execution

Launch the Attack:

Exploitation Process:

Exploit Steps Breakdown:

  1. Version Detection: Confirms vulnerable rConfig 3.9.6

  2. Authentication: Successfully logs into rConfig

  3. Payload Upload: Uploads PHP-based reverse shell

  4. Payload Trigger: Executes uploaded payload

  5. Stage Transfer: Sends Meterpreter stage

  6. Cleanup: Removes uploaded payload file

  7. 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

  1. Thorough enumeration of services and versions

  2. Web application assessment for vulnerabilities

  3. Configuration analysis for misconfigurations

  4. User enumeration for potential targets

Exploitation

  1. Research target-specific vulnerabilities thoroughly

  2. Test exploits in controlled environments first

  3. Understand exploit mechanisms before deployment

  4. Plan payload delivery based on target constraints

Post-Exploitation

  1. Stabilize shell access immediately

  2. Gather system intelligence for privilege escalation

  3. Establish persistence if authorized

  4. Document findings for reporting

Operational Security

  1. Minimize log generation during testing

  2. Clean up artifacts after assessment

  3. Use encrypted communications when possible

  4. 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.

Shell Interpreter Direct Execution

/bin/sh Interactive Mode

Basic Interactive Shell:

Expected Output:

Features:

  • Interactive mode (-i): Enables interactive functionality

  • Basic shell: Minimal features but reliable

  • 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

  1. Assess available resources on target system

  2. Start with most reliable methods (Python, /bin/sh)

  3. Fall back to system utilities if needed

  4. Consider permission requirements for each method

  5. Test shell stability after spawning

Operational Considerations

  1. Minimize noise during shell spawning

  2. Avoid triggering security alerts with unusual commands

  3. Document successful methods for future reference

  4. Plan for shell loss and recovery methods

  5. Understand environment limitations before proceeding

Security Awareness

  1. Monitor process creation that might be logged

  2. Understand command auditing on target system

  3. Consider shell history and logging implications

  4. Plan cleanup procedures for spawned processes

  5. 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.

Last updated