소스 검색

修复命令行bug

liaofei 5 년 전
부모
커밋
83bf5e90b5
2개의 변경된 파일121개의 추가작업 그리고 1개의 파일을 삭제
  1. 18 0
      crmeb/app/admin/controller/store/CopyTaobao.php
  2. 103 1
      crmeb/crmeb/services/FileService.php

+ 18 - 0
crmeb/app/admin/controller/store/CopyTaobao.php

@@ -742,6 +742,9 @@ class CopyTaobao extends AuthController
         if (!strlen(trim($name))) {
             //TODO 获取要下载的文件名称
             $downloadImageInfo = $this->getImageExtname($url);
+            if(!$this->checkExtname($url,$downloadImageInfo['ext_name'])){
+                return JsonService::fail('文件后缀不合法');
+            }
             $name = $downloadImageInfo['file_name'];
             if (!strlen(trim($name))) return '';
         }
@@ -797,6 +800,21 @@ class CopyTaobao extends AuthController
         return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
     }
 
+    /**
+     * 验证下载图片文件后缀
+     * @param string $url
+     * @param string $ex
+     * @return bool
+     */
+    public function checkExtname($url = '',$ex = 'jpg')
+    {
+        if(in_array($ex, ['jpg', 'jpeg', 'gif', 'png', 'swf', 'bmp']) && ( function_exists('getimagesize') && !@getimagesize($url) ))
+        {
+            return true;
+        }
+        return false;
+    }
+
     /*
       $filepath = 绝对路径,末尾有斜杠 /
       $name = 图片文件名

+ 103 - 1
crmeb/crmeb/services/FileService.php

@@ -39,8 +39,15 @@ class FileService
 		@return:   		成功=true
 	*/
 	
