Remote File Inclusion

high Severity
Detected By: active Mode

What is Remote File Inclusion?

Remote File Inclusion (RFI) is a serious security vulnerability that allows attackers to include and execute external files within an application's environment. This typically happens when an application dynamically loads files based on user input without proper validation. If exploited, RFI can lead to unauthorized access, data leakage, and remote code execution (RCE) on the server.

Many web applications allow dynamic file inclusion, often for themes, language packs, or modular functionality. However, when user input is directly used in file system APIs without proper validation, an attacker can trick the application into including a remote script, potentially taking full control of the server.

How risky is Remote File Inclusion?

The risk associated with RFI depends on how the included file is processed by the server.

If the application simply embeds the remote file's content in the HTTP response, the impact is limited to client-side attacks, such as Cross-Site Scripting (XSS). However, if the server executes the remote file as code, the attacker can achieve Remote Code Execution (RCE), allowing them to run arbitrary commands, exfiltrate sensitive data, or take complete control of the server.

Programming languages like PHP are particularly vulnerable if allow_url_include is enabled, as they allow the execution of remote scripts via include() or require(). Similarly, Node.js applications that use require() or fs.readFile() improperly can expose themselves to RFI attacks.

Recommendations to prevent Remote File Inclusion

To prevent Remote File Inclusion (RFI), applications must never accept user input as direct arguments for file system functions like include(), require(), fs.readFile(), or file_get_contents(). Allowing dynamic file inclusion without strict validation creates an easy attack vector for remote code execution. Instead, applications should enforce a whitelist of allowed files, ensuring only explicitly permitted files can be included. Any request that does not match this whitelist should be rejected immediately.

Validating and sanitizing user input is critical to preventing path manipulation and directory traversal attacks. Additionally, remote file inclusion settings should be disabled at the server level—for example, by setting allow_url_include=Off in PHP environments. Running applications with minimal privileges further reduces the impact of a successful attack. By following these practices, organizations can effectively eliminate RFI vulnerabilities and protect against unauthorized file execution.

How to fix Remote File Inclusion?

The best mitigation is to use a predefined whitelist of allowed files and strictly validate user input. By restricting file inclusion to known, trusted files, you can prevent attackers from loading arbitrary scripts. Here are secure implementations in Python (Flask) and JavaScript (Node.js) that demonstrate how to prevent RFI:

from flask import Flask, request, send_from_directory
import os

app = Flask(__name__)

# Define a whitelist of allowed files
ALLOWED_FILES = {"home": "home.html", "about": "about.html"}

@app.route('/page', methods=['GET'])
def serve_page():
    file_key = request.args.get('file', 'home')  # Default to 'home'
    if file_key in ALLOWED_FILES:
        return send_from_directory('templates', ALLOWED_FILES[file_key])  # Securely serve file
    else:
        return "Invalid file request", 400

if __name__ == '__main__':
    app.run(debug=True)

This secure Python implementation prevents RFI by restricting file inclusion to a predefined whitelist (ALLOWED_FILES). Only known, trusted files can be loaded, eliminating the risk of executing malicious remote files.

Note: These scripts are for educational purposes only. Always ensure you have permission before scanning or testing websites you don't own or operate.

Severity Level
high
85%

Vulnissimo scored the severity risk of this vulnerability to 85 out of 100. This means that it requires immediate attention.

Test Your Application for Remote File Inclusion Now

Vulnissimo tests for Remote File Inclusion and many more. Sharpen your security posture with our advanced web vulnerability scanner.