Subversion Repositories php-qbpwcf

Rev

Rev 3 | Rev 190 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 168
Line 106... Line 106...
106
$preTimeFloat["imap"]=0;
106
$preTimeFloat["imap"]=0;
107
 
107
 
108
#初始化記錄 named log 最後變更的時間
108
#初始化記錄 named log 最後變更的時間
109
$preTimeFloat["named"]=0;
109
$preTimeFloat["named"]=0;
110
 
110
 
-
 
111
#初始化記錄 sshd log 最後變更的時間
-
 
112
$preTimeFloat["sshd"]=0;
-
 
113
 
111
#初始化不啟用debug模式
114
#初始化不啟用debug模式
112
$debug=false;
115
$debug=false;
113
 
116
 
114
#預設暫存的白名單ip有效時間為1小時
117
#預設暫存的白名單ip有效時間為1小時
115
$tmp_white_ip_valid_time=1;
118
$tmp_white_ip_valid_time=1;
Line 268... Line 271...
268
		$debug=true;
271
		$debug=true;
269
 
272
 
270
		#comment
273
		#comment
271
		echo "debug mode".PHP_EOL;
274
		echo "debug mode".PHP_EOL;
272
 
275
 
273
		}#if end	
276
		}#if end
274
 
277
 
275
	}#if end
278
	}#if end
276
 
279
 
277
#如果有client參數
280
#如果有client參數
278
if(isset($parseArgu["content"]["client"])){
281
if(isset($parseArgu["content"]["client"])){
Line 430... Line 433...
430
			$res=$unixDomainSockClient["content"];
433
			$res=$unixDomainSockClient["content"];
431
 
434
 
432
			#解析回應
435
			#解析回應
433
			$res=(array)json_decode(trim($res));
436
			$res=(array)json_decode(trim($res));
434
				
437
				
435
			#要求 server 端增加暫時白名單 - end	
438
			#要求 server 端增加暫時白名單 - end
436
				
439
				
437
			}#foreach end
440
			}#foreach end
438
	
441
	
439
		}#if end
442
		}#if end
440
		
443
		
Line 544... Line 547...
544
	exit(1);
547
	exit(1);
545
 
548
 
546
	}#if end
549
	}#if end
547
 
550
 
548
#預設的白名單為自己對外的IP
551
#預設的白名單為自己對外的IP
549
$excludeIp=array($getServerRealIP["content"][0]);	
552
$excludeIp=array($getServerRealIP["content"]);
550
 
553
 
551
#提示內建白名單
554
#提示內建白名單
552
echo "內建白名單:".$excludeIp[0].PHP_EOL;
555
echo "內建白名單:".$excludeIp[0].PHP_EOL;
553
	
556
	
554
#如果有 exclude 參數
557
#如果有 exclude 參數
Line 749... Line 752...
749
	#反之收到 update-tmp-white-ip-valid-time
752
	#反之收到 update-tmp-white-ip-valid-time
750
	if(isset($receivedArray["update-tmp-white-ip-valid-time"])){
753
	if(isset($receivedArray["update-tmp-white-ip-valid-time"])){
751
	
754
	
752
		#更新白名單有效時間
755
		#更新白名單有效時間
753
		$params["info"]["tmp-white-ip-valid-time"]=$receivedArray["update-tmp-white-ip-valid-time"];
756
		$params["info"]["tmp-white-ip-valid-time"]=$receivedArray["update-tmp-white-ip-valid-time"];
754
			
757
		
755
		#debug mode
758
		#debug mode
756
		if($debug){
759
		if($debug){
757
			
760
			
758
			#debug
761
			#debug
759
			var_dump(__LINE__,$params["info"]["tmp-white-ip-valid-time"]);
762
			var_dump(__LINE__,$params["info"]["tmp-white-ip-valid-time"]);
760
	
763
			
761
			}#if end
764
			}#if end
762
		
765
		
763
		}#if end
766
		}#if end
764
			
767
	
765
	#初始化要回傳的結果
768
	#初始化要回傳的結果
766
	$result=array();	
769
	$result=array();
767
		
770
		
768
	#設置執行正常
771
	#設置執行正常
769
	$result["status"]="true";	
772
	$result["status"]="true";
770
		
773
	
771
	#設置可以繼續執行後面的函式
774
	#設置可以繼續執行後面的函式
772
	$result["continue"]="true";
775
	$result["continue"]="true";
773
	
776
	
774
	#回傳結果
777
	#回傳結果
775
	return $result;
778
	return $result;
776
 
779
 
777
	}#function addOnProcessFunc end
780
	}#function addOnProcessFunc end
778
 
781
 
-
 
782
/*
-
 
783
#說明:
779
#沒事時要重複執行的函式
784
#沒事時要重複執行的函式
-
 
785
#參數:
-
 
786
#$params["info"]["preTimeFloat"]["http"],http log的最新時間.
-
 
787
#$params["info"]["preTimeFloat"]["https"],https log的最新時間.
-
 
788
#$params["info"]["preTimeFloat"]["smtp"],postfix.service log的最新時間.
-
 
789
#$params["info"]["preTimeFloat"]["imap"],dovecot.service log的最新時間.
-
 
790
#$params["info"]["preTimeFloat"]["named"],named.service log的最新時間.
-
 
791
#$params["info"]["preTimeFloat"]["sshd"],sshd.service log的最新時間.
-
 
792
*/
780
function funcToRunWhenIdle($params){
793
function funcToRunWhenIdle(&$params){
781
 
794
 
782
	#預設不 debug
795
	#預設不 debug
783
	$debug=false;
796
	$debug=false;
784
 
797
 
785
	#如果有debug鍵數值.
798
	#如果有debug鍵數值.
Line 805... Line 818...
805
		echo "\$preHttpTimeFloat:".$params["info"]["preTimeFloat"]["http"].PHP_EOL;
818
		echo "\$preHttpTimeFloat:".$params["info"]["preTimeFloat"]["http"].PHP_EOL;
806
		echo "\$preHttpsTimeFloat:".$params["info"]["preTimeFloat"]["https"].PHP_EOL;
819
		echo "\$preHttpsTimeFloat:".$params["info"]["preTimeFloat"]["https"].PHP_EOL;
807
		echo "\$preSmtpTimeFloat:".$params["info"]["preTimeFloat"]["smtp"].PHP_EOL;
820
		echo "\$preSmtpTimeFloat:".$params["info"]["preTimeFloat"]["smtp"].PHP_EOL;
808
		echo "\$preImapTimeFloat:".$params["info"]["preTimeFloat"]["imap"].PHP_EOL;
821
		echo "\$preImapTimeFloat:".$params["info"]["preTimeFloat"]["imap"].PHP_EOL;
809
		echo "\$preNamedTimeFloat:".$params["info"]["preTimeFloat"]["named"].PHP_EOL;
822
		echo "\$preNamedTimeFloat:".$params["info"]["preTimeFloat"]["named"].PHP_EOL;
-
 
823
		echo "\$preSshddTimeFloat:".$params["info"]["preTimeFloat"]["sshd"].PHP_EOL;
810
 
824
 
811
		}#if end
825
		}#if end
812
 
826
 
813
	#函式說明:
827
	#函式說明:
814
	#檢查多個檔案與資料夾是否存在.
828
	#檢查多個檔案與資料夾是否存在.
Line 822... Line 836...
822
	#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱,如果不存在則代表路徑是網址.
836
	#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱,如果不存在則代表路徑是網址.
823
	#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址,若"web"參數為"true",才會有該內容.
837
	#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址,若"web"參數為"true",才會有該內容.
824
	#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
838
	#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
825
	#必填參數:
839
	#必填參數:
826
	#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
840
	#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
827
	$conf["fileArray"]=array("/var/log/httpd/access_log");
841
	$conf["fileAccess::checkMultiFileExist"]["fileArray"]=array("/var/log/httpd/access_log");
828
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
842
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
829
	$conf["fileArgu"]=__FILE__;
843
	$conf["fileAccess::checkMultiFileExist"]["fileArgu"]=__FILE__;
830
	#可省略參數:
844
	#可省略參數:
831
	#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
845
	#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
832
	#$conf["disableWebSearch"]="false";
846
	#$conf["disableWebSearch"]="false";
833
	#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
847
	#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
834
	$conf["userDir"]="false";
848
	$conf["fileAccess::checkMultiFileExist"]["userDir"]="false";
835
	#$conf["web"],字串,檔案是放在web就是"true",反之為檔案系統"false",預設為"true".
849
	#$conf["web"],字串,檔案是放在web就是"true",反之為檔案系統"false",預設為"true".
836
	$conf["web"]="false";
850
	$conf["fileAccess::checkMultiFileExist"]["web"]="false";
837
	#參考資料:
851
	#參考資料:
838
	#http://php.net/manual/en/function.file-exists.php
852
	#http://php.net/manual/en/function.file-exists.php
839
	#http://php.net/manual/en/control-structures.foreach.php
853
	#http://php.net/manual/en/control-structures.foreach.php
840
	#備註:
854
	#備註:
841
	#函數file_exists檢查的路徑為檔案系統的路徑
855
	#函數file_exists檢查的路徑為檔案系統的路徑
842
	#$result["varName"][$i]結果未實作
856
	#$result["varName"][$i]結果未實作
843
	$checkMultiFileExist=fileAccess::checkMultiFileExist($conf);
857
	$checkMultiFileExist=fileAccess::checkMultiFileExist($conf["fileAccess::checkMultiFileExist"]);
844
	unset($conf);
858
	unset($conf["fileAccess::checkMultiFileExist"]);
845
 
859
 
846
	#如果運行失敗
860
	#如果運行失敗
847
	if($checkMultiFileExist["status"]==="false"){
861
	if($checkMultiFileExist["status"]==="false"){
848
	
862
	
849
		#設置執行失敗
863
		#設置執行失敗
Line 858... Line 872...
858
		#結束執行
872
		#結束執行
859
		exit(1);
873
		exit(1);
860
	
874
	
861
		}#if end
875
		}#if end
862
	
876
	
863
	#初始化 http log 的最後變更時間
-
 
864
	$httpTimeFloat=0;
-
 
865
	
-
 
866
	#如果存在 http log
877
	#如果存在 http log
