php關於輸入過濾小結

摘要: web的安全性一部分取決於輸入過濾

    Web的攻擊,大部分是來自於外部,如Url上添加一些字段注入($_GET輸入),表單的提交注入(一般爲$_POST),所以在接收數據時對數據進行過濾,是很有必要的。

  一.  一般php自帶的過濾方法有:

    1.空過濾

     trim過濾字符串首尾空格

$name = trim($_POST['name']);

    2.標籤過濾 : 

     strip_tags會將字符串中的php標籤(<?php ?>)Html標籤(<h1></h1><script></script>....等)移除。一定程序上阻止了惡意注入。

//for example:
$_POST['name'] = "<script>alert('hehe');</script>";
var_dump($_POST['name']);//彈出信息框 'hehe'
$name = strip_tags($_POST['name']);
var_dump($name);  //string(14"alert('hehe');"

  3.轉數據類型

   若知道要接收的數據是整形或浮點形,可以直接轉數據類型。

//轉整形 
$number = intval($_POST['number']);
$price  = floatval($_POST['price']);

   4.移除xss攻擊(跨站腳本攻擊)

    xss攻擊有時會把標籤轉換成其他數據,strip_tags防止不了,

    ThinkPHP中有個remove_xss方法,可以將大部分xss攻擊阻止。這方法在 ./ThinkPHP/Extend/Function/extend.php中,爲了方便使用,可以放到項目的common.php裏。

    方法如下

    

/**
 * @from extend.php
 * 過濾xss攻擊
 * @param str $val
 * @return mixed
 */
function remove_xss($val) {
	// remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
	// this prevents some character re-spacing such as <java\0script>
	// note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
	$val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/''', $val);

	// straight replacements, the user should never need these since they're normal characters
	// this prevents like <IMG SRC=@avascript:alert('XSS')>
	$search = 'abcdefghijklmnopqrstuvwxyz';
	$search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	$search .= '1234567890!@#$%^&*()';
	$search .= '~`";:?+/={}[]-_|\'\\';
	for ($i = 0; $i < strlen($search); $i++) {
		// ;? matches the ;, which is optional
		// 0{0,7} matches any padded zeros, which are optional and go up to 8 chars

		// @ @ search for the hex values
		$val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
		// @ @ 0{0,7} matches '0' zero to seven times
		$val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
	}

	// now the only remaining whitespace attacks are \t, \n, and \r
	$ra1 = array('javascript''vbscript''expression''applet''meta''xml''blink''link''style''script',
	              'embed''object''iframe''frame''frameset''ilayer''layer''bgsound''title''base');
	$ra2 = array('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');
	$ra = array_merge($ra1, $ra2);

	$found = true// keep replacing as long as the previous round replaced something
	while ($found == true) {
		$val_before = $val;
		for ($i = 0; $i < sizeof($ra); $i++) {
			$pattern = '/';
			for ($j = 0; $j < strlen($ra[$i]); $j++) {
				if ($j > 0) {
					$pattern .= '(';
					$pattern .= '(&#[xX]0{0,8}([9ab]);)';
					$pattern .= '|';
					$pattern .= '|(&#0{0,8}([9|10|13]);)';
					$pattern .= ')*';
				}
				$pattern .= $ra[$i][$j];
			}
			$pattern .= '/i';
			$replacement = substr($ra[$i], 02).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
			$val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
			if ($val_before == $val) {
				// no replacements were made, so exit the loop
				$found = false;
			}
		}
	}
	return $val;
}

   調用時如下

$name = remove_xss($_POST['name']);

 5.保存文章內容類轉義

       使用kindeditor之類的內容編輯器時,因爲提交到後臺時是以Html形式提交的,而且需要保存到數據庫,爲了防止sql注入,需要在進數據庫前進行特殊字符轉義,這時用過濾標籤的方法或各類的方法都不適合。只能對標籤和特殊符號進行轉義,這時使用到的方法是addslashes。

        addslashes在使用前先檢查一下,php是否自動開啓了自動轉義。用get_magic_quotes_gpc()方法判斷,如果已開,則是true,否爲false。

    if(!get_magic_quotes_gpc()){
     $content = addslashes($_POST['content']);
    }else{
      $content$_POST['content'];
    }

         這樣就完成了轉義,然而在展示頁面,從數據庫拿出來的內容是經過轉義的html,如果直接展示,html標籤等都識別不到,會直接輸出轉義過的字符串。這時需要用反轉義來還原數據。如下

echo stripslashes($content);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章