php通用防注入主要是過濾一些sql命令與php post get傳過來的參考我們/要過濾一些非法字符,這樣可以防止基本的注入了,那關第于apache 服務器安裝設置方法也是必須的,管理員用戶名和密碼都采取md5加密,這樣就能有效地防止了php的注入.
還有服務器和mysql教程也要加強一些安全防范.
對于linux服務器的安全設置:
加密口令,使用“/usr/sbin/authconfig”工具打開密碼的shadow功能,對password進行加密,禁止訪問重要文件,進入linux命令界面,在提示符下輸入:
- #chmod 600 /etc/inetd.conf //改變文件屬性為600
- #chattr +i /etc/inetd.conf //保證文件屬主為root
- #chattr –i /etc/inetd.conf // 對該文件的改變做限制
禁止任何用戶通過su命令改變為root用戶,在su配置文件即/etc/pam.d/目錄下的開頭添加下面兩行:
auth sufficient /lib/security/pam_rootok.so debug
auth required /lib/security/pam_whell.so group=wheel
刪除所有的特殊帳戶
#userdel lp等等 刪除用戶
#groupdel lp等等 刪除組
禁止不使用的suid/sgid程序
#find / -type f (-perm -04000 - o –perm -02000 ) -execls –lg {};代碼如下:
- $arrfiltrate=array("'",";","union","select","insert","update","delete","load_file","outfile");
- //出錯后要跳轉的url
- $strgourl="";
- //開源代碼Vevb.com
- function funstringexist($strfiltrate,$arrfiltrate)
- {
- foreach ($arrfiltrate as $key=>$value)
- {
- if (eregi($value,$strfiltrate))
- {
- return true;
- }
- }
- return false;
- }
- //合并$_post 、 $_get和$_cookie
- if(function_exists(array_merge))
- {
- $arrpostgetcookiesession=array_merge($http_post_vars,$http_get_vars,$http_cookie_vars);
- $string = implode("",$arrpostgetcookiesession);
- }
- //驗證
- if(funstringexist($string,$arrfiltrate))
- {
- echo "<script language="javascript">alert("提示,非法字符");</script>";
- }
- else
- {
- echo "<script language="javascript">window.location="".$strgourl."";</script>";
- }
php通用防注入安全代碼.
說明:判斷傳遞的變量中是否含有非法字符,如$_post、$_get
功能:防注入.
- //要過濾的非法字符
- $arrfiltrate=array("'",";","union");
- //出錯后要跳轉的url,不填則默認前一頁
- $strgourl="";
- //是否存在數組中的值
- function funstringexist($strfiltrate,$arrfiltrate){
- foreach ($arrfiltrate as $key=>$value){
- if (eregi($value,$strfiltrate)){
- return true;
- }
- }
- return false;
- }
- //合并$_post 和 $_get
- if(function_exists(array_merge)){
- $arrpostandget=array_merge($http_post_vars,$http_get_vars);
- }else{
- foreach($http_post_vars as $key=>$value){
- $arrpostandget[]=$value;
- }
- foreach($http_get_vars as $key=>$value){
- $arrpostandget[]=$value;
- }
- }
- //驗證開始
- foreach($arrpostandget as $key=>$value){
- if (funstringexist($value,$arrfiltrate)){
- echo "alert(/"neeao提示,非法字符/");";
- if (emptyempty($strgourl)){
- echo "history.go(-1);";
- }else{
- echo "window.location=/"".$strgourl."/";";
- }
- exit;
- }
- }
看一下關于注入細節.
轉化成ascii后是char(97,108,112,104,97),轉化成16進制是0x616c706861,我們將在光盤中提供16進制和ascii轉換工具,好了直接在瀏覽器里輸入:
http://localhost/site/admin/login.php?username=char(97,108,112,104,97)%23
sql語句變成:
select * from alphaaut hor where username=char(97,108,112,104,97)# and password=
正如我們期望的那樣,他順利執行了,我們得到我們想要的,當然咯,我們也可以這樣構造:
http://m.survivalescaperooms.com/site/admin/login.php?username=0x616c706861%23
sql語句變成:
select * from alphaauthor where username=0x616c706861%23# and password=
我們再一次是成功者了,很有成就感吧,或許你會問我們是否可以把#也放在char()里,實際上char(97,108,112,104,97)相當于 alpha,注意是alpha上加引號,表示alpha字符串,我們知道在mysql中如果執行如下代碼:
mysql> select * from dl_users where username=alpha;error 1054 (42s22): unknown column alpha in where clause
看返回錯誤了,因為他會認為alpha是一個變量,所以我們得在alpha上加引號,代碼如下:
mysql> select * from dl_users where username= alpha ;
新聞熱點
疑難解答