Back to Learn

What is Arbitrary Code Execution? | NOC.org

Understanding Arbitrary Code Execution

Arbitrary Code Execution (ACE) is a vulnerability that allows an attacker to run any commands or code of their choosing on a target system. When the attack can be performed remotely over a network without physical access to the machine, it is called Remote Code Execution (RCE). These are among the most critical vulnerabilities in security because they give attackers complete control over the affected system.

An RCE vulnerability effectively turns your web server into the attacker's computer. They can install backdoors, steal data, modify files, pivot to internal networks, launch attacks against other systems, or encrypt your files for ransomware. CVSS scores for RCE vulnerabilities are almost always rated Critical (9.0 or higher) because the impact is total system compromise.

Common Attack Vectors

File Upload Exploits

Many web applications allow users to upload files such as images, documents, or attachments. If the application does not properly validate uploaded files, an attacker can upload a script file (such as a PHP web shell) and then access it through the web server to execute commands.

A typical attack proceeds as follows:

  1. The attacker finds a file upload form that accepts images but does not verify the actual file type.
  2. They upload a PHP file named shell.php.jpg or modify the MIME type header to bypass basic checks.
  3. The file is saved to a publicly accessible directory on the web server.
  4. The attacker navigates to https://example.com/uploads/shell.php and gains a command shell on the server.

Command Injection

Command injection occurs when an application passes user-supplied input to a system shell command without proper sanitization. For example, a network diagnostic tool might accept a hostname and pass it to the ping command:

$output = shell_exec('ping -c 4 ' . $_GET['host']);

An attacker can append additional commands using shell metacharacters:

host=example.com; cat /etc/passwd

The semicolon terminates the ping command and executes the attacker's command. Similar metacharacters include | (pipe), && (AND), || (OR), and backticks for command substitution.

Deserialization Vulnerabilities

Many programming languages allow objects to be serialized (converted to a byte stream) for storage or transmission and then deserialized (reconstructed) later. If an application deserializes data from an untrusted source, an attacker can craft a malicious serialized object that executes code during the deserialization process.

The Log4Shell vulnerability (CVE-2021-44228) is a well-known example. The Log4j library in Java would process specially crafted strings in log messages, triggering JNDI lookups that could load and execute remote code. This single vulnerability affected hundreds of thousands of applications worldwide.

Server-Side Template Injection (SSTI)

Template engines like Jinja2, Twig, and Freemarker allow developers to embed dynamic content in HTML templates. If user input is inserted directly into a template string rather than passed as data, an attacker can inject template expressions that execute code on the server.

Code Evaluation Functions

Functions like eval() in PHP, Python, and JavaScript execute strings as code. If user input reaches these functions, it grants the attacker direct code execution. This also applies to similar functions like exec(), system(), passthru(), and preg_replace() with the /e modifier in older PHP versions.

Real-World Impact

Once an attacker achieves code execution on a web server, the typical post-exploitation sequence includes:

  • Persistence: Installing web shells, cron jobs, or SSH keys to maintain access even if the original vulnerability is patched.
  • Privilege escalation: Exploiting local vulnerabilities to gain root or administrator access from the web server's limited user account.
  • Lateral movement: Scanning internal networks and exploiting other systems accessible from the compromised server.
  • Data exfiltration: Accessing databases, configuration files, and sensitive data stored on the server or connected systems.
  • Cryptomining: Installing cryptocurrency miners that consume server resources for the attacker's profit.

Prevention Strategies

Secure File Upload Handling

  • Validate file types by checking magic bytes (file headers), not just the extension or MIME type.
  • Store uploaded files outside the web root so they cannot be executed by the web server.
  • Rename uploaded files to random names, removing any user-supplied file name.
  • Serve uploaded files through a separate domain or CDN to isolate them from your application.
  • Disable script execution in upload directories using web server configuration.

Avoid Dangerous Functions

Never pass user input to functions that execute system commands or evaluate code. If you must interact with the operating system, use language-specific libraries that do not invoke a shell. In PHP, use escapeshellarg() and escapeshellcmd() as a last resort, but prefer alternatives that avoid shell interaction entirely.

Input Validation and Sandboxing

Apply strict input validation on all data that interacts with the file system, shell commands, or template engines. Use allowlists for expected values. Run applications in containers or sandboxed environments that limit the impact of a successful exploit.

Keep Software Updated

Many RCE vulnerabilities exist in third-party libraries and frameworks. Maintain an inventory of your dependencies and apply security patches promptly. Use dependency scanning tools to identify known vulnerable versions in your software supply chain.

WAF and Runtime Protection

A Web Application Firewall can block many common RCE payloads by detecting shell commands, encoded scripts, and known exploit patterns in HTTP requests. Runtime application self-protection (RASP) tools can detect and block code execution attempts from within the application itself.

Summary

Arbitrary code execution vulnerabilities represent the most severe class of web security flaws. They allow attackers to fully compromise your server, and the consequences range from data theft to ransomware to attacks against your internal network. Prevention requires secure coding practices including proper file upload handling, avoiding dangerous functions, strict input validation, timely patching, and WAF protection as an additional defense layer.

Improve Your Websites Speed and Security

14 days free trial. No credit card required.