Discovering the Web Shell
Discovering and Mitigating Web Shells in Web Servers
Web shells are one of the most common post-exploitation tools attackers use to maintain backdoor access. They allow remote command execution, file manipulation, and other malicious activities. Here's how to effectively discover, analyze, and mitigate web shells.
1. Identifying Web Shells
Common Functions in PHP Web Shells
Web shells often utilize specific PHP functions to execute commands or manipulate the server environment. These include:
Command Execution:
system()
,shell_exec()
,exec()
,passthru()
,popen()
Dynamic Code Execution:
eval()
,assert()
,preg_replace()
,include()
,require()
Encoding and Decoding:
base64_decode()
,str_rot13()
,gzuncompress()
,edoced_46esab
(reverse ofbase64_decode
)File Manipulation:
fopen()
,fwrite()
,fread()
,unlink()
System Information:
php_uname()
,getenv()
Scanning for Web Shells Using grep
To scan for files containing suspicious functions:
2. Shell Hiding Techniques and Detection
Attackers employ various techniques to hide their web shells.
a. Remote Summoning
The web shell fetches its malicious payload from a remote server.
Detection: Look for functions fetching remote content:
b. Encrypted or Obfuscated Shells
Web shells may be encoded in base64 or use obfuscation techniques.
Detection:
c. Hidden in Images (EXIF Data)
Malicious code is stored in image metadata and executed by the server.
Example: The attacker adds a PHP shell in the
Comment
field of an image:Detection: Search for image metadata parsing:
3. Log Analysis for Web Shell Detection
Web shells often leave traces in access logs when accessed by the attacker.
Filter Log Entries for Suspicious Access
Search for web shell access:
Identify unusual file uploads or executions:
Example Log Entries:
4. Mitigation and Protection
Once a web shell is identified, it is critical to act swiftly to remove it and secure the server.
a. Eradication
Delete the Web Shell:
Restore from Backup (if available).
b. Secure Server Configuration
Disable Dangerous Functions in
php.ini
:This prevents the execution of high-risk functions.
Apply Permissions: Limit file upload directories to prevent PHP execution:
Regular Updates: Keep web applications, libraries, and server software up to date to avoid vulnerabilities.
c. File Integrity Monitoring
Use File Monitoring Tools: Tools like Tripwire or AIDE can detect unauthorized changes.
Set Up Hash Checks: Maintain a list of file hashes and periodically compare:
d. Web Application Firewall (WAF)
Deploy a WAF to block malicious requests, such as those targeting known web shell patterns.
e. Network Security
Monitor Outbound Traffic: Identify unusual communication from the server to external IPs.
5. Example Case: Real-World Detection
Scenario:
An e-commerce website's server is suspected of hosting a web shell.
Initial Detection:
Analyze access logs:
Identify encoded requests:
Remediation:
Remove the shell:
Disable uploads in vulnerable directories:
Hardening:
Enable strict Content Security Policies (CSP).
Implement a WAF and actively monitor all incoming traffic.
Key Points
Web shells are a serious threat that provide attackers with persistent access to compromised servers. By employing proactive scanning, log analysis, and secure configurations, organizations can detect and mitigate these backdoors effectively, minimizing the risk of further exploitation.
Last updated