867
	if($checkMultiFileExist["allExist"]==="true"){
878
	if($checkMultiFileExist["allExist"]==="true"){
868
	
879
		
869
		#函數說明:
880
		#函式說明:
870
		#依據取得檔案的擁有着資訊
881
		#呼叫shell執行系統命令,並取得回傳的內容.
871
		#回傳結果:
882
		#回傳結果:
872
		#$result["status"],"true"爲建立成功,"false"爲建立失敗.
883
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
873
		#$result["error"],錯誤訊息陣列.
884
		#$result["error"],錯誤訊息陣列.
874
		#$result["function"],函數名稱. 
885
		#$result["function"],當前執行的函數名稱.
875
		#$result["content"],檔案資訊陣列.
886
		#$result["argu"],使用的參數.
876
		#$result["content"]["is_folder"],是否為目錄,"true"代表是,"false"代表不是.	
-
 
877
		#$result["content"]["ownerPerm"],檔案擁有者權限資訊.
-
 
878
		#$result["content"]["groupPerm"],檔案歸屬群組權限資訊.
887
		#$result["cmd"],執行的指令內容.
879
		#$result["content"]["otherPerm"],檔案對於其他身份使用者的權限資訊.
888
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
880
		#$result["content"]["subElementCount"],目錄底下的檔案目錄數量.
889
		#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
881
		#$result["content"]["ownerName"],檔案擁有着資訊.
890
		#$result["content"],為執行完後的輸出字串.
882
		#$result["content"]["groupName"],檔案所屬擁有着資訊.
891
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
883
		#$result["content"]["size"],檔案大小.
892
		#$result["running"],是否還在執行.
884
		#$result["content"]["modifyDate"],檔案變更年月日.
893
		#$result["pid"],pid.
885
		#$result["content"]["modifyTime"],檔案變更時分秒.
894
		#$result["statusCode"],執行結束後的代碼.
886
		#$result["content"]["modifyTimeFloat"],檔案變更時間秒的float數值.
895
		#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
887
		#$result["content"]["timezone"],檔案變更時間的時區與UTC的差距.
896
		#$result["noEcaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
888
		#必填參數:
897
		#必填參數:
-
 
898
		#$conf["command"],字串,要執行的指令.
-
 
899
		$conf["external::callShell"]["command"]="tail";
889
		#$conf["fileArgu"],字串,當前檔案的位置亦即__FILE__的內容.
900
		#$conf["fileArgu"],字串,變數__FILE__的內容.
890
		$conf["fileArgu"]=__FILE__;
901
		$conf["external::callShell"]["fileArgu"]=__FILE__;
-
 
902
		#可省略參數:
891
		#$conf["file"],字串,要查看擁有者資訊的檔案.
903
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
892
		$conf["file"]="/var/log/httpd/access_log";
904
		$conf["external::callShell"]["argu"]=array("-n","1","/var/log/httpd/access_log");
-
 
905
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
-
 
906
		#$conf["arguIsAddr"]=array();
-
 
907
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
-
 
908
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
-
 
909
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
-
 
910
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
-
 
911
		#$conf["enablePrintDescription"]="true";
-
 
912
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
-
 
913
		#$conf["printDescription"]="";
-
 
914
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
-
 
915
		$conf["external::callShell"]["escapeshellarg"]="true";
-
 
916
		#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
-
 
917
		#$conf["thereIsShellVar"]=array();
-
 
918
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
-
 
919
		#$conf["username"]="";
-
 
920
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
-
 
921
		#$conf["password"]="";	
-
 
922
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
-
 
923
		#$conf["useScript"]="";
-
 
924
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
-
 
925
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
-
 
926
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
-
 
927
		#$conf["inBackGround"]="";
-
 
928
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
893
		$conf["web"]="false";
929
		#$conf["getErr"]="false";
-
 
930
		#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
-
 
931
		#$conf["doNotRun"]="false";
894
		#參考資料:
932
		#參考資料:
895
		#fileowner=>http://php.net/manual/en/function.fileowner.php
933
		#exec=>http://php.net/manual/en/function.exec.php
896
		#posix_getpwuid=>http://php.net/manual/en/function.posix-getpwuid.php
934
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
-
 
935
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
-
 
936
		#備註:
-
 
937
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
-
 
938
		#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
897
		$httpFileInfo=fileAccess::fileInfo($conf);
939
		$callShell=external::callShell($conf["external::callShell"]);
898
		unset($conf);
940
		unset($conf["external::callShell"]);
899
		
941
		
900
		#如果運行失敗
942
		#如果執行異常
901
		if($httpFileInfo["status"]==="false"){
943
		if($callShell["status"]==="false"){
902
		
944
		
903
			#設置執行失敗
-
 
904
			$result["status"]="false";
-
 
905
 
-
 
906
			#設置錯誤訊息
-
 
907
			$result["error"]=$httpFileInfo;
-
 
908
 
-
 
909
			#印出結果
945
			#debug
910
			var_dump($result);
946
			var_dump($callShell);
911
			
947
		
912
			#結束執行
948
			#結束並回傳代表1,表示異常
913
			exit(1);
949
			exit(1);
914
		
950
		
915
			}#if end
951
			}#if end
916
			
952
			
-
 
953
		#如果輸出有一行
-
 
954
		if(count($callShell["output"])===1){
-
 
955
		
-
 
956
			#debug
-
 
957
			if($debug){
-
 
958
				
-
 
959
				#標題提示
-
 
960
				echo "最新一筆log".PHP_EOL;
-
 
961
				
-
 
962
				#印出log
-
 
963
				echo $callShell["output"][0].PHP_EOL;
-
 
964
				
-
 
965
				}#if end
-
 
966
		
-
 
967
			#範例log
-
 
968
			#193.42.43.36 - - [30/Sep/2025:13:15:04 +0800] "GET / HTTP/2.0" 200 138772
-
 
969
			#${ip} ${doNotNeed} [${day}/${month}/${year}:${hour}:${min}:${sec} ${timezone}] ${method} ${path} ${protocol} ${responseCode} ${bytesSend}
-
 
970
		
-
 
971
			#解析時間
-
 
972
			#函式說明:
-
 
973
			#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
-
 
974
			#回傳結果:
-
 
975
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
976
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
-
 
977
			#$result["function"],當前執行的函式名稱.
-
 
978
			#$result["argu"],所使用的參數.
-
 
979
			#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
-
 
980
			#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
-
 
981
			#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
-
 
982
			#必填參數:
-
 
983
			#$conf["input"],字串,要檢查的字串.
-
 
984
			$conf["search::findSpecifyStrFormat"]["input"]=$callShell["output"][0];
-
 
985
			#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
986
			$conf["search::findSpecifyStrFormat"]["format"]="\${ip} \${doNotNeed} [\${day}/\${month}/\${year}:\${hour}:\${min}:\${sec} \${timezone}] \${method} \${path} \${protocol} \${responseCode} \${bytesSend}";
-
 
987
			#可省略參數:
-
 
988
			#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
989
			#$conf["varEqual"]=array(null,"found");
-
 
990
			#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
-
 
991
			#$conf["varCon"]=array("no_tail"=>" not");
-
 
992
			#參考資料:
-
 
993
			#無.
-
 
994
			#備註:
-
 
995
			#無.
-
 
996
			$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
-
 
997
			unset($conf["search::findSpecifyStrFormat"]);
-
 
998
		
-
 
999
			#如果執行異常
-
 
1000
			if($findSpecifyStrFormat["status"]==="false"){
-
 
1001
			
-
 
1002
				#debug
-
 
1003
				var_dump($findSpecifyStrFormat);
-
 
1004
			
-
 
1005
				#結束並回傳代表1,表示異常
-
 
1006
				exit(1);
-
 
1007
			
-
 
1008
				}#if end
-
 
1009
				
-
 
1010
			#如果格式有符合
-
 
1011
			if($findSpecifyStrFormat["found"]==="true"){
-
 
1012
			
-
 
1013
				#記錄 http log 的最後變更時間
-
 
1014
				$httpTimeFloat=strtotime($findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0].":".$findSpecifyStrFormat["parsedVar"]["sec"][0]);
-
 
1015
				
-
 
1016
				}#if end
-
 
1017
		
-
 
1018
			}#if end
-
 
1019
			
-
 
1020
			#反之沒變動
-
 
1021
			else{
-
 
1022
			
-
 
1023
				#沿用上次時間
-
 
1024
				$httpTimeFloat=$params["info"]["preTimeFloat"]["http"];
-
 
1025
				
-
 
1026
				}#else ebd
-
 
1027
			
917
		#debug
1028
		#debug
918
		if($debug){
1029
		if($debug){
919
			
1030
			
920
			#comment
1031
			#comment
921
			echo "accessed file /var/log/httpd/access_log".PHP_EOL;
1032
			echo "accessed file /var/log/httpd/access_log".PHP_EOL;
922
			
1033
			
923
			}#if end
1034
			}#if end
924
		
-
 
925
		#記錄 http log 的最後變更時間
-
 
926
		$httpTimeFloat=$httpFileInfo["content"]["modifyTimeFloat"];
-
 
927
	
1035
	
928
		}#if end
1036
		}#if end
929
 
1037
 
930
	#函式說明:
1038
	#函式說明:
931
	#檢查多個檔案與資料夾是否存在.
1039
	#檢查多個檔案與資料夾是否存在.
Line 939... Line 1047...
939
	#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱,如果不存在則代表路徑是網址.
1047
	#$result["varNameFullPath"][$i],爲第$i個資料夾或檔案的完整檔案系統路徑與名稱,如果不存在則代表路徑是網址.
940
	#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址,若"web"參數為"true",才會有該內容.
1048
	#$result["varNameWebPath"][$i],為第$i個資料夾或檔案的網址,若"web"參數為"true",才會有該內容.
941
	#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
1049
	#$result["varExist"][$i],爲第$i個資料夾或檔案是否存在,"true"代表存在,"false"代表不存在。
942
	#必填參數:
1050
	#必填參數:
943
	#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
1051
	#$conf["fileArray"],陣列字串,要檢查是否存在的檔案有哪些,須爲一維陣列數值。
944
	$conf["fileArray"]=array("/var/log/httpd/ssl_access_log");
1052
	$conf["fileAccess::checkMultiFileExist"]["fileArray"]=array("/var/log/httpd/ssl_access_log");
945
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1053
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
946
	$conf["fileArgu"]=__FILE__;
1054
	$conf["fileAccess::checkMultiFileExist"]["fileArgu"]=__FILE__;
947
	#可省略參數:
1055
	#可省略參數:
948
	#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
1056
	#$conf["disableWebSearch"],"字串",是否取消「當檔案找不到時,改用catchWebContent類別的wget函數來檢查檔案是否存在於網路上」的功能,"false"不取消,若要取消該功能請設為"true",若抓到的內容為空字串則會視為檔案不存在,預設為"true".
949
	#$conf["disableWebSearch"]="false";
1057
	#$conf["disableWebSearch"]="false";
950
	#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
1058
	#$conf["userDir"],字串,網頁是否置放於家目錄底下,"true"為是,"false"為不是,預設為"true".
951
	$conf["userDir"]="false";
1059
	$conf["fileAccess::checkMultiFileExist"]["userDir"]="false";
952
	#$conf["web"],字串,檔案是放在web就是"true",反之為檔案系統"false",預設為"true".
1060
	#$conf["web"],字串,檔案是放在web就是"true",反之為檔案系統"false",預設為"true".
953
	$conf["web"]="false";
1061
	$conf["fileAccess::checkMultiFileExist"]["web"]="false";
954
	#參考資料:
1062
	#參考資料:
955
	#http://php.net/manual/en/function.file-exists.php
1063
	#http://php.net/manual/en/function.file-exists.php
956
	#http://php.net/manual/en/control-structures.foreach.php
1064
	#http://php.net/manual/en/control-structures.foreach.php
957
	#備註:
1065
	#備註:
958
	#函數file_exists檢查的路徑為檔案系統的路徑
1066
	#函數file_exists檢查的路徑為檔案系統的路徑
959
	#$result["varName"][$i]結果未實作
1067
	#$result["varName"][$i]結果未實作
960
	$checkMultiFileExist=fileAccess::checkMultiFileExist($conf);
1068
	$checkMultiFileExist=fileAccess::checkMultiFileExist($conf["fileAccess::checkMultiFileExist"]);
961
	unset($conf);
1069
	unset($conf["fileAccess::checkMultiFileExist"]);
962
 
1070
 
963
	#如果運行失敗
1071
	#如果運行失敗
