Web shells and remote code execution: how servers get taken over and how defenders find it

The bug classes that lead to code execution on a web server, what a web shell is and how it is planted, the file, process, and log evidence defenders hunt for, and how to respond. Defensive only; no shells or exploit modules.

Lesson 13 of 69 · 13 min read · practice only with authorization

Diagram

From a bug to persistent access

Follow the path. One example at a time.

Step 1 of 4

Bug

Unrestricted upload, deserialization, template injection, command injection, or an unpatched framework.

Illustration: Bug
# Bug
# Unrestricted upload, deserialization, template
# injection, command injection, or an unpatched framework.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Uploads that execute

    What fails
    An upload stored under the web root with an executable extension becomes code.
    Who is affected
    The whole server and the data it can reach.
    What you would notice
    User uploads served from the same origin and folder as the application.
    What you change
    Store uploads outside the web root, rename them, serve from a separate domain, and never execute them.
  2. Process lineage tells

    What fails
    Web servers rarely need to start shells or network tools.
    Who is affected
    The host and its credentials.
    What you would notice
    w3wp.exe, httpd, php-fpm, or java spawning cmd.exe, sh, powershell, or curl.
    What you change
    Alert on that lineage and block it with application control where possible.
  3. Patch the framework

    What fails
    Many takeovers use known bugs in frameworks and appliances.
    Who is affected
    Every internet-facing instance on an old release.
    What you would notice
    A KEV-listed CVE in a public-facing framework version.
    What you change
    Patch internet-facing apps on the KEV timeline and keep an inventory of versions.

Hunting for a planted shell

Compare the deployed files against the release artifact or version control; anything extra is suspect. Look for recently modified scripts in upload, cache, and static folders, and for access-log entries where a static-looking path receives POST requests with 200 responses.

$ git -C /var/www/app status --porcelain
?? public/uploads/cache/img_thumb.php      <- not in the release
$ grep "POST /uploads/cache/img_thumb.php" /var/log/nginx/access.log | head -3
198.51.100.23 - - [02/Nov/2026:03:14:07] "POST /uploads/cache/img_thumb.php HTTP/1.1" 200 412

Responding

Isolate the host, preserve the web root, logs, and memory, identify the entry bug from logs around the file’s creation time, rotate every secret the application could read, and redeploy from a clean build. Deleting the file alone leaves the entry bug and any second shell.

About offensive frameworks

Penetration-testing frameworks and web-shell tools exist and appear in older tutorials. In an engagement they are used only under explicit scope, and the report focuses on the entry bug and the fix. This page stays on the defender’s side.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

php-fpm spawns /bin/sh which runs curl. What should a detection rule do?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapMetasploit: a framework, not a permission slipMetasploit organizes known checks and modules so a tester can validate a specific issue in a lab.

Practice

  1. LabTerminal mission: The .htaccess that made JPEGs run as PHPAn .htaccess file in the uploads folder tells Apache to run .jpg files as PHP. Find it, the uploaded “image” that abused it, and the commands passed in its query string.