Server-Side Code Injection

User input is evaluated as program code by the server-side language, for example through eval(), so an attacker can run their own code on the server.

SeverityCritical
CVSS 3.19.8 CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H Open in the calculator
CVSS 4.09.3 CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N Open in the calculator
CWECWE-94: Improper Control of Generation of Code ('Code Injection')
OWASP Top 10A05:2025 Injection
MITRE ATT&CKT1190 Exploit Public-Facing Application, T1059 Command and Scripting Interpreter
CategoryInjection

Description

The application passes values supplied by the user to a function that compiles or evaluates code in its own language at run time, such as eval() in PHP, Python, JavaScript or Ruby, exec() in Python, the Function constructor in JavaScript, or a scripting engine that evaluates expressions held in strings. Developers often do this to evaluate a calculation, to read a data structure written in the language's own syntax, or to call a function chosen by name. Any input that reaches such a function can contain code of the attacker's choosing.

An attacker can therefore run arbitrary code inside the application process, with the privileges of the account that runs it, and from there read and modify all data the application can reach, read secrets from its configuration and environment, and use the server as a foothold for attacking internal systems. The weakness differs from OS command injection in that the input is run by the application's own interpreter rather than by a shell, although the interpreter can usually be used to run operating system commands as well. No user interaction is needed, and the impact is usually equivalent to a full compromise of the application server.

Server-side code injection typically shows itself on a test when an expression supplied in a parameter is evaluated, for example an arithmetic expression whose result appears in the response, a language function that produces a measurable delay, or an error message showing that the input was parsed as code. Scanners such as Burp Suite report it under language-specific names, such as PHP code injection or server-side JavaScript code injection, and blind cases are confirmed with time delays or out-of-band DNS lookups.

Remediation

Do not evaluate user input as code:

  • Remove calls that evaluate strings as code, such as eval(), exec(), new Function() and string arguments to setTimeout(), wherever any part of the string can come from outside the application.
  • Replace them with alternatives that treat input as data: a JSON parser instead of evaluating object literals, a purpose-built expression parser or arithmetic library for user-defined formulas, and an allowlist that maps permitted names to functions instead of resolving a function or class from a user-supplied name.
  • Do not rely on blocklists or on language-level sandboxes, such as the Node.js vm module, to make evaluation safe. They are routinely bypassed and are not security boundaries.
  • Run the application with the least privilege it needs and with restricted outbound network access, so that successful exploitation gives as little as possible.

As an interim measure, disable the affected feature or block the affected parameters at a web application firewall until the code is changed. Confirm the fix by repeating the original test with expressions that would produce a measurable result or delay if evaluated, and checking that they are treated as literal text.

References


Add This Finding to Your Library

In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.

Free trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Web Applications Findings

Every Web Applications finding