LOCAL/REMOTE FILE INCLUSION

LOCAL/REMOTE FILE INCLUSION

 DEFINISI

•Local File Inclusion (LFI) adalah sebuah kerentanan dalam aplikasi web yang memungkinkan penyerang untuk memasukkan file lokal ke dalam eksekusi kode melalui input yang tidak divalidasi.

 • Sedangkan serangan yang melibatkan upload file lalu mengeksekusi file tersebut disebut Remote File Inclusion (RFI)


TUJUAN SERANGAN 

• Membaca file sensitif di server. 

• Mengeksekusi skrip berbahaya. 

• Mencuri data atau mendapatkan akses lebih lanjut.


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!";
?>

• Buka browser dan akses http://localhost/lfi/index.php. 



•Untuk membaca file di sistem Windows, kita bisa mencoba membaca file C:\xampp\htdocs\lfi\home.php dengan mengubah parameter URL: http://localhost/lfi/index.php?page=C:/xampp/htdocs/lfi/home.php

•Coba akses file sensitif seperti konfigurasi Apache: http://localhost/lfi/index.php?page=C:/xampp/apache/conf/httpd.conf Catatan: Penyerang bisa mencoba berbagai path untuk menemukan file yang bisa diakses.

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!";
?>

•Akses http://localhost/lfi/upload.php, pilih file shell.php, dan upload.



• Setelah file diupload, akses URL untuk mengeksekusi: http://localhost/uploads/shell.php


TUGAS 
• Tambahkan script pada file tersebut agar terhindar dari serangan LFI & RFI

Mencegah LFI

Modifikasi index.php untuk menghindari LFI:

<?php
// index.php
$whitelist = ['home.php', 'about.php', 'contact.php']; // Tambahkan file yang diijinkan
$page = isset($_GET['page']) ? $_GET['page'] : 'home.php';
if (in_array($page, $whitelist)) {
    include($page);
} else {
    echo "File not found!";
}
?>

Mencegah RFI

Modifikasi upload.php untuk menghindari RFI:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $fileType = pathinfo($target_file, PATHINFO_EXTENSION);
    $allowedTypes = ['jpg', 'jpeg', 'png', 'gif', 'pdf']; // File yang diijinkan

    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, & PDF files are allowed.";
    }
}
?>


Hasilnya: 

pilih choose file lalu upload shell.php dan klik button upload file akan muncul hasil seperti dibawah ini :



Dampak dari Serangan LFI & RFI

  • LFI:

    • Membaca file sensitif (misalnya konfigurasi server, file password).
    • Mengeksekusi skrip berbahaya yang ada di server.
    • Eskalasi hak akses dengan membaca file seperti /etc/passwd di sistem UNIX.
  • RFI:

    • Mengunggah dan mengeksekusi file berbahaya dari server eksternal.
    • Mengeksekusi skrip berbahaya yang bisa mengambil alih server.
    • Pencurian data dan eskalasi hak akses.







TERIMA KASIH






Komentar

Postingan populer dari blog ini

DASAR WEB HACKING

BRUTE FORCE