Table of Contents
相关推荐:
Home Backend Development PHP Tutorial Detailed explanation of how webshell scans files for Trojans in PHP

Detailed explanation of how webshell scans files for Trojans in PHP

May 18, 2018 pm 04:52 PM
php web webshell

这篇文章主要介绍了PHP实现webshell扫描文件木马的方法,涉及php针对数值传输的过滤操作与文件识别等相关安全操作技巧,需要的朋友可以参考下

具体如下:

可扫描 weevelyshell 生成 或加密的shell 及各种变异webshell

目前仅支持php

支持扫描 weevelyshell 生成 或加密的shell
支持扫描callback一句话shell
支持各种php大马

代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset=&#39;gb2312&#39;>
    <title>PHP web shell scan</title>
</head>
<body>
</body>
<?php
define("SELF",php_self());
error_reporting(E_ERROR);
ini_set(&#39;max_execution_time&#39;,20000);
ini_set(&#39;memory_limit&#39;,&#39;512M&#39;);
header("content-Type: text/html; charset=gb2312");
function weevelyshell($file){
    $content=file_get_contents($file);
    if(
        (
        preg_match(&#39;#(\$\w{2,4}\s?=\s?str_replace\("\w+","","[\w_]+"\);\s?)+#s&#39;,$content)&&
      preg_match(&#39;#(\$\w{2,4}\s?=\s?"[\w\d\+\/\=]+";\s?)+#&#39;,$content)&&               preg_match(&#39;#\$[\w]{2,4}\s?=\s\$[\w]{2,4}\(\&#39;\&#39;,\s?\$\w{2,4}\(\$\w{2,4}\("\w{1,4}",\s?"",\s?\$\w{2,4}\.\$\w{2,4}\.\$\w{2,4}\.\$\w{2,4}\)\)\);\s+?\$\w{2,4}\(\)\;#&#39;,$content))
          ||
          (preg_match(&#39;#\$\w+\d\s?=\s?str_replace\(\"[\w\d]+\",\"\",\"[\w\d]+\"\);#s&#39;,$content)&&
        preg_match(&#39;#\$\w+\s?=\s?\$[\w\d]+\(\&#39;\&#39;,\s?\$[\w\d]+\(\$\w+\(\$\w+\(\"[[:punct:]]+\",\s?\"\",\s?\$\w+\.\$\w+\.\$\w+\.\$\w+\)\)\)\);\s?\$\w+\(\);#s&#39;,$content))
        ){
        return true;
    }
}
function callbackshell($file){
    $content=file_get_contents($file);
    if(
        preg_match(&#39;#\$\w+\s?=\s?\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[.*?\]#is&#39;,$content)&&
        preg_match(&#39;#\$\w+\s?=\s?(?:new)?\s?array\w*\s?\(.*?_(?:GET|POST|REQUEST|COOKIE|SERVER)\[.*?\].*?\)+#is&#39;,$content)&&
        preg_match(&#39;#(?:array_(?:reduce|map|udiff|walk|walk_recursive|filter)|u[ak]sort)\s?\(.*?\)+?#is&#39;,$content)
        )
            return true;
}
function php_self(){
  $php_self=substr($_SERVER[&#39;PHP_SELF&#39;],strrpos($_SERVER[&#39;PHP_SELF&#39;],&#39;/&#39;)+1);
  return $php_self;
}
$matches = array(
        &#39;/mb_ereg_replace\([\&#39;\*\s\,\.\"]+\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[[\&#39;\"].*?[\&#39;\"][\]][\,\s\&#39;\"]+e[\&#39;\"]&#39;/is,
        &#39;/preg_filter\([\&#39;\"\|\.\*e]+.*\$_(?:GET|POST|REQUEST|COOKIE|SERVER)/is&#39;,
        &#39;/create_function\s?\(.*assert\(/is&#39;,
        &#39;/ini_get\(\&#39;safe_mode\&#39;\)/i&#39;,
        &#39;/get_current_user\(.*?\)/i&#39;,
        &#39;/@?assert\s?\(\$.*?\)/i&#39;,
        &#39;/proc_open\s?\(.*?pipe\&#39;,\s?\&#39;w\&#39;\)/is&#39;,
    &#39;/sTr_RepLaCe\s?\([\&#39;\"].*?[\&#39;\"],[\&#39;\"].*?[\&#39;\"]\s?,\s?\&#39;a[[:alnum:][:punct:]]+?s[[:alnum:][:punct:]]+?s[[:alnum:][:punct:]]+?e[[:alnum:][:punct:]]+?r[[:alnum:][:punct:]]+?t[[:alnum:][:punct:]]+?\)/i&#39;,
        &#39;/preg_replace_callback\(.*?create_function\(/is&#39;,
        &#39;/filter_var(?:_array)?\s?.*?\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[[\&#39;\"][[:punct:][:alnum:]]+[\&#39;\"]\][[:punct:][:alnum:][:space:]]+?assert[\&#39;\"]\)/is&#39;,
        &#39;/ob_start\([\&#39;\"]+assert[\&#39;\"]+\)/is&#39;,
        &#39;/new\s?ReflectionFunction\(.*?->invoke\(/is&#39;,
      &#39;/PDO::FETCH_FUNC/&#39;,
        &#39;/\$\w+.*\s?(?:=|->)\s?.*?[\&#39;\"]assert[\&#39;\"]\)?/i&#39;,
        &#39;/\$\w+->(?:sqlite)?createFunction\(.*?\)/i&#39;,
        &#39;/eval\([\"\&#39;]?\\\?\$\w+\s?=\s?.*?\)/i&#39;,
        &#39;/eval\(.*?gzinflate\(base64_decode\(/i&#39;,
        &#39;/copy\(\$HTTP_POST_FILES\[\&#39;\w+\&#39;\]\s?\[\&#39;tmp_name\&#39;\]/i&#39;,
        &#39;/register_(?:shutdown|tick)_function\s?\(\$\w+,\s\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[.*?\]\)/is&#39;,
        &#39;/register_(?:shutdown|tick)_function\s?\(?[\&#39;\"]assert[\"\&#39;].*?\)/i&#39;,
        &#39;/call_user_func.*?\([\"|\&#39;]assert[\"|\&#39;],.*\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[[\&#39;|\"].*\]\)+/is&#39;,
      &#39;/preg_replace\(.*?e.*?\&#39;\s?,\s?.*?\w+\(.*?\)/i&#39;,
    &#39;/function_exists\s*\(\s*[\&#39;|\"](popen|exec|proc_open|system|passthru)+[\&#39;|\"]\s*\)/i&#39;,
    &#39;/(exec|shell_exec|system|passthru)+\s*\(\s*\$_(\w+)\[(.*)\]\s*\)/i&#39;,
    &#39;/(exec|shell_exec|system|passthru)+\s*\(\$\w+\)/i&#39;,
    &#39;/(exec|shell_exec|system|passthru)\s?\(\w+\(\"http_.*\"\)\)/i&#39;,
         &#39;/(?:john\.barker446@gmail\.com|xb5@hotmail\.com|shopen@aventgrup\.net|milw0rm\.com|www\.aventgrup\.net|mgeisler@mgeisler\.net)/i&#39;,
      &#39;/Php\s*?Shell/i&#39;,
    &#39;/((udp|tcp)\:\/\/(.*)\;)+/i&#39;,
    &#39;/preg_replace\s*\((.*)\/e(.*)\,\s*\$_(.*)\,(.*)\)/i&#39;,
    &#39;/preg_replace\s*\((.*)\(base64_decode\(\$/i&#39;,
    &#39;/(eval|assert|include|require|include_once|require_once)+\s*\(\s*(base64_decode|str_rot13|gz(\w+)|file_(\w+)_contents|(.*)php\:\/\/input)+/i&#39;,
    &#39;/(eval|assert|include|require|include_once|require_once|array_map|array_walk)+\s*\(.*?\$_(?:GET|POST|REQUEST|COOKIE|SERVER|SESSION)+\[(.*)\]\s*\)/i&#39;,
    &#39;/eval\s*\(\s*\(\s*\$\$(\w+)/i&#39;,
      &#39;/((?:include|require|include_once|require_once)+\s*\(?\s*[\&#39;|\"]\w+\.(?!php).*[\&#39;|\"])/i&#39;,
    &#39;/\$_(\w+)(.*)(eval|assert|include|require|include_once|require_once)+\s*\(\s*\$(\w+)\s*\)/i&#39;,
    &#39;/\(\s*\$_FILES\[(.*)\]\[(.*)\]\s*\,\s*\$_(GET|POST|REQUEST|FILES)+\[(.*)\]\[(.*)\]\s*\)/i&#39;,
    &#39;/(fopen|fwrite|fputs|file_put_contents)+\s*\((.*)\$_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\](.*)\)/i&#39;,
    &#39;/echo\s*curl_exec\s*\(\s*\$(\w+)\s*\)/i&#39;,
    &#39;/new com\s*\(\s*[\&#39;|\"]shell(.*)[\&#39;|\"]\s*\)/i&#39;,
    &#39;/\$(.*)\s*\((.*)\/e(.*)\,\s*\$_(.*)\,(.*)\)/i&#39;,
    &#39;/\$_\=(.*)\$_/i&#39;,
    &#39;/\$_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\]\(\s*\$(.*)\)/i&#39;,
    &#39;/\$(\w+)\s*\(\s*\$_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\]\s*\)/i&#39;,
    &#39;/\$(\w+)\s*\(\s*\$\{(.*)\}/i&#39;,
    &#39;/\$(\w+)\s*\(\s*chr\(\d+\)/i&#39;
);
function antivirus($dir,$exs,$matches) {
    if(($handle = @opendir($dir)) == NULL) return false;
    while(false !== ($name = readdir($handle))) {
        if($name == &#39;.&#39; || $name == &#39;..&#39;) continue;
        $path = $dir.$name;
        if(strstr($name,SELF)) continue;
        //$path=iconv("UTF-8","gb2312",$path);
        if(is_dir($path)) {
            //chmod($path,0777);/*主要针对一些0111的目录*/
            if(is_readable($path)) antivirus($path.&#39;/&#39;,$exs,$matches);
        } elseif(strpos($name,&#39;;&#39;) > -1 || strpos($name,&#39;%00&#39;) > -1 || strpos($name,&#39;/&#39;) > -1) {
            echo &#39;特征 <input type="text" style="width:250px;" value="解析漏洞">     &#39;.$path.&#39;<p></p>&#39;; flush(); ob_flush();
        }
        else {
            if(!preg_match($exs,$name)) continue;
            if(filesize($path) > 10000000) continue;
            $fp = fopen($path,&#39;r&#39;);
            $code = fread($fp,filesize($path));
            fclose($fp);
            if(empty($code)) continue;
            if(weevelyshell($path)){
            echo &#39;特征 <input type="text" style="width:250px;" value="weevely 加密shell">     &#39;.$path.&#39;<p></p>&#39;; flush(); ob_flush();
        }elseif(callbackshell($path)){
                echo &#39;特征 <input type="text" style="width:250px;" value="Callback shell">     &#39;.$path.&#39;<p></p>&#39;; flush(); ob_flush();
        }
            foreach($matches as $matche) {
                $array = array();
                preg_match($matche,$code,$array);
                if(!$array) continue;
                if(strpos($array[0],"\x24\x74\x68\x69\x73\x2d\x3e")) continue;
                $len = strlen($array[0]);
                if($len > 6 && $len < 200) {
                    echo &#39;特征 <input type="text" style="width:250px;" value="&#39;.htmlspecialchars($array[0]).&#39;">    &#39;.$path.&#39;<p></p>&#39;;
                    flush(); ob_flush(); break;
                }
            }
            unset($code,$array);
        }
    }
    closedir($handle);
    return true;
}
function strdir($str) { return str_replace(array(&#39;\\&#39;,&#39;//&#39;,&#39;//&#39;),array(&#39;/&#39;,&#39;/&#39;,&#39;/&#39;),chop($str)); }
echo &#39;<form method="POST">&#39;;
echo &#39;路径: <input type="text" name="dir" value="&#39;.($_POST[&#39;dir&#39;] ? strdir($_POST[&#39;dir&#39;].&#39;/&#39;) : strdir($_SERVER[&#39;DOCUMENT_ROOT&#39;].&#39;/&#39;)).&#39;" style="width:398px;"><p></p>&#39;;
echo &#39;后缀: <input type="text" name="exs" value="&#39;.($_POST[&#39;exs&#39;] ? $_POST[&#39;exs&#39;] : &#39;.php|.inc|.phtml&#39;).&#39;" style="width:398px;"><p></p>&#39;;
echo &#39;操作: <input type="submit" style="width:80px;" value="scan"><p></p>&#39;;
echo &#39;</form>&#39;;
if(file_exists($_POST[&#39;dir&#39;]) && $_POST[&#39;exs&#39;]) {
    $dir = strdir($_POST[&#39;dir&#39;].&#39;/&#39;);
    $exs = &#39;/(&#39;.str_replace(&#39;.&#39;,&#39;\\.&#39;,$_POST[&#39;exs&#39;]).&#39;)/i&#39;;
    echo antivirus($dir,$exs,$matches) ? &#39;</br ><p></p>扫描完毕!&#39; : &#39;</br > <p></p>扫描中断&#39;;
}
?>
</html>
Copy after login

相关推荐:

PHP webshell扫描木马文件的实现方法

详解PHP安全之webshell和后门检测示例

Webshell实现与隐藏探究

The above is the detailed content of Detailed explanation of how webshell scans files for Trojans in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles