Subversion Repositories php-qbpwcf

Rev

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

Rev 168 Rev 198
Line 22535... Line 22535...
22535
 
22535
 
22536
		}#function find end
22536
		}#function find end
22537
		
22537
		
22538
	/*
22538
	/*
22539
	#函式說明:
22539
	#函式說明:
22540
	#檢查 httpd 與 postfix/smtpd 的 log 與 named 的 log 與 sshd 的 log 把惡意連線的 IP 用防火牆阻阻擋
22540
	#檢查 httpd 與 postfix/smtp 與 dovecot/imap 與 named 與 sshd 的 log 把惡意連線的 IP 用防火牆阻阻擋.
22541
	#回傳結果:
22541
	#回傳結果:
22542
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
22542
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
22543
	#$result["error"],錯誤訊息.
22543
	#$result["error"],錯誤訊息.
22544
	#$result["function"],當前執行的函式名稱.
22544
	#$result["function"],當前執行的函式名稱.
22545
	#$result["argu"],所使用的參數.
22545
	#$result["argu"],所使用的參數.
Line 22734... Line 22734...
22734
			#回傳結果
22734
			#回傳結果
22735
			return $result;
22735
			return $result;
22736
		
22736
		
22737
			}#if end
22737
			}#if end
22738
		
22738
		
22739
		#過濾掉對於含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.  	
22739
		#過濾掉對於含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.
22740
		#函式說明:
22740
		#函式說明:
22741
		#檢查多個字串中的每個字串是否有多個關鍵字
22741
		#檢查多個字串中的每個字串是否有多個關鍵字
22742
		#回傳的結果:
22742
		#回傳的結果:
22743
		#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
22743
		#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
22744
		#$result["function"],當前執行的函式名稱.
22744
		#$result["function"],當前執行的函式名稱.
Line 22840... Line 22840...
22840
			$info["ip"]=$spiltMutiString["spiltString"][$i]["dataArray"][0];
22840
			$info["ip"]=$spiltMutiString["spiltString"][$i]["dataArray"][0];
22841
				
22841
				
22842
			#取得可能有問題的IP位置資訊
22842
			#取得可能有問題的IP位置資訊
22843
			$ips_byIp[$spiltMutiString["spiltString"][$i]["dataArray"][0]][]=$info;
22843
			$ips_byIp[$spiltMutiString["spiltString"][$i]["dataArray"][0]][]=$info;
22844
		
22844
		
22845
		
-
 
22846
			}#for end
22845
			}#for end
22847
			
22846
			
22848
		#針對每個可能有問題的IP
22847
		#針對每個可能有問題的IP
22849
		foreach($ips_byIp as $ip => $infos){
22848
		foreach($ips_byIp as $ip => $infos){
22850
		
22849
		
Line 22963... Line 22962...
22963
			}#if end
22962
			}#if end
22964
		
22963
		
22965
		#過濾掉對於含有 ".css",".js","favicon.ico",".jpg",".png" 關鍵字串的結果.  	
22964
		#過濾掉對於含有 ".css",".js","favicon.ico",".jpg",".png" 關鍵字串的結果.  	
22966
		#函式說明:
22965
		#函式說明:
22967
		#檢查多個字串中的每個字串是否有多個關鍵字
22966
		#檢查多個字串中的每個字串是否有多個關鍵字
22968
		#回傳的結果:
22967
		#回傳的結果:
22969
		#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
22968
		#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
22970
		#$result["function"],當前執行的函式名稱.
22969
		#$result["function"],當前執行的函式名稱.
22971
		#$result["error"],錯誤訊息.
22970
		#$result["error"],錯誤訊息.
22972
		#$result["argu"],使用的參數.
22971
		#$result["argu"],使用的參數.
22973
		#$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列.
22972
		#$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列.
Line 22994... Line 22993...
22994
 
22993
 
22995
			#回傳結果
22994
			#回傳結果
22996
			return $result;
22995
			return $result;
22997
			
22996
			
22998
			}#if end
22997
			}#if end
22999
			
22998
		
23000
		#取得不含有 ".css",".js","favicon.ico",".jpg",".png" 關鍵字串的結果.
22999
		#取得不含有 ".css",".js","favicon.ico",".jpg",".png" 關鍵字串的結果.
23001
		$searchOutPut["content"]=$findManyKeyWordsFromManyString["foundedFalseKey"];	
23000
		$searchOutPut["content"]=$findManyKeyWordsFromManyString["foundedFalseKey"];	
23002
		
23001
		
23003
		#更新計數
23002
		#更新計數
23004
		$searchOutPut["count"]=count($searchOutPut["content"]);
23003
		$searchOutPut["count"]=count($searchOutPut["content"]);
23005
			
23004
		
23006
		#用空格分割字串
23005
		#用空格分割字串
23007
		#函式說明:
23006
		#函式說明:
23008
		#將多個固定格式的字串分開,並回傳分開的結果
23007
		#將多個固定格式的字串分開,並回傳分開的結果
23009
		#回傳的參數:
23008
		#回傳的參數:
23010
		#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
23009
		#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
Line 23067... Line 23066...
23067
			
23066
			
23068
			#取得可能有問題的IP位置,key為[ip][int]
23067
			#取得可能有問題的IP位置,key為[ip][int]
23069
			$ips_byIp_https[$spiltMutiString["spiltString"][$i]["dataArray"][0]][]=$info;
23068
			$ips_byIp_https[$spiltMutiString["spiltString"][$i]["dataArray"][0]][]=$info;
23070
		
23069
		
23071
			}#for end
23070
			}#for end
-
 
23071
		
-
 
23072
		#debug
-
 
23073
		#var_dump(__LINE__,"http待整理的可能有問題IP清單",$ips_byIp_https);
23072
			
23074
		
23073
		#針對每個可能有問題的IP
23075
		#針對每個可能有問題的IP
23074
		foreach($ips_byIp_https as $infos){
23076
		foreach($ips_byIp_https as $infos){
23075
		
23077
		
23076
			#計數 ip 於同時間前來的計數
23078
			#計數 ip 於同時間前來的計數
23077
			$ipSameTimeCount=0;
23079
			$ipSameTimeCount=0;
Line 23125... Line 23127...
23125
			
23127
			
23126
				}#foreach end
23128
				}#foreach end
23127
		
23129
		
23128
			}#foreach end
23130
			}#foreach end
23129
		
23131
		
-
 
23132
		#debug
-
 
23133
		#var_dump(__LINE__,"http有問題IP清單",$ips_https);
-
 
23134
		
23130
		#透過 journalctl -a -e -f --unit=postfix.service | grep "SASL LOGIN authentication failed" 來取得認證失敗的 ip
23135
		#透過 journalctl -a -e -f --unit=postfix.service | grep "SASL LOGIN authentication failed" 來取得認證失敗的 ip
23131
		#函式說明:
23136
		#函式說明:
23132
		#呼叫shell執行系統命令,並取得回傳的內容.
23137
		#呼叫shell執行系統命令,並取得回傳的內容.
23133
		#回傳的結果:
23138
		#回傳的結果:
23134
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
23139
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 23197... Line 23202...
23197
			}#if end
23202
			}#if end
23198
		
23203
		
23199
		#儲存有問題的smtp IP
23204
		#儲存有問題的smtp IP
23200
		$ips_smtp=array();
23205
		$ips_smtp=array();
23201
		
23206
		
23202
		#狀態碼不為1才執行	
23207
		#狀態碼不為1才執行
23203
		if($callShell["statusCode"]!==1){	
23208
		if($callShell["statusCode"]!==1){
23204
				
23209
			
23205
			#截取出的格式
23210
			#截取出的格式
23206
			#Sep 28 07:54:16 qbpwcf.org postfix/smtpd[2766456]: warning: unknown[158.94.208.72]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=1q2w
23211
			#Sep 28 07:54:16 qbpwcf.org postfix/smtpd[2766456]: warning: unknown[158.94.208.72]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=1q2w
23207
			#Sep 28 07:57:15 qbpwcf.org postfix/smtpd[2768441]: warning: unknown[213.209.157.249]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=sybase
23212
			#Sep 28 07:57:15 qbpwcf.org postfix/smtpd[2768441]: warning: unknown[213.209.157.249]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=sybase
23208
			#Sep 28 08:01:09 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[213.209.157.165]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=spam
23213
			#Sep 28 08:01:09 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[213.209.157.165]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=spam
23209
			#Sep 28 08:01:51 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[103.109.20.174]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=print@qbpwcf.org
23214
			#Sep 28 08:01:51 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[103.109.20.174]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=print@qbpwcf.org
Line 23346... Line 23351...
23346
					return $result;
23351
					return $result;
23347
					
23352
					
23348
					}#if end
23353
					}#if end
23349
				
23354
				
23350
				#儲存時間
23355
				#儲存時間
23351
				$occurTime[$index]=$spiltString["dataArray"][0]." ".$spiltString["dataArray"][1]." ".$spiltString["dataArray"][2];
23356
				$occurTime[$index]=strtotime($spiltString["dataArray"][0]." ".$spiltString["dataArray"][1]." ".$spiltString["dataArray"][2]);
23352
				
23357
				
23353
				#含有IP的內容範例
23358
				#含有IP的內容範例
23354
				#Sep 28 07:54:16 qbpwcf.org postfix/smtpd[2766456]: warning: unknown[158.94.208.72]
23359
				#Sep 28 07:54:16 qbpwcf.org postfix/smtpd[2766456]: warning: unknown[158.94.208.72]
23355
				#Sep 28 07:57:15 qbpwcf.org postfix/smtpd[2768441]: warning: unknown[213.209.157.249]
23360
				#Sep 28 07:57:15 qbpwcf.org postfix/smtpd[2768441]: warning: unknown[213.209.157.249]
23356
				#Sep 28 08:01:09 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[213.209.157.165]
23361
				#Sep 28 08:01:09 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[213.209.157.165]
Line 23673... Line 23678...
23673
					continue;
23678
					continue;
23674
				
23679
				
23675
					}#if end
23680
					}#if end
23676
					
23681
					
23677
				#儲存時間點
23682
				#儲存時間點
23678
				$info["time"]=$findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0];
23683
				$info["time"]=$findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0].":".$findSpecifyStrFormat["parsedVar"]["sec"][0];
23679
			
23684
			
23680
				#儲存ip
23685
				#儲存ip
23681
				$info["ip"]=$findSpecifyStrFormat["parsedVar"]["ip"][0];
23686
				$info["ip"]=$findSpecifyStrFormat["parsedVar"]["ip"][0];
23682
			
23687
			
23683
				#儲存查詢的目標
23688
				#儲存查詢的目標
Line 23686... Line 23691...
23686
				#儲存疑似有問題的ip
23691
				#儲存疑似有問題的ip
23687
				$ips_named[$info["ip"]][]=$info;
23692
				$ips_named[$info["ip"]][]=$info;
23688
			
23693
			
23689
				}#foreach end
23694
				}#foreach end
23690
			
23695
			
-
 
23696
			/*
-
 
23697
			
23691
			#暫存可能有問題的ip資訊
23698
			#暫存可能有問題的ip資訊
23692
			$risky_ips=array();
23699
			$risky_ips=array();
23693
			
23700
			
23694
			#針對每個ip
23701
			#針對每個ip
23695
			foreach($ips_named as $ip => $info){
23702
			foreach($ips_named as $ip => $info){
Line 23704... Line 23711...
23704
					
23711
					
23705
				#儲存可能有問題的ip資訊
23712
				#儲存可能有問題的ip資訊
23706
				$risky_ips[]=$ips_named[$ip];
23713
				$risky_ips[]=$ips_named[$ip];
23707
				
23714
				
23708
				}#foreach end
23715
				}#foreach end
-
 
23716
				
-
 
23717
			*/
