-
header('content-type:text/html;charset=gbk');
- set_time_limit(0);//防止逾時
- /**
- *
- * php目錄掃描監控增強版
- *
- * @version 1.0
- *
- 以下幾個變數使用前需要手動設定
- *
- **/
- /*===================== 程式設定================ =======*/
- $pass="test";//設定密碼
- $jkdir="."; //設定監控掃描的目錄,目前目錄為'.',上一級目錄為'..',也可以設定絕對路徑,後面不要加斜杠,預設為目前目錄
- $logfilename="./m.log";//設定儲存log的路徑,可以放置在任意位置
- $exclude=array('data','images');//排除目錄
- $danger='eval|cmd|passthru|gzuncompress';//設定要查找的危險的函數以確定是否木馬檔案
- $suffix='php|inc';//設定要掃描檔案的後綴
- /*===================== 設定結束=====================*/
-
- $filename=$_GET['filename'];
- $check=$_GET[' check'];
- $jumpoff=false;
- $url = $_SERVER['PHP_SELF'];
- $thisfile = end(explode('/',$url));
- $jump ="{$thisfile}|".implode('|',$exclude);
- $jkdir_num=$file_num=$danger_num=0;
- define('M_PATH',$jkdir);
- define>define('M_PATH',$jkdir);
- define ('M_LOG',$logfilename);
- if ($check=='check')
- {
- $safearr = explode("|",$jump);
- $start_time=microtime( true);
- safe_check($jkdir);
- $end_time=microtime(true);
- $total=$end_time-$start_time;
- $file_num=$file_num-$jkdir_num; $message= " 檔案數:".$file_num;
- $message.= " 資料夾數:".$jkdir_num;
- $message.= " 可疑檔案數:".$danger_num;
- $ message.= " 執行時間:".$total;
- echo $message;
- }else{
- if ($_GET['m']=="del") Delete();//處理檔案刪除
- //讀取檔案內容
- if(isset($_GET['readfile'])){
- //輸出檢視密碼,密碼校驗正確以後輸出檔案內容
- if( empty($_POST['passchack'])){
- echo""
- ."";
- exit;
- }elseif(isset($_POST['passchack'])&&$_POST['passchack']==$ pass){
- $code=file_get_contents($_GET['readfile']);
- echo"";
- exit;
- }else{
- exit;
- }
-
- }else{
- record_md5(M_PATH);
- if(file_exists(M_LOG)){
- $log = unserialize(file_get_contents(M_LOG));
- } array();
- }
-
- if($_GET['savethis']==1){
- //儲存目前檔案md5到日誌檔案
- @unlink(M_LOG);
- file_put_contents(M_LOG,serialize($file_list));
- echo "保存成功!點擊返回";
- exit;
- }
- if(empty($log)){
- echo "目前還沒有建立日誌檔案!點選[儲存目前]建立日誌檔! ";
- }else{
- if($file_list==$log){
- echo "本資料夾沒有做過任何改動!";
- }else{
- if(count($file_list) > 0 ){
- foreach($file_list as $file => $md5){
- if(!isset($log[$ file])){
- echo "新增檔案:".$file.""." 建立時間:".date("Y-m-d H:i:s",filectime($file))." 修改時間:".date("Y-m-d H:i:s",filemtime($file))." 原始碼刪除
";
- }else{
- if($log[$file] != $md5){
- echo "修改檔案:".$file." a>"." 建立時間:".date("Y-m-d H:i:s",filectime($file))." 修改時間:".date("Y-m-d H:i:s",filemtime($file) )." 原始碼
";
-
- unset($log[$file]);
- }else{
- unset($log[$file]);
- }
- }
- }
- }
- if(count($log)>0){
- foreach ($log as $file => $md5){
- echo "刪除檔案:".$file."
";
- }
- }
- }
- }
- }
- }
-
- //計算md5
- function record_md5($jkdir){
- glo $file1 ,$exclude;
- if(is_dir($jkdir)){
- $file=scandir($jkdir);
- foreach($fileas $f){
- if($f!=' .' && $f!='..' && !in_array($f, $exclude)){
- $path = $jkdir.'/'.$f;
- if(is_dir($path)) {
- record_md5($path);
- }else{
- $file_list[$path]=md5_file($path);
- }
- }
- }
- }
- }
-
- function Safe_Check($jkdir)//遍歷檔案
- {
- global $danger ,$suffix ,$jkdir_num ,$file_num ,$danger_num
- while ($file=$hand->read())
- {
- $filename=$jkdir.'/'.$file;
- if ( !$jumpoff) {
- if(Jump($filename))continue;
- }
- if(@is_dir($filename) && $file != '.' && $file!= '..' && $file!='./..')
- { $jkdir_num ;
- Safe_Check($filename);
- }
- if (preg_match_all ("/.($suffix)/i", $filename,$out))
- {
-
- $str='';
- $fp = @fopen($filename,'r')or die('沒有權限');
- while(!feof($fp))
- {
- $str .= fgets($fp,1024);
- }
- fclose($fp);
- if( preg_match_all (" /($danger)[ rnt]{0,}([[(])/i",$str,$out))
- {
- echo "可疑檔案:{$filename}"." 建立時間:".date("Y-m-d H:i:s",filectime($filename))." 修改時間:".date( "Y-m-d H:i:s",filemtime($filename))." 查看程式碼 刪除
";
- $danger_num ;
- }
- }
- $danger_num ;
- }
- }
- $ file_num ;
- }
- }
- function Edit()//查看可疑檔案
- {
- global $filename;
- $filename = str_replace("..","",$ filename);
- $file = $filename;
- $content = "";
- if(is_file($file))
- {
- $fp = fopen($file,"r" )or die('沒有權限');
- $content = fread($fp,filesize($file));
- fclose($fp);
- $content = htmlspecialchars($content);
-
- }
- echo "rn";
- exitititity ();
- }
- function Delete()//刪除檔案
- { global $filename,$pass;
- if(empty($_POST['passchack'])){
- echo ""
- ." "; exit; }elseif(isset($_POST['passchack'])&&$_POST['passchack']==$pass){
- (is_file($filename))?($mes=unlink($filename)?'刪除成功':'刪除失敗檢視權限'):'';
- echo $mes;
- exit();
- }else{
- echo '密碼錯誤! ';
- exit;
- }
- }
- function Jump($file)//跳過檔案
- {
- global $jump,$safearr;
- if($jump != '')
- {
- foreach($safearr as $v)
- {
- if($v=='') continue;
- if( eregi($v,$file ) ) return true ;
- }
- }
- return false;
- }
- ?>
-
[查看檔案變更] |[儲存目前文件指紋]|[掃描可疑文件]
複製程式碼
|