LOCAL/REMOTE FILE INCLUSION
DEMO 1: SERANGAN LFI
- Buat sebuah folder di C:\xampp\htdocs\ bernama lfi.
- Di dalam folder lfi, buat file bernama index.php dengan isi sebagai berikut:
<?php
// index.php
$page = isset($_GET['page']) ? $_GET['page'] : 'home.php';
include($page);
?>
- Buat file home.php di folder yang sama dengan isi:
<?php
echo "Welcome to the home page!";
?>
DEMO 2: SERANGAN RFI
- Buat folder di C:\xampp\htdocs\ bernama uploads.
- Buat file upload.php di C:\xampp\htdocs\lfi\ dengan isi sebagai berikut:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars(basename($_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>
</body>
</html>
- Buat file PHP berbahaya (shell.php) dengan isi:
<?php
echo "Shell executed!";
?>
Untuk mengakses http://localhost/lfi/upload.php, pilih file shell.php, dan
upload.
maka hasil sebagai berikut :
TUGAS
1. Tambahkan script pada file tersebut agar terhindar dari
serangan LFI & RFI
- Mencegah LFI
Berikut adalah contoh script yang dimodifikasi untuk mencegah LFI: (index.php)
<?php
// index.php
$whitelist = ['home.php', 'about.php', 'contact.php']; // Daftar file yang diizinkan
$page = isset($_GET['page']) ? $_GET['page'] : 'home.php';
if (in_array($page, $whitelist)) {
include($page);
} else {
echo "Halaman tidak ditemukan.";
}
?>
- Mencegah RFI
Lalu contoh script yang dimodifikasi untuk mencegah RFI: (upload.php)
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Validasi jenis file
$allowedTypes = ['jpg', 'png', 'gif', 'jpeg']; // Contoh ekstensi file yang diizinkan
if (in_array($fileType, $allowedTypes)) {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars(basename($_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
} else {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>
</body>
</html>
2. Jelaskan dampak dari serangan LFI & RFI
LFI (Local File Inclusion)
- Disclosure of Sensitive Information: Penyerang dapat membaca file sensitif di server seperti file konfigurasi, yang bisa mengungkap informasi penting seperti kredensial database.
- Remote Code Execution: Penyerang dapat menggunakan LFI untuk menjalankan skrip PHP yang ada di server, yang dapat menyebabkan eksekusi kode jarak jauh.
RFI (Remote File Inclusion)
- Remote Code Execution: Penyerang dapat menyertakan file dari server lain yang berisi kode berbahaya dan mengeksekusinya di server target.
- Malware Insertion: Penyerang dapat menyisipkan malware atau backdoor ke dalam server, yang dapat digunakan untuk mengambil alih server atau mencuri data.
Dengan menerapkan langkah-langkah pencegahan di atas, Anda dapat mengurangi risiko serangan LFI dan RFI dan meningkatkan keamanan aplikasi web Anda.
Komentar
Posting Komentar