964
	if($checkMultiFileExist["status"]==="false"){
1072
	if($checkMultiFileExist["status"]==="false"){
965
	
1073
	
966
		#設置執行失敗
1074
		#設置執行失敗
Line 974... Line 1082...
974
		
1082
		
975
		#結束執行
1083
		#結束執行
976
		exit(1);
1084
		exit(1);
977
	
1085
	
978
		}#if end
1086
		}#if end
979
		
-
 
980
	#初始化 https log 的最後變更時間
-
 
981
	$httpsTimeFloat=0;	
-
 
982
		
1087
	
983
	#如果存在 https log
1088
	#如果存在 https log
984
	if($checkMultiFileExist["allExist"]==="true"){
1089
	if($checkMultiFileExist["allExist"]==="true"){
985
	
1090
	
986
		#函數說明:
1091
		#函式說明:
987
		#依據取得檔案的擁有着資訊
1092
		#呼叫shell執行系統命令,並取得回傳的內容.
988
		#回傳結果:
1093
		#回傳結果:
989
		#$result["status"],"true"爲建立成功,"false"爲建立失敗.
1094
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
990
		#$result["error"],錯誤訊息陣列.
1095
		#$result["error"],錯誤訊息陣列.
991
		#$result["function"],函數名稱. 
1096
		#$result["function"],當前執行的函數名稱.
992
		#$result["content"],檔案資訊陣列.
1097
		#$result["argu"],使用的參數.
993
		#$result["content"]["is_folder"],是否為目錄,"true"代表是,"false"代表不是.	
-
 
994
		#$result["content"]["ownerPerm"],檔案擁有者權限資訊.
-
 
995
		#$result["content"]["groupPerm"],檔案歸屬群組權限資訊.
1098
		#$result["cmd"],執行的指令內容.
996
		#$result["content"]["otherPerm"],檔案對於其他身份使用者的權限資訊.
1099
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
997
		#$result["content"]["subElementCount"],目錄底下的檔案目錄數量.
1100
		#$result["output"],爲執行完後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
998
		#$result["content"]["ownerName"],檔案擁有着資訊.
1101
		#$result["content"],為執行完後的輸出字串.
999
		#$result["content"]["groupName"],檔案所屬擁有着資訊.
1102
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
1000
		#$result["content"]["size"],檔案大小.
1103
		#$result["running"],是否還在執行.
1001
		#$result["content"]["modifyDate"],檔案變更年月日.
1104
		#$result["pid"],pid.
1002
		#$result["content"]["modifyTime"],檔案變更時分秒.
1105
		#$result["statusCode"],執行結束後的代碼.
1003
		#$result["content"]["modifyTimeFloat"],檔案變更時間秒的float數值.
1106
		#$result["escape"],陣列,儲存重新排序過且已經escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
1004
		#$result["content"]["timezone"],檔案變更時間的時區與UTC的差距.
1107
		#$result["noEcaped"],陣列,儲存重新排序過未經過escape過的指令(key為"cmd")與參數(key為"argu")與兩者組合的一維陣列(key為"array").
1005
		#必填參數:
1108
		#必填參數:
-
 
1109
		#$conf["command"],字串,要執行的指令.
-
 
1110
		$conf["external::callShell"]["command"]="tail";
1006
		#$conf["fileArgu"],字串,當前檔案的位置亦即__FILE__的內容.
1111
		#$conf["fileArgu"],字串,變數__FILE__的內容.
1007
		$conf["fileArgu"]=__FILE__;
1112
		$conf["external::callShell"]["fileArgu"]=__FILE__;
-
 
1113
		#可省略參數:
1008
		#$conf["file"],字串,要查看擁有者資訊的檔案.
1114
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1009
		$conf["file"]="/var/log/httpd/ssl_access_log";
1115
		$conf["external::callShell"]["argu"]=array("-n","1","/var/log/httpd/ssl_access_log");
-
 
1116
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
-
 
1117
		#$conf["arguIsAddr"]=array();
-
 
1118
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
-
 
1119
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
-
 
1120
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
-
 
1121
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
-
 
1122
		#$conf["enablePrintDescription"]="true";
-
 
1123
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
-
 
1124
		#$conf["printDescription"]="";
-
 
1125
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".如果參數為"< 、<< 、> 、>> 、| 、2>&1"之一則不會過濾.
-
 
1126
		$conf["external::callShell"]["escapeshellarg"]="true";
-
 
1127
		#$conf["thereIsShellVar"],陣列字串,指令搭配的參數"argu",若含有「\'」,則取代為「"」.每個argu參數都要有對應的元素."true"代表要置換.
-
 
1128
		#$conf["thereIsShellVar"]=array();
-
 
1129
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
-
 
1130
		#$conf["username"]="";
-
 
1131
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
-
 
1132
		#$conf["password"]="";	
-
 
1133
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
-
 
1134
		#$conf["useScript"]="";
-
 
1135
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
-
 
1136
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
-
 
1137
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
-
 
1138
		#$conf["inBackGround"]="";
-
 
1139
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
1010
		$conf["web"]="false";
1140
		#$conf["getErr"]="false";
-
 
1141
		#$conf["doNotRun"],字串,"true"代表不執行指令,預設為"false"會執行指令.
-
 
1142
		#$conf["doNotRun"]="false";
1011
		#參考資料:
1143
		#參考資料:
1012
		#fileowner=>http://php.net/manual/en/function.fileowner.php
1144
		#exec=>http://php.net/manual/en/function.exec.php
1013
		#posix_getpwuid=>http://php.net/manual/en/function.posix-getpwuid.php
1145
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
1014
		$httpsFileInfo=fileAccess::fileInfo($conf);
1146
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
1015
		unset($conf);
1147
		#備註:
1016
 
-
 
-
 
1148
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
-
 
1149
		#若使用的 command、argu 參數,含有 ~ 則會被視為字串,若有需要其於 shell 中代表的家目錄位置,可用 fileAccess::tildeToPath 來進行轉換.
1017
		#如果運行失敗
1150
		$callShell=external::callShell($conf["external::callShell"]);
1018
		if($httpsFileInfo["status"]==="false"){
1151
		unset($conf["external::callShell"]);
1019
		
1152
		
1020
			#設置執行失敗
1153
		#如果執行異常
1021
			$result["status"]="false";
1154
		if($callShell["status"]==="false"){
1022
 
-
 
1023
			#設置錯誤訊息
-
 
1024
			$result["error"]=$httpsFileInfo;
-
 
1025
 
1155
		
1026
			#印出結果
1156
			#debug
1027
			var_dump($result);
1157
			var_dump($callShell);
1028
			
1158
		
1029
			#結束執行
1159
			#結束並回傳代表1,表示異常
1030
			exit(1);
1160
			exit(1);
1031
		
1161
		
1032
			}#if end
1162
			}#if end
-
 
1163
			
-
 
1164
		#如果輸出有一行
-
 
1165
		if(count($callShell["output"])===1){
-
 
1166
		
-
 
1167
			#debug
-
 
1168
			if($debug){
-
 
1169
				
-
 
1170
				#標題提示
-
 
1171
				echo "最新一筆log".PHP_EOL;
-
 
1172
				
-
 
1173
				#印出log
-
 
1174
				echo $callShell["output"][0].PHP_EOL;
-
 
1175
				
-
 
1176
				}#if end
-
 
1177
		
-
 
1178
			#範例log
-
 
1179
			#193.42.43.36 - - [30/Sep/2025:13:15:04 +0800] "GET / HTTP/2.0" 200 138772
-
 
1180
			#${ip} ${doNotNeed} [${day}/${month}/${year}:${hour}:${min}:${sec} ${timezone}] ${method} ${path} ${protocol} ${responseCode} ${bytesSend}
-
 
1181
		
-
 
1182
			#解析時間
-
 
1183
			#函式說明:
-
 
1184
			#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
-
 
1185
			#回傳結果:
-
 
1186
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
1187
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
-
 
1188
			#$result["function"],當前執行的函式名稱.
-
 
1189
			#$result["argu"],所使用的參數.
-
 
1190
			#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
-
 
1191
			#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
-
 
1192
			#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
-
 
1193
			#必填參數:
-
 
1194
			#$conf["input"],字串,要檢查的字串.
-
 
1195
			$conf["input"]=$callShell["output"][0];
-
 
1196
			#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
1197
			$conf["format"]="\${ip} \${doNotNeed} [\${day}/\${month}/\${year}:\${hour}:\${min}:\${sec} \${timezone}] \${method} \${path} \${protocol} \${responseCode} \${bytesSend}";
-
 
1198
			#可省略參數:
-
 
1199
			#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
1200
			#$conf["varEqual"]=array(null,"found");
-
 
1201
			#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
-
 
1202
			#$conf["varCon"]=array("no_tail"=>" not");
-
 
1203
			#參考資料:
-
 
1204
			#無.
-
 
1205
			#備註:
-
 
1206
			#無.
-
 
1207
			$findSpecifyStrFormat=search::findSpecifyStrFormat($conf);
-
 
1208
			unset($conf);
-
 
1209
		
-
 
1210
			#如果執行異常
-
 
1211
			if($findSpecifyStrFormat["status"]==="false"){
-
 
1212
			
-
 
1213
				#debug
-
 
1214
				var_dump($findSpecifyStrFormat);
-
 
1215
			
-
 
1216
				#結束並回傳代表1,表示異常
-
 
1217
				exit(1);
-
 
1218
			
-
 
1219
				}#if end
-
 
1220
				
-
 
1221
			#如果格式有符合
-
 
1222
			if($findSpecifyStrFormat["found"]==="true"){
-
 
1223
				
-
 
1224
				#記錄 http log 的最後變更時間
-
 
1225
				$httpsTimeFloat=strtotime($findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0].":".$findSpecifyStrFormat["parsedVar"]["sec"][0]);
-
 
1226
				
-
 
1227
				}#if end
-
 
1228
		
-
 
1229
			}#if end
-
 
1230
 
-
 
1231
		#反之沒變動
-
 
1232
		else{
-
 
1233
		
-
 
1234
			#沿用上次時間
-
 
1235
			$httpsTimeFloat=$params["info"]["preTimeFloat"]["https"];
-
 
1236
			
-
 
1237
			}#else ebd
1033
 
1238
 
1034
		#debug
1239
		#debug
1035
		if($debug){
1240
		if($debug){
1036
			
1241
			
1037
			#comment
1242
			#comment
1038
			echo "accessed file /var/log/httpd/ssl_access_log".PHP_EOL;
1243
			echo "accessed file /var/log/httpd/ssl_access_log".PHP_EOL;
1039
			
1244
			
1040
			}#if end
1245
			}#if end
1041
			
1246
		
1042
		#記錄 https log 的最後變更時間
-
 
1043
		$httpsTimeFloat=$httpsFileInfo["content"]["modifyTimeFloat"];
-
 
1044
	
-
 
1045
		}#if end
1247
		}#if end
1046
	
1248
	
1047
	#透過運行以下指令取得最後變更時間
1249
	#透過運行以下指令取得最後變更時間
1048
	#journalctl -a -e | grep postfix/smtpd | grep " connect from unknown" | tail -n 1
1250
	#journalctl -a -e --unit=postfix.service | tail -n 1;
1049
	#涵式說明:
1251
	#涵式說明:
1050
	#呼叫shell執行系統命令,並取得回傳的內容.
1252
	#呼叫shell執行系統命令,並取得回傳的內容.
1051
	#回傳的結果:
1253
	#回傳的結果:
