1 <?php 2 /** 3 * 安全模块 4 * Email:[email protected] 5 * 主要针对xss跨站攻击、sql注入等敏感字符串进行过滤 6 * @author hkshadow 7 */ 8 class safeMode{ 9 10 /** 11 * 执行过滤 12 * @param 1 linux/2 http/3 Db/ $group 13 * @param 保存路径以及文件名/文件名/null $projectName 14 */ 15 public function xss($group = 1,$projectName = NULL){ 16 //正则条件 17 $referer = empty ( $_SERVER [‘HTTP_REFERER‘] ) ? array () : array ($_SERVER [‘HTTP_REFERER‘] ); 18 $getfilter = "‘|<[^>]*?>|^\\+\/v(8|9)|\\b(and|or)\\b.+?(>|<|=|\\bin\\b|\\blike\\b)|\\/\\*.+?\\*\\/|<\\s*script\\b|\\bEXEC\\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\\s+(TABLE|DATABASE)"; 19 $postfilter = "^\\+\/v(8|9)|\\b(and|or)\\b.{1,6}?(=|>|<|\\bin\\b|\\blike\\b)|\\/\\*.+?\\*\\/|<\\s*script\\b|<\\s*img\\b|\\bEXEC\\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\\s+(TABLE|DATABASE)"; 20 $cookiefilter = "\\b(and|or)\\b.{1,6}?(=|>|<|\\bin\\b|\\blike\\b)|\\/\\*.+?\\*\\/|<\\s*script\\b|\\bEXEC\\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\\s+(TABLE|DATABASE)"; 21 22 // $ArrPGC=array_merge($_GET,$_POST,$_COOKIE); 23 24 //遍历过滤 25 foreach ( $_GET as $key => $value ) { 26 $this->stopAttack ( $key, $value, $getfilter ,$group , $projectName); 27 } 28 //遍历过滤 29 foreach ( $_POST as $key => $value ) { 30 $this->stopAttack ( $key, $value, $postfilter ,$group , $projectName); 31 } 32 //遍历过滤 33 foreach ( $_COOKIE as $key => $value ) { 34 $this->stopAttack ( $key, $value, $cookiefilter ,$group , $projectName); 35 } 36 //遍历过滤 37 foreach ( $referer as $key => $value ) { 38 $this->stopAttack ( $key, $value, $getfilter ,$group , $projectName); 39 } 40 } 41 42 /** 43 * 匹配敏感字符串,并处理 44 * @param 参数key $strFiltKey 45 * @param 参数value $strFiltValue 46 * @param 正则条件 $arrFiltReq 47 * @param 项目名 $joinName 48 * @param 1 linux/2 http/3 Db/ $group 49 * @param 项目名/文件名/null $projectName 50 */ 51 public function stopAttack($strFiltKey, $strFiltValue, $arrFiltReq,$group = 1,$projectName = NULL) { 52 53 $strFiltValue = $this->arr_foreach ( $strFiltValue ); 54 //匹配参数值是否合法 55 if (preg_match ( "/" . $arrFiltReq . "/is", $strFiltValue ) == 1) { 56 //记录ip 57 $ip = "操作IP: ".$_SERVER["REMOTE_ADDR"]; 58 //记录操作时间 59 $time = " 操作时间: ".strftime("%Y-%m-%d %H:%M:%S"); 60 //记录详细页面带参数 61 $thePage = " 操作页面: ".$this->request_uri(); 62 //记录提交方式 63 $type = " 提交方式: ".$_SERVER["REQUEST_METHOD"]; 64 //记录提交参数 65 $key = " 提交参数: ".$strFiltKey; 66 //记录参数 67 $value = " 提交数据: ".htmlspecialchars($strFiltValue); 68 //写入日志 69 $strWord = $ip.$time.$thePage.$type.$key.$value; 70 //保存为linux类型 71 if($group == 1){ 72 $this->log_result_common($strWord,$projectName); 73 } 74 //保存为可web浏览 75 if($group == 2){ 76 $strWord .= "<br>"; 77 $this->slog($strWord,$projectName); 78 } 79 //保存至数据库 80 if($group == 3){ 81 $this->sDb($strWord); 82 } 83 //过滤参数 84 $_REQUEST[$strFiltKey] = ‘‘; 85 //这里不作退出处理 86 //exit; 87 } 88 89 //匹配参数是否合法 90 if (preg_match ( "/" . $arrFiltReq . "/is", $strFiltKey ) == 1) { 91 //记录ip 92 $ip = "操作IP: ".$_SERVER["REMOTE_ADDR"]; 93 //记录操作时间 94 $time = " 操作时间: ".strftime("%Y-%m-%d %H:%M:%S"); 95 //记录详细页面带参数 96 $thePage = " 操作页面: ".$this->request_uri(); 97 //记录提交方式 98 $type = " 提交方式: ".$_SERVER["REQUEST_METHOD"]; 99 //记录提交参数 100 $key = " 提交参数: ".$strFiltKey; 101 //记录参数 102 $value = " 提交数据: ".htmlspecialchars($strFiltValue); 103 //写入日志 104 $strWord = $ip.$time.$thePage.$type.$key.$value; 105 //保存为linux类型 106 if($group == 1){ 107 $this->log_result_common($strWord,$projectName); 108 } 109 //保存为可web浏览 110 if($group == 2){ 111 $strWord .= "<br>"; 112 $this->slog($strWord,$projectName); 113 } 114 //保存至数据库 115 if($group == 3){ 116 $this->sDb($strWord); 117 } 118 //过滤参数 119 $_REQUEST[$strFiltKey] = ‘‘; 120 //这里不作退出处理 121 //exit; 122 } 123 } 124 125 /** 126 * 获取当前url带具体参数 127 * @return string 128 */ 129 public function request_uri() { 130 if (isset ( $_SERVER [‘REQUEST_URI‘] )) { 131 $uri = $_SERVER [‘REQUEST_URI‘]; 132 } else { 133 if (isset ( $_SERVER [‘argv‘] )) { 134 $uri = $_SERVER [‘PHP_SELF‘] . ‘?‘ . $_SERVER [‘argv‘] [0]; 135 } else { 136 $uri = $_SERVER [‘PHP_SELF‘] . ‘?‘ . $_SERVER [‘QUERY_STRING‘]; 137 } 138 } 139 return $uri; 140 } 141 142 143 /** 144 * 日志记录(linux模式) 145 * @param 保存内容 $strWord 146 * @param 保存文件名$strPathName 147 */ 148 public function log_result_common($strWord, $strPathName = NULL) { 149 if($strPathName == NULL){ 150 $strPath = "/var/tmp/"; 151 $strDay = date(‘Y-m-d‘); 152 $strPathName = $strPath."common_log_".$strDay.‘.log‘; 153 } 154 155 $fp = fopen($strPathName,"a"); 156 flock($fp, LOCK_EX) ; 157 fwrite($fp,$strWord." date ".date(‘Y-m-d H:i:s‘,time())."\t\n"); 158 flock($fp, LOCK_UN); 159 fclose($fp); 160 } 161 162 /** 163 * 写入日志(支持http查看) 164 * @param 日志内容 $strWord 165 * @param web页面文件名 $fileName 166 */ 167 public function slog($strWord,$fileName = NULL) { 168 if($fileName == NULL){ 169 $toppath = $_SERVER ["DOCUMENT_ROOT"] . "/log.htm"; 170 }else{ 171 $toppath = $_SERVER ["DOCUMENT_ROOT"] .‘/‘. $fileName; 172 } 173 $Ts = fopen ( $toppath, "a+" ); 174 fputs ( $Ts, $strWord . "\r\n" ); 175 fclose ( $Ts ); 176 } 177 178 /** 179 * 写入日志(数据库) 180 * @param 日志内容 $strWord 181 */ 182 public function sDb($strWord){ 183 //.... 184 } 185 186 /** 187 * 递归数组 188 * @param array $arr 189 * @return unknown|string 190 */ 191 public function arr_foreach($arr) { 192 static $str = ‘‘; 193 if (! is_array ( $arr )) { 194 return $arr; 195 } 196 foreach ( $arr as $key => $val ) { 197 if (is_array ( $val )) { 198 $this->arr_foreach ( $val ); 199 } else { 200 $str [] = $val; 201 } 202 } 203 return implode ( $str ); 204 } 205 } 206 ?>
时间: 2024-10-08 13:46:49