cybersecurity
pentesting
web security
LFI
RFI
In 2014, Sony Pictures Entertainment was affected by a significant cyber attack. While the attack involved multiple vectors, one of the vulnerabilities exploited was Local File Inclusion (LFI). The hack was so impactfull that the company had to cancel the release of the movie "The Interview". In this article we will see how Local File Inclusion works and how to prevent it.
Local File Inclusion (LFI) is a type of vulnerability most commonly found in web applications. It occurs when an application includes a file based on user input without properly sanitizing or validating that input. This can allow an attacker to manipulate the input to include files from the server's local file system, potentially exposing sensitive information or enabling further attacks.
/etc/passwd
One of the most well-known exploits of LFI vulnerabilities is the ability to view the contents of the /etc/passwd
file on Unix-based systems. This file contains information about the system's users and can provide valuable information to an attacker.
Consider a web application that includes a file based on a URL parameter:
http://example.com/index.php?file=user.txt
If the application does not properly validate the file
parameter, an attacker could manipulate it to include the /etc/passwd
file:
http://example.com/index.php?file=/etc/passwd
This would result in the application including the /etc/passwd
file, potentially exposing sensitive information about the system's users.
To avoid Local File Inclusion (LFI) vulnerabilities in a Python Flask application, it is crucial to properly validate and sanitize user inputs. Below is an example of how to securely handle file inclusion in Flask:
1from flask import Flask, request, abort 2 3app = Flask(__name__) 4 5@app.route('/') 6def index(): 7 file = request.args.get('file') 8 if file and file.endswith('.txt'): 9 try: 10 with open(file, 'r') as f: 11 content = f.read() 12 return content 13 except FileNotFoundError: 14 abort(404) 15 abort(400) 16 17if __name__ == '__main__': 18 app.run(debug=True)
1const express = require('express'); 2const path = require('path'); 3const app = express(); 4 5app.get('/', (req, res) => { 6 const file = req.query.file; 7 if (file && file.endsWith('.txt')) { 8 try { 9 const content = fs.readFileSync(file, 'utf8'); 10 res.send(content); 11 } catch (error) { 12 res.status(404).send('File not found'); 13 } 14 } else { 15 res.status(400).send('Invalid file'); 16 } 17}); 18 19app.listen(3000, () => { 20 console.log('Server is running on port 3000'); 21});
This code properly validates the file
parameter to ensure that it is a valid file and not a malicious path.
Detecting Local File Inclusion (LFI) vulnerabilities is crucial for maintaining the security of web applications. Here are some tools that can be used to detect LFI:
Burp Suite: A comprehensive web vulnerability scanner that can detect various vulnerabilities, including LFI. Here is how you can use Burp Suite to detect LFI:
/etc/passwd
, ../../../../etc/passwd
).OWASP ZAP (Zed Attack Proxy): An open-source web application security scanner that can be used to find LFI vulnerabilities. To use OWASP ZAP for detecting LFI, follow these steps:
/etc/passwd
, ../../../../etc/passwd
).Nikto: An open-source web server scanner that performs comprehensive tests against web servers for multiple vulnerabilities, including LFI.
Acunetix: A commercial web vulnerability scanner that can detect LFI among other vulnerabilities. It provides detailed reports and remediation advice.
Wfuzz: A tool designed for brute-forcing web applications, which can be used to detect LFI by fuzzing file paths and parameters.
SQLMap: Primarily designed for SQL injection, SQLMap can be adapted to identify LFI vulnerabilities by probing for file inclusion patterns. Here are the steps to use SQLMap for detecting LFI:
--file-read
option followed by the file path you want to test. For example:
sqlmap -u "http://example.com/vulnerable_page.php?file=1" --file-read="/etc/passwd"
By following these steps, you can effectively use SQLMap to detect LFI vulnerabilities in web applications.
Nmap with NSE (Nmap Scripting Engine): Nmap can be extended with custom scripts to detect LFI vulnerabilities. The NSE allows for the creation of scripts that can automate the detection of various vulnerabilities.
Metasploit: A penetration testing framework that includes modules for detecting and exploiting LFI vulnerabilities. Here are the steps to use Metasploit to detect LFI:
msfconsole
command in your terminal.search lfi
use auxiliary/scanner/http/lfi
set RHOSTS http://example.com
set TARGETURI /vulnerable_page.php?file=
run
Using these tools, security professionals can identify and mitigate LFI vulnerabilities to protect web applications from potential attacks.
In 2014, Sony Pictures Entertainment was affected by a significant cyber attack. While the attack involved multiple vectors, one of the vulnerabilities exploited was Local File Inclusion (LFI). The attackers were able to gain access to sensitive files and data, leading to a massive data breach that exposed confidential information, emails, and unreleased films.
The consequences of this hack were severe and far-reaching:
Data Exposure: Confidential employee information, including Social Security numbers, salaries, and personal emails, was leaked.
Financial Loss: The company faced significant costs for cybersecurity improvements, legal fees, and settlements. The estimated financial impact exceeded $100 million.
Reputation Damage: Sony's public image suffered, affecting relationships with employees, partners, and customers.
Operational Disruption: Sony's computer systems were offline for weeks, severely impacting business operations.
Film Release Issues: The hack led to the temporary cancellation of "The Interview" movie release, causing further financial and reputational damage.
Geopolitical Tensions: The incident escalated diplomatic tensions between the United States and North Korea, as the latter was accused of being behind the attack.
This incident underscores the critical importance of robust cybersecurity measures, including protection against vulnerabilities like LFI.