1052
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1254
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1053
	#$result["error"],錯誤訊息陣列.
1255
	#$result["error"],錯誤訊息陣列.
Line 1062... Line 1264...
1062
	#$result["statusCode"],執行結束後的代碼.
1264
	#$result["statusCode"],執行結束後的代碼.
1063
	#必填的參數
1265
	#必填的參數
1064
	#$conf["command"],字串,要執行的指令與.
1266
	#$conf["command"],字串,要執行的指令與.
1065
	$conf["external::callShell"]["command"]="journalctl";
1267
	$conf["external::callShell"]["command"]="journalctl";
1066
	#$conf["fileArgu"],字串,變數__FILE__的內容.
1268
	#$conf["fileArgu"],字串,變數__FILE__的內容.
1067
	$conf["external::callShell"]["fileArgu"]=__FILE__;		
1269
	$conf["external::callShell"]["fileArgu"]=__FILE__;
1068
	#可省略參數:
1270
	#可省略參數:
1069
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1271
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1070
	$conf["external::callShell"]["argu"]=array("-a","-e","|","grep","postfix/smtps/smtpd","|","grep"," connect from unknown","|","tail","-n","1");
1272
	$conf["external::callShell"]["argu"]=array("-a","-e","--unit=postfix.service","|","tail","-n","1");
1071
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
1273
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
1072
	#$conf["arguIsAddr"]=array();	
1274
	#$conf["arguIsAddr"]=array();	
1073
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1275
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1074
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1276
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1075
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
1277
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
Line 1126... Line 1328...
1126
		}#if end
1328
		}#if end
1127
	
1329
	
1128
	#取得行數
1330
	#取得行數
1129
	$lineCount=count($callShell["output"]);
1331
	$lineCount=count($callShell["output"]);
1130
	
1332
	
-
 
1333
	#函式說明:
-
 
1334
	#多層if判斷.
-
 
1335
	#回傳結果:
-
 
1336
	#$result["status"],字串,"true"代表執行正常;"false"代表執行異常.
-
 
1337
	#$result["function"],當前函式的名稱.
-
 
1338
	#$result["pass"],字串,"true"代表通過多層if判斷;反之為"false".
-
 
1339
	#必填參數:
-
 
1340
	#$conf["get_defined_vars"],陣列,所有已經定義的變數.
-
 
1341
	$conf["controlStructures::nestedIf"]["get_defined_vars"]=get_defined_vars();
-
 
1342
	#$conf["varToControlLeft"],變數陣列,依照順序每層要做判斷的變數.
-
 
1343
	$conf["controlStructures::nestedIf"]["varToControlLeft"]=array("lineCount","callShell[\"output\"]");
-
 
1344
	#$conf["conditions"],字串陣列,依照順序每層變數判斷的條件,例如">","<",">=","<=","!=","==","<==","!==","===".
-
 
1345
	$conf["controlStructures::nestedIf"]["conditions"]=array("===","!==");
-
 
1346
	#$conf["varToControlRight"],變數陣列,依照順序每層變數判斷期望的結果.
-
 
1347
	$conf["controlStructures::nestedIf"]["varToControlRight"]=array(1,"-- No entries --");
-
 
1348
	#可省略參數:
-
 
1349
	#無.
-
 
1350
	#參考資料:
-
 
1351
	#無.
-
 
1352
	#備註:
-
 
1353
	#無.
-
 
1354
	$nestedIf=controlStructures::nestedIf($conf["controlStructures::nestedIf"]);
-
 
1355
	unset($conf["controlStructures::nestedIf"]);
-
 
1356
		
1131
	#如果行數等於1
1357
	#如果執行異常
1132
	if($lineCount===1){
1358
	if($nestedIf["status"]==="false"){
-
 
1359
	
-
 
1360
		#設置執行異常
-
 
1361
		$result["status"]="false";
-
 
1362
		
-
 
1363
		#設置執行錯誤
-
 
1364
		$result["error"]=$nestedIf;
-
 
1365
		
-
 
1366
		#回傳結果
-
 
1367
		return $result;
-
 
1368
	
-
 
1369
		}#if end
1133
	
1370
	
-
 
1371
	#如果不通過判斷
-
 
1372
	if($nestedIf["pass"]==="false"){
-
 
1373
	
-
 
1374
		#沿用上次時間
-
 
1375
		$smtpTimeFloat=$params["info"]["preTimeFloat"]["smtp"];
-
 
1376
		
-
 
1377
		}#if end
-
 
1378
		
-
 
1379
	#反之
-
 
1380
	else{
-
 
1381
 
1134
		#debug
1382
		#debug
1135
		if($debug){
1383
		if($debug){
1136
			
1384
			
1137
			#最新一筆惡意IP
1385
			#標題提示
1138
			#echo "最新一筆惡意IP:".PHP_EOL;
1386
			echo "最新一筆log".PHP_EOL;
-
 
1387
			
-
 
1388
			#印出log
1139
			#var_dump($callShell["output"]);
1389
			echo $callShell["output"][0].PHP_EOL;
1140
			
1390
			
1141
			}#if end
1391
			}#if end
1142
		
1392
		
1143
		#Aug 09 07:12:09 www.qbpwcf.org postfix/smtpd[20829]: connect from unknown[185.234.219.62]
1393
		#Aug 09 07:12:09 www.qbpwcf.org postfix/smtpd[20829]: connect from unknown[185.234.219.62]
1144
		#Apr 04 15:11:16 mail.qbpwcf.org postfix/smtps/smtpd[10145]: connect from unknown[212.70.149.72]
1394
		#Apr 04 15:11:16 mail.qbpwcf.org postfix/smtps/smtpd[10145]: connect from unknown[212.70.149.72]
1145
		$unProcesedStr=$callShell["output"];
1395
		#${month} ${day} ${hour}:${min}:${sec} ${doNotNeed}"
1146
		
1396
		
1147
		#分割字串
1397
		#解析時間點
1148
		#涵式說明:
1398
		#函式說明:
1149
		#將多個固定格式的字串分開,並回傳分開的結果
1399
		#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
1150
		#回傳的參數:
1400
		#回傳結果:
1151
		#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
1401
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1152
		#$result["error"],錯誤訊息陣列.
1402
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
1153
		#$result["function"],當前執行的函數名稱.
1403
		#$result["function"],當前執行的函式名稱.
1154
		#$result["spiltString"][$i]["oriStr"],爲第i個字串的原始內容
1404
		#$result["argu"],所使用的參數.
1155
		#$result["spiltString"][$i]["dataArray"],爲第($i+1)個字串分割後的字串陣列
1405
		#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
1156
		#$result["spiltString"][$i]["dataArray"][$j],爲第($i+1)的分割好的字串的第($j+1)段內容
1406
		#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
1157
		#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
1407
		#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
1158
		#必填的參數:
1408
		#必填參數:
1159
		#$conf["stringIn"],字串陣列,要處理的字串陣列.
1409
		#$conf["input"],字串,要檢查的字串.
1160
		$conf["stringProcess::spiltMutiString"]["stringIn"]=$unProcesedStr;
1410
		$conf["search::findSpecifyStrFormat"]["input"]=$callShell["output"][0];
-
 
1411
		#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
1412
		$conf["search::findSpecifyStrFormat"]["format"]="\${month} \${day} \${hour}:\${min}:\${sec} \${doNotNeed}";
-
 
1413
		#可省略參數:
-
 
1414
		#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
1415
		#$conf["varEqual"]=array(null,"found");
1161
		#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
1416
		#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
1162
		$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=" ";
1417
		#$conf["varCon"]=array("no_tail"=>" not");
-
 
1418
		#參考資料:
-
 
1419
		#無.
-
 
1420
		#備註:
-
 
1421
		#無.
1163
		$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
1422
		$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
1164
		unset($conf["stringProcess::spiltMutiString"]);
1423
		unset($conf["search::findSpecifyStrFormat"]);
1165
		
1424
		
1166
		#如果分割失敗
1425
		#如果執行失敗
1167
		if($spiltMutiString["status"]==="false"){
1426
		if($findSpecifyStrFormat["status"]==="false"){
1168
		
1427
		
1169
			#設置執行失敗
1428
			#設置執行失敗
1170
			$result["status"]="false";
1429
			$result["status"]="false";
1171
 
1430
 
1172
			#設置錯誤訊息
1431
			#設置錯誤訊息
1173
			$result["error"]=$spiltMutiString;
1432
			$result["error"]=$findSpecifyStrFormat;
1174
 
1433
 
1175
			#印出結果
1434
			#回傳結果
1176
			var_dump($result);
1435
			var_dump($result);
1177
			
1436
			
1178
			#停止執行
1437
			#結束執行
-
 
1438
			exit(1);
-
 
1439
			
-
 
1440
			}#if end
-
 
1441
			
-
 
1442
		#如果log沒有時間戳記
-
 
1443
		if($findSpecifyStrFormat["found"]==="false"){
-
 
1444
		
-
 
1445
			#設置執行失敗
-
 
1446
			$result["status"]="false";
-
 
1447
 
-
 
1448
			#設置錯誤訊息
-
 
1449
			$result["error"]=$findSpecifyStrFormat;
-
 
1450
 
-
 
1451
			#回傳結果
-
 
1452
			var_dump($result);
-
 
1453
			
-
 
1454
			#結束執行
1179
			exit(1);
1455
			exit(1);
1180
			
1456
			
1181
			}#if end
1457
			}#if end
1182
		
1458
		
1183
		#記錄 smtpd log 最後變更的時間
1459
		#記錄 smtpd log 最後變更的時間
1184
		$smtpTimeFloat=$spiltMutiString["spiltString"][0]["dataArray"][2];
1460
		$smtpTimeFloat=strtotime($findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0].":".$findSpecifyStrFormat["parsedVar"]["sec"][0]);
1185
		
1461
		
1186
		}#if end	
1462
		}#if end
1187
	
-
 
1188
	#反之沒變動
-
 
1189
	else{
-
 
1190
	
1463
	
1191
		#沿用上次時間
-
 
1192
		$smtpTimeFloat=$params["info"]["preTimeFloat"]["smtp"];
-
 
1193
		
-
 
1194
		}#else ebd
-
 
1195
		
-
 
1196
	#透過運行以下指令取得最後變更時間
1464
	#透過運行以下指令取得最後變更時間
1197
	#journalctl -a -e | grep dovecot | grep " failed" | tail -n 1
1465
	#journalctl -a -e --unit=dovecot.service | tail -n 1
1198
	#涵式說明:
1466
	#涵式說明:
1199
	#呼叫shell執行系統命令,並取得回傳的內容.
1467
	#呼叫shell執行系統命令,並取得回傳的內容.
1200
	#回傳的結果:
1468
	#回傳的結果:
1201
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1469
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1202
	#$result["error"],錯誤訊息陣列.
1470
	#$result["error"],錯誤訊息陣列.
Line 1211... Line 1479...
1211
	#$result["statusCode"],執行結束後的代碼.
1479
	#$result["statusCode"],執行結束後的代碼.
1212
	#必填的參數
1480
	#必填的參數
1213
	#$conf["command"],字串,要執行的指令與.
1481
	#$conf["command"],字串,要執行的指令與.
1214
	$conf["external::callShell"]["command"]="journalctl";
1482
	$conf["external::callShell"]["command"]="journalctl";
1215
	#$conf["fileArgu"],字串,變數__FILE__的內容.