-	static function write_file($filename, $writetext, $openmod='w')
+	public static function write_file($filename, $writetext, $openmod='w')
 	{
+        if (!self::checkPath($filename)) {
+            return false;
+        }
+        if (!self::checkContent($writetext)) {
+            return false;
+        }
+
 		if(@$fp = fopen($filename, $openmod)) 
 		{
 			flock($fp, 2);
@@ -953,4 +960,99 @@ class FileService
         return is_writable($file);
     }
 
+    /**
+     * 验证目录结构中是否包含特殊字符
+     * @param $path
+     * @return bool
+     */
+    public static function checkPath($path)
+    {
+        $str = ['/%00/', '"/\/|\~|\,|\。|\!|\?|\“|\”|\【|\】|\『|\』|\:|\;|\《|\》|\’|\‘|\ |\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\/|\;|\'|\`|\=|\\\|\|/"'];
+        foreach ($str as $value) {
+            if (preg_match($value, $path)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * 验证文件内容中可能存在的shell
+     * @param $content
+     * @return bool
+     */
+    public static function checkContent($content)
+    {
+        //加密类型shell
+        if ((preg_match('#(\$\w{2,4}\s?=\s?str_replace\("\w+","","[\w_]+"\);\s?)+#s', $content) && preg_match('#(\$\w{2,4}\s?=\s?"[\w\d\+\/\=]+";\s?)+#', $content) && preg_match('#\$[\w]{2,4}\s?=\s\$[\w]{2,4}\(\'\',\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}\(\)\;#', $content))
+            ||
+            (preg_match('#\$\w+\d\s?=\s?str_replace\(\"[\w\d]+\",\"\",\"[\w\d]+\"\);#s', $content) && preg_match('#\$\w+\s?=\s?\$[\w\d]+\(\'\',\s?\$[\w\d]+\(\$\w+\(\$\w+\(\"[[:punct:]]+\",\s?\"\",\s?\$\w+\.\$\w+\.\$\w+\.\$\w+\)\)\)\);\s?\$\w+\(\);#s', $content))
+        ) {
+            return false;
+        }
+        //回调类型
+        if (preg_match('#\$\w+\s?=\s?\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[.*?\]#is', $content) &&
+            preg_match('#\$\w+\s?=\s?(?:new)?\s?array\w*\s?\(.*?_(?:GET|POST|REQUEST|COOKIE|SERVER)\[.*?\].*?\)+#is', $content) &&
+            preg_match('#(?:array_(?:reduce|map|udiff|walk|walk_recursive|filter)|u[ak]sort)\s?\(.*?\)+?#is', $content)
+        ) {
+            return false;
+        }
+        //内容过滤
+        $matches = [
+            '/mb_ereg_replace\([\'\*\s\,\.\"]+\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[[\'\"].*?[\'\"][\]][\,\s\'\"]+e[\'\"]/is',
+            '/preg_filter\([\'\"\|\.\*e]+.*\$_(?:GET|POST|REQUEST|COOKIE|SERVER)/is',
+            '/create_function\s?\(.*assert\(/is',
+            '/ini_get\(\'safe_mode\'\)/i',
+            '/get_current_user\(.*?\)/i',
+            '/@?assert\s?\(\$.*?\)/i',
+            '/proc_open\s?\(.*?pipe\',\s?\'w\'\)/is',
+            '/sTr_RepLaCe\s?\([\'\"].*?[\'\"],[\'\"].*?[\'\"]\s?,\s?\'a[[:alnum:][:punct:]]+?s[[:alnum:][:punct:]]+?s[[:alnum:][:punct:]]+?e[[:alnum:][:punct:]]+?r[[:alnum:][:punct:]]+?t[[:alnum:][:punct:]]+?\)/i',
+            '/preg_replace_callback\(.*?create_function\(/is',
+            '/filter_var(?:_array)?\s?.*?\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[[\'\"][[:punct:][:alnum:]]+[\'\"]\][[:punct:][:alnum:][:space:]]+?assert[\'\"]\)/is',
+            '/ob_start\([\'\"]+assert[\'\"]+\)/is',
+            '/new\s?ReflectionFunction\(.*?->invoke\(/is',
+            '/PDO::FETCH_FUNC/',
+            '/\$\w+.*\s?(?:=|->)\s?.*?[\'\"]assert[\'\"]\)?/i',
+            '/\$\w+->(?:sqlite)?createFunction\(.*?\)/i',
+            '/eval\([\"\']?\\\?\$\w+\s?=\s?.*?\)/i',
+            '/eval\(.*?gzinflate\(base64_decode\(/i',
+            '/copy\(\$HTTP_POST_FILES\[\'\w+\'\]\s?\[\'tmp_name\'\]/i',
+            '/register_(?:shutdown|tick)_function\s?\(\$\w+,\s\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[.*?\]\)/is',
+            '/register_(?:shutdown|tick)_function\s?\(?[\'\"]assert[\"\'].*?\)/i',
+            '/call_user_func.*?\([\"|\']assert[\"|\'],.*\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\[[\'|\"].*\]\)+/is',
+            '/preg_replace\(.*?e.*?\'\s?,\s?.*?\w+\(.*?\)/i',
+            '/function_exists\s*\(\s*[\'|\"](popen|exec|proc_open|system|passthru)+[\'|\"]\s*\)/i',
+            '/(exec|shell_exec|system|passthru)+\s*\(\s*\$_(\w+)\[(.*)\]\s*\)/i',
+            '/(exec|shell_exec|system|passthru)+\s*\(\$\w+\)/i',
+            '/(exec|shell_exec|system|passthru)\s?\(\w+\(\"http_.*\"\)\)/i',
+            '/(?:john\.barker446@gmail\.com|xb5@hotmail\.com|shopen@aventgrup\.net|milw0rm\.com|www\.aventgrup\.net|mgeisler@mgeisler\.net)/i',
+            '/Php\s*?Shell/i',
+            '/((udp|tcp)\:\/\/(.*)\;)+/i',
+            '/preg_replace\s*\((.*)\/e(.*)\,\s*\$_(.*)\,(.*)\)/i',
+            '/preg_replace\s*\((.*)\(base64_decode\(\$/i',
+            '/(eval|assert|include|require|include_once|require_once)+\s*\(\s*(base64_decode|str_rot13|gz(\w+)|file_(\w+)_contents|(.*)php\:\/\/input)+/i',
+            '/(eval|assert|include|require|include_once|require_once|array_map|array_walk)+\s*\(.*?\$_(?:GET|POST|REQUEST|COOKIE|SERVER|SESSION)+\[(.*)\]\s*\)/i',
+            '/eval\s*\(\s*\(\s*\$\$(\w+)/i',
+            '/((?:include|require|include_once|require_once)+\s*\(?\s*[\'|\"]\w+\.(?!php).*[\'|\"])/i',
+            '/\$_(\w+)(.*)(eval|assert|include|require|include_once|require_once)+\s*\(\s*\$(\w+)\s*\)/i',
+            '/\(\s*\$_FILES\[(.*)\]\[(.*)\]\s*\,\s*\$_(GET|POST|REQUEST|FILES)+\[(.*)\]\[(.*)\]\s*\)/i',
+            '/(fopen|fwrite|fputs|file_put_contents)+\s*\((.*)\$_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\](.*)\)/i',
+            '/echo\s*curl_exec\s*\(\s*\$(\w+)\s*\)/i',
+            '/new com\s*\(\s*[\'|\"]shell(.*)[\'|\"]\s*\)/i',
+            '/\$(.*)\s*\((.*)\/e(.*)\,\s*\$_(.*)\,(.*)\)/i',
+            '/\$_\=(.*)\$_/i',
+            '/\$_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\]\(\s*\$(.*)\)/i',
+            '/\$(\w+)\s*\(\s*\$_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\]\s*\)/i',
+            '/\$(\w+)\s*\(\s*\$\{(.*)\}/i',
+            '/\$(\w+)\s*\(\s*chr\(\d+\)/i'
+        ];
+        foreach ($matches as $value) {
+            if (preg_match($value, $content)) {
+                return false;
+            }
+        }
+        return true;
+
+    }
+
 }