RFI

PHP Example

Vulnerable Code:

phpCopy<?php
$template = $_GET['template'];
include($template);
?>

Reason for Vulnerability:

This code allows an attacker to include files from remote servers, potentially executing malicious code.

Fixed Code:

phpCopy<?php
$allowed_templates = ['header', 'footer', 'sidebar'];
$template = $_GET['template'];

if (!in_array($template, $allowed_templates)) {
    die('Invalid template');
}

include __DIR__ . '/templates/' . $template . '.php';
?>

Reason for Fix:

The fixed code uses a whitelist of allowed templates and includes files from a specific local directory, preventing RFI.


Example 1: PHP

Vulnerable Code:

Reason for vulnerability: User input is directly included, allowing RFI.

Fixed Code:

Reason for fix: Validate and restrict URLs to trusted domains.

Example 2: Python

Vulnerable Code:

Reason for vulnerability: User input is directly used in the URL, allowing RFI.

Fixed Code:

Reason for fix: Validate and restrict URLs to trusted domains.

Last updated