1483
	#$conf["fileArgu"],字串,變數__FILE__的內容.
1216
	$conf["external::callShell"]["fileArgu"]=__FILE__;		
1484
	$conf["external::callShell"]["fileArgu"]=__FILE__;
1217
	#可省略參數:
1485
	#可省略參數:
1218
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1486
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1219
	$conf["external::callShell"]["argu"]=array("-a","-e","|","grep","dovecot","|","grep"," failed","|","tail","-n","1");
1487
	$conf["external::callShell"]["argu"]=array("-a","-e","--unit=dovecot.service","|","tail","-n","1");
1220
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
1488
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
1221
	#$conf["arguIsAddr"]=array();	
1489
	#$conf["arguIsAddr"]=array();	
1222
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1490
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1223
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1491
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1224
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
1492
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
Line 1275... Line 1543...
1275
		}#if end
1543
		}#if end
1276
	
1544
	
1277
	#取得行數
1545
	#取得行數
1278
	$lineCount=count($callShell["output"]);
1546
	$lineCount=count($callShell["output"]);
1279
	
1547
	
-
 
1548
	#函式說明:
-
 
1549
	#多層if判斷.
-
 
1550
	#回傳結果:
-
 
1551
	#$result["status"],字串,"true"代表執行正常;"false"代表執行異常.
-
 
1552
	#$result["function"],當前函式的名稱.
-
 
1553
	#$result["pass"],字串,"true"代表通過多層if判斷;反之為"false".
-
 
1554
	#必填參數:
-
 
1555
	#$conf["get_defined_vars"],陣列,所有已經定義的變數.
-
 
1556
	$conf["controlStructures::nestedIf"]["get_defined_vars"]=get_defined_vars();
-
 
1557
	#$conf["varToControlLeft"],變數陣列,依照順序每層要做判斷的變數.
-
 
1558
	$conf["controlStructures::nestedIf"]["varToControlLeft"]=array("lineCount","callShell[\"output\"]");
-
 
1559
	#$conf["conditions"],字串陣列,依照順序每層變數判斷的條件,例如">","<",">=","<=","!=","==","<==","!==","===".
-
 
1560
	$conf["controlStructures::nestedIf"]["conditions"]=array("===","!==");
-
 
1561
	#$conf["varToControlRight"],變數陣列,依照順序每層變數判斷期望的結果.
-
 
1562
	$conf["controlStructures::nestedIf"]["varToControlRight"]=array(1,"-- No entries --");
-
 
1563
	#可省略參數:
-
 
1564
	#無.
-
 
1565
	#參考資料:
-
 
1566
	#無.
-
 
1567
	#備註:
-
 
1568
	#無.
-
 
1569
	$nestedIf=controlStructures::nestedIf($conf["controlStructures::nestedIf"]);
-
 
1570
	unset($conf["controlStructures::nestedIf"]);
-
 
1571
		
1280
	#如果行數等於1
1572
	#如果執行異常
-
 
1573
	if($nestedIf["status"]==="false"){
-
 
1574
	
-
 
1575
		#設置執行異常
-
 
1576
		$result["status"]="false";
-
 
1577
		
-
 
1578
		#設置執行錯誤
-
 
1579
		$result["error"]=$nestedIf;
-
 
1580
		
-
 
1581
		#回傳結果
-
 
1582
		return $result;
-
 
1583
	
-
 
1584
		}#if end
-
 
1585
	
-
 
1586
	#如果不通過判斷
1281
	if($lineCount===1){
1587
	if($nestedIf["pass"]==="false"){
-
 
1588
	
-
 
1589
		#沿用上次時間
-
 
1590
		$imapTimeFloat=$params["info"]["preTimeFloat"]["imap"];
-
 
1591
		
-
 
1592
		}#if end
-
 
1593
	
-
 
1594
	#反之
-
 
1595
	else{
1282
	
1596
	
1283
		#debug
1597
		#debug
1284
		if($debug){
1598
		if($debug){
1285
			
1599
			
1286
			#最新一筆惡意IP
1600
			#標題提示
1287
			echo "最新一筆惡意IP:".PHP_EOL;
1601
			echo "最新一筆log".PHP_EOL;
-
 
1602
			
-
 
1603
			#印出log
1288
			var_dump($callShell["output"]);
1604
			echo $callShell["output"][0].PHP_EOL;
1289
			
1605
			
1290
			}#if end
1606
			}#if end
1291
		
1607
		
1292
		#Feb 23 07:44:29 localhost.localdomain dovecot[1427]: imap-login: Disconnected (no auth attempts in 0 secs): user=<>, rip=101.136.215.61, lip=169.254.1.1, TLS handshaking: SSL_accept() failed: error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown: SSL alert number 46, session=<9oMMYvW7ihhliNc9>
1608
		#Feb 23 07:44:29 localhost.localdomain dovecot[1427]: imap-login: Disconnected (no auth attempts in 0 secs): user=<>, rip=101.136.215.61, lip=169.254.1.1, TLS handshaking: SSL_accept() failed: error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown: SSL alert number 46, session=<9oMMYvW7ihhliNc9>
1293
		$unProcesedStr=$callShell["output"];
1609
		#${month} ${day} ${hour}:${min}:${sec} ${doNotNeed}"
1294
		
1610
		
1295
		#分割字串
1611
		#解析時間點
1296
		#涵式說明:
1612
		#函式說明:
1297
		#將多個固定格式的字串分開,並回傳分開的結果
1613
		#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
1298
		#回傳的參數:
1614
		#回傳結果:
1299
		#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
1615
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1300
		#$result["error"],錯誤訊息陣列.
1616
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
1301
		#$result["function"],當前執行的函數名稱.
1617
		#$result["function"],當前執行的函式名稱.
1302
		#$result["spiltString"][$i]["oriStr"],爲第i個字串的原始內容
1618
		#$result["argu"],所使用的參數.
1303
		#$result["spiltString"][$i]["dataArray"],爲第($i+1)個字串分割後的字串陣列
1619
		#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
1304
		#$result["spiltString"][$i]["dataArray"][$j],爲第($i+1)的分割好的字串的第($j+1)段內容
1620
		#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
1305
		#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
1621
		#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
1306
		#必填的參數:
1622
		#必填參數:
1307
		#$conf["stringIn"],字串陣列,要處理的字串陣列.
1623
		#$conf["input"],字串,要檢查的字串.
1308
		$conf["stringProcess::spiltMutiString"]["stringIn"]=$unProcesedStr;
1624
		$conf["search::findSpecifyStrFormat"]["input"]=$callShell["output"][0];
-
 
1625
		#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
1626
		$conf["search::findSpecifyStrFormat"]["format"]="\${month} \${day} \${hour}:\${min}:\${sec} \${doNotNeed}";
-
 
1627
		#可省略參數:
-
 
1628
		#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
1629
		#$conf["varEqual"]=array(null,"found");
1309
		#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
1630
		#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
1310
		$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=" ";
1631
		#$conf["varCon"]=array("no_tail"=>" not");
-
 
1632
		#參考資料:
-
 
1633
		#無.
-
 
1634
		#備註:
-
 
1635
		#無.
1311
		$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
1636
		$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
1312
		unset($conf["stringProcess::spiltMutiString"]);
1637
		unset($conf["search::findSpecifyStrFormat"]);
1313
		
1638
		
1314
		#如果分割失敗
1639
		#如果執行失敗
1315
		if($spiltMutiString["status"]==="false"){
1640
		if($findSpecifyStrFormat["status"]==="false"){
1316
		
1641
		
1317
			#設置執行失敗
1642
			#設置執行失敗
1318
			$result["status"]="false";
1643
			$result["status"]="false";
1319
 
1644
 
1320
			#設置錯誤訊息
1645
			#設置錯誤訊息
1321
			$result["error"]=$spiltMutiString;
1646
			$result["error"]=$findSpecifyStrFormat;
1322
 
1647
 
1323
			#印出結果
1648
			#回傳結果
1324
			var_dump($result);
1649
			var_dump($result);
1325
			
1650
			
1326
			#停止執行
1651
			#結束執行
1327
			exit(1);
1652
			exit(1);
1328
			
1653
			
1329
			}#if end
1654
			}#if end
1330
			
1655
			
1331
		#記錄 smtpd log 最後變更的時間
1656
		#如果log沒有時間戳記
1332
		$imapTimeFloat=$spiltMutiString["spiltString"][0]["dataArray"][2];
1657
		if($findSpecifyStrFormat["found"]==="false"){
1333
		
1658
		
-
 
1659
			#設置執行失敗
-
 
1660
			$result["status"]="false";
-
 
1661
 
-
 
1662
			#設置錯誤訊息
-
 
1663
			$result["error"]=$findSpecifyStrFormat;
-
 
1664
 
-
 
1665
			#回傳結果
-
 
1666
			var_dump($result);
-
 
1667
			
-
 
1668
			#結束執行
-
 
1669
			exit(1);
-
 
1670
			
1334
		}#if end
1671
			}#if end
1335
		
1672
		
1336
	#反之沒變動
-
 
1337
	else{
-
 
1338
	
-
 
1339
		#沿用上次時間
1673
		#記錄 dovecot log 最後變更的時間
1340
		$imapTimeFloat=$params["info"]["preTimeFloat"]["imap"];
1674
		$imapTimeFloat=strtotime($findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0].":".$findSpecifyStrFormat["parsedVar"]["sec"][0]);
1341
		
1675
		
1342
		}#else ebd
1676
		}#if end
1343
	
1677
	
1344
	#透過 journalctl -a -e | grep named | tail -n 1 來取得最後一筆 dns 查詢記錄	
1678
	#透過 journalctl -a -e --unit=named.service | tail -n 1 來取得最後一筆 dns 查詢記錄
1345
	#涵式說明:
1679
	#涵式說明:
1346
	#呼叫shell執行系統命令,並取得回傳的內容.
1680
	#呼叫shell執行系統命令,並取得回傳的內容.
1347
	#回傳的結果:
1681
	#回傳的結果:
1348
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1682
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1349
	#$result["error"],錯誤訊息陣列.
1683
	#$result["error"],錯誤訊息陣列.
Line 1358... Line 1692...
1358
	#$result["statusCode"],執行結束後的代碼.
1692
	#$result["statusCode"],執行結束後的代碼.
1359
	#必填的參數
1693
	#必填的參數
1360
	#$conf["command"],字串,要執行的指令與.
1694
	#$conf["command"],字串,要執行的指令與.
1361
	$conf["external::callShell"]["command"]="journalctl";
1695
	$conf["external::callShell"]["command"]="journalctl";
1362
	#$conf["fileArgu"],字串,變數__FILE__的內容.
1696
	#$conf["fileArgu"],字串,變數__FILE__的內容.
1363
	$conf["external::callShell"]["fileArgu"]=__FILE__;		
1697
	$conf["external::callShell"]["fileArgu"]=__FILE__;
1364
	#可省略參數:
1698
	#可省略參數:
1365
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1699
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1366
	$conf["external::callShell"]["argu"]=array("-a","-e","|","grep"," named","|","tail","-n","1");
1700
	$conf["external::callShell"]["argu"]=array("-a","-e","--unit=named.service","|","tail","-n","1");
1367
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
1701
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
1368
	#$conf["arguIsAddr"]=array();	
1702
	#$conf["arguIsAddr"]=array();	
1369
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1703
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
1370
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1704
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
1371
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
1705
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
Line 1422... Line 1756...
1422
		}#if end
