我們知道Web上提交數(shù)據(jù)有兩種方式,一種是get、一種是post,那么很多常見的sql注射就是從get方式入手的,而且注射的語句里面一定是包含一些sql語句的,因為沒有sql語句,那么如何進行,sql語句有四大句:select、update、delete、insert.
那么我們?nèi)绻谖覀兲峤坏臄?shù)據(jù)中進行過濾是不是能夠避免這些問題呢?于是我們使用正則就構(gòu)建如下函數(shù),代碼如下:
- /*
- 函數(shù)名稱:inject_check()
- 函數(shù)作用:檢測提交的值是不是含有SQL注射的字符,防止注射,保護服務(wù)器安全
- 參 數(shù):$sql_str: 提交的變量
- 返 回 值:返回檢測結(jié)果,ture or false
- 函數(shù)作者:heiyeluren
- */
- //開源代碼Vevb.com
- function inject_check($sql_str)
- {
- return eregi('select|insert|update|delete|'|/*|*|../|./|union|into|load_file|outfile', $sql_str); // 進行過濾
- }
我們函數(shù)里把 select,insert,update,delete,union,into,load_file,outfile /*, ./ , ../ , ' 等等危險的參數(shù)字符串全部過濾掉,那么就能夠控制提交的參數(shù)了,程序可以這么構(gòu)建,代碼如下:
- <?php
- if (inject_check($_GET['id']))
- {
- exit('你提交的數(shù)據(jù)非法,請檢查后重新提交!');
- }
- else
- {
- $id = $_GET['id'];
- echo '提交的數(shù)據(jù)合法,請繼續(xù)!';
- }
- ?>
假設(shè)我們提交URL為:a.php?id=1,那么就會提示:
"提交的數(shù)據(jù)合法,請繼續(xù)!",如果我們提交 a.php?id=1%27 select * from tb_name,就會出現(xiàn)提示:"你提交的數(shù)據(jù)非法,請檢查后重新提交!",那么就達到了我們的要求.
但是,問題還沒有解決,假如我們提交的是 a.php?id=1asdfasdfasdf 呢,我們這個是符合上面的規(guī)則的,但是呢,它是不符合要求的,于是我們?yōu)榱丝赡芷渌那闆r,我們再構(gòu)建一個函數(shù)來進行檢查,代碼如下:
- /*
- 函數(shù)名稱:verify_id()
- 函數(shù)作用:校驗提交的ID類值是否合法
- 參 數(shù):$id: 提交的ID值
- 返 回 值:返回處理后的ID
- 函數(shù)作者:heiyeluren
- */
- function verify_id($id=null)
- {
- if (!$id) { exit('沒有提交參數(shù)!'); } // 是否為空判斷
- elseif (inject_check($id)) { exit('提交的參數(shù)非法!'); } // 注射判斷
- elseif (!is_numeric($id)) { exit('提交的參數(shù)非法!'); } // 數(shù)字判斷
- $id = intval($id); // 整型化
- return $id;
- }
呵呵,那么我們就能夠進行校驗了,于是我們上面的程序代碼就變成了下面的:
- <?php
- if (inject_check($_GET['id']))
- {
- exit('你提交的數(shù)據(jù)非法,請檢查后重新提交!');
- }
- else
- {
- $id = verify_id($_GET['id']); // 這里引用了我們的過濾函數(shù),對$id進行過濾
- echo '提交的數(shù)據(jù)合法,請繼續(xù)!';
- }
- ?>
好,問題到這里似乎都解決了,但是我們有沒有考慮過post提交的數(shù)據(jù),大批量的數(shù)據(jù)呢?
比如一些字符可能會對數(shù)據(jù)庫造成危害,比如 ' _ ', ' % ',這些字符都有特殊意義,那么我們?nèi)绻M行控制呢?還有一點,就是當我們的php.ini里面的magic_quotes_gpc = off 的時候,那么提交的不符合數(shù)據(jù)庫規(guī)則的數(shù)據(jù)都是不會自動在前面加' '的,那么我們要控制這些問題,于是構(gòu)建如下函數(shù),代碼如下:
- /*
- 函數(shù)名稱:str_check()
- 函數(shù)作用:對提交的字符串進行過濾
- 參 數(shù):$var: 要處理的字符串
- 返 回 值:返回過濾后的字符串
- 函數(shù)作者:heiyeluren
- */
- function str_check( $str )
- {
- if (!get_magic_quotes_gpc()) // 判斷magic_quotes_gpc是否打開
- {
- $str = addslashes($str); // 進行過濾
- }
- $str = str_replace("_", "_", $str); // 把 '_'過濾掉
- $str = str_replace("%", "%", $str); // 把' % '過濾掉
- return $str;
- }
OK,我們又一次的避免了服務(wù)器被淪陷的危險.
最后,再考慮提交一些大批量數(shù)據(jù)的情況,比如發(fā)貼,或者寫文章、新聞,我們需要一些函數(shù)來幫我們過濾和進行轉(zhuǎn)換,再上面函數(shù)的基礎(chǔ)上,我們構(gòu)建如下函數(shù),代碼如下:
- /*
- 函數(shù)名稱:post_check()
- 函數(shù)作用:對提交的編輯內(nèi)容進行處理
- 參 數(shù):$post: 要提交的內(nèi)容
- 返 回 值:$post: 返回過濾后的內(nèi)容
- 函數(shù)作者:heiyeluren
- */
- //開源代碼Vevb.com
- function post_check($post)
- {
- if (!get_magic_quotes_gpc()) // 判斷magic_quotes_gpc是否為打開
- {
- $post = addslashes($post); // 進行magic_quotes_gpc沒有打開的情況對提交數(shù)據(jù)的過濾
- }
- $post = str_replace("_", "_", $post); // 把 '_'過濾掉
- $post = str_replace("%", "%", $post); // 把' % '過濾掉
- $post = nl2br($post); // 回車轉(zhuǎn)換
- $post= htmlspecialchars($post); // html標記轉(zhuǎn)換
- return $post;
- }
新聞熱點
疑難解答