今天就跟大家聊聊有關如何在php中做sql過濾,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
SQL注入攻擊指的是通過構建特殊的輸入作為參數傳入Web應用程序,而這些輸入大都是SQL語法里的一些組合,通過執行SQL語句進而執行攻擊者所要的操作,其主要原因是程序沒有細致地過濾用戶輸入的數據,致使非法數據侵入系統。因此,在執行sql語句前,一定要對用戶輸入的數據進行過濾處理。
防止sql注入的函數,過濾掉那些非法的字符,提高sql安全性,同時也可以過濾XSS的攻擊。
function filter($str) { if (empty($str)) return false; $str = htmlspecialchars($str); $str = str_replace( '/', "", $str); $str = str_replace( '"', "", $str); $str = str_replace( '(', "", $str); $str = str_replace( ')', "", $str); $str = str_replace( 'CR', "", $str); $str = str_replace( 'ASCII', "", $str); $str = str_replace( 'ASCII 0x0d', "", $str); $str = str_replace( 'LF', "", $str); $str = str_replace( 'ASCII 0x0a', "", $str); $str = str_replace( ',', "", $str); $str = str_replace( '%', "", $str); $str = str_replace( ';', "", $str); $str = str_replace( 'eval', "", $str); $str = str_replace( 'open', "", $str); $str = str_replace( 'sysopen', "", $str); $str = str_replace( 'system', "", $str); $str = str_replace( '$', "", $str); $str = str_replace( "'", "", $str); $str = str_replace( "'", "", $str); $str = str_replace( 'ASCII 0x08', "", $str); $str = str_replace( '"', "", $str); $str = str_replace( '"', "", $str); $str = str_replace("", "", $str); $str = str_replace(">", "", $str); $str = str_replace("<", "", $str); $str = str_replace("<SCRIPT>", "", $str); $str = str_replace("</SCRIPT>", "", $str); $str = str_replace("<script>", "", $str); $str = str_replace("</script>", "", $str); $str = str_replace("select","",$str); $str = str_replace("join","",$str); $str = str_replace("union","",$str); $str = str_replace("where","",$str); $str = str_replace("insert","",$str); $str = str_replace("delete","",$str); $str = str_replace("update","",$str); $str = str_replace("like","",$str); $str = str_replace("drop","",$str); $str = str_replace("DROP","",$str); $str = str_replace("create","",$str); $str = str_replace("modify","",$str); $str = str_replace("rename","",$str); $str = str_replace("alter","",$str); $str = str_replace("cas","",$str); $str = str_replace("&","",$str); $str = str_replace(">","",$str); $str = str_replace("<","",$str); $str = str_replace(" ",chr(32),$str); $str = str_replace(" ",chr(9),$str); $str = str_replace(" ",chr(9),$str); $str = str_replace("&",chr(34),$str); $str = str_replace("'",chr(39),$str); $str = str_replace("<br />",chr(13),$str); $str = str_replace("''","'",$str); $str = str_replace("css","'",$str); $str = str_replace("CSS","'",$str); $str = str_replace("<!--","",$str); $str = str_replace("convert","",$str); $str = str_replace("md5","",$str); $str = str_replace("passwd","",$str); $str = str_replace("password","",$str); $str = str_replace("../","",$str); $str = str_replace("./","",$str); $str = str_replace("Array","",$str); $str = str_replace("or 1='1'","",$str); $str = str_replace(";set|set&set;","",$str); $str = str_replace("`set|set&set`","",$str); $str = str_replace("--","",$str); $str = str_replace("OR","",$str); $str = str_replace('"',"",$str); $str = str_replace("*","",$str); $str = str_replace("-","",$str); $str = str_replace("+","",$str); $str = str_replace("/","",$str); $str = str_replace("=","",$str); $str = str_replace("'/","",$str); $str = str_replace("-- ","",$str); $str = str_replace(" -- ","",$str); $str = str_replace(" --","",$str); $str = str_replace("(","",$str); $str = str_replace(")","",$str); $str = str_replace("{","",$str); $str = str_replace("}","",$str); $str = str_replace("-1","",$str); $str = str_replace("1","",$str); $str = str_replace(".","",$str); $str = str_replace("response","",$str); $str = str_replace("write","",$str); $str = str_replace("|","",$str); $str = str_replace("`","",$str); $str = str_replace(";","",$str); $str = str_replace("etc","",$str); $str = str_replace("root","",$str); $str = str_replace("//","",$str); $str = str_replace("!=","",$str); $str = str_replace("$","",$str); $str = str_replace("&","",$str); $str = str_replace("&&","",$str); $str = str_replace("==","",$str); $str = str_replace("#","",$str); $str = str_replace("@","",$str); $str = str_replace("mailto:","",$str); $str = str_replace("CHAR","",$str); $str = str_replace("char","",$str); return $str; }
更加簡便的防止sql注入的方法(推薦使用這個):
if (!get_magic_quotes_gpc()) // 判斷magic_quotes_gpc是否為打開 { $post = addslashes($name); // magic_quotes_gpc沒有打開的時候把數據過濾 } $name = str_replace("_", "\_", $name); // 把 '_'過濾掉 $name = str_replace("%", "\%", $name); // 把' % '過濾掉 $name = nl2br($name); // 回車轉換 $name= htmlspecialchars($name); // html標記轉換 return $name;
PHP防XSS 防SQL注入的代碼
/** * 過濾參數 * @param string $str 接受的參數 * @return string */ static public function filterWords($str) { $farr = array( "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU", "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU", "/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|dump/is" ); $str = preg_replace($farr,'',$str); return $str; } /** * 過濾接受的參數或者數組,如$_GET,$_POST * @param array|string $arr 接受的參數或者數組 * @return array|string */ static public function filterArr($arr) { if(is_array($arr)){ foreach($arr as $k => $v){ $arr[$k] = self::filterWords($v); } }else{ $arr = self::filterWords($v); } return $arr; }
在防止被注入攻擊時,常會用到函數:htmlspecialchars()和addslashes() 、trim()函數。這兩個函數都是對特殊字符進行轉義。
1)addslashes()作用及使用
addslashes()通常用于防止sql注入,它可對通過get,post和cookie傳遞過來的參數的單引號和雙引號已經null前加“\”進行轉義
如:如變量$str=$_POST["str"];的值為:bb' or 1='1。通過addslashes()函數過濾后會變為:bb\' or 1=\'1;
2)htmlspecialchars()作用及使用
htmlspecialchars()也是對字符進行轉義,與addslashes()不同的是htmlspecialchars()是將特殊字符用引用實體替換。
如<script>alert('xss')</script>通過htmlspecialchars()過濾后為<script>alert('xss')</script>
3)addslashes()與htmlspecialchars()的區別
除了兩個函數的轉義方式不同外,它們的使用也不同。
addslashes()通過用于防止sql語句注入,在執行sql語句前對通過get、post和cookie傳遞來的參數中的單引號,雙引號,\ 和null進行轉義。
但sql執行成功后,插入到數據庫中的數據是不帶有轉義字符\的。這是如果插入到數據庫中的是一些js腳本,當這些腳本被讀取出來時還是會被執行。
這時我們可對讀取出來的數據使用htmlspecialchars()進行過濾,避免執行被注入的腳本。
看完上述內容,你們對如何在php中做sql過濾有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。