Web shell
| Class | Persistence |
|---|---|
| Also known as | Webshell, web backdoor |
| MITRE ATT&CK | T1505.003 Server Software Component: Web Shell |
| Typical severity | Critical |
| Affects | Servers that run scripts from a folder they publish: PHP, ASP.NET and Java hosts, and CMS installs such as WordPress |
A web shell is a script that an attacker places in a folder a web server publishes, so that an ordinary HTTP request to the script's address runs commands on that server.1 It is usually a small piece of PHP, ASP or JSP code.2 The file stays after the flaw that let it in is patched, so attackers often install one only to keep a way back in, and recovery is incomplete until every such file is removed (see Responding to unauthorised changes to a website).2
Microsoft 365 Defender data showed an average of 140,000 web shell encounters a month on servers from August 2020 to January 2021, up from a monthly average of 77,000 the year before.2
How a web shell works
Many example nginx configurations for PHP pass every address that ends in .php to the PHP interpreter.3 If a file named avatar.php appears in an uploads folder, a request for /uploads/avatar.php runs it like any page the developer wrote.
The script reads a value from the request, such as a form field, a parameter or the User-Agent header, and passes it to a function that runs an operating system command or evaluates code. The output comes back as the response. Words that would give the script away, such as a command name, are often absent from the file and arrive only with the attacker's request.2 Commands run with the rights of the web server's own account, which can usually add, delete and run files, and can reach internal machines that have no internet access.4
Some shells, such as China Chopper, pair the script with a client program on the attacker's computer.1 Others can only upload files; they look harmless on review and are used to bring in a full shell later.2
The code can also hide inside a picture, inert on a laptop and live when a server that executes code serves it.2 With PHP's default path handling behind nginx, a request for /forum/avatar/1232.jpg/file.php, where only the image exists, makes PHP run the image as a script.3
Routes onto the server
A web shell is a second step. Something else first lets the attacker create a file in the document root or change one already there.5 Known routes include upload forms that save whatever they receive, remote and local file inclusion, SQL injection, cross-site scripting, flaws in a CMS such as WordPress, and an admin panel left open that also offers an upload button.4
On a default WordPress install a stolen admin password is enough, because the dashboard lets administrators edit the PHP files of plugins and themes. That editor runs code, which makes it often the first tool an attacker opens after signing in.6
Exchange Server, March 2021
On 2 March 2021 Microsoft disclosed four flaws in on-premises Exchange Server that a group it names HAFNIUM was exploiting before any fix existed.7 CVE-2021-26855, a server-side request forgery, let the attacker authenticate as the Exchange server, and CVE-2021-26858 and CVE-2021-27065 let an authenticated caller write a file to any path on it. After getting in, the group placed web shells in folders such as C:\inetpub\wwwroot\aspnet_client\, with names like help.aspx, healthcheck.aspx and errorEE.aspx. It then dumped the memory of the Windows LSASS process with Procdump, packed stolen data with 7-Zip and downloaded the Exchange offline address book, which lists an organisation and its users.7
Finding one
Antivirus products sometimes miss web shells, and an attacker can change a shell's code until a signature no longer matches.42 The method that finds most of them compares the files on the live server with a known-good copy, such as a fresh install of the same version with its updates, and reviews every file that was added or changed:5
diff -r -q /path/to/good/copy/ /var/www/html/Other signs are a script in the web root dated after the last update of the application, a file that contains eval or cmd.exe, and log lines that show an image receiving POST requests. Some shells pose as an error page with a hidden sign-in form, or show search engine crawlers a different page from the one their owner sees.4
In the access log, a shell's address is typically requested by one visitor, with few distinct user agents and often no Referer header, while normal pages are reached through links and load their images and scripts alongside.4 This count also flags harmless pages.5
On the server, a strong signal is the web server process starting programs that a web application rarely starts: a command shell, PowerShell, or commands such as whoami and net that an intruder runs to learn where they are.21
Prevention
A web shell almost always needs a new or changed file in a folder the server publishes,5 and a server that runs code from that folder.2 Closing either one stops it.
Only the accounts that deploy the site should be able to change the web directory.1 In WordPress, plugin files should be writable only by the owner's account, and one line in wp-config.php turns off the dashboard file editor.6
define( 'DISALLOW_FILE_EDIT', true );Uploaded files belong outside the document root or on another host, each under a name the application generates, with the extension checked against a short allowlist after the name is decoded. A filter that only looks for .jpg somewhere in the name passes shell.jpg.php; others miss a null byte (.php%00.jpg) or mixed case (.pHp). An uploaded .htaccess or web.config can tell the server to execute a harmless-looking extension, so per-folder overrides such as Apache's AllowOverride should be set to None on upload folders.8
In nginx, a nested location refuses PHP inside the uploads folder, and cgi.fix_pathinfo=0 in php.ini stops PHP from guessing at a file when the requested path does not exist:3
location /uploads {
location ~ \.php$ { return 403; }
}On Vercel, a function's file system is read-only apart from /tmp, and a file written there belongs to one instance and is gone when that instance stops.9
Prompt for a coding agent
Find every place this project saves uploaded files. Change each one to save outside the public folder, under a generated name, with the extension checked against an allowlist. If there is an nginx or Apache configuration, stop scripts from running in upload folders. List every file you changed.
In SecFlash
SecFlash reports uploads saved under the name the browser sent, with no check of their type, as SF-SEC-794 on the Security page. When the app loads a file from an address given in a request, it reports SF-SEC-608, also on the Security page.
See also
- Unrestricted file upload
- Command injection
- Website defacement
- Responding to unauthorised changes to a website
- Scheduled task persistence
- SSH authorized key persistence
- Category: Persistence
References
- a b c d MITRE ATT&CK (last modified 12 May 2026). "Server Software Component: Web Shell, Sub-technique T1505.003". MITRE ATT&CK.
- a b c d e f g h i Microsoft 365 Defender Research Team (11 February 2021). "Web shell attacks continue to rise". Microsoft Security Blog.
- a b c "Pitfalls and Common Mistakes: Passing Uncontrolled Requests to PHP". NGINX Wiki (source on GitHub).
- a b c d e US-CERT with UK, Australian, Canadian and New Zealand partners (10 November 2015, revised 9 August 2017). "Compromised Web Servers and Web Shells: Threat Awareness and Guidance (TA15-314A)". cisa.gov.
- a b c d NSA Cybersecurity (2020). "Mitigating Web Shells". GitHub. Companion to the NSA and ASD guidance "Detect and Prevent Web Shell Malware".
- a b "Hardening WordPress". WordPress Developer Resources.
- a b Microsoft Threat Intelligence Center (2 March 2021). "HAFNIUM targeting Exchange Servers with 0-day exploits". Microsoft Security Blog.
- back "File Upload Cheat Sheet". OWASP Cheat Sheet Series.
- back "Runtimes: File system support". Vercel Docs. Vercel.
External links
- Mitigating Web Shells, NSA Cybersecurity, scripts and signatures for finding web shells