secure.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. if (!empty($_POST)) $_POST = wjSec($_POST);
  3. if (!empty($_GET)) $_GET = wjSec($_GET);
  4. if (!empty($_COOKIE)) $_COOKIE = wjSec($_COOKIE);
  5. if (!empty($_SESSION)) $_SESSION = wjSec($_SESSION);
  6. if (!empty($_FILES)) $_FILES = wjSec($_FILES);
  7. function wjSec(&$array) {
  8. if (is_array($array)) {
  9. foreach ($array as $k => $v) {
  10. if (($k == 'addr' && strlen($v) == 34) || $k == 'content') {
  11. $array [$k] = $v;
  12. } else {
  13. $array [$k] = wjSec($v);
  14. }
  15. }
  16. } else if (is_numeric($array)) {
  17. $array = wjStrFilter($array, 0, 0);
  18. } else {
  19. $array = wjStrFilter($array);
  20. }
  21. return $array;
  22. }
  23. function wjStrFilter($str, $pi_Def = "", $pi_iType = 1) {
  24. if (isset($_GET[$str]))
  25. $str = trim($_GET[$str]);
  26. else if (isset($_POST[$str]))
  27. $str = trim($_POST[$str]);
  28. else if ($str)
  29. $str = trim($str);
  30. else
  31. return $pi_Def;
  32. // INT
  33. if ($pi_iType == 0) {
  34. if (is_numeric($str))
  35. return $str;
  36. else
  37. return $pi_Def;
  38. }
  39. // echo $str;
  40. // String
  41. if ($str) {
  42. $str = str_replace('%20', '', $str);
  43. $str = str_replace('%27', '', $str);
  44. $str = str_replace('%2527', '', $str);
  45. $str = str_replace('*', '', $str);
  46. $str = str_replace('"', '&quot;', $str);
  47. $str = str_replace("'", '', $str);
  48. $str = str_replace('"', '', $str);
  49. $str = str_replace('<', '&lt;', $str);
  50. $str = str_replace('>', '&gt;', $str);
  51. $str = str_replace("{", '', $str);
  52. $str = str_replace('}', '', $str);
  53. $str = str_replace('#', '', $str);
  54. $str = str_replace('--', '', $str);
  55. $str = str_replace('%', '', $str);
  56. $str = preg_replace("/insert/i", "", $str);
  57. $str = preg_replace("/update/i", "", $str);
  58. $str = preg_replace("/delete/i", "", $str);
  59. $str = preg_replace("/select/i", "", $str);
  60. $str = preg_replace("/drop/i", "", $str);
  61. $str = preg_replace("/load_file/i", "", $str);
  62. $str = preg_replace("/outfile/i", "", $str);
  63. $str = preg_replace("/into/i", "", $str);
  64. $str = preg_replace("/exec/i", "", $str);
  65. $str = preg_replace("/tw_/i", "", $str);
  66. $str = preg_replace("/union/i", "", $str);
  67. $str = preg_replace("/%/i", "", $str);
  68. if (get_magic_quotes_gpc()) {
  69. $str = str_replace("\\\"", "&quot;", $str);
  70. $str = str_replace("\\''", "&#039;", $str);
  71. } else {
  72. $str = addslashes($str);
  73. $str = str_replace("\"", "&quot;", $str);
  74. $str = str_replace("'", "&#039;", $str);
  75. }
  76. $str = mysql_escape_string($str);
  77. $str = RemoveXSS($str);
  78. $str = RemoveWJ($str);
  79. }
  80. return $str;
  81. }
  82. function RemoveXSS($val) {
  83. // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
  84. // this prevents some character re-spacing such as <java\0script>
  85. // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
  86. //$val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
  87. // straight replacements, the user should never need these since they're normal characters
  88. // this prevents like <IMG SRC=@avascript:alert('XSS')>
  89. $search = 'abcdefghijklmnopqrstuvwxyz';
  90. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  91. $search .= '1234567890!@#$%^&*()';
  92. $search .= '~`";:?+/={}[]-|\'\\';
  93. for ($i = 0; $i < strlen($search); $i++) {
  94. // ;? matches the ;, which is optional
  95. // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
  96. // @ @ search for the hex values
  97. $val = preg_replace('/(&#[xX]0{0,8}' . dechex(ord($search[$i])) . ';?)/i', $search[$i], $val); // with a ;
  98. // @ @ 0{0,7} matches '0' zero to seven times
  99. $val = preg_replace('/(�{0,8}' . ord($search[$i]) . ';?)/', $search[$i], $val); // with a ;
  100. }
  101. // now the only remaining whitespace attacks are \t, \n, and \r
  102. $ra1 = ['javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base'];
  103. $ra2 = ['onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload'];
  104. $ra = array_merge($ra1, $ra2);
  105. $found = true; // keep replacing as long as the previous round replaced something
  106. while ($found == true) {
  107. $val_before = $val;
  108. for ($i = 0; $i < sizeof($ra); $i++) {
  109. $pattern = '/';
  110. for ($j = 0; $j < strlen($ra[$i]); $j++) {
  111. if ($j > 0) {
  112. $pattern .= '(';
  113. $pattern .= '(&#[xX]0{0,8}([9ab]);)';
  114. $pattern .= '|';
  115. $pattern .= '|(�{0,8}([9|10|13]);)';
  116. $pattern .= ')*';
  117. }
  118. $pattern .= $ra[$i][$j];
  119. }
  120. $pattern .= '/i';
  121. $replacement = substr($ra[$i], 0, 2) . '<x>' . substr($ra[$i], 2); // add in <> to nerf the tag
  122. $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
  123. if ($val_before == $val) {
  124. // no replacements were made, so exit the loop
  125. $found = false;
  126. }
  127. }
  128. }
  129. return $val;
  130. }
  131. function RemoveWJ($val) {
  132. $val = fliter_script($val);
  133. $val = fliter_html($val);
  134. $val = fliter_sql($val);
  135. $val = fliter_str($val);
  136. $val = filter_dir($val);
  137. $val = filter_path($val);
  138. $val = filter_phptag($val);
  139. $val = str_out($val);
  140. return $val;
  141. }
  142. /**
  143. * 安全过滤类-过滤javascript,css,iframes,object等不安全参数 过滤级别高
  144. * Controller中使用方法:$this->controller->fliter_script($value)
  145. * @param string $value 需要过滤的值
  146. * @return string
  147. */
  148. function fliter_script($value) {
  149. $value = preg_replace("/(javascript:)?on(click|load|key|mouse|error|abort|move|unload|change|dblclick|move|reset|resize|submit)/i", "&111n\\2", $value);
  150. $value = preg_replace("/(.*?)<\/script>/si", "", $value);
  151. $value = preg_replace("/(.*?)<\/iframe>/si", "", $value);
  152. $value = preg_replace("//iesU", '', $value);
  153. return $value;
  154. }
  155. /**
  156. * 安全过滤类-过滤HTML标签
  157. * Controller中使用方法:$this->controller->fliter_html($value)
  158. * @param string $value 需要过滤的值
  159. * @return string
  160. */
  161. function fliter_html($value) {
  162. // return $value;
  163. if (function_exists('htmlspecialchars')) {
  164. return htmlspecialchars($value);
  165. // $b = htmlspecialchars_decode($a);
  166. // return $a;
  167. } else {
  168. return str_replace(["&", '"', "'", "<", ">"], ["&", "\"", "'", "<", ">"], $value);
  169. }
  170. }
  171. /**
  172. * 安全过滤类-对进入的数据加下划线 防止SQL注入
  173. * Controller中使用方法:$this->controller->fliter_sql($value)
  174. * @param string $value 需要过滤的值
  175. * @return string
  176. */
  177. function fliter_sql($value) {
  178. $sql = ["select", 'insert', "update", "delete", "\'", "\/\*",
  179. "\.\.\/", "\.\/", "union", "into", "load_file", "outfile"];
  180. $sql_re = ["", "", "", "", "", "", "", "", "", "", "", ""];
  181. return str_replace($sql, $sql_re, $value);
  182. }
  183. /**
  184. * 安全过滤类-字符串过滤 过滤特殊有危害字符
  185. * Controller中使用方法:$this->controller->fliter_str($value)
  186. * @param string $value 需要过滤的值
  187. * @return string
  188. */
  189. function fliter_str($value) {
  190. $badstr = ["\0", "%00", "\r", '&', ' ', '"', "'", "<", ">", " ", "%3C", "%3E"];
  191. $newstr = ['', '', '', '&', ' ', '"', "'", "<", ">", " ", "<", ">"];
  192. $value = str_replace($badstr, $newstr, $value);
  193. $value = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $value);
  194. return $value;
  195. }
  196. /**
  197. * 私有路劲安全转化
  198. * Controller中使用方法:$this->controller->filter_dir($fileName)
  199. * @param string $fileName
  200. * @return string
  201. */
  202. function filter_dir($fileName) {
  203. $tmpname = strtolower($fileName);
  204. $temp = [':/', "\0", ".."];
  205. if (str_replace($temp, '', $tmpname) !== $tmpname) {
  206. return false;
  207. }
  208. return $fileName;
  209. }
  210. /**
  211. * 过滤目录
  212. * Controller中使用方法:$this->controller->filter_path($path)
  213. * @param string $path
  214. * @return array
  215. */
  216. function filter_path($path) {
  217. $path = str_replace(["'", '#', '=', '`', '$', '%', '&'], '', $path);
  218. return rtrim(preg_replace('/(\/){2,}|(\\\){1,}/', '/', $path), '/');
  219. }
  220. /**
  221. * 过滤PHP标签
  222. * Controller中使用方法:$this->controller->filter_phptag($string)
  223. * @param string $string
  224. * @return string
  225. */
  226. function filter_phptag($string) {
  227. return str_replace([''], ['<?', '?>'], $string);
  228. }
  229. /**
  230. * 安全过滤类-返回函数
  231. * Controller中使用方法:$this->controller->str_out($value)
  232. * @param string $value 需要过滤的值
  233. * @return string
  234. */
  235. function str_out($value) {
  236. $badstr = ["<", ">", "%3C", "%3E"];
  237. $newstr = ["<", ">", "<", ">"];
  238. $value = str_replace($newstr, $badstr, $value);
  239. return stripslashes($value); //下划线
  240. }
  241. ?>