Remote File Inclusion

  • CAPEC 252
  • CWE 98
  • PCI 3.2-6.5.8
  • WASC 33
  • OWASP 2013-A4

Remote File Inclusion (RFI) is a severe vulnerability that occurs when an application accepts untrusted input that references external resources (URLs) and subsequently retrieves and executes or includes that remote content on the server. Unlike Local File Inclusion (LFI), which targets files already present on the host, RFI allows an attacker to host malicious payloads on an external server and force the vulnerable application to fetch and run that payload — typically resulting in immediate remote code execution and full server compromise.

Common RFI vectors:

  • User-controlled parameters used directly in include/require calls that accept URLs or support URL wrappers.
  • Misconfigured environments or interpreters that allow remote resource loading (e.g., PHP with allow_url_include = On and allow_url_fopen = On).
  • Dynamic module/plugin loaders or template engines that accept remote URIs without restriction.
  • Features that fetch external templates, widgets, or scripts based on user-supplied URLs.

Typical impacts:

  • Full remote code execution: attacker-supplied code runs in the context of the web application process.
  • Data theft: exposure of application source, configuration files, and secrets.
  • Pivot and persistence: install backdoors, create persistent web shells, or move laterally within the network.
  • Reputation and regulatory damage: immediate and wide-reaching compromise can lead to data breaches and service outages.

Why RFI happens:

  • Allowing external URIs in file inclusion APIs or enabling URL wrappers in interpreters.
  • Trusting user input to determine resource locations without validation or whitelisting.
  • Excessive interpreter features enabled in production for convenience or legacy compatibility.
  • Lack of egress controls that would otherwise prevent the server from reaching attacker-controlled hosts.

Detection indicators:

  • Outbound HTTP/DNS requests from the web server to unknown or attacker-controlled domains.
  • Application code paths where include/require/load operations are fed by request parameters.
  • Monitoring/logging showing unusual content retrieved at inclusion points or errors following remote includes.
  • Dynamic scanning with payloads pointing to a controlled remote host that results in command execution.
Remediation

Mitigating RFI requires removing the ability to include arbitrary remote content, enforcing strict validation/whitelisting, and constraining network capabilities of application hosts.

  1. Disable Remote Inclusion Features
    Configure the runtime/interpreter to disallow loading remote resources via include APIs (e.g., in PHP set allow_url_include = Off, allow_url_fopen = Off where feasible). Remove or disable URL wrappers and remote file access in production.

  2. Never Trust User-Supplied URLs for Inclusion
    Do not pass user-provided URLs directly to include, require, import, or dynamic loader functions. If a resource must be selected by users, use server-side mappings (IDs → vetted local files) or a strict whitelist of allowed locations.

  3. Whitelist Hosts or Resources
    If external integrations are necessary, define an explicit whitelist of approved hostnames and resource paths. Validate that requested URIs match the whitelist and use only normalized, canonicalized URLs.

  4. Fetch and Validate Before Execution
    If external content must be fetched, download it into a secure, isolated staging area first, then perform rigorous validation (content type checks, signature verification, malware scanning). Never directly include remote content in an executable context.

  5. Network Egress Controls
    Apply network-level controls to prevent application servers from making arbitrary outbound connections. Use firewall rules, proxying with allowlists, or cloud egress policies to limit reachable hosts and ports.

  6. Principle of Least Privilege
    Run the application with minimal privileges and restrict file system and execution rights. Ensure the application process cannot write into code directories or execute arbitrary files added at runtime.

  7. Secure Uploads and Plugin Management
    For user-contributed plugins, templates, or code, require administrative approval, store uploads outside the executable path, and scan/validate content before any inclusion.

  8. Runtime Hardening
    Use containerization, chroot, AppArmor, or SELinux profiles to confine runtime behavior and reduce the blast radius if remote content is accidentally executed.

  9. Error Handling and Logging
    Do not expose remote fetch or include errors to end users. Log remote access attempts and include sufficient context to trace the source of fetched content (source URL, response headers, timestamps).

References