1756
		}#if end
1423
	
1757
	
1424
	#取得行數
1758
	#取得行數
1425
	$lineCount=count($callShell["output"]);
1759
	$lineCount=count($callShell["output"]);
1426
	
1760
	
-
 
1761
	#函式說明:
-
 
1762
	#多層if判斷.
-
 
1763
	#回傳結果:
-
 
1764
	#$result["status"],字串,"true"代表執行正常;"false"代表執行異常.
-
 
1765
	#$result["function"],當前函式的名稱.
-
 
1766
	#$result["pass"],字串,"true"代表通過多層if判斷;反之為"false".
-
 
1767
	#必填參數:
-
 
1768
	#$conf["get_defined_vars"],陣列,所有已經定義的變數.
-
 
1769
	$conf["controlStructures::nestedIf"]["get_defined_vars"]=get_defined_vars();
-
 
1770
	#$conf["varToControlLeft"],變數陣列,依照順序每層要做判斷的變數.
-
 
1771
	$conf["controlStructures::nestedIf"]["varToControlLeft"]=array("lineCount","callShell[\"output\"]");
-
 
1772
	#$conf["conditions"],字串陣列,依照順序每層變數判斷的條件,例如">","<",">=","<=","!=","==","<==","!==","===".
-
 
1773
	$conf["controlStructures::nestedIf"]["conditions"]=array("===","!==");
-
 
1774
	#$conf["varToControlRight"],變數陣列,依照順序每層變數判斷期望的結果.
-
 
1775
	$conf["controlStructures::nestedIf"]["varToControlRight"]=array(1,"-- No entries --");
-
 
1776
	#可省略參數:
-
 
1777
	#無.
-
 
1778
	#參考資料:
-
 
1779
	#無.
-
 
1780
	#備註:
-
 
1781
	#無.
-
 
1782
	$nestedIf=controlStructures::nestedIf($conf["controlStructures::nestedIf"]);
-
 
1783
	unset($conf["controlStructures::nestedIf"]);
-
 
1784
		
1427
	#如果行數等於1
1785
	#如果執行異常
-
 
1786
	if($nestedIf["status"]==="false"){
-
 
1787
	
-
 
1788
		#設置執行異常
-
 
1789
		$result["status"]="false";
-
 
1790
		
-
 
1791
		#設置執行錯誤
-
 
1792
		$result["error"]=$nestedIf;
-
 
1793
		
-
 
1794
		#回傳結果
-
 
1795
		return $result;
-
 
1796
	
-
 
1797
		}#if end
-
 
1798
	
-
 
1799
	#如果不通過判斷
1428
	if($lineCount===1){
1800
	if($nestedIf["pass"]==="false"){
-
 
1801
	
-
 
1802
		#沿用上次的時間
-
 
1803
		$namedTimeFloat=$params["info"]["preTimeFloat"]["named"];
-
 
1804
	
-
 
1805
		}#if end
-
 
1806
	
-
 
1807
	#反之
-
 
1808
	else{
1429
	
1809
	
1430
		#debug
1810
		#debug
1431
		if($debug){
1811
		if($debug){
1432
			
1812
			
1433
			#最新一筆惡意IP
1813
			#標題提示
1434
			echo "最新一筆惡意IP:".PHP_EOL;
1814
			echo "最新一筆log".PHP_EOL;
-
 
1815
			
-
 
1816
			#印出log
1435
			var_dump($callShell["output"]);
1817
			echo $callShell["output"][0].PHP_EOL;
1436
			
1818
			
1437
			}#if end
1819
			}#if end
-
 
1820
			
-
 
1821
		#Nov 24 20:55:15 localhost.localdomain named[99512]: client @0x7f7714041d10 191.7.219.100#6238 (PEACECORPS.GOV): query (cache) 'PEACECORPS.GOV/ANY/IN' denied
-
 
1822
		#${month} ${day} ${hour}:${min}:${sec} ${doNotNeed}
1438
		
1823
		
1439
		#Aug 09 07:12:09 www.qbpwcf.org postfix/smtpd[20829]: connect from unknown[185.234.219.62]
-
 
1440
		$unProcesedStr=$callShell["output"];
-
 
1441
		
-
 
1442
		#分割字串
1824
		#解析時間點
1443
		#涵式說明:
1825
		#函式說明:
1444
		#將多個固定格式的字串分開,並回傳分開的結果
1826
		#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
1445
		#回傳的參數:
1827
		#回傳結果:
1446
		#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
1828
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1447
		#$result["error"],錯誤訊息陣列.
1829
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
1448
		#$result["function"],當前執行的函數名稱.
1830
		#$result["function"],當前執行的函式名稱.
1449
		#$result["spiltString"][$i]["oriStr"],爲第i個字串的原始內容
1831
		#$result["argu"],所使用的參數.
1450
		#$result["spiltString"][$i]["dataArray"],爲第($i+1)個字串分割後的字串陣列
1832
		#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
1451
		#$result["spiltString"][$i]["dataArray"][$j],爲第($i+1)的分割好的字串的第($j+1)段內容
1833
		#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
1452
		#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
1834
		#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
1453
		#必填的參數:
1835
		#必填參數:
1454
		#$conf["stringIn"],字串陣列,要處理的字串陣列.
1836
		#$conf["input"],字串,要檢查的字串.
1455
		$conf["stringProcess::spiltMutiString"]["stringIn"]=$unProcesedStr;
1837
		$conf["search::findSpecifyStrFormat"]["input"]=$callShell["output"][0];
-
 
1838
		#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
1839
		$conf["search::findSpecifyStrFormat"]["format"]="\${month} \${day} \${hour}:\${min}:\${sec} \${doNotNeed}";
-
 
1840
		#可省略參數:
-
 
1841
		#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
1842
		#$conf["varEqual"]=array(null,"found");
1456
		#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
1843
		#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
1457
		$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=" ";
1844
		#$conf["varCon"]=array("no_tail"=>" not");
-
 
1845
		#參考資料:
-
 
1846
		#無.
-
 
1847
		#備註:
-
 
1848
		#無.
1458
		$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
1849
		$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
1459
		unset($conf["stringProcess::spiltMutiString"]);
1850
		unset($conf["search::findSpecifyStrFormat"]);
1460
		
1851
		
1461
		#如果分割失敗
1852
		#如果執行失敗
1462
		if($spiltMutiString["status"]==="false"){
1853
		if($findSpecifyStrFormat["status"]==="false"){
1463
		
1854
		
1464
			#設置執行失敗
1855
			#設置執行失敗
1465
			$result["status"]="false";
1856
			$result["status"]="false";
1466
 
1857
 
1467
			#設置錯誤訊息
1858
			#設置錯誤訊息
1468
			$result["error"]=$spiltMutiString;
1859
			$result["error"]=$findSpecifyStrFormat;
1469
 
1860
 
1470
			#印出結果
1861
			#回傳結果
1471
			var_dump($result);
1862
			var_dump($result);
1472
			
1863
			
1473
			#停止執行
1864
			#結束執行
1474
			exit(1);
1865
			exit(1);
1475
			
1866
			
1476
			}#if end
1867
			}#if end
1477
			
1868
			
-
 
1869
		#如果log沒有時間戳記
-
 
1870
		if($findSpecifyStrFormat["found"]==="false"){
-
 
1871
		
-
 
1872
			#設置執行失敗
-
 
1873
			$result["status"]="false";
-
 
1874
 
-
 
1875
			#設置錯誤訊息
-
 
1876
			$result["error"]=$findSpecifyStrFormat;
-
 
1877
 
-
 
1878
			#回傳結果
-
 
1879
			var_dump($result);
-
 
1880
			
-
 
1881
			#結束執行
-
 
1882
			exit(1);
-
 
1883
			
-
 
1884
			}#if end
-
 
1885
		
1478
		#記錄 named log 最後變更的時間
1886
		#記錄 named log 最後變更的時間
1479
		$namedTimeFloat=$spiltMutiString["spiltString"][0]["dataArray"][2];	
1887
		$namedTimeFloat=strtotime($findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0].":".$findSpecifyStrFormat["parsedVar"]["sec"][0]);
-
 
1888
		
-
 
1889
		}#else end
-
 
1890
	
-
 
1891
	#透過 journalctl -a -e --unit=sshd.service | tail -n 1 來取得最後一筆 dns 查詢記錄
-
 
1892
	#涵式說明:
-
 
1893
	#呼叫shell執行系統命令,並取得回傳的內容.
-
 
1894
	#回傳的結果:
-
 
1895
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
1896
	#$result["error"],錯誤訊息陣列.
-
 
1897
	#$result["function"],當前執行的函數名稱.
-
 
1898
	#$result["argu"],使用的參數.
-
 
1899
	#$result["cmd"],執行的指令內容.
-
 
1900
	#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
-
 
1901
	#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
-
 
1902
	#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
-
 
1903
	#$result["running"],是否還在執行.
-
 
1904
	#$result["pid"],pid.
-
 
1905
	#$result["statusCode"],執行結束後的代碼.
-
 
1906
	#必填的參數
-
 
1907
	#$conf["command"],字串,要執行的指令與.
-
 
1908
	$conf["external::callShell"]["command"]="journalctl";
-
 
1909
	#$conf["fileArgu"],字串,變數__FILE__的內容.
-
 
1910
	$conf["external::callShell"]["fileArgu"]=__FILE__;
-
 
1911
	#可省略參數:
-
 
1912
	#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
-
 
1913
	$conf["external::callShell"]["argu"]=array("-a","-e","--unit=sshd.service","|","tail","-n","1");
-
 
1914
	#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
-
 
1915
	#$conf["arguIsAddr"]=array();	
-
 
1916
	#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
-
 
1917
	#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
-
 
1918
	#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
-
 
1919
	#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
-
 
1920
	#$conf["enablePrintDescription"]="true";
-
 
1921
	#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
-
 
1922
	#$conf["printDescription"]="";
-
 
1923
	#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
-
 
1924
	$conf["external::callShell"]["escapeshellarg"]="true";
-
 
1925
	#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
-
 
1926
	#$conf["username"]="";
-
 
1927
	#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
-
 
1928
	#$conf["password"]="";
-
 
1929
	#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
-
 
1930
	#$conf["useScript"]="";
-
 
1931
	#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
-
 
1932
	#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
-
 
1933
	#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
-
 
1934
	#$conf["inBackGround"]="";
-
 
1935
	#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
-
 
1936
	#$conf["getErr"]="false";
-
 
1937
	#備註:
-
 
1938
	#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
-
 
1939
	#參考資料:
-
 
1940
	#exec=>http://php.net/manual/en/function.exec.php
-
 
1941
	#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
-
 
1942
	#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
-
 
1943
	$callShell=external::callShell($conf["external::callShell"]);
-
 
1944
	unset($conf["external::callShell"]);
-
 
1945
	
-
 
1946
	#如果執行失敗
-
 
1947
	if($callShell["status"]==="false"){
-
 
1948
	
-
 
1949
		#設置執行失敗
-
 
1950
		$result["status"]="false";
-
 
1951
 
-
 
1952
		#設置錯誤訊息
-
 
1953
		$result["error"]=$callShell;
-
 
1954
 
-
 
1955
		#回傳結果
-
 
1956
		var_dump($result);
-
 
1957
		
-
 
1958
		#結束執行
-
 
1959
		exit(1);
1480
		
1960
		
1481
		}#if end
1961
		}#if end
-
 
