Hunting for web shells means proving, server by server, that every script a web server can execute is one you deployed, and that the web server process is not doing things a web server should never do. This guide sets out a repeatable hunt for IIS, Apache and Nginx servers built on three independent data sets: files on disk, access logs and process activity. It covers the hypotheses, the log fields you need, illustrative queries and how to triage a suspicious file without destroying evidence.
It is written for threat hunters, SOC analysts and server administrators. If you have already confirmed a web shell and need to respond, use the Web Shell Response Playbook. For PHP specific indicators and clean-up, see PHP web shells. For hunting method in general, see the Threat Hunting Guide.
Key takeaways
- A web shell is a script placed on a web server that gives an attacker remote command execution through ordinary HTTP requests. MITRE ATT&CK tracks it as T1505.003 Web Shell, a persistence technique, and it commonly follows T1190 Exploit Public-Facing Application.
- No single data source is enough. Files show what is present, access logs show what is being called, and process telemetry shows what the web server is doing. Hunt all three and look for agreement.
- The NSA's Mitigating Web Shells guidance states that the most effective method of discovering most web shells is to compare files with a known-good version of the application.
- Web server processes spawning shells and reconnaissance commands are one of the strongest signals available, because benign web applications rarely do it.
- Web servers often hold application secrets, database connection strings and service account credentials. A confirmed web shell means those credentials must be treated as exposed.
- All queries on this page are illustrative. Map field names to your own log pipeline and test before relying on them.
The hunt question and hypotheses
Frame the hunt as testable statements, each scoped to a set of internet facing servers and a time window:
- H1, unexpected files: a script file exists in a web served directory that does not match the deployed application release.
- H2, rare requested resources: a script on the server is requested by very few clients, usually with POST, and returns success.
- H3, web server child processes: the web server worker process has launched command interpreters or system utilities.
- H4, server modules: a malicious module has been loaded into the web server itself, which on IIS ATT&CK tracks as T1505.004 IIS Components. This variant leaves no script file to find, so process and configuration checks matter more.
- H5, unexpected network flows: the web server is initiating connections to internal hosts or the internet that its role does not require.
Prioritise servers that have recently had a vulnerability published for their application stack, especially anything on the CISA Known Exploited Vulnerabilities Catalog. Record hypotheses in the Threat Hunt Hypothesis Backlog Template.
Data sources by platform
Before hunting, confirm which fields you actually log. Default formats differ by platform and some useful fields are off by default.
IIS
Microsoft documents the W3C extended log fields, including date, time (in UTC), s-ip, cs-method, cs-uri-stem, cs-uri-query, s-port, cs-username, c-ip, cs(User-Agent), cs(Referrer), cs-host, sc-status, sc-substatus, sc-bytes, cs-bytes and time-taken. For web shell hunting, make sure cs-method, cs-uri-stem, cs-uri-query, c-ip, cs(User-Agent), cs(Referrer), sc-status, cs-bytes and sc-bytes are all enabled. Bytes received is particularly useful because commands arrive in request bodies.
Apache HTTP Server
The Apache documentation defines the Combined Log Format as %h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i": client address, identity, authenticated user, time, request line (method, resource and protocol), final status, response size, referrer and user agent. Combined format does not log request body size or time taken, so consider adding them in a custom format for internet facing servers.
Nginx
The nginx documentation defines the predefined combined format as $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent", with a default of access_log logs/access.log combined. Like Apache's format, it omits request body size, so add a custom format if you can. Remember that Nginx often fronts an application server, so the script that actually executes may live on a different host, and you need logs and file checks there too.
Endpoint telemetry
On Windows, Sysmon event 1 (process creation, with full command line and parent) and event 11 (file creation) cover H3 and H1. Sysmon event 3 covers H5 but is disabled by default. On Linux, auditd or an EDR agent provides the equivalent process records. The Sysmon Guide covers configuration.
Hunt 1: files that should not be there
The NSA guidance recommends comparing the production web server against a clean, known-good version of the application, using tools such as WinDiff or its dirChecker.ps1 script on Windows and diff -r -q on Linux. It also notes that results need manual review because benign changes are common.
A practical file hunt:
- Get a reference. Use the release artefact from your build pipeline, not a copy taken from another production server that may also be compromised.
- Compare recursively. List files that are added or changed in served directories, upload folders and any directory the application can write to.
- Focus on executable types. On IIS, server side script and page extensions; on Apache and Nginx fronted stacks, PHP, JSP and other interpreted types the stack will run. Files with image or text extensions in directories configured to execute scripts also matter.
- Check timestamps with care. Attackers alter file times. Sysmon event 2 records a process changing a file creation time, which is itself worth hunting on web servers.
- Scan with signatures. The NSA provides YARA rules in core and extended sets; it describes the core rules as covering indicators for common web shells that are unlikely to occur in benign application files. Signatures only catch known shells, so treat them as a supplement to comparison.
Where a reference is impossible, stack instead. Across a farm of identically deployed servers, a file present on only one is a lead.
Hunt 2: access log anomalies
A web shell must be called over HTTP, so it leaves traces in access logs even when the file is well hidden. The NSA guidance suggests looking for requests with uncommon user agents, referrers or client addresses, and its log analysis scripts report the least requested resources that returned a success status. It also warns that this approach is likely to produce significant false positives in many environments, so it works best as a lead generator rather than an alert.
Illustrative logic for IIS W3C logs (the same logic applies to the request, status and referrer fields in Apache and Nginx combined logs):
where sc-status between 200 and 299 and cs-uri-stem ends with a server side script extension | summarize Clients = dcount(c-ip), Requests = count(), Posts = countif(cs-method == "POST"), UAs = dcount(cs(User-Agent)) by cs-uri-stem | where Clients <= 3 | order by Clients asc
Signals that raise a rare resource to a lead:
- POST requests to a script that the application never posts to, often with an empty referrer.
- Requests from one or two client addresses only, especially hosting or anonymising networks.
- Large request sizes (cs-bytes on IIS) with small, consistent responses, which suggests commands in and short output back.
- Query strings containing encoded payloads or command fragments.
- The first request to a new file shortly after a burst of exploitation attempts against the same server.
Correlate with Hunt 1: a file that is both outside the reference build and rarely requested is a strong lead.
Hunt 3: what the web server process is doing
This is often the highest-value hunt. The NSA guidance flags child processes of the IIS worker process, C:\Windows\System32\inetsrv\w3wp.exe, and lists cmd.exe, powershell.exe, net.exe, whoami.exe, ipconfig.exe and wmic.exe as suspicious children, noting that it is uncommon for most benign web servers to launch ipconfig. For Linux, it flags processes launched by the Apache web server user, listing cat, ls, ifconfig, ip, netstat, uname and whoami. The same principle applies to the Nginx worker user and to any application server user behind Nginx.
Illustrative KQL for Microsoft Defender XDR, using DeviceProcessEvents columns:
DeviceProcessEvents | where Timestamp > ago(30d) | where InitiatingProcessFileName =~ "w3wp.exe" | where FileName in~ ("cmd.exe","powershell.exe","net.exe","whoami.exe","ipconfig.exe","wmic.exe") | project Timestamp, DeviceName, AccountName, FileName, ProcessCommandLine, InitiatingProcessCommandLine
For Sysmon, filter event 1 on records whose parent process is the web server worker and stack the child process image and command line by server. Expect a small number of legitimate children (for example, applications that call a known helper). Allow list those by exact command line, not by image name.
Also check the web server configuration for modules and handlers you did not install, which addresses H4. On IIS, compare the installed native and managed modules against your build. On Apache and Nginx, compare loaded modules and configuration includes against configuration management.
The KQL for Threat Hunting Guide covers the query language in more depth.
Hunt 4: unexpected network flows
The NSA guidance recommends watching for web servers making requests to internal nodes and for non-web servers answering HTTP or HTTPS requests. Web shells are frequently used as a pivot into the internal network, so a web server that suddenly opens SMB, RDP or directory service connections to internal hosts deserves immediate attention. Stack outbound and east-west connections from web servers by destination and port over 30 days, and investigate new combinations.
Triage: confirming a suspicious file
When a lead points at a specific file, triage without tipping off the attacker or destroying evidence:
- Preserve first. Take a copy of the file with its metadata, a copy of the relevant logs and, if possible, a snapshot of the server before anything else.
- Read the file in a safe viewer. Look for code that evaluates request parameters, decodes and executes content, writes files or runs operating system commands. Never browse to it on the live server.
- Find the first request. Search access logs for the earliest request to the file and the client address that made it. Then search for everything else that address requested.
- Find the write. Use file creation events and the logs around that time to work out how the file arrived: an upload feature, an exploited vulnerability or stolen deployment credentials.
- Check process activity. Look for web server child processes around the times the file was requested.
- Escalate. A confirmed web shell is an incident. Hand over to the Web Shell Response Playbook rather than deleting the file during the hunt.
Where identity fits
A web server is rarely just a web server. It holds database connection strings, API keys for downstream services, cloud credentials and sometimes domain service account passwords for application pools. An attacker with a web shell runs with the identity of the web server process and can read whatever that identity can read.
Build identity checks into the hunt:
- List the account each web server process and application pool runs as, and confirm it is not a privileged domain account.
- List the secrets in configuration files under the web root and on the server. These are exposed if a shell is confirmed.
- Look for those credentials being used from unexpected locations after the earliest shell request.
- Plan rotation of every exposed credential as part of response, not after it.
The Service Account Security Guide and Secrets Management Guide cover reducing what a web shell can reach.
Turning hunts into detections
- Alert on web server worker processes spawning command interpreters and reconnaissance utilities, with an exact command line allow list.
- Run scheduled file comparison against release artefacts and alert on any new executable file in served directories.
- Consider file integrity monitoring on web directories. The NSA guidance notes that web shells almost always rely on modifying existing web application files, and suggests starting monitoring rules at an informational level to tune exceptions before moving to blocking.
- Alert on web servers initiating connections to internal management ports.
- Keep the rare resource analysis as a weekly hunt rather than an alert, given its false positive rate.
Common failure patterns
- Comparing against another production server. If both are compromised, the comparison shows nothing.
- Logs without the useful fields. Default formats on Apache and Nginx omit request body size, and IIS fields can be switched off. Check before you hunt.
- Hunting only the reverse proxy. Where Nginx fronts an application server, the shell lives on the application tier, and that is where the file and process hunts must run.
- Signature scanning as the whole hunt. Modified or custom shells will not match, and in-memory modules leave no file.
- Allow listing child processes by name. Allowing cmd.exe because one application uses it hides every web shell on that server.
- Deleting the file on discovery. This alerts the attacker, loses evidence and leaves the original entry point and any stolen credentials untouched.
- Forgetting the credentials. Removing the shell without rotating the secrets the web server held leaves the attacker a way back.
Practitioner checklist
- Inventory internet facing web servers, their platform and the application tier behind any reverse proxy.
- Confirm access logs include method, resource, query, client address, user agent, referrer, status and request size.
- Confirm process creation with parent and command line is collected from web and application servers.
- Compare served and writable directories against clean release artefacts.
- Run rare resource analysis on successful script requests and correlate with file findings.
- Hunt web server worker processes for command interpreter and reconnaissance children.
- Check loaded web server modules against configuration management.
- Stack outbound and internal network flows from web servers.
- Preserve evidence and escalate any confirmed shell to incident response.
- List the identities and secrets on each web server so exposure can be scoped quickly.
Standards and references
- NSA Cybersecurity: Mitigating Web Shells
- Microsoft: W3C logging fields
- Apache HTTP Server 2.4: Log Files
- nginx: ngx_http_log_module
- Microsoft Sysinternals: Sysmon
- MITRE ATT&CK: T1505.003 Web Shell
- MITRE ATT&CK: T1505.004 IIS Components
- CISA: Known Exploited Vulnerabilities Catalog
Related NHI Mgmt Group resources: Threat Hunting Guide · Web Shell Response Playbook · PHP web shells · ATT&CK Web Shells Software Hub