23709
			
23718
			
23710
			#針對每個可能有問題ip的每個log行
23719
			#針對每個可能有問題ip的每個log行
23711
			foreach($ips_named as $ip => $infos){
23720
			foreach($ips_named as $ip => $infos){
23712
			
23721
			
23713
				#初始化記錄ip來訪不大於1分鐘的計數
23722
				#初始化記錄ip來訪不大於1分鐘的計數
Line 23718... Line 23727...
23718
				
23727
				
23719
					#unixtime
23728
					#unixtime
23720
					$time=strtotime($info["time"]);
23729
					$time=strtotime($info["time"]);
23721
				
23730
				
23722
					#如果沒有下筆記錄
23731
					#如果沒有下筆記錄
23723
					if(!isset($risky_ips[$index+1])){
23732
					if(!isset($infos[$index+1])){
23724
					
23733
					
23725
						#結束 foreach
23734
						#結束 foreach
23726
						break;
23735
						break;
23727
						
23736
						
23728
						}#if end
23737
						}#if end
23729
				
23738
				
23730
					#取得unixtime
23739
					#取得unixtime
23731
					$next_time=strtotime($risky_ips[$index+1]["time"]);
23740
					$next_time=strtotime($infos[$index+1]["time"]);
23732
				
23741
				
23733
					#如果時間間隔大於60秒
23742
					#如果時間間隔大於60秒
23734
					if($next_time-$time>60){
23743
					if($next_time-$time>60){
23735
					
23744
					
23736
						#跳過,看下筆記錄
23745
						#跳過,看下筆記錄