1962
	
-
 
1963
	#debug
-
 
1964
	if($debug){
1482
		
1965
		
-
 
1966
		#comment
-
 
1967
		echo "accessed sshd log".PHP_EOL;
-
 
1968
		
-
 
1969
		}#if end
-
 
1970
	
-
 
1971
	#取得行數
-
 
1972
	$lineCount=count($callShell["output"]);
-
 
1973
	
-
 
1974
	#函式說明:
-
 
1975
	#多層if判斷.
-
 
1976
	#回傳結果:
-
 
1977
	#$result["status"],字串,"true"代表執行正常;"false"代表執行異常.
-
 
1978
	#$result["function"],當前函式的名稱.
-
 
1979
	#$result["pass"],字串,"true"代表通過多層if判斷;反之為"false".
-
 
1980
	#必填參數:
-
 
1981
	#$conf["get_defined_vars"],陣列,所有已經定義的變數.
-
 
1982
	$conf["controlStructures::nestedIf"]["get_defined_vars"]=get_defined_vars();
-
 
1983
	#$conf["varToControlLeft"],變數陣列,依照順序每層要做判斷的變數.
-
 
1984
	$conf["controlStructures::nestedIf"]["varToControlLeft"]=array("lineCount","callShell[\"output\"]");
-
 
1985
	#$conf["conditions"],字串陣列,依照順序每層變數判斷的條件,例如">","<",">=","<=","!=","==","<==","!==","===".
-
 
1986
	$conf["controlStructures::nestedIf"]["conditions"]=array("===","!==");
-
 
1987
	#$conf["varToControlRight"],變數陣列,依照順序每層變數判斷期望的結果.
-
 
1988
	$conf["controlStructures::nestedIf"]["varToControlRight"]=array(1,"-- No entries --");
1483
	#反之無變動
1989
	#可省略參數:
-
 
1990
	#無.
-
 
1991
	#參考資料:
-
 
1992
	#無.
-
 
1993
	#備註:
1484
	else{
1994
	#無.
-
 
1995
	$nestedIf=controlStructures::nestedIf($conf["controlStructures::nestedIf"]);
-
 
1996
	unset($conf["controlStructures::nestedIf"]);
-
 
1997
		
-
 
1998
	#如果執行異常
-
 
1999
	if($nestedIf["status"]==="false"){
-
 
2000
	
-
 
2001
		#設置執行異常
-
 
2002
		$result["status"]="false";
-
 
2003
		
-
 
2004
		#設置執行錯誤
-
 
2005
		$result["error"]=$nestedIf;
-
 
2006
		
-
 
2007
		#回傳結果
-
 
2008
		return $result;
-
 
2009
	
-
 
2010
		}#if end
-
 
2011
	
-
 
2012
	#如果不通過判斷
-
 
2013
	if($nestedIf["pass"]==="false"){
1485
 
2014
	
1486
		#沿用上次的時間
2015
		#沿用上次的時間
1487
		$namedTimeFloat=$params["info"]["preTimeFloat"]["named"];
2016
		$sshdTimeFloat=$params["info"]["preTimeFloat"]["sshd"];
-
 
2017
		
-
 
2018
		}#if end
1488
	
2019
	
-
 
2020
	#反之
-
 
2021
	else{
-
 
2022
	
-
 
2023
		#debug
-
 
2024
		if($debug){
-
 
2025
			
-
 
2026
			#標題提示
-
 
2027
			echo "最新一筆log".PHP_EOL;
-
 
2028
			
-
 
2029
			#印出log
-
 
2030
			echo $callShell["output"][0].PHP_EOL;
-
 
2031
			
-
 
2032
			}#if end
-
 
2033
		
-
 
2034
		#Sep 30 00:36:36 silverblue-guest.qbpwcf.org sshd-session[1607010]: Invalid user lizepeng from 60.213.10.69 port 39156
-
 
2035
		#${month} ${day} ${hour}:${min}:${sec} ${doNotNeed}
-
 
2036
		
-
 
2037
		#解析時間點
-
 
2038
		#函式說明:
-
 
2039
		#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
-
 
2040
		#回傳結果:
-
 
2041
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
2042
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
-
 
2043
		#$result["function"],當前執行的函式名稱.
-
 
2044
		#$result["argu"],所使用的參數.
-
 
2045
		#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
-
 
2046
		#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
-
 
2047
		#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
-
 
2048
		#必填參數:
-
 
2049
		#$conf["input"],字串,要檢查的字串.
-
 
2050
		$conf["search::findSpecifyStrFormat"]["input"]=$callShell["output"][0];
-
 
2051
		#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
2052
		$conf["search::findSpecifyStrFormat"]["format"]="\${month} \${day} \${hour}:\${min}:\${sec} \${doNotNeed}";
-
 
2053
		#可省略參數:
-
 
2054
		#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
2055
		#$conf["varEqual"]=array(null,"found");
1489
		}#else end
2056
		#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
-
 
2057
		#$conf["varCon"]=array("no_tail"=>" not");
-
 
2058
		#參考資料:
-
 
2059
		#無.
-
 
2060
		#備註:
-
 
2061
		#無.
-
 
2062
		$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
-
 
2063
		unset($conf["search::findSpecifyStrFormat"]);
-
 
2064
		
-
 
2065
		#如果執行失敗
-
 
2066
		if($findSpecifyStrFormat["status"]==="false"){
-
 
2067
		
-
 
2068
			#設置執行失敗
-
 
2069
			$result["status"]="false";
-
 
2070
 
-
 
2071
			#設置錯誤訊息
-
 
2072
			$result["error"]=$findSpecifyStrFormat;
-
 
2073
 
-
 
2074
			#回傳結果
-
 
2075
			var_dump($result);
-
 
2076
			
-
 
2077
			#結束執行
-
 
2078
			exit(1);
-
 
2079
			
-
 
2080
			}#if end
-
 
2081
			
-
 
2082
		#如果log沒有時間戳記
-
 
2083
		if($findSpecifyStrFormat["found"]==="false"){
-
 
2084
		
-
 
2085
			#設置執行失敗
-
 
2086
			$result["status"]="false";
-
 
2087
 
-
 
2088
			#設置錯誤訊息
-
 
2089
			$result["error"]=$findSpecifyStrFormat;
-
 
2090
 
-
 
2091
			#回傳結果
-
 
2092
			var_dump($result);
-
 
2093
			
-
 
2094
			#結束執行
-
 
2095
			exit(1);
-
 
2096
			
-
 
2097
			}#if end
1490
		
2098
		
-
 
2099
		#記錄 sshd log 最後變更的時間
-
 
2100
		$sshdTimeFloat=strtotime($findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0].":".$findSpecifyStrFormat["parsedVar"]["sec"][0]);
-
 
2101
		
-
 
2102
		}#else end
-
 
2103
	
1491
	#debug mode
2104
	#debug mode
1492
	if($debug){
2105
	if($debug){
1493
 
2106
 
1494
		#comment
2107
		#comment
1495
		echo "\$httpTimeFloat:".$httpTimeFloat.PHP_EOL;
2108
		echo "\$httpTimeFloat:".$httpTimeFloat.PHP_EOL;
1496
		echo "\$httpsTimeFloat:".$httpsTimeFloat.PHP_EOL;
2109
		echo "\$httpsTimeFloat:".$httpsTimeFloat.PHP_EOL;
1497
		echo "\$smtpTimeFloat:".$smtpTimeFloat.PHP_EOL;
2110
		echo "\$smtpTimeFloat:".$smtpTimeFloat.PHP_EOL;
1498
		echo "\$imapTimeFloat:".$imapTimeFloat.PHP_EOL;
2111
		echo "\$imapTimeFloat:".$imapTimeFloat.PHP_EOL;
1499
		echo "\$namedTimeFloat:".$namedTimeFloat.PHP_EOL;
2112
		echo "\$namedTimeFloat:".$namedTimeFloat.PHP_EOL;
-
 
2113
		echo "\$sshdTimeFloat:".$sshdTimeFloat.PHP_EOL;
1500
 
2114
 
1501
		}#if end
2115
		}#if end
1502
 
2116
 
1503
	#第一次運行,不做事.
2117
	#第一次運行,不做事.
