Web shell: a backdoor planted on your web server

A web shell is a backdoor script that an attacker places on a web server to control it remotely. It does not break in by itself. It appears after another weakness has been used, such as a file upload without checks or remote code execution, or after access has been taken over. Its job is to give the attacker a lasting foothold. It is one of the web attacks that matter after the first intrusion rather than during it.

Section Web attacks Updated Technique ATT&CK T1505.003

Quick facts

Web shell at a glance
ItemDetail
Also calledBackdoor script
WeaknessCWE-434: unrestricted upload of file with dangerous type, when the way in is an upload
ATT&CK techniqueT1505.003: Server Software Component, Web Shell, filed under persistence
Ways inAn upload without checks, remote code execution, taken-over access
Does a WAF stop it?Partly. It can refuse an upload and record requests to a planted script. It does not see files already on disk

How a web shell gets onto a server

A web shell on a web server is the result of another failure, so the useful question is which door was open. Four classes of door are worth knowing.

An upload without checks. A file upload form accepts a file of a dangerous type and the server treats it as something to run. This is the weakness CWE-434 describes. The class has its own page: file upload vulnerability.

Remote code execution. A flaw that lets an attacker run commands on the server can also be used to leave a script behind. The flaw itself is covered on command injection.

Taken-over access. Stolen or guessed administrator or hosting credentials give an attacker the same write access as the owner.

A vulnerable plugin or component. A flaw in third-party code can offer either of the first two doors.

The script left behind is a backdoor, and it outlives the flaw that placed it. The common web application weaknesses are catalogued in the OWASP Top 10.

What an attacker does with a web shell

ATT&CK describes a web shell as a backdoor script on an openly accessible web server that gives the attacker access to it as a gateway into a network, and files it under persistence. In practice that means persistence: the attacker keeps access to the web server even after the original flaw is closed, can run commands through it, and may read data the server can read or move on to other systems. Finding one therefore means looking at the file upload or remote code execution flaw behind it, not only at the file.

Website defacement

Defacement is the visible end of the same story. The attacker changes what the site shows, such as a replaced home page or a banner. It needs write access to the site, which can come from a web shell, from remote code execution or from a file upload flaw. The sign is plain, since visitors or staff notice the page has changed, and the damage is to reputation as much as to the web server. A defaced page also means whoever changed it had a way in, and a web shell or another backdoor may still be there.

Signs of a web shell

No single sign of a web shell proves anything. Several together are a reason to look closely, starting with the upload directory and the access log.

  • New or changed script files in the upload directory, or anywhere scripts do not belong on the web server.
  • Script files with names that do not fit the rest of the site, or modification times that do not match a deployment.
  • Repeated POST requests to one script file that no page links to.
  • The web server process starting command interpreters or other programs it has no reason to start.
  • Outgoing connections from the web server host to addresses it has no reason to contact.
  • Pages that changed without a release behind them.

The process sign is one that ATT&CK lists for web shell detection: unexpected file creation in web directories, followed by the web server process spawning command shells or script interpreters.

How to detect a web shell

Web shell detection is a comparison of three places with what you expect.

Files. File integrity monitoring compares the web directory with a known-good copy, such as the last release. A signature scanner finds what it already knows; a comparison finds what is new.

Logs. The access log shows the request patterns above, such as a POST to a file that receives nothing else.

The host. Process monitoring and an antivirus scanner show what the web server process is doing and what lies in its directories.

A web application firewall adds a view from the request side. In monitor mode it evaluates every request and records the result without stopping anything, so you can see which uploads and which paths are being tried before you decide what to refuse. The modes are off, monitor and block, and starting in monitor is the safer order.

What a WAF can and cannot see

A web application firewall reads HTTP requests. That gives it two openings against a web shell: the request that carries a file upload, and the later requests that call a script on the server. In block mode it can refuse an upload of a file type your application does not accept and a request to a path that should not receive traffic.

It cannot see inside the server. A file already on disk is invisible to it. A web shell written through a flaw the WAF did not recognise, or a login with stolen credentials, can reach the application as a request that looks normal. A WAF is one layer and does not replace patching, file permissions or integrity checks. A quiet WAF log is one piece of evidence, not proof that no web shell exists; read it next to the access log of the web server.

How to block uploads

Against a web shell, the main controls on a file upload sit in the application and on the web server. These follow the OWASP File Upload Cheat Sheet:

  1. List the allowed extensions, and allow only those the feature needs.
  2. Check the type of the file itself. The Content-Type header can be spoofed, so do not trust it.
  3. Let the application generate the filename.
  4. Allow uploads only for authorised users.
  5. Store files on a different server or outside the webroot, and serve them through a handler in the application.
  6. Scan the content with an antivirus or sandbox where available.
  7. Keep the upload directory from running anything: disable per-directory configuration overrides there as a second layer, and give files the least privilege that works. Only authorised accounts should be able to write to the web directory.

Add the WAF as the outer layer. A rule on file upload requests can refuse a file type before it reaches the application; set it up in monitor mode first and read what it would have blocked.

The OWASP File Upload Cheat Sheet has the full list.

What to do if you find a web shell

Deleting the web shell file is not the whole response. The door it came through is still open, and a backdoor is built for persistence.

  1. Isolate the affected site or server, and keep a copy of the files and logs before cleaning.
  2. Find the way in. Read the logs around the first appearance of the file, then fix that weakness.
  3. Look for more. An attacker with one backdoor script may have left others; compare the whole web directory with a clean copy and read the access log for requests to other files.
  4. Change the secrets the server could read: passwords, keys, tokens and database credentials.
  5. Restore from a copy you know is clean, then redeploy.
  6. Keep watching afterwards, with file integrity monitoring and a web application firewall in monitor mode.

Questions

What is a web shell?

A web shell is a backdoor script an attacker places on a web server to control it remotely. It arrives after another weakness, such as an unchecked upload or remote code execution, or after taken-over access.

What is website defacement?

Website defacement is the unauthorised change of what a site shows, such as a replaced home page. A web shell can supply the write access it needs, so a defaced page is a reason to search for one.

Can a WAF stop a web shell?

Partly. A WAF can refuse an upload of a file type you do not accept and record requests to a suspicious path. It does not see files already on the server.

How do I check my site for a web shell?

Compare the web directory with a known-good copy, read the access log for POST requests to unexpected script files, and check what the web server process starts.