濫用include
1.漏洞原因:
Include是編寫PHP網站中最常用的函數,并且支持相對路徑。有很多PHP腳本直接把某輸入變量作為Include的參數,造成任意引用腳本、絕對路徑泄露等漏洞。看以下代碼:
| ... $includepage=$_GET["includepage"]; include($includepage); ... |
| $pagelist=array("test1.php","test2.php","test3.php"); //這里規定可進行include的文件 if(isset($_GET["includepage"])) //判斷是否有$includepage { $includepage=$_GET["includepage"]; foreach($pagelist as $prepage) { if($includepage==$prepage) //檢查文件是否在允許列表中 { include($prepage); $checkfind=true; break; } } if($checkfind==true){ unset($checkfind); } else{ die("無效引用頁!"); } } |
| $id=$_GET["id"]; $query="SELECT * FROM my_table where id='".$id."'"; //很經典的SQL注入漏洞 $result=mysql_query($query); |
| $text1=$_POST["text1"]; $text2=$_POST["text2"]; $text3=$_POST["text3"]; $fd=fopen("test.php","a"); fwrite($fd,"/r/n$text1&line;$text2&line;$text3"); fclose($fd); |
| //構造過濾函數 function flt_tags($text) { $badwords=array("操你媽","fuck"); //詞匯過濾列表 $text=rtrim($text); foreach($badwords as $badword) //這里進行詞匯的過濾 { if(stristr($text,$badword)==true){ die("錯誤:你提交的內容含有敏感字眼,請不要提交敏感內容。"); } } $text=htmlspecialchars($text); //HTML替換 //這兩行把回車替換為 $text=str_replace("/r"," ",$text); $text=str_replace("/n","",$text); $text=str_replace("&line;","│",$text); //文本數據庫分隔符"&line;"替換為全角的"│" $text=preg_replace("//s{ 2 }/"," ",$text); //空格替換 $text=preg_replace("//t/"," ",$text); //還是空格替換 if(get_magic_quotes_gpc()){ $text=stripslashes($text); } //如果magic_quotes開啟,則進行/'的替換 return $text; } $text1=$_POST["text1"]; $text2=$_POST["text2"]; $text3=$_POST["text3"]; //過濾全部輸入 $text1=flt_tags($text1); $text2=flt_tags($text2); $text3=flt_tags($text3); $fd=fopen("test.php","a"); fwrite($fd,"/r/n$text1&line;$text2&line;$text3"); fclose($fd); |
| $cookiesign="admincookiesign"; //判斷是否Admin的cookie變量 $adminsign=$_COOKIE["sign"]; //獲取用戶的cookie變量 if($adminsign==$cookiesign) { $admin=true; } if($admin){ echo "現在是管理員狀態。"; } |
| $cookiesign="admincookiesign"; //判斷是否Admin的cookie變量 $adminsign=$_COOKIE["sign"]; //獲取用戶的cookie變量 if($adminsign==$cookiesign) { $admin=true; } else { $admin=false; } if($admin){ echo "現在是管理員狀態。"; } |
| $cookiesign="admincookiesign"; //判斷是否Admin的cookie變量 $adminsign=$_COOKIE["sign"]; //獲取用戶的cookie變量 if($adminsign==$cookiesign) { define(admin,true); } else { define(admin,false); } if(admin){ echo "現在是管理員狀態。"; } |
| Notice: Use of undefined constant test - assumed 'test' in D:/interpub/bigfly/test.php on line 3 |
| string set_error_handler ( callback error_handler [, int error_types]) |
| //admin為管理員的身份判定,true為管理員。 //自定義的錯誤處理函數一定要有這4個輸入變量$errno,$errstr,$errfile,$errline,否則無效。 function my_error_handler($errno,$errstr,$errfile,$errline) { //如果不是管理員就過濾實際路徑 if(!admin) { $errfile=str_replace(getcwd(),"",$errfile); $errstr=str_replace(getcwd(),"",$errstr); } switch($errno) { case E_ERROR: echo "ERROR: [ID $errno] $errstr (Line: $errline of $errfile) /n"; echo "程序已經停止運行,請聯系管理員。"; //遇到Error級錯誤時退出腳本 exit; break; case E_WARNING: echo "WARNING: [ID $errno] $errstr (Line: $errline of $errfile) /n"; break; default: //不顯示Notice級的錯誤 break; } } //把錯誤處理設置為my_error_handler函數 set_error_handler("my_error_handler"); … |
| ... $text1=flt_tags($text1); $text2=flt_tags($text2); $text3=flt_tags($text3); $fd=fopen("data.php","a"); fwrite($fd,"/r/n$text1&line;$text2&line;$text3"); fclose($fd); ... |
| $_SESSION["allowgbookpost"]=time(); //登記填寫時的時間 |
| if(strtoupper($_SERVER["REQUEST_METHOD"])!=”POST”){ die("錯誤:請勿在外部提交。"); } //檢查頁面獲得方法是否為POST if(!isset($_SESSION["allowgbookpost"]) or (time()-$_SESSION["allowgbookpost"] < 10)){ die("錯誤:請勿在外部提交。"); } //檢查留言填寫時的時間 if(isset($_SESSION["gbookposttime"]) and (time()-$_SESSION["gbookposttime"] < 120)){ die("錯誤:兩次提交留言的間隔不得少于 2 分鐘。"); } //檢查留言間隔 unset($_SESSION["allowgbookpost"]); //注銷allowgbookpost變量以防止一次進入填寫頁面多次進行提交 $_SESSION["gbookposttime"]=time(); //登記發送留言的時間,防止灌水或惡意攻擊 ... 數據處理及保存 ... |
新聞熱點
疑難解答