1504
	if($params["info"]["preTimeFloat"]["http"]===0 && $params["info"]["preTimeFloat"]["https"]===0 && $params["info"]["preTimeFloat"]["smtp"]===0 && $params["info"]["preTimeFloat"]["imap"]===0 && $params["info"]["preTimeFloat"]["named"]===0){
2118
	if($params["info"]["preTimeFloat"]["http"]===0 && $params["info"]["preTimeFloat"]["https"]===0 && $params["info"]["preTimeFloat"]["smtp"]===0 && $params["info"]["preTimeFloat"]["imap"]===0 && $params["info"]["preTimeFloat"]["named"]===0){
Line 1516... Line 2130...
1516
		$params["info"]["preTimeFloat"]["imap"]=$imapTimeFloat;
2130
		$params["info"]["preTimeFloat"]["imap"]=$imapTimeFloat;
1517
		
2131
		
1518
		#將本次nmaed記錄覆寫到上次的記錄
2132
		#將本次nmaed記錄覆寫到上次的記錄
1519
		$params["info"]["preTimeFloat"]["named"]=$namedTimeFloat;
2133
		$params["info"]["preTimeFloat"]["named"]=$namedTimeFloat;
1520
		
2134
		
-
 
2135
		#將本次sshd記錄覆寫到上次的記錄
-
 
2136
		$params["info"]["preTimeFloat"]["sshd"]=$sshdTimeFloat;
-
 
2137
		
1521
		#睡一秒
2138
		#睡一秒
1522
		sleep(1);
2139
		sleep(1);
1523
 
2140
 
1524
		#debug mode
2141
		#debug mode
1525
		if($debug){
2142
		if($debug){
Line 1533... Line 2150...
1533
		return;
2150
		return;
1534
	
2151
	
1535
		}#if end
2152
		}#if end
1536
		
2153
		
1537
	#非第一次運行,且 http 與 https 與 imap 與 smtp 與 named log 都沒變動過.
2154
	#非第一次運行,且 http 與 https 與 imap 與 smtp 與 named log 都沒變動過.
1538
	else if($params["info"]["preTimeFloat"]["http"]===$httpTimeFloat && $params["info"]["preTimeFloat"]["https"]===$httpsTimeFloat && $params["info"]["preTimeFloat"]["smtp"]===$smtpTimeFloat && $params["info"]["preTimeFloat"]["imap"]===$imapTimeFloat && $params["info"]["preTimeFloat"]["named"]===$namedTimeFloat ){
2155
	else if($params["info"]["preTimeFloat"]["http"]===$httpTimeFloat && $params["info"]["preTimeFloat"]["https"]===$httpsTimeFloat && $params["info"]["preTimeFloat"]["smtp"]===$smtpTimeFloat && $params["info"]["preTimeFloat"]["imap"]===$imapTimeFloat && $params["info"]["preTimeFloat"]["named"]===$namedTimeFloat && $params["info"]["preTimeFloat"]["sshd"]===$sshdTimeFloat){
1539
	
2156
	
1540
		#睡一秒
2157
		#睡一秒
1541
		sleep(1);
2158
		sleep(1);
1542
		
2159
		
1543
		#debug mode
2160
		#debug mode
Line 1561... Line 2178...
1561
		echo "\$preHttpTimeFloat:".$params["info"]["preTimeFloat"]["http"].PHP_EOL;
2178
		echo "\$preHttpTimeFloat:".$params["info"]["preTimeFloat"]["http"].PHP_EOL;
1562
		echo "\$preHttpsTimeFloat:".$params["info"]["preTimeFloat"]["https"].PHP_EOL;
2179
		echo "\$preHttpsTimeFloat:".$params["info"]["preTimeFloat"]["https"].PHP_EOL;
1563
		echo "\$preSmtpTimeFloat:".$params["info"]["preTimeFloat"]["smtp"].PHP_EOL;
2180
		echo "\$preSmtpTimeFloat:".$params["info"]["preTimeFloat"]["smtp"].PHP_EOL;
1564
		echo "\$preImapTimeFloat:".$params["info"]["preTimeFloat"]["imap"].PHP_EOL;
2181
		echo "\$preImapTimeFloat:".$params["info"]["preTimeFloat"]["imap"].PHP_EOL;
1565
		echo "\$preNamedTimeFloat:".$params["info"]["preTimeFloat"]["named"].PHP_EOL;
2182
		echo "\$preNamedTimeFloat:".$params["info"]["preTimeFloat"]["named"].PHP_EOL;
-
 
2183
		echo "\$preSshdTimeFloat:".$params["info"]["preTimeFloat"]["sshd"].PHP_EOL;
1566
		echo "\$httpTimeFloat:".$httpTimeFloat.PHP_EOL;
2184
		echo "\$httpTimeFloat:".$httpTimeFloat.PHP_EOL;
1567
		echo "\$httpsTimeFloat:".$httpsTimeFloat.PHP_EOL;
2185
		echo "\$httpsTimeFloat:".$httpsTimeFloat.PHP_EOL;
1568
		echo "\$smtpTimeFloat:".$smtpTimeFloat.PHP_EOL;
2186
		echo "\$smtpTimeFloat:".$smtpTimeFloat.PHP_EOL;
1569
		echo "\$imapTimeFloat:".$imapTimeFloat.PHP_EOL;
2187
		echo "\$imapTimeFloat:".$imapTimeFloat.PHP_EOL;
1570
		echo "\$namedTimeFloat:".$namedTimeFloat.PHP_EOL;
2188
		echo "\$namedTimeFloat:".$namedTimeFloat.PHP_EOL;
-
 
2189
		echo "\$sshdTimeFloat:".$sshdTimeFloat.PHP_EOL;
1571
 
2190
 
1572
		}#if end
2191
		}#if end
1573
 
2192
 
1574
	#將本次http記錄覆寫到上次的記錄
2193
	#將本次http記錄覆寫到上次的記錄
1575
	$params["info"]["preTimeFloat"]["http"]=$httpTimeFloat;
2194
	$params["info"]["preTimeFloat"]["http"]=$httpTimeFloat;
1576
	
2195
	
1577
	#將本次https記錄覆寫到上次的記錄
2196
	#將本次https記錄覆寫到上次的記錄
1578
	$params["info"]["preTimeFloat"]["https"]=$httpsTimeFloat;
2197
	$params["info"]["preTimeFloat"]["https"]=$httpsTimeFloat;
1579
	
2198
	
-
 
2199
	#將本次smtp記錄覆寫到上次的記錄
-
 
2200
	$params["info"]["preTimeFloat"]["smtp"]=$smtpTimeFloat;
-
 
2201
	
-
 
2202
	#將本次imap記錄覆寫到上次的記錄
-
 
2203
	$params["info"]["preTimeFloat"]["imap"]=$imapTimeFloat;
-
 
2204
	
-
 
2205
	#將本次named記錄覆寫到上次的記錄
-
 
2206
	$params["info"]["preTimeFloat"]["named"]=$namedTimeFloat;
-
 
2207
	
-
 
2208
	#將本次sshd記錄覆寫到上次的記錄
-
 
2209
	$params["info"]["preTimeFloat"]["sshd"]=$sshdTimeFloat;
-
 
2210
	
1580
	#初始化儲存暫時ip白名單的陣列
2211
	#初始化儲存暫時ip白名單的陣列
1581
	$excludeIpsTmp=array();
2212
	$excludeIpsTmp=array();
1582
	
2213
	
1583
	#針對每個暫時白名單ip
2214
	#針對每個暫時白名單ip
1584
	foreach($params["info"]["excludeIpsTmp"] as $index=>$tmpIpInfo){
2215
	foreach($params["info"]["excludeIpsTmp"] as $index=>$tmpIpInfo){
Line 1592... Line 2223...
1592
			}#if end
2223
			}#if end
1593
			
2224
			
1594
		#反之
2225
		#反之
1595
		else{
2226
		else{
1596
		
2227
		
1597
			#變成 $excludeIpsTmp 的內容	
2228
			#變成 $excludeIpsTmp 的內容
1598
			$excludeIpsTmp[]=$params["info"]["excludeIpsTmp"][$index]["ip"];
2229
			$excludeIpsTmp[]=$params["info"]["excludeIpsTmp"][$index]["ip"];
1599
		
2230
		
1600
			}#else end
2231
			}#else end
1601
		
2232
		
1602
		}#foreach end
2233
		}#foreach end
Line 1608... Line 2239...
1608
	#$result["error"],錯誤訊息陣列.
2239
	#$result["error"],錯誤訊息陣列.
1609
	#$result["function"],當前執行的函數.
2240
	#$result["function"],當前執行的函數.
1610
	#$result["content"],合併好的一維陣列.
2241
	#$result["content"],合併好的一維陣列.
1611
	#必填參數
2242
	#必填參數
1612
	#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
2243
	#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
1613
	$conf["inputArray"]=array($params["info"]["excludeIp"],$excludeIpsTmp);
2244
	$conf["arrays::mergeMultiDimensionArray"]["inputArray"]=array($params["info"]["excludeIp"],$excludeIpsTmp);
1614
	#可省略參數:
2245
	#可省略參數:
1615
	#$conf["allowRepeat"],字串,預設為"false",不允許重複的結果;反之為"true".
2246
	#$conf["allowRepeat"],字串,預設為"false",不允許重複的結果;反之為"true".
1616
	#$conf["allowRepeat"]="true";
2247
	#$conf["allowRepeat"]="true";
1617
	#$conf["equalKeyStruc"],字串陣列,若 allowRepeat 參數為 "false", 這該參數生效.該參數為用來判斷每個陣列的哪個鍵值路徑底下的數值相同時要進行取代,後者會取代前者.
2248
	#$conf["equalKeyStruc"],字串陣列,若 allowRepeat 參數為 "false", 這該參數生效.該參數為用來判斷每個陣列的哪個鍵值路徑底下的數值相同時要進行取代,後者會取代前者.
1618
	#$conf["equalKeyStruc"]=array();
2249
	#$conf["equalKeyStruc"]=array();
1619
	#參考資料:
2250
	#參考資料:
1620
	#無.
2251
	#無.
1621
	#備註:
2252
	#備註:
1622
	#無.
2253
	#無.
1623
	$mergeMultiDimensionArray=arrays::mergeMultiDimensionArray($conf);
2254
	$mergeMultiDimensionArray=arrays::mergeMultiDimensionArray($conf["arrays::mergeMultiDimensionArray"]);
1624
	unset($conf);
2255
	unset($conf["arrays::mergeMultiDimensionArray"]);
1625
	
2256
	
1626
	#如果執行失敗
2257
	#如果執行失敗
1627
	if($mergeMultiDimensionArray["status"]==="false"){
2258
	if($mergeMultiDimensionArray["status"]==="false"){
1628
	
2259
	
1629
		#設置執行失敗
2260
		#設置執行失敗
Line 1640... Line 2271...
1640
		
2271
		
1641
		}#if end
2272
		}#if end
1642
		
2273
		
1643
	#debug
2274
	#debug
1644
	#var_dump(__LINE__,$mergeMultiDimensionArray);
2275
	#var_dump(__LINE__,$mergeMultiDimensionArray);
1645
		
2276
	
1646
	#涵式說明:
2277
	#涵式說明:
1647
	#檢查 httpd 與 postfix/smtpd 的 log 與 named 的 log 與 sshd 的 log 把惡意連線的 IP 用防火牆阻阻擋
2278
	#檢查 httpd 與 postfix/smtpd 的 log 與 named 的 log 與 sshd 的 log 把惡意連線的 IP 用防火牆阻擋
1648
	#回傳結果:
2279
	#回傳結果:
1649
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
2280
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1650
	#$result["error"],錯誤訊息.
2281
	#$result["error"],錯誤訊息.
1651
	#$result["function"],當前執行的函數名稱.
2282
	#$result["function"],當前執行的函數名稱.
1652
	#$result["argu"],所使用的參數.
2283
	#$result["argu"],所使用的參數.
1653
	#$result["found"],是否有找到符合的檔案,"true"代表有;"false"代表沒有.
2284
	#$result["found"],是否有找到符合的檔案,"true"代表有;"false"代表沒有.
1654
	#$result["content"],找到的檔案陣列.
2285
	#$result["content"],找到的檔案陣列.
1655
	#必填參數:
2286
	#必填參數:
1656
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
2287
	#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
1657
	$conf["fileArgu"]=__FILE__;
2288
	$conf["cmd::blockAcctackIp"]["fileArgu"]=__FILE__;
1658
	#可省略參數:
2289
	#可省略參數:
1659
	#$conf["excludeIp"],字串陣列,白名單ip陣列.
2290
	#$conf["excludeIp"],字串陣列,白名單ip陣列.
1660
	$conf["excludeIp"]=$mergeMultiDimensionArray["content"];
2291
	$conf["cmd::blockAcctackIp"]["excludeIp"]=$mergeMultiDimensionArray["content"];
1661
	#$conf["logPath"],字串,httpd的log位置,預設為 "/var/log/httpd"
2292
	#$conf["logPath"],字串,httpd的log位置,預設為 "/var/log/httpd"
1662
	#$conf["logPath"]="";
2293
	#$conf["logPath"]="";
1663
	#$conf["username"],字串,要用什麼使用者來執行,預設為root使用者
2294
	#$conf["username"],字串,要用什麼使用者來執行,預設為root使用者
1664
	#$conf["username"]="";
2295
	#$conf["username"]="";
1665
	#$conf["password"],字串,root使用者的密碼,
2296
	#$conf["password"],字串,root使用者的密碼,
1666
	#$conf["password"]="";
2297
	#$conf["password"]="";
1667
	#$conf["getIplistOnly"],字串,是否不阻擋IP只取得有問題的IP,預設為"false",要阻擋IP;"true"代表只取得有問題的IP.
2298
	#$conf["getIplistOnly"],字串,是否不阻擋IP只取得有問題的IP,預設為"false",要阻擋IP;"true"代表只取得有問題的IP.
1668
	#$conf["getIplistOnly"]="true";
2299
	#$conf["getIplistOnly"]="true";
1669
	$blockAcctackIp=cmd::blockAcctackIp($conf);
2300
	$blockAcctackIp=cmd::blockAcctackIp($conf["cmd::blockAcctackIp"]);
1670
	unset($conf);
2301
	unset($conf["cmd::blockAcctackIp"]);
1671
 
2302
 
1672
	#如果執行失敗
2303
	#如果執行失敗
1673
	if($blockAcctackIp["status"]==="false"){
2304
	if($blockAcctackIp["status"]==="false"){
1674
 
2305
 
1675
		#設置執行失敗
2306
		#設置執行失敗