Remote code execution (RCE): how the attack works
Remote code execution (RCE) means an attacker who is not on your machine can make your server run code of their choosing. It is a result, not a single technique: injection, unsafe deserialization and a flaw in a library can each end there. An RCE attack is any request, message or file built to trigger such a flaw.
Quick facts
- Code injection
- CWE-94 Improper Control of Generation of Code
- Command injection
- CWE-78 OS command injection
- Deserialization
- CWE-502 Deserialization of Untrusted Data
- OWASP categories
- A05:2025 Injection; A03:2025 Software Supply Chain Failures; A08:2025 Software or Data Integrity Failures
- Example
- CVE-2021-44228 (Log4Shell), published 10 Dec 2021
- Does a WAF stop it?
- Partly. It can refuse known request patterns while you patch; it does not remove the flaw
Weakness names come from MITRE's CWE list and the category names from the OWASP Top 10:2025.
What remote code execution is
Every server runs code the owner chose. Remote code execution is the point where that stops being true: some input, delivered over the network, is treated as instructions. The code then runs with the same rights as the process that was fooled, so a web server running as an account that can read the application's configuration hands the attacker that configuration.
The name is shared by two different things. Sometimes it describes a vulnerability class, a flaw that allows execution. Sometimes it describes the incident, an attacker actually running something. This page uses the first meaning and says when it means the second.
RCE sits near the top of any severity ranking because nothing in the application's own logic limits what comes next. A broken access check exposes one record; code execution exposes whatever the process can touch. The web attacks overview places it alongside the flaws that usually cause it. A library flaw is the best-known case: Log4Shell; another route is insecure deserialization.
The main classes
The routes below differ in where the attacker's input ends up, which also decides what can be done about each.
Injection into commands and code
If a value from a request is joined into a shell command, the shell reads it as part of the command. That is OS command injection, CWE-78. If the value reaches an evaluator in the application's own language, it is code injection, CWE-94. Both are covered in detail, with prevention, on the command injection page.
Template engines are a variant. When user text becomes part of the template rather than a value placed into it, expressions in that text are evaluated. MITRE tracks this as CWE-1336. The test and the fix are in the OWASP guidance linked from the command injection page: treat user text as data, and sandbox any engine that users are allowed to edit. See server-side template injection for how it works.
Unsafe deserialization
Many languages can turn a stream of bytes back into live objects. If the stream comes from an untrusted source, the attacker chooses which objects are built, and in some runtimes building certain objects triggers code that was already on the server. CWE-502 describes it in one line: the product deserializes untrusted data without making sure the result is valid.
Nothing in the request looks like a command. It is an opaque blob in a cookie, a header or a body field, which makes it hard to recognise by pattern. OWASP files the topic under software or data integrity failures. The usual remedy is to stop accepting serialized objects from clients, or to sign them and use a format that carries only data.
Vulnerable components
Most applications are mostly other people's code. A flaw in a framework, a parser or a logging library becomes a flaw in every application that includes it, and the application's own code can be flawless. The 2025 OWASP list treats this under software supply chain failures.
These cases tend to be disclosed with a CVE identifier and a fixed version, and attackers scan for the affected software soon after. Until the release is installed, the flaw is an exploit target; before a fix exists at all, it is a zero-day.
Writing a file the server will run
A flaw that lets an attacker put a script where the server executes it also ends in RCE. The two usual entrances are an unrestricted file upload and a path traversal that can write outside the intended directory. The planted script is often a web shell, which keeps the access after the original flaw is closed.
Example: Log4Shell, CVE-2021-44228
Log4Shell is the best-known recent case of the vulnerable-component route. The NVD record describes Apache Log4j2 versions from 2.0-beta9 through 2.15.0: its JNDI features did not protect against attacker-controlled LDAP and other JNDI endpoints, so when message lookup substitution was enabled, an attacker who controlled log messages or their parameters could execute arbitrary code loaded from an LDAP server. NVD lists CWE-20, CWE-400, CWE-502 and CWE-917 for it, and the record shows it was added to CISA's Known Exploited Vulnerabilities catalog on the day it was published.
What made it hard is that the dangerous text did not need to reach a special endpoint. Anything an application logged could carry it: a header, a form field, a username. Teams had to find every place Log4j was used, including copies bundled inside other products. Log4j 2.15.0 disabled the behaviour by default and 2.16.0 removed it, per the same record; the practical fix was an upgrade, and where one was not yet possible, filtering requests for the lookup syntax was a stopgap.
It is a useful case because it shows both halves of this topic. The flaw was in a dependency no request filter could repair, and the first thing many operators could do in the first hours was exactly that: filter.
What an attacker does next
Code running on the server can read what the application can read: configuration, environment variables with database passwords and API keys, and the data those credentials open. From the server the attacker can also reach systems on the internal network that are not exposed to the internet.
Two settings limit the damage when prevention fails. The application should run with the fewest privileges it needs, and the server should be allowed only the outbound connections it actually makes, because a fetched payload or a stolen file needs a way out. Neither stops the injection. Both change what it costs.
Where a WAF helps and where it does not
A web application firewall inspects requests before they reach the origin. For remote code execution it has two jobs of different value.
Where it helps. When a vulnerable component is disclosed and a request pattern that triggers it is known, a rule can match that pattern. That is virtual patching: the flaw stays in the code, but the request that reaches it is refused. It is a way to buy days for testing and rollout, not a repair. A rule can also match the plain shapes of injection, such as shell separators and common system program names in a parameter.
Where it does not. A filter reads the request it is given. It cannot see a payload that is encoded in a way it does not decode, assembled from pieces inside the application, or sent over a channel that does not go through HTTP. Serialized objects are opaque to it. An upload whose danger is in the file's contents has no suspicious syntax in the request. And a rule written for one known trigger says nothing about the next variation of the flaw. Treat a WAF as one layer, and the patch as the actual fix.
- Start in monitor. Matches are recorded and every request is still served. Review the log of flagged requests against real traffic before anything is refused; the page on WAF monitor mode explains the habit.
- Check what would have been refused. Legitimate requests sometimes contain shell-like text, for example in a support form. Sort those out as false positives and add narrow exceptions.
- Move to block deliberately. Switch the domain to block once the log looks right, ideally in steps, as in a staged rollout. Keep the patch on the schedule regardless.
How to reduce the risk
- Keep an inventory of components so that when a CVE like Log4Shell appears you can answer where it is used within hours, not weeks.
- Patch on a schedule, and faster for flaws that are known to be exploited. CISA's catalog above is one list to watch.
- Do not evaluate input. No shell calls built from request data, no eval, no user-editable template without a sandbox.
- Do not deserialize what clients send. Use a data-only format, or sign and verify before reading.
- Limit the blast radius. Least privilege for the process, restricted outbound connections, secrets scoped to what each service needs.
Questions
What is remote code execution (RCE)?
Remote code execution (RCE) is a flaw that lets an attacker run their own code or commands on a server over the network, with the privileges of the vulnerable program. It is usually the end result of another weakness, such as injection, unsafe deserialization or a vulnerable library.
What is an RCE attack?
An RCE attack is a request, message or file crafted so that a vulnerable application executes attacker-chosen code. The attacker needs no prior login in many cases, which is why vulnerabilities of this kind are patched first and exploited quickly.
Can a WAF stop remote code execution?
Partly. A WAF can match request patterns known to trigger a specific flaw and refuse them, which buys time before a patch is installed. It cannot repair the vulnerable code, and it can miss encoded, nested or non-HTTP paths to the same flaw.
What is Log4Shell?
Log4Shell is the name for CVE-2021-44228, a remote code execution flaw in Apache Log4j 2. Log4j could resolve JNDI lookups inside logged text, so attacker-controlled input that reached a log line could make the server load and run remote code. The fix is to upgrade Log4j.
How do you prevent remote code execution?
Keep dependencies patched, never pass input to a shell or an evaluator, do not deserialize untrusted data, run the application with least privilege and limit its outbound connections. A WAF in monitor and then block mode adds a filtering layer on top of these, not instead of them.