Subversion Repositories php-qbpwcf

Rev

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

Rev 162 Rev 165
Line 22768... Line 22768...
22768
 
22768
 
22769
				#回傳結果
22769
				#回傳結果
22770
				return $result;
22770
				return $result;
22771
			
22771
			
22772
				}#if end
22772
				}#if end
22773
				
22773
			
22774
			}#if end
22774
			}#if end
22775
		
22775
		
22776
		#過濾掉對於含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.  	
22776
		#過濾掉對於含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.  	
22777
		#函式說明:
22777
		#函式說明:
22778
		#檢查多個字串中的每個字串是否有多個關鍵字
22778
		#檢查多個字串中的每個字串是否有多個關鍵字
Line 23197... Line 23197...
23197
			
23197
			
23198
				}#foreach end
23198
				}#foreach end
23199
		
23199
		
23200
			}#foreach end
23200
			}#foreach end
23201
		
23201
		
23202
		#透過 journalctl -a -e | grep "postfix/smtps/smtpd" | grep " connect from unknown" 來取得 IP
23202
		#透過 journalctl -a -e -f --unit=postfix.service | grep "SASL LOGIN authentication failed" 來取得認證失敗的 ip
23203
		#函式說明:
23203
		#函式說明:
23204
		#呼叫shell執行系統命令,並取得回傳的內容.
23204
		#呼叫shell執行系統命令,並取得回傳的內容.
23205
		#回傳的結果:
23205
		#回傳的結果:
23206
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
23206
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
23207
		#$result["error"],錯誤訊息陣列.
23207
		#$result["error"],錯誤訊息陣列.
Line 23219... Line 23219...
23219
		$conf["external::callShell"]["command"]="journalctl";
23219
		$conf["external::callShell"]["command"]="journalctl";
23220
		#$conf["fileArgu"],字串,變數__FILE__的內容.
23220
		#$conf["fileArgu"],字串,變數__FILE__的內容.
23221
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];		
23221
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];		
23222
		#可省略參數:
23222
		#可省略參數:
23223
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
23223
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
23224
		$conf["external::callShell"]["argu"]=array("-a","-e","|","grep","postfix/smtps/smtpd","|","grep"," connect from unknown");
23224
		$conf["external::callShell"]["argu"]=array("-a","-e","--unit=postfix.service","|","grep","SASL LOGIN authentication failed");
23225
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函式的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
23225
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函式的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
23226
		#$conf["arguIsAddr"]=array();	
23226
		#$conf["arguIsAddr"]=array();	
23227
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
23227
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
23228
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
23228
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
23229
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
23229
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
Line 23273... Line 23273...
23273
		
23273
		
23274
		#狀態碼不為1才執行	
23274
		#狀態碼不為1才執行	
23275
		if($callShell["statusCode"]!==1){	
23275
		if($callShell["statusCode"]!==1){	
23276
				
23276
				
23277
			#截取出的格式
23277
			#截取出的格式
23278
			#Aug 08 18:05:51 www.qbpwcf.org postfix/smtpd[27933]: connect from unknown[185.234.219.62]
23278
			#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
-
 
23279
			#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
23279
			#Aug 08 18:14:29 www.qbpwcf.org postfix/smtpd[16133]: connect from unknown[185.234.219.62]
23280
			#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
-
 
23281
			#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
23280
			#Aug 08 18:23:12 www.qbpwcf.org postfix/smtpd[11623]: connect from unknown[185.234.219.62]
23282
			#Sep 28 08:03:14 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[62.60.130.148]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=admin2
23281
			#Apr 04 15:11:16 mail.qbpwcf.org postfix/smtps/smtpd[10145]: connect from unknown[212.70.149.72]
23283
			#Sep 28 08:16:06 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[103.109.20.174]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=user@qbpwcf.org
-
 
23284
			#Sep 28 08:16:44 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=hualing@qbpwcf.org
-
 
23285
			#Sep 28 08:16:53 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=hualing@qbpwcf.org
-
 
23286
			#Sep 28 08:17:06 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=hualing@qbpwcf.org
-
 
23287
			#Sep 28 08:17:16 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]: SASL LOGIN authentication failed: Connection lost to authentication server, sasl_username=hualing@qbpwcf.org
23282
			#Apr 04 15:58:01 mail.qbpwcf.org postfix/smtps/smtpd[12382]: connect from unknown[212.70.149.72]
23288
			#Sep 28 08:20:28 qbpwcf.org postfix/smtpd[2784204]: warning: unknown[158.94.208.72]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=1q2w1q2w
23283
			#Mar 09 13:44:00 qbpwcf.org postfix/smtpd[1058247]: connect from unknown[194.48.251.15]
23289
			#Sep 28 08:23:09 qbpwcf.org postfix/smtpd[2785986]: warning: unknown[213.209.157.249]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=informix
23284
			#Mar 09 13:44:01 qbpwcf.org postfix/smtpd[1058247]: disconnect from unknown[194.48.251.15] ehlo=1 auth=0/1 quit=1 commands=2/3
23290
			#Sep 28 08:23:39 qbpwcf.org postfix/smtpd[2785986]: warning: unknown[62.60.130.148]: SASL LOGIN authentication failed: (reason unavailable), sasl_username=admin3
-
 
23291
			
-
 
23292
			#分割用的關鍵字
-
 
23293
			$keyword=": SASL LOGIN authentication failed: ";
23285
			
23294
			
23286
			#分割字串
23295
			#分割字串
23287
			#函式說明:
23296
			#函式說明:
23288
			#將多個固定格式的字串分開,並回傳分開的結果
23297
			#將多個固定格式的字串分開,並回傳分開的結果
23289
			#回傳的參數:
23298
			#回傳的參數:
Line 23296... Line 23305...
23296
			#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
23305
			#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
23297
			#必填參數:
23306
			#必填參數:
23298
			#$conf["stringIn"],字串陣列,要處理的字串陣列.
23307
			#$conf["stringIn"],字串陣列,要處理的字串陣列.
23299
			$conf["stringProcess::spiltMutiString"]["stringIn"]=$callShell["output"];
23308
			$conf["stringProcess::spiltMutiString"]["stringIn"]=$callShell["output"];
23300
			#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
23309
			#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
23301
			$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=" ";
23310
			$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=$keyword;
23302
			$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
23311
			$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
23303
			unset($conf["stringProcess::spiltMutiString"]);
23312
			unset($conf["stringProcess::spiltMutiString"]);
23304
			
23313
			
23305
			#如果分割失敗
23314
			#如果分割失敗
23306
			if($spiltMutiString["status"]==="false"){
23315
			if($spiltMutiString["status"]==="false"){
Line 23312... Line 23321...
23312
				$result["error"]=$spiltMutiString;
23321
				$result["error"]=$spiltMutiString;
23313
 
23322
 
23314
				#回傳結果
23323
				#回傳結果
23315
				return $result;
23324
				return $result;
23316
				
23325
				
23317
				}#if end	
23326
				}#if end
23318
				
23327
				
23319
			#初始化記錄每行log的時間點
23328
			#初始化記錄每行log的時間點
23320
			$occurTime=array();	
23329
			$occurTime=array();
-
 
23330
				
-
 
23331
			#初始化儲存log行資訊的變數
-
 
23332
			$oriLog=array();
23321
				
23333
				
23322
			#根據每筆記錄
23334
			#根據每筆記錄
23323
			foreach($spiltMutiString["spiltString"] as $index => $splitStr){
23335
			foreach($spiltMutiString["spiltString"] as $index => $splitStr){
23324
				
23336
				
-
 
23337
				#分段1的內容範例
-
 
23338
				#Sep 28 07:54:16 qbpwcf.org postfix/smtpd[2766456]: warning: unknown[158.94.208.72]
-
 
23339
				#Sep 28 07:57:15 qbpwcf.org postfix/smtpd[2768441]: warning: unknown[213.209.157.249]
-
 
23340
				#Sep 28 08:01:09 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[213.209.157.165]
-
 
23341
				#Sep 28 08:01:51 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[103.109.20.174]
-
 
23342
				#Sep 28 08:03:14 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[62.60.130.148]
-
 
23343
				#Sep 28 08:16:06 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[103.109.20.174]
-
 
23344
				#Sep 28 08:16:44 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]
-
 
23345
				#Sep 28 08:16:53 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]
-
 
23346
				#Sep 28 08:17:06 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]
-
 
23347
				#Sep 28 08:17:16 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]
-
 
23348
				#Sep 28 08:20:28 qbpwcf.org postfix/smtpd[2784204]: warning: unknown[158.94.208.72]
-
 
23349
				#Sep 28 08:23:09 qbpwcf.org postfix/smtpd[2785986]: warning: unknown[213.209.157.249]
-
 
23350
				#Sep 28 08:23:39 qbpwcf.org postfix/smtpd[2785986]: warning: unknown[62.60.130.148]
-
 
23351
				
-
 
23352
				#分段2的內容範例
-
 
23353
				#(reason unavailable), sasl_username=1q2w
-
 
23354
				#(reason unavailable), sasl_username=sybase
-
 
23355
				#(reason unavailable), sasl_username=spam
-
 
23356
				#(reason unavailable), sasl_username=print@qbpwcf.org
-
 
23357
				#(reason unavailable), sasl_username=admin2
-
 
23358
				#(reason unavailable), sasl_username=user@qbpwcf.org
-
 
23359
				#(reason unavailable), sasl_username=hualing@qbpwcf.org
-
 
23360
				#(reason unavailable), sasl_username=hualing@qbpwcf.org
-
 
23361
				#(reason unavailable), sasl_username=hualing@qbpwcf.org
-
 
23362
				#Connection lost to authentication server, sasl_username=hualing@qbpwcf.org
-
 
23363
				#(reason unavailable), sasl_username=1q2w1q2w
-
 
23364
				#(reason unavailable), sasl_username=informix
-
 
23365
				#(reason unavailable), sasl_username=admin3
-
 
23366
				
-
 
23367
				#用 " " 分割 分段1,以取得 月日時分秒
-
 
23368
				#函式說明:
-
 
23369
				#將固定格式的字串分開,並回傳分開的結果.
-
 
23370
				#回傳結果:
-
 
23371
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
23372
				#$result["error"],錯誤訊息陣列
-
 
23373
				#$result["function"],當前執行的函數名稱.
-
 
23374
				#$result["argu"],使用的參數.
-
 
23375
				#$result["oriStr"],要分割的原始字串內容
-
 
23376
				#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
-
 
23377
				#$result["dataCounts"],爲總共分成幾段
-
 
23378
				#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
23325
				#儲存時間
23379
				#必填參數:
-
 
23380
				#$conf["stringIn"],字串,要處理的字串.
23326
				$occurTime[$index]=$spiltMutiString["spiltString"][$index]["dataArray"][0]." ".$spiltMutiString["spiltString"][$index]["dataArray"][1]." ".$spiltMutiString["spiltString"][$index]["dataArray"][2];
23381
				$conf["stringProcess::spiltString"]["stringIn"]=$spiltMutiString["spiltString"][$index]["dataArray"][0];
-
 
23382
				#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
-
 
23383
				$conf["stringProcess::spiltString"]["spiltSymbol"]=" ";
-
 
23384
				#可省略參數:
-
 
23385
				#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
-
 
23386
				#$conf["allowEmptyStr"]="false";
-
 
23387
				#參考資料:
-
 
23388
				#無.
-
 
23389
				#備註:
-
 
23390
				#無.
-
 
23391
				$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
-
 
23392
				unset($conf["stringProcess::spiltString"]);
23327
				
23393
				
23328
				}#foreach end	
23394
				#如果分割失敗
-
 
23395
				if($spiltString["status"]==="false"){
23329
				
23396
				
23330
			#第一次分割的關鍵字
-
 
23331
			$splitKeyWord=" connect from unknown[";
-
 
23332
		
-
 
23333
			#分割字串
-
 
23334
			#函式說明:
-
 
23335
			#將多個固定格式的字串分開,並回傳分開的結果
-
 
23336
			#回傳的參數:
-
 
23337
			#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
-
 
23338
			#$result["error"],錯誤訊息陣列.
-
 
23339
			#$result["function"],當前執行的函式名稱.
-
 
23340
			#$result["spiltString"][$i]["oriStr"],爲第i個字串的原始內容
-
 
23341
			#$result["spiltString"][$i]["dataArray"],爲第($i+1)個字串分割後的字串陣列
-
 
23342
			#$result["spiltString"][$i]["dataArray"][$j],爲第($i+1)的分割好的字串的第($j+1)段內容
-
 
23343
			#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
-
 
23344
			#必填參數:
-
 
23345
			#$conf["stringIn"],字串陣列,要處理的字串陣列.
-
 
23346
			$conf["stringProcess::spiltMutiString"]["stringIn"]=$callShell["output"];
-
 
23347
			#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
-
 
23348
			$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=$splitKeyWord;
-
 
23349
			$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
-
 
23350
			unset($conf["stringProcess::spiltMutiString"]);
-
 
23351
			
-
 
23352
			#如果分割失敗
-
 
23353
			if($spiltMutiString["status"]==="false"){
-
 
23354
			
-
 
23355
				#設置執行失敗
23397
					#設置執行失敗
23356
				$result["status"]="false";
23398
					$result["status"]="false";
23357
 
23399
 
23358
				#設置錯誤訊息
23400
					#設置錯誤訊息
23359
				$result["error"]=$spiltMutiString;
23401
					$result["error"]=$spiltString;
23360
 
23402
 
23361
				#回傳結果
23403
					#回傳結果
23362
				return $result;
23404
					return $result;
-
 
23405
					
-
 
23406
					}#if end
-
 
23407
					
-
 
23408
				#如果分割失敗
-
 
23409
				if($spiltString["dataCounts"]<3){
23363
				
23410
				
23364
				}#if end
23411
					#設置執行失敗
-
 
23412
					$result["status"]="false";
23365
						
23413
 
23366
			#儲存尚未處理好的字串
23414
					#設置錯誤訊息
23367
			$unProcessedStr=array();
23415
					$result["error"]=$spiltString;
23368
			
23416
 
23369
			#初始化儲存log行資訊的變數
23417
					#回傳結果
23370
			$oriLog=array();
23418
					return $result;
23371
						
23419
					
23372
			#根據每筆記錄
23420
					}#if end
23373
			foreach($spiltMutiString["spiltString"] as $index => $splitStr){
-
 
23374
				
23421
				
23375
				#儲存原始的log行內容
23422
				#儲存時間
23376
				$oriLog[$index]=$spiltMutiString["spiltString"][$index ]["oriStr"];
23423
				$occurTime[$index]=$spiltString["dataArray"][0]." ".$spiltString["dataArray"][1]." ".$spiltString["dataArray"][2];
23377
						
-
 
23378
				#取得所需的字串
-
 
23379
				$unProcessedStr[]=$splitStr["dataArray"][1];
-
 
23380
				
23424
				
-
 
23425
				#含有IP的內容範例
-
 
23426
				#Sep 28 07:54:16 qbpwcf.org postfix/smtpd[2766456]: warning: unknown[158.94.208.72]
-
 
23427
				#Sep 28 07:57:15 qbpwcf.org postfix/smtpd[2768441]: warning: unknown[213.209.157.249]
-
 
23428
				#Sep 28 08:01:09 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[213.209.157.165]
-
 
23429
				#Sep 28 08:01:51 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[103.109.20.174]
-
 
23430
				#Sep 28 08:03:14 qbpwcf.org postfix/smtpd[2771112]: warning: unknown[62.60.130.148]
-
 
23431
				#Sep 28 08:16:06 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[103.109.20.174]
-
 
23432
				#Sep 28 08:16:44 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]
-
 
23433
				#Sep 28 08:16:53 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]
-
 
23434
				#Sep 28 08:17:06 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]
-
 
23435
				#Sep 28 08:17:16 qbpwcf.org postfix/smtpd[2781279]: warning: unknown[178.16.53.142]
-
 
23436
				#Sep 28 08:20:28 qbpwcf.org postfix/smtpd[2784204]: warning: unknown[158.94.208.72]
-
 
23437
				#Sep 28 08:23:09 qbpwcf.org postfix/smtpd[2785986]: warning: unknown[213.209.157.249]
-
 
23438
				#Sep 28 08:23:39 qbpwcf.org postfix/smtpd[2785986]: warning: unknown[62.60.130.148]
-
 
23439
				#"${doNotNeed} postfix/smtpd[${pid}]${doNotNeed}[${ip}]"
-
 
23440
				
-
 
23441
				#取得 ip
-
 
23442
				#函式說明:
-
 
23443
				#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
-
 
23444
				#回傳結果:
-
 
23445
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
23446
				#$reuslt["error"],執行不正常結束的錯訊息陣列.
-
 
23447
				#$result["function"],當前執行的函式名稱.
-
 
23448
				#$result["argu"],所使用的參數.
-
 
23449
				#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
-
 
23450
				#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
-
 
23451
				#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
-
 
23452
				#必填參數:
-
 
23453
				#$conf["input"],字串,要檢查的字串.
-
 
23454
				$conf["search::findSpecifyStrFormat"]["input"]=$spiltMutiString["spiltString"][$index]["dataArray"][1];
-
 
23455
				#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
23456
				$conf["search::findSpecifyStrFormat"]["format"]="\${doNotNeed} postfix/smtpd[\${pid}]\${doNotNeed}[\${ip}]";
-
 
23457
				#可省略參數:
-
 
23458
				#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
23459
				#$conf["search::findSpecifyStrFormat"]["varEqual"]=array(null,"found");
23381
				}#foreach end
23460
				#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
-
 
23461
				#$conf["varCon"]=array("no_tail"=>" not");
-
 
23462
				#參考資料:
-
 
23463
				#無.
-
 
23464
				#備註:
-
 
23465
				#無.
-
 
23466
				$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
-
 
23467
				unset($conf["search::findSpecifyStrFormat"]);
-
 
23468
				
-
 
23469
				#如果分割失敗
-
 
23470
				if($findSpecifyStrFormat["status"]==="false"){
23382
				
23471
				
23383
			#第二次分割的關鍵字
-
 
23384
			$splitKeyWord="]";
-
 
23385
 
-
 
23386
			#分割字串
-
 
23387
			#函式說明:
-
 
23388
			#將多個固定格式的字串分開,並回傳分開的結果
-
 
23389
			#回傳的參數:
-
 
23390
			#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
-
 
23391
			#$result["error"],錯誤訊息陣列.
-
 
23392
			#$result["function"],當前執行的函式名稱.
-
 
23393
			#$result["spiltString"][$i]["oriStr"],爲第i個字串的原始內容
-
 
23394
			#$result["spiltString"][$i]["dataArray"],爲第($i+1)個字串分割後的字串陣列
-
 
23395
			#$result["spiltString"][$i]["dataArray"][$j],爲第($i+1)的分割好的字串的第($j+1)段內容
-
 
23396
			#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
-
 
23397
			#必填參數:
-
 
23398
			#$conf["stringIn"],字串陣列,要處理的字串陣列.
-
 
23399
			$conf["stringProcess::spiltMutiString"]["stringIn"]=$unProcessedStr;
-
 
23400
			#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
-
 
23401
			$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=$splitKeyWord;
-
 
23402
			$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
-
 
23403
			unset($conf["stringProcess::spiltMutiString"]);
-
 
23404
			
-
 
23405
			#如果分割失敗
-
 
23406
			if($spiltMutiString["status"]==="false"){
-
 
23407
			
-
 
23408
				#設置執行失敗
23472
					#設置執行失敗
23409
				$result["status"]="false";
23473
					$result["status"]="false";
23410
 
23474
 
23411
				#設置錯誤訊息
23475
					#設置錯誤訊息
23412
				$result["error"]=$spiltMutiString;
23476
					$result["error"]=$findSpecifyStrFormat;
23413
 
23477
 
23414
				#回傳結果
23478
					#回傳結果
23415
				return $result;
23479
					return $result;
-
 
23480
					
-
 
23481
					}#if end
23416
				
23482
				
-
 
23483
				#如果沒有符合格式
-
 
23484
				if($findSpecifyStrFormat["found"]==="false"){
-
 
23485
				
-
 
23486
					#剔除時間點 記錄
-
 
23487
					array_pop($occurTime);
-
 
23488
				
-
 
23489
					#跳過該行
-
 
23490
					continue;
-
 
23491
				
23417
				}#if end
23492
					}#if end
23418
			
23493
				
23419
			#根據每筆記錄
23494
				#取得解析好的ip
-
 
23495
				$ip=$findSpecifyStrFormat["parsedVar"]["ip"];
-
 
23496
				
-
 
23497
				#儲存原始的log行內容
23420
			foreach($spiltMutiString["spiltString"] as $index => $splitStr){
23498
				$oriLog[$index]=$spiltMutiString["spiltString"][$index ]["oriStr"];
23421
			
23499
				
23422
				#有問題的資訊
23500
				#初始化儲存有問題的資訊
23423
				$info=array();
23501
				$info=array();
23424
				
23502
				
23425
				#設置問題對應的log行內容
23503
				#設置問題對應的log行內容
23426
				$info["log"]=$oriLog[$index];
23504
				$info["log"]=$oriLog[$index];
23427
				
23505
				
23428
				#設置問題時間點
23506
				#設置問題時間點
23429
				$info["time"]=$occurTime[$index];
23507
				$info["time"]=$occurTime[$index];
23430
					
23508
				
23431
				#記錄有問題的ip
23509
				#記錄有問題的ip
23432
				$info["ip"]=$splitStr["dataArray"][0];
23510
				$info["ip"]=$ip;
23433
			
23511
				
23434
				#取得所需的有問題的IP
23512
				#取得所需的有問題的IP
23435
				$ips_smtp[]=$info;
23513
				$ips_smtp[]=$info;
23436
				
23514
				
23437
				}#foreach end
23515
				}#foreach end
23438
				
23516
			
23439
			}#if end
23517
			}#if end
23440
		
23518
		
23441
		#透過 journalctl -e | grep named | grep " client " 來取得 IP
23519
		#透過 journalctl -a -e -f --unit=named.service | grep ' query ' 來取得 IP
23442
		#函式說明:
23520
		#函式說明:
23443
		#呼叫shell執行系統命令,並取得回傳的內容.
23521
		#呼叫shell執行系統命令,並取得回傳的內容.
23444
		#回傳的結果:
23522
		#回傳的結果:
23445
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
23523
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
23446
		#$result["error"],錯誤訊息陣列.
23524
		#$result["error"],錯誤訊息陣列.
Line 23455... Line 23533...
23455
		#$result["statusCode"],執行結束後的代碼.
23533
		#$result["statusCode"],執行結束後的代碼.
23456
		#必填參數:
23534
		#必填參數:
23457
		#$conf["command"],字串,要執行的指令與.
23535
		#$conf["command"],字串,要執行的指令與.
23458
		$conf["external::callShell"]["command"]="journalctl";
23536
		$conf["external::callShell"]["command"]="journalctl";
23459
		#$conf["fileArgu"],字串,變數__FILE__的內容.
23537
		#$conf["fileArgu"],字串,變數__FILE__的內容.
23460
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];		
23538
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
23461
		#可省略參數:
23539
		#可省略參數:
23462
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
23540
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
23463
		$conf["external::callShell"]["argu"]=array("-a","-e","|","grep"," named","|","grep"," client ");
23541
		$conf["external::callShell"]["argu"]=array("-a","-e","--unit=named.service","|","grep"," query ");
23464
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函式的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
23542
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函式的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
23465
		#$conf["arguIsAddr"]=array();	
23543
		#$conf["arguIsAddr"]=array();	
23466
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
23544
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
23467
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
23545
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
23468
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
23546
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
Line 23506... Line 23584...
23506
			return $result;
23584
			return $result;
23507
			
23585
			
23508
			}#if end
23586
			}#if end
23509
			
23587
			
23510
		#記錄攻擊者的IP
23588
		#記錄攻擊者的IP
23511
		$attacker_ips_namd=array();	
23589
		$attacker_ips_namd=array();
-
 
23590
			
-
 
23591
		#狀態碼不為1才執行
-
 
23592
		if($callShell["statusCode"]!==1){
23512
			
23593
			
23513
		#狀態碼不為1才執行	
-
 
23514
		if($callShell["statusCode"]!==1){		
-
 
23515
				
-
 
23516
			#截取出的格式
23594
			#截取出的格式
23517
			#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
23595
			#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
23518
			#Nov 24 20:55:15 localhost.localdomain named[99512]: client @0x7f77180297c0 191.7.219.100#6238 (PEACECORPS.GOV): query (cache) 'PEACECORPS.GOV/ANY/IN' denied
23596
			#Nov 24 20:55:15 localhost.localdomain named[99512]: client @0x7f77180297c0 191.7.219.100#6238 (PEACECORPS.GOV): query (cache) 'PEACECORPS.GOV/ANY/IN' denied
23519
			#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
23597
			#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
23520
			#Nov 24 20:55:15 localhost.localdomain named[99512]: client @0x7f77180297c0 191.7.219.100#6238 (PEACECORPS.GOV): query (cache) 'PEACECORPS.GOV/ANY/IN' denied
23598
			#Nov 24 20:55:15 localhost.localdomain named[99512]: client @0x7f77180297c0 191.7.219.100#6238 (PEACECORPS.GOV): query (cache) 'PEACECORPS.GOV/ANY/IN' denied
Line 23546... Line 23624...
23546
			#Dec 14 18:51:45 localhost.localdomain named[1159]: client @0x7f3dc801bc80 61.220.11.198#44377 (aesopower-cms-sock-latest.qbpwcf.org): query 'aesopower-cms-sock-latest.qbpwcf.org/A/IN' denied
23624
			#Dec 14 18:51:45 localhost.localdomain named[1159]: client @0x7f3dc801bc80 61.220.11.198#44377 (aesopower-cms-sock-latest.qbpwcf.org): query 'aesopower-cms-sock-latest.qbpwcf.org/A/IN' denied
23547
			#Dec 14 18:51:45 localhost.localdomain named[1159]: client @0x7f3dc8003700 61.220.11.198#4481 (dns.qbpwcf.org): query 'dns.qbpwcf.org/AAAA/IN' denied
23625
			#Dec 14 18:51:45 localhost.localdomain named[1159]: client @0x7f3dc8003700 61.220.11.198#4481 (dns.qbpwcf.org): query 'dns.qbpwcf.org/AAAA/IN' denied
23548
			#Dec 14 18:51:45 localhost.localdomain named[1159]: client @0x7f3dc8003700 61.220.11.198#53061 (aesopower-cms-sock-latest.qbpwcf.org): query 'aesopower-cms-sock-latest.qbpwcf.org/A/IN' denied
23626
			#Dec 14 18:51:45 localhost.localdomain named[1159]: client @0x7f3dc8003700 61.220.11.198#53061 (aesopower-cms-sock-latest.qbpwcf.org): query 'aesopower-cms-sock-latest.qbpwcf.org/A/IN' denied
23549
			#Dec 14 18:51:45 localhost.localdomain named[1159]: client @0x7f3dc8003700 61.220.11.198#31654 (dns.qbpwcf.org): query 'dns.qbpwcf.org/AAAA/IN' denied
23627
			#Dec 14 18:51:45 localhost.localdomain named[1159]: client @0x7f3dc8003700 61.220.11.198#31654 (dns.qbpwcf.org): query 'dns.qbpwcf.org/AAAA/IN' denied
23550
			#Dec 14 18:51:45 localhost.localdomain named[1159]: client @0x7f3dc802a410 61.220.11.198#57701 (aesopower-cms-sock-latest.qbpwcf.org): query 'aesopower-cms-sock-latest.qbpwcf.org/A/IN' denied
23628
			#Dec 14 18:51:45 localhost.localdomain named[1159]: client @0x7f3dc802a410 61.220.11.198#57701 (aesopower-cms-sock-latest.qbpwcf.org): query 'aesopower-cms-sock-latest.qbpwcf.org/A/IN' denied
-
 
23629
			#Sep 29 03:37:20 dns.qbpwcf.org named[94]: client @0x7f60e00734e8 61.166.210.84#49954 (twitter.com): query failed (REFUSED) for twitter.com/IN/AAAA at ../../../lib/ns/query.c:5691
-
 
23630
			#Sep 29 03:37:20 dns.qbpwcf.org named[94]: client @0x7f60e006f428 61.166.210.84#49954 (twitter.com): query failed (REFUSED) for twitter.com/IN/A at ../../../lib/ns/query.c:5691
-
 
23631
			#Sep 29 03:37:21 dns.qbpwcf.org named[94]: client @0x7f60e80889a8 61.166.210.84#50057 (astrill4u.com): query failed (REFUSED) for astrill4u.com/IN/AAAA at ../../../lib/ns/query.c:5691
-
 
23632
			#Sep 29 03:37:21 dns.qbpwcf.org named[94]: client @0x7f60e805ed08 61.166.210.84#50057 (astrill4u.com): query failed (REFUSED) for astrill4u.com/IN/A at ../../../lib/ns/query.c:5691
-
 
23633
			#Sep 29 03:46:33 dns.qbpwcf.org named[94]: client @0x7f60d402b618 165.22.223.147#16200 (tool.lu): query failed (REFUSED) for tool.lu/IN/A at ../../../lib/ns/query.c:5691
-
 
23634
			#Sep 29 03:49:16 dns.qbpwcf.org named[94]: client @0x7f60e0060a28 76.20.229.207#80 (sl): query failed (REFUSED) for sl/IN/ANY at ../../../lib/ns/query.c:5691
-
 
23635
			#Sep 29 04:19:10 dns.qbpwcf.org named[94]: client @0x7f60e0060a28 76.20.229.207#80 (sl): query failed (REFUSED) for sl/IN/ANY at ../../../lib/ns/query.c:5691
-
 
23636
			#Sep 29 04:20:28 dns.qbpwcf.org named[94]: client @0x7f60e0060a28 76.20.229.207#80 (sl): query failed (REFUSED) for sl/IN/ANY at ../../../lib/ns/query.c:5691
-
 
23637
			#Sep 29 04:24:20 dns.qbpwcf.org named[94]: client @0x7f60e0060a28 76.20.229.207#80 (sl): query failed (REFUSED) for sl/IN/ANY at ../../../lib/ns/query.c:5691
-
 
23638
			#Sep 29 04:24:59 dns.qbpwcf.org named[94]: client @0x7f60e0060a28 76.20.229.207#80 (sl): query failed (REFUSED) for sl/IN/ANY at ../../../lib/ns/query.c:5691
-
 
23639
			#Sep 29 04:40:48 dns.qbpwcf.org named[94]: client @0x7f60f9377888 174.96.56.126#80 (sl): query failed (REFUSED) for sl/IN/ANY at ../../../lib/ns/query.c:5691
-
 
23640
			#${month} ${day} ${hour}:${min}:${sec} ${doNotNeed} client ${doNotNeed} ${ip}#${remoteRevPort} (${$queryTarget}): ${doNotNeed}
23551
		
23641
		
23552
			#第一次分割的關鍵字
-
 
23553
			$splitKeyWord="#";
-
 
23554
		
-
 
23555
			#分割字串
-
 
23556
			#函式說明:
23642
			#函式說明:
23557
			#將多個固定格式的字串分開,並回傳分開的結果
23643
			#檢查多個字串中的每個字串是否有多個關鍵字
23558
			#回傳的參數:
23644
			#回傳結果:
23559
			#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
23645
			#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
-
 
23646
			#$result["function"],當前執行的函數名稱.
23560
			#$result["error"],錯誤訊息陣列.
23647
			#$result["error"],錯誤訊息.
23561
			#$result["function"],當前執行的函式名稱.
23648
			#$result["argu"],使用的參數.
23562
			#$result["spiltString"][$i]["oriStr"],爲第i個字串的原始內容
23649
			#$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列,與其數值內容.
23563
			#$result["spiltString"][$i]["dataArray"],爲第($i+1)個字串分割後的字串陣列
23650
			#$result["foundedKeyWords"],找到的關鍵字陣列.
23564
			#$result["spiltString"][$i]["dataArray"][$j],爲第($i+1)的分割好的字串的第($j+1)段內容
23651
			#$result["foundedFalseKey"],結果為"false"的被搜尋元素key陣列,與其數值內容.
23565
			#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
23652
			#$result["foundedTrueKeyWords"],二維陣列,各個字串有找到的關鍵字陣列.
-
 
23653
			#$result["foundedAll"],是否每個關鍵字都有找到,"true"代表每個都有找到,"false"代表沒有每個都找到.
-
 
23654
			#$result["keyWordsIncludeStr"],陣列,儲存有找到關鍵字的來源的索引(sourceIndex)與其內容(sourceVal)跟找到的關鍵字項目陣列(KeyWords).
23566
			#必填參數:
23655
			#必填參數:
23567
			#$conf["stringIn"],字串陣列,要處理的字串陣列.
23656
			#$conf["keyWords"],字串陣列,想要搜尋的關鍵字.
23568
			$conf["stringProcess::spiltMutiString"]["stringIn"]=$callShell["output"];
23657
			$conf["search::findManyKeyWordsFromManyString"]["keyWords"]=array("(REFUSED)"," denied");
23569
			#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
23658
			#$conf["stringArray"],字串陣列,要被搜尋的字串內容陣列.
23570
			$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=$splitKeyWord;
23659
			$conf["search::findManyKeyWordsFromManyString"]["stringArray"]=$callShell["output"];
-
 
23660
			#可省略參數:
-
 
23661
			#$conf["completeEqual"],字串,是否內容要完全符合,不能多出任何不符合的內容,預設為"false"不需要完全符合.
-
 
23662
			#$conf["completeEqual"]="true";
-
 
23663
			#參考資料:
-
 
23664
			#無.
-
 
23665
			#備註:
-
 
23666
			#無.
23571
			$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
23667
			$findManyKeyWordsFromManyString=search::findManyKeyWordsFromManyString($conf["search::findManyKeyWordsFromManyString"]);
23572
			unset($conf["stringProcess::spiltMutiString"]);
23668
			unset($conf["search::findManyKeyWordsFromManyString"]);
23573
			
23669
			
23574
			#如果分割失敗
23670
			#如果執行失敗
23575
			if($spiltMutiString["status"]==="false"){
23671
			if($findManyKeyWordsFromManyString["status"]==="false"){
23576
			
23672
			
23577
				#設置執行失敗
23673
				#設置執行失敗
23578
				$result["status"]="false";
23674
				$result["status"]="false";
23579
 
23675
 
23580
				#設置錯誤訊息
23676
				#設置錯誤訊息
23581
				$result["error"]=$spiltMutiString;
23677
				$result["error"]=$findManyKeyWordsFromManyString;
23582
 
23678
 
23583
				#回傳結果
23679
				#回傳結果
23584
				return $result;
23680
				return $result;
23585
				
23681
				
23586
				}#if end
23682
				}#if end
23587
			
23683
			
23588
			#儲存未處理的dns查詢字串
-
 
23589
			$quertString=array();
-
 
23590
				
-
 
23591
			#儲存尚未處理好的字串
-
 
23592
			$unProcessedStr=array();
-
 
23593
			
-
 
23594
			#初始化儲存原始log行內容
23684
			#初始化儲存原始log行內容
23595
			$oriLog=array();
23685
			#$oriLog=array();
23596
			
23686
			
23597
			#根據每筆記錄
-
 
23598
			foreach($spiltMutiString["spiltString"] as $index => $splitStr){
-
 
23599
			
-
 
23600
				#如果切割出來小於兩段
23687
			#儲存疑似有問題的IP
23601
				if($splitStr["dataCounts"]<2){
-
 
23602
				
-
 
23603
					#換下一筆資料
23688
			$ips_named=array();
23604
					continue;
-
 
23605
				
-
 
23606
					}#if end
-
 
23607
			
23689
			
23608
				#取得原始log行內容
23690
			#初始化儲存確定有問題的ip
23609
				$oriLog[]=$splitStr["oriStr"];
23691
			$attacker_ips_namd=array();
23610
			
23692
			
23611
				#取得所需的字串
23693
			#針對每個符合的行
23612
				$unProcessedStr[]=$splitStr["dataArray"][0];
23694
			foreach($findManyKeyWordsFromManyString["foundedTrueKey"] as $oriLog){
23613
				
-
 
23614
				#儲存查詢的dns字串
-
 
23615
				$quertString[]=$splitStr["dataArray"][1];
-
 
23616
				
-
 
23617
				}#foreach end
-
 
23618
				
-
 
23619
			#第二次分割的關鍵字
-
 
23620
			$splitKeyWord=" ";
-
 
23621
 
23695
 
23622
			#分割字串
-
 
23623
			#函式說明:
-
 
23624
			#將多個固定格式的字串分開,並回傳分開的結果
-
 
23625
			#回傳的參數:
-
 
23626
			#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
-
 
23627
			#$result["error"],錯誤訊息陣列.
23696
				#初始化儲存資訊的暫存陣列
23628
			#$result["function"],當前執行的函式名稱.
-
 
23629
			#$result["spiltString"][$i]["oriStr"],爲第i個字串的原始內容
-
 
23630
			#$result["spiltString"][$i]["dataArray"],爲第($i+1)個字串分割後的字串陣列
-
 
23631
			#$result["spiltString"][$i]["dataArray"][$j],爲第($i+1)的分割好的字串的第($j+1)段內容
-
 
23632
			#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
-
 
23633
			#必填參數:
23697
				$info=array();
23634
			#$conf["stringIn"],字串陣列,要處理的字串陣列.
-
 
23635
			$conf["stringProcess::spiltMutiString"]["stringIn"]=$unProcessedStr;
-
 
23636
			#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
-
 
23637
			$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=$splitKeyWord;
-
 
23638
			$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
-
 
23639
			unset($conf["stringProcess::spiltMutiString"]);
-
 
23640
			
-
 
23641
			#如果分割失敗
-
 
23642
			if($spiltMutiString["status"]==="false"){
-
 
23643
			
-
 
23644
				#設置執行失敗
-
 
23645
				$result["status"]="false";
-
 
23646
 
23698
 
-
 
23699
				#解析時間與來源ip
-
 
23700
				#函式說明:
-
 
23701
				#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
-
 
23702
				#回傳結果:
-
 
23703
				#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
23704
				#$reuslt["error"],執行不正常結束的錯訊息陣列.
-
 
23705
				#$result["function"],當前執行的函式名稱.
-
 
23706
				#$result["argu"],所使用的參數.
-
 
23707
				#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
-
 
23708
				#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
-
 
23709
				#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
-
 
23710
				#必填參數:
-
 
23711
				#$conf["input"],字串,要檢查的字串.
-
 
23712
				$conf["search::findSpecifyStrFormat"]["input"]=$oriLog;
-
 
23713
				#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
23714
				$conf["search::findSpecifyStrFormat"]["format"]="\${month} \${day} \${hour}:\${min}:\${sec} \${doNotNeed} client \${doNotNeed} \${ip}#\${remoteRevPort} (\${queryTarget}): \${doNotNeed}";
-
 
23715
				#可省略參數:
-
 
23716
				#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
23717
				#$conf["search::findSpecifyStrFormat"]["varEqual"]=array(null,"found");
23647
				#設置錯誤訊息
23718
				#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
-
 
23719
				#$conf["varCon"]=array("no_tail"=>" not");
-
 
23720
				#參考資料:
-
 
23721
				#無.
-
 
23722
				#備註:
-
 
23723
				#無.
-
 
23724
				$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
-
 
23725
				unset($conf["search::findSpecifyStrFormat"]);
-
 
23726
				
-
 
23727
				#如果分割失敗
-
 
23728
				if($findSpecifyStrFormat["status"]==="false"){
-
 
23729
				
-
 
23730
					#設置執行失敗
23648
				$result["error"]=$spiltMutiString;
23731
					$result["status"]="false";
23649
 
23732
 
-
 
23733
					#設置錯誤訊息
-
 
23734
					$result["error"]=$findSpecifyStrFormat;
-
 
23735
 
23650
				#回傳結果
23736
					#回傳結果
23651
				return $result;
23737
					return $result;
-
 
23738
					
-
 
23739
					}#if end
23652
				
23740
				
-
 
23741
				#如果沒有符合格式
-
 
23742
				if($findSpecifyStrFormat["found"]==="false"){
-
 
23743
					
-
 
23744
					#跳過該行
23653
				}#if end
23745
					continue;
23654
				
23746
				
23655
			#儲存有問題的IP
23747
					}#if end
-
 
23748
					
23656
			$ips_named=array();
23749
				#儲存時間點
-
 
23750
				$info["time"]=$findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0];
23657
			
23751
			
23658
			#根據每筆記錄
23752
				#儲存ip
23659
			foreach($spiltMutiString["spiltString"] as $index => $splitStr){
23753
				$info["ip"]=$findSpecifyStrFormat["parsedVar"]["ip"][0];
23660
			
23754
			
23661
				#取得所需的可能有問題的IP
23755
				#儲存查詢的目標
23662
				$ips_named[]=array("ip"=>$splitStr["dataArray"][$splitStr["dataCounts"]-1],"time"=>$splitStr["dataArray"][2],"log"=>$oriLog[$index]);
23756
				$info["queryTarget"]=$findSpecifyStrFormat["parsedVar"]["queryTarget"][0];
23663
				
23757
				
-
 
23758
				#儲存疑似有問題的ip
23664
				}#foreach end
23759
				$ips_named[$info["ip"]][]=$info;
23665
			
23760
			
23666
			#第三次分割的關鍵字
-
 
23667
			$splitKeyWord=" ";
23761
				}#foreach end
23668
			
23762
			
23669
			#分割字串
-
 
23670
			#函式說明:
-
 
23671
			#將多個固定格式的字串分開,並回傳分開的結果
-
 
23672
			#回傳的參數:
-
 
23673
			#$result["status"],執行成功與否,若爲"true",代表執行成功,若爲"false"代表執失敗。
-
 
23674
			#$result["error"],錯誤訊息陣列.
-
 
23675
			#$result["function"],當前執行的函式名稱.
23763
			#暫存可能有問題的ip資訊
23676
			#$result["spiltString"][$i]["oriStr"],爲第i個字串的原始內容
-
 
23677
			#$result["spiltString"][$i]["dataArray"],爲第($i+1)個字串分割後的字串陣列
-
 
23678
			#$result["spiltString"][$i]["dataArray"][$j],爲第($i+1)的分割好的字串的第($j+1)段內容
-
 
23679
			#$result["spiltString"][$i]["dataCounts"],爲第($i+1)個字串分割後總共分成幾段
-
 
23680
			#必填參數:
23764
			$risky_ips=array();
23681
			#$conf["stringIn"],字串陣列,要處理的字串陣列.
-
 
23682
			$conf["stringProcess::spiltMutiString"]["stringIn"]=$quertString;
-
 
23683
			#$conf["spiltSymbol"],字串,爲要以哪個符號作爲分割.
-
 
23684
			$conf["stringProcess::spiltMutiString"]["spiltSymbol"]=$splitKeyWord;
-
 
23685
			$spiltMutiString=stringProcess::spiltMutiString($conf["stringProcess::spiltMutiString"]);
-
 
23686
			unset($conf["stringProcess::spiltMutiString"]);
-
 
23687
			
23765
			
23688
			#如果分割失敗
23766
			#針對每個ip
23689
			if($spiltMutiString["status"]==="false"){
23767
			foreach($ips_named as $ip => $info){
23690
			
23768
			
23691
				#設置執行失敗
23769
				#如果同樣ip不到3次
23692
				$result["status"]="false";
23770
				if(count($ips_named[$ip])<3){
23693
 
-
 
23694
				#設置錯誤訊息
-
 
23695
				$result["error"]=$spiltMutiString;
-
 
23696
 
-
 
23697
				#回傳結果
-
 
23698
				return $result;
-
 
23699
				
23771
				
-
 
23772
					#換看下個ip
23700
				}#if end
23773
					continue;
23701
				
23774
				
23702
			#根據每筆記錄
23775
					}#if end
23703
			foreach($spiltMutiString["spiltString"] as $index=>$splitStr){
-
 
23704
			
23776
					
23705
				#取得所需的可能有問題IP所查詢的dns
23777
				#儲存可能有問題的ip資訊
23706
				$ips_named[$index]["dns"]=$splitStr["dataArray"][$splitStr["dataCounts"]-2];
23778
				$risky_ips[]=$ips_named[$ip];
23707
				
23779
				
23708
				}#foreach end
23780
				}#foreach end
23709
			
23781
			
23710
			#初始化記錄時間點
-
 
23711
			$time=0;
-
 
23712
			
-
 
23713
			#初始化記錄當前ip
-
 
23714
			$ip="";
-
 
23715
			
-
 
23716
			#初始化記錄當前查詢的dns
-
 
23717
			$dns="";
-
 
23718
			
-
 
23719
			#初始化記錄同時間,ip重複的次數.
23782
			#針對每個可能有問題ip的每個log行
23720
			$count=0;
-
 
23721
			
-
 
23722
			#針對每筆資料
-
 
23723
			for($i=0;$i<count($ips_named);$i++){
23783
			foreach($ips_named as $ip => $infos){
23724
			
23784
			
23725
				#如果時間點不同
23785
				#初始化記錄ip來訪不大於1分鐘的計數
23726
				if($ips_named[$i]["time"]!==$time){
23786
				$count=0;
23727
				
23787
				
23728
					#初始化記錄時間點
23788
				#針對每個log行資訊
23729
					$time=$ips_named[$i]["time"];
23789
				foreach($infos as $index => $info){
23730
					
-
 
23731
					#初始化記錄當前ip
-
 
23732
					$ip=$ips_named[$i]["ip"];
-
 
23733
					
-
 
23734
					#初始化記錄當前的dns
-
 
23735
					$dns=$ips_named[$i]["dns"];
-
 
23736
					
-
 
23737
					#初始化記錄同時間,ip重複的次數.
-
 
23738
					$count=1;
-
 
23739
				
23790
				
23740
					}#if end
23791
					#unixtime
23741
				
-
 
23742
				#反之時間點相同且,但ip不同
-
 
23743
				else if($ips_named[$i]["ip"]!==$ip){
23792
					$time=strtotime($info["time"]);
23744
				
23793
				
23745
					#初始化記錄時間點
23794
					#如果沒有下筆記錄
23746
					$time=$ips_named[$i]["time"];
23795
					if(!isset($risky_ips[$index+1])){
23747
					
-
 
23748
					#初始化記錄當前ip
-
 
23749
					$ip=$ips_named[$i]["ip"];
-
 
23750
					
23796
					
23751
					#初始化記錄當前的dns
-
 
23752
					$dns=$ips_named[$i]["dns"];
23797
						#結束 foreach
23753
					
23798
						break;
23754
					#初始化記錄同時間,ip重複的次數.
-
 
23755
					$count=1;				
-
 
23756
			
23799
						
23757
					}#if end
23800
						}#if end
23758
				
23801
				
23759
				#反之時間點相同,但dns不同
23802
					#取得unixtime
23760
				else if($ips_named[$i]["dns"]!==$dns){
23803
					$next_time=strtotime($risky_ips[$index+1]["time"]);
23761
				
23804
				
23762
					#初始化記錄時間點
23805
					#如果時間間隔大於60秒
23763
					$time=$ips_named[$i]["time"];
-
 
23764
					
-
 
23765
					#初始化記錄當前ip
-
 
23766
					$ip=$ips_named[$i]["ip"];
23806
					if($next_time-$time>60){
23767
					
23807
					
23768
					#初始化記錄當前的dns
23808
						#跳過,看下筆記錄
23769
					$dns=$ips_named[$i]["dns"];
23809
						continue;
23770
					
23810
					
23771
					#初始化記錄同時間,ip重複的次數.
-
 
23772
					$count=1;	
-
 
23773
				
-
 
23774
					}#if end
23811
						}#if end
23775
				
23812
						
23776
				#反之時間點與IP與dns皆相同
-
 
23777
				else{
-
 
23778
				
-
 
23779
					#計數+1
23813
					#計數+1
23780
					$count++;
23814
					$count++;
-
 
23815
				
-
 
23816
					}#foreach end
23781
					
23817
					
23782
					#如果計數5次
23818
				#如果有達到3次
23783
					if($count===5){
23819
				if($count>=3){
23784
					
-
 
23785
						#若該IP是已經重複
-
 
23786
						if(in_array($ips_named[$i]["ip"],$attacker_ips_namd)){
-
 
23787
							
-
 
23788
							#跳過
-
 
23789
							continue;
-
 
23790
					
-
 
23791
							}#if end
-
 
23792
						
-
 
23793
						#反之是新IP
-
 
23794
						else{
-
 
23795
						
-
 
23796
							#記錄起來
-
 
23797
							$attacker_ips_namd[]=$ips_named[$i];
-
 
23798
						
-
 
23799
							}#else end
-
 
23800
						
-
 
23801
						}#if end
-
 
23802
				
23820
				
-
 
23821
					#記錄攻擊者的IP
23803
					}#else end
23822
					$attacker_ips_namd[]=$ip;
23804
				
23823
				
-
 
23824
					}#if end
-
 
23825
			
23805
				}#for end
23826
				}#foreach end
23806
			
23827
			
23807
			}#if end
23828
			}#if end
23808
		
23829
		
23809
		#透過 journalctl -a -e | grep dovecot | grep failed 來取得 IP
23830
		#透過 journalctl -a -e --unit=dovecot.service 來取得 IP
23810
		#函式說明:
23831
		#函式說明:
23811
		#呼叫shell執行系統命令,並取得回傳的內容.
23832
		#呼叫shell執行系統命令,並取得回傳的內容.
23812
		#回傳的結果:
23833
		#回傳的結果:
23813
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
23834
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
23814
		#$result["error"],錯誤訊息陣列.
23835
		#$result["error"],錯誤訊息陣列.
Line 23823... Line 23844...
23823
		#$result["statusCode"],執行結束後的代碼.
23844
		#$result["statusCode"],執行結束後的代碼.
23824
		#必填參數:
23845
		#必填參數:
23825
		#$conf["command"],字串,要執行的指令與.
23846
		#$conf["command"],字串,要執行的指令與.
23826
		$conf["external::callShell"]["command"]="journalctl";
23847
		$conf["external::callShell"]["command"]="journalctl";
23827
		#$conf["fileArgu"],字串,變數__FILE__的內容.
23848
		#$conf["fileArgu"],字串,變數__FILE__的內容.
23828
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];		
23849
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
23829
		#可省略參數:
23850
		#可省略參數:
23830
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
23851
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
23831
		$conf["external::callShell"]["argu"]=array("-a","-e","|","grep","dovecot","|","grep","failed");
23852
		$conf["external::callShell"]["argu"]=array("-a","-e","--unit=dovecot.service");
23832
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函式的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
23853
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函式的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
23833
		#$conf["arguIsAddr"]=array();	
23854
		#$conf["arguIsAddr"]=array();	
23834
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
23855
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
23835
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
23856
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
23836
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
23857
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
Line 23860... Line 23881...
23860
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
23881
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
23861
		$callShell=external::callShell($conf["external::callShell"]);
23882
		$callShell=external::callShell($conf["external::callShell"]);
23862
		unset($conf["external::callShell"]);
23883
		unset($conf["external::callShell"]);
23863
		
23884
		
23864
		#如果執行失敗
23885
		#如果執行失敗
23865
		if($callShell["status"]==="false" && ( $callShell["statusCode"]!==0 && $callShell["statusCode"]!==1 ) ){
23886
		if($callShell["status"]==="false"){
23866
		
23887
		
23867
			#設置執行失敗
23888
			#設置執行失敗
23868
			$result["status"]="false";
23889
			$result["status"]="false";
23869
 
23890
 
23870
			#設置錯誤訊息
23891
			#設置錯誤訊息
Line 24009... Line 24030...
24009
		Feb 23 07:59: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=<TMaql/W7zhpliNc9>
24030
		Feb 23 07:59: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=<TMaql/W7zhpliNc9>
24010
		Feb 23 07:59: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=<KnKtl/W7GBtliNc9>
24031
		Feb 23 07:59: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=<KnKtl/W7GBtliNc9>
24011
		Feb 23 07:59: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=<Lkixl/W7ixtliNc9>
24032
		Feb 23 07:59: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=<Lkixl/W7ixtliNc9>
24012
		Feb 23 07:59:30 localhost.localdomain dovecot[1427]: imap-login: Disconnected (no auth attempts in 1 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=<8bWzl/W7ARhliNc9>
24033
		Feb 23 07:59:30 localhost.localdomain dovecot[1427]: imap-login: Disconnected (no auth attempts in 1 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=<8bWzl/W7ARhliNc9>
24013
		Feb 23 07:59:30 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=<70q2l/W7PBhliNc9>
24034
		Feb 23 07:59:30 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=<70q2l/W7PBhliNc9>
-
 
24035
		Sep 29 06:53:42 qbpwcf.org dovecot[98]: imap-login: Disconnected: Connection closed (no auth attempts in 0 secs): user=<>, rip=3.231.151.171, lip=169.254.2.1, TLS, session=<MaKQGOs/wIMD55er>
-
 
24036
		Sep 29 07:42:58 qbpwcf.org auth[3737748]: pam_unix(dovecot:auth): check pass; user unknown
-
 
24037
		Sep 29 07:42:58 qbpwcf.org auth[3737748]: pam_unix(dovecot:auth): authentication failure; logname=liveuser uid=0 euid=0 tty=dovecot ruser=account@qbpwcf.org rhost=62.60.131.29
-
 
24038
		Sep 29 08:10:02 qbpwcf.org dovecot[98]: imap(liveuser)<3755807><SvyIKew/xLpyIuFn>: Disconnected: Connection closed (UID SEARCH finished 0.039 secs ago) in=119 out=1504 deleted=0 expunged=0 trashed=0 hdr_count=0 hdr_bytes=0 body_count=0 body_bytes=0
-
 
24039
		Sep 29 08:12:02 qbpwcf.org dovecot[98]: imap-login: Login: user=<liveuser>, method=PLAIN, rip=149.102.158.38, lip=169.254.2.1, mpid=3757116, TLS, session=<B2O9MOw/TrmVZp4m>
-
 
24040
		Sep 29 08:12:04 qbpwcf.org dovecot[98]: imap(liveuser)<3757116><B2O9MOw/TrmVZp4m>: Disconnected: Logged out in=240 out=21891 deleted=0 expunged=0 trashed=0 hdr_count=0 hdr_bytes=0 body_count=0 body_bytes=0
-
 
24041
		Sep 29 08:12:06 qbpwcf.org dovecot[98]: imap-login: Login: user=<liveuser>, method=PLAIN, rip=149.102.158.38, lip=169.254.2.1, mpid=3757144, TLS, session=<S0rwMOw/XLmVZp4m>
-
 
24042
		Sep 29 08:12:09 qbpwcf.org dovecot[98]: imap(liveuser)<3757144><S0rwMOw/XLmVZp4m>: Disconnected: Logged out in=885 out=25454 deleted=0 expunged=0 trashed=0 hdr_count=2 hdr_bytes=21658 body_count=0 body_bytes=0
-
 
24043
		Sep 29 08:28:48 qbpwcf.org dovecot[98]: imap-login: Login: user=<liveuser>, method=PLAIN, rip=114.34.225.103, lip=169.254.2.1, mpid=3768144, TLS, session=<Gk6ybOw/nNpyIuFn>
-
 
24044
		Sep 29 08:28:51 qbpwcf.org dovecot[98]: imap(liveuser)<3768144><Gk6ybOw/nNpyIuFn>: Disconnected: Connection closed (LIST finished 1.939 secs ago) in=50 out=17609 deleted=0 expunged=0 trashed=0 hdr_count=0 hdr_bytes=0 body_count=0 body_bytes=0
-
 
24045
		Sep 29 08:28:51 qbpwcf.org dovecot[98]: imap-login: Login: user=<liveuser>, method=PLAIN, rip=114.34.225.103, lip=169.254.2.1, mpid=3768149, TLS, session=<ct7bbOw/qtpyIuFn>
-
 
24046
		Sep 29 08:28:52 qbpwcf.org dovecot[98]: imap(liveuser)<3768149><ct7bbOw/qtpyIuFn>: Disconnected: Connection closed (UID FETCH finished 0.045 secs ago) in=196 out=2381 deleted=0 expunged=0 trashed=0 hdr_count=0 hdr_bytes=0 body_count=0 body_bytes=0
-
 
24047
		Sep 29 08:28:53 qbpwcf.org dovecot[98]: imap-login: Login: user=<liveuser>, method=PLAIN, rip=114.34.225.103, lip=169.254.2.1, mpid=3768155, TLS, session=<DvP1bOw/tNpyIuFn>
-
 
24048
		Sep 29 08:28:55 qbpwcf.org dovecot[98]: imap(liveuser)<3768155><DvP1bOw/tNpyIuFn>: Disconnected: Connection closed (UID SEARCH finished 0.180 secs ago) in=119 out=1504 deleted=0 expunged=0 trashed=0 hdr_count=0 hdr_bytes=0 body_count=0 body_bytes=0
-
 
24049
		Sep 29 08:39:24 qbpwcf.org auth[3775219]: pam_unix(dovecot:auth): check pass; user unknown
-
 
24050
		Sep 29 08:39:24 qbpwcf.org auth[3775219]: pam_unix(dovecot:auth): authentication failure; logname=liveuser uid=0 euid=0 tty=dovecot ruser=test rhost=196.251.92.78
-
 
24051
		Sep 29 08:40:44 qbpwcf.org auth[3776113]: pam_unix(dovecot:auth): check pass; user unknown
-
 
24052
		Sep 29 08:40:44 qbpwcf.org auth[3776113]: pam_unix(dovecot:auth): authentication failure; logname=liveuser uid=0 euid=0 tty=dovecot ruser=1q2w3e4r5t4567 rhost=158.94.208.72
-
 
24053
		Sep 29 08:41:18 qbpwcf.org auth[3776113]: pam_unix(dovecot:auth): check pass; user unknown
-
 
24054
		Sep 29 08:41:18 qbpwcf.org auth[3776113]: pam_unix(dovecot:auth): authentication failure; logname=liveuser uid=0 euid=0 tty=dovecot ruser=spam rhost=62.60.130.148
24014
		*/
24055
		*/
24015
		
24056
		
24016
		#針對每列輸出
24057
		#認證失敗的關鍵字
24017
		foreach($callShell["output"] as $line){
24058
		$keyWordAF="authentication failure";
24018
		
24059
		
24019
			#用 " " 區分內容
24060
		#未認證或SSL錯誤
24020
			#函式說明:
-
 
24021
			#將固定格式的字串分開,並回傳分開的結果。
-
 
24022
			#回傳結果:
-
 
24023
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24024
			#$result["error"],錯誤訊息陣列
-
 
24025
			#$result["function"],當前執行的函式名稱.
-
 
24026
			#$result["argu"],使用的參數.
24061
		$keyWordNAA="no auth attempts";
24027
			#$result["oriStr"],要分割的原始字串內容
-
 
24028
			#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
-
 
24029
			#$result["dataCounts"],爲總共分成幾段
-
 
24030
			#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
-
 
24031
			#必填參數:
-
 
24032
			#$conf["stringIn"],字串,要處理的字串.
-
 
24033
			$conf["stringProcess::spiltString"]["stringIn"]=$line;
-
 
24034
			#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
-
 
24035
			$conf["stringProcess::spiltString"]["spiltSymbol"]=" ";
-
 
24036
			#可省略參數:
-
 
24037
			#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
-
 
24038
			$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
-
 
24039
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
-
 
24040
			unset($conf["stringProcess::spiltString"]);
-
 
24041
		
24062
		
-
 
24063
		#搜尋符合關鍵字的log行
-
 
24064
		#函式說明:
-
 
24065
		#檢查多個字串中的每個字串是否有多個關鍵字
-
 
24066
		#回傳結果:
-
 
24067
		#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
-
 
24068
		#$result["function"],當前執行的函數名稱.
-
 
24069
		#$result["error"],錯誤訊息.
-
 
24070
		#$result["argu"],使用的參數.
-
 
24071
		#$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列,與其數值內容.
-
 
24072
		#$result["foundedKeyWords"],找到的關鍵字陣列.
-
 
24073
		#$result["foundedFalseKey"],結果為"false"的被搜尋元素key陣列,與其數值內容.
-
 
24074
		#$result["foundedTrueKeyWords"],二維陣列,各個字串有找到的關鍵字陣列.
-
 
24075
		#$result["foundedAll"],是否每個關鍵字都有找到,"true"代表每個都有找到,"false"代表沒有每個都找到.
-
 
24076
		#$result["keyWordsIncludeStr"],陣列,儲存有找到關鍵字的來源的索引(sourceIndex)與其內容(sourceVal)跟找到的關鍵字項目陣列(KeyWords).
-
 
24077
		#必填參數:
-
 
24078
		#$conf["keyWords"],字串陣列,想要搜尋的關鍵字.
-
 
24079
		$conf["search::findManyKeyWordsFromManyString"]["keyWords"]=array($keyWordAF,$keyWordNAA);
-
 
24080
		#$conf["stringArray"],字串陣列,要被搜尋的字串內容陣列.
-
 
24081
		$conf["search::findManyKeyWordsFromManyString"]["stringArray"]=$callShell["output"];
-
 
24082
		#可省略參數:
-
 
24083
		#$conf["completeEqual"],字串,是否內容要完全符合,不能多出任何不符合的內容,預設為"false"不需要完全符合.
-
 
24084
		#$conf["completeEqual"]="true";
-
 
24085
		#參考資料:
-
 
24086
		#無.
-
 
24087
		#備註:
-
 
24088
		#無.
-
 
24089
		$findManyKeyWordsFromManyString=search::findManyKeyWordsFromManyString($conf["search::findManyKeyWordsFromManyString"]);
-
 
24090
		unset($conf["search::findManyKeyWordsFromManyString"]);
-
 
24091
		
24042
			#如果分割失敗
24092
		#如果執行失敗
24043
			if($spiltString["status"]==="false"){
24093
		if($findManyKeyWordsFromManyString["status"]==="false"){
24044
			
24094
		
24045
				#設置執行失敗
24095
			#設置執行失敗
24046
				$result["status"]="false";
24096
			$result["status"]="false";
24047
 
24097
 
24048
				#設置錯誤訊息
24098
			#設置錯誤訊息
24049
				$result["error"]=$spiltString;
24099
			$result["error"]=$findManyKeyWordsFromManyString;
24050
 
24100
 
24051
				#回傳結果
24101
			#回傳結果
24052
				return $result;
24102
			return $result;
24053
				
24103
			
24054
				}#if end
24104
			}#if end
24055
				
-
 
24056
			#取得時間
-
 
24057
			$time=$spiltString["dataArray"][2];
-
 
24058
		
24105
		
24059
			#用 "rip=" 切割
-
 
24060
			#函式說明:
-
 
24061
			#將固定格式的字串分開,並回傳分開的結果。
24106
		#初始化儲存可能有問題的存取Dovecot服務遠端ip
24062
			#回傳結果:
-
 
24063
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24064
			#$result["error"],錯誤訊息陣列
-
 
24065
			#$result["function"],當前執行的函式名稱.
-
 
24066
			#$result["argu"],使用的參數.
-
 
24067
			#$result["oriStr"],要分割的原始字串內容
-
 
24068
			#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
-
 
24069
			#$result["dataCounts"],爲總共分成幾段
-
 
24070
			#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
-
 
24071
			#必填參數:
-
 
24072
			#$conf["stringIn"],字串,要處理的字串.
-
 
24073
			$conf["stringProcess::spiltString"]["stringIn"]=$line;
-
 
24074
			#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
-
 
24075
			$conf["stringProcess::spiltString"]["spiltSymbol"]="rip=";
-
 
24076
			#可省略參數:
-
 
24077
			#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
-
 
24078
			$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
-
 
24079
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
-
 
24080
			unset($conf["stringProcess::spiltString"]);
24107
		$rickyIpsOfDocecot=array();
24081
		
24108
		
-
 
24109
		#針對每個符合的log行
-
 
24110
		foreach($findManyKeyWordsFromManyString["foundedTrueKey"] as $index => $oriLog){
-
 
24111
		
24082
			#如果分割失敗
24112
			#找到的關鍵字
24083
			if($spiltString["status"]==="false"){
24113
			$foundKeyWord=$findManyKeyWordsFromManyString["foundedTrueKeyWords"][$index][0];
24084
			
24114
			
-
 
24115
			#依照符合的關鍵字
-
 
24116
			switch($foundKeyWord){
-
 
24117
			
-
 
24118
				#如果是認證失敗
-
 
24119
				case $keyWordAF:
-
 
24120
		
-
 
24121
					#範例
-
 
24122
					#Sep 29 08:41:18 qbpwcf.org auth[3776113]: pam_unix(dovecot:auth): authentication failure; logname=liveuser uid=0 euid=0 tty=dovecot ruser=spam rhost=62.60.130.148
-
 
24123
					#${month} ${day} ${hour}:${min}:${sec} ${doNotNeed} : authentication failure; logname=${account} ${doNotNeed} rhost=${ip}
-
 
24124
		
-
 
24125
					#取得時間點跟遠端IP
-
 
24126
					#函式說明:
-
 
24127
					#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
-
 
24128
					#回傳結果:
-
 
24129
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24130
					#$reuslt["error"],執行不正常結束的錯訊息陣列.
-
 
24131
					#$result["function"],當前執行的函式名稱.
-
 
24132
					#$result["argu"],所使用的參數.
-
 
24133
					#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
-
 
24134
					#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
-
 
24135
					#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
-
 
24136
					#必填參數:
-
 
24137
					#$conf["input"],字串,要檢查的字串.
-
 
24138
					$conf["search::findSpecifyStrFormat"]["input"]=$oriLog;
-
 
24139
					#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
24140
					$conf["search::findSpecifyStrFormat"]["format"]="\${month} \${day} \${hour}:\${min}:\${sec} \${doNotNeed} : authentication failure; logname=\${account} \${doNotNeed} rhost=\${ip}";
-
 
24141
					#可省略參數:
-
 
24142
					#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
24143
					#$conf["search::findSpecifyStrFormat"]["varEqual"]=array(null,"found");
24085
				#設置執行失敗
24144
					#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
-
 
24145
					#$conf["varCon"]=array("no_tail"=>" not");
-
 
24146
					#參考資料:
-
 
24147
					#無.
-
 
24148
					#備註:
-
 
24149
					#無.
-
 
24150
					$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
-
 
24151
					unset($conf["search::findSpecifyStrFormat"]);
-
 
24152
					
-
 
24153
					#如果分割失敗
-
 
24154
					if($findSpecifyStrFormat["status"]==="false"){
-
 
24155
					
-
 
24156
						#設置執行失敗
24086
				$result["status"]="false";
24157
						$result["status"]="false";
24087
 
24158
 
24088
				#設置錯誤訊息
24159
						#設置錯誤訊息
24089
				$result["error"]=$spiltString;
24160
						$result["error"]=$findSpecifyStrFormat;
24090
 
24161
 
24091
				#回傳結果
24162
						#回傳結果
-
 
24163
						return $result;
-
 
24164
						
-
 
24165
						}#if end
-
 
24166
					
-
 
24167
					#如果沒有符合格式
-
 
24168
					if($findSpecifyStrFormat["found"]==="false"){
-
 
24169
						
-
 
24170
						#跳過該行
-
 
24171
						continue 2;
-
 
24172
					
-
 
24173
						}#if end
-
 
24174
					
-
 
24175
					#取得時間點
24092
				return $result;
24176
					$time=$findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0].":".$findSpecifyStrFormat["parsedVar"]["sec"][0];
24093
				
24177
				
24094
				}#if end 
24178
					#取得ip
-
 
24179
					$ip=$findSpecifyStrFormat["parsedVar"]["ip"][0];
-
 
24180
					
-
 
24181
					#儲存其資訊
-
 
24182
					$rickyIpsOfDocecot[$ip][]=array("time"=>$time,"ip"=>$ip,"log"=>$oriLog);
24095
				
24183
				
-
 
24184
					#跳出  switch
-
 
24185
					break;
-
 
24186
				
-
 
24187
				#如果是未認證或SSL錯誤
-
 
24188
				case $keyWordNAA:
-
 
24189
				
-
 
24190
					#範例
24096
			#如果不存在 client ip 字串
24191
					#Feb 23 07:59:30 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=<70q2l/W7PBhliNc9>
-
 
24192
					#Sep 29 06:53:42 qbpwcf.org dovecot[98]: imap-login: Disconnected: Connection closed (no auth attempts in 0 secs): user=<>, rip=3.231.151.171, lip=169.254.2.1, TLS, session=<MaKQGOs/wIMD55er>
-
 
24193
					#${month} ${day} ${hour}:${min}:${sec} ${doNotNeed} ( no auth attempts in ${doNotNeed} user=<${account}>, rip=${ip}, ${doNotNeed}
-
 
24194
					
-
 
24195
					#取得時間點跟遠端IP
-
 
24196
					#函式說明:
-
 
24197
					#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
-
 
24198
					#回傳結果:
-
 
24199
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24200
					#$reuslt["error"],執行不正常結束的錯訊息陣列.
-
 
24201
					#$result["function"],當前執行的函式名稱.
-
 
24202
					#$result["argu"],所使用的參數.
-
 
24203
					#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
-
 
24204
					#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
-
 
24205
					#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
-
 
24206
					#必填參數:
-
 
24207
					#$conf["input"],字串,要檢查的字串.
-
 
24208
					$conf["search::findSpecifyStrFormat"]["input"]=$oriLog;
-
 
24209
					#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
24210
					$conf["search::findSpecifyStrFormat"]["format"]="\${month} \${day} \${hour}:\${min}:\${sec} \${doNotNeed} ( no auth attempts in \${doNotNeed} user=<\${account}>, rip=\${ip}, \${doNotNeed}";
-
 
24211
					#可省略參數:
-
 
24212
					#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
24213
					#$conf["search::findSpecifyStrFormat"]["varEqual"]=array(null,"found");
24097
			if(!isset($spiltString["dataArray"][1])){
24214
					#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
-
 
24215
					#$conf["varCon"]=array("no_tail"=>" not");
-
 
24216
					#參考資料:
-
 
24217
					#無.
-
 
24218
					#備註:
-
 
24219
					#無.
-
 
24220
					$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
-
 
24221
					unset($conf["search::findSpecifyStrFormat"]);
-
 
24222
					
-
 
24223
					#如果分割失敗
-
 
24224
					if($findSpecifyStrFormat["status"]==="false"){
-
 
24225
					
-
 
24226
						#設置執行失敗
-
 
24227
						$result["status"]="false";
-
 
24228
 
-
 
24229
						#設置錯誤訊息
-
 
24230
						$result["error"]=$findSpecifyStrFormat;
-
 
24231
 
-
 
24232
						#回傳結果
-
 
24233
						return $result;
-
 
24234
						
-
 
24235
						}#if end
24098
			
24236
					
-
 
24237
					#如果沒有符合格式
-
 
24238
					if($findSpecifyStrFormat["found"]==="false"){
-
 
24239
						
24099
				#跳過
24240
						#跳過該行
-
 
24241
						continue 2;
-
 
24242
					
-
 
24243
						}#if end
-
 
24244
					
-
 
24245
					#取得時間點
24100
				continue;
24246
					$time=$findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0].":".$findSpecifyStrFormat["parsedVar"]["sec"][0];
-
 
24247
				
-
 
24248
					#取得ip
-
 
24249
					$ip=$findSpecifyStrFormat["parsedVar"]["ip"][0];
-
 
24250
					
-
 
24251
					#儲存其資訊
-
 
24252
					$rickyIpsOfDocecot[$ip][]=array("time"=>$time,"ip"=>$ip,"log"=>$oriLog);
-
 
24253
					
-
 
24254
					#跳出  switch
-
 
24255
					break;
-
 
24256
					
-
 
24257
				#不應該執行到這邊
-
 
24258
				default:
24101
			
24259
			
-
 
24260
					#設置執行失敗
-
 
24261
					$result["status"]="false";
-
 
24262
 
-
 
24263
					#設置錯誤訊息
-
 
24264
					$result["error"][]="unexpected error";
-
 
24265
 
-
 
24266
					#設置錯誤訊息
-
 
24267
					$result["error"][]=$findManyKeyWordsFromManyString;
-
 
24268
 
24102
				}#if end
24269
					#回傳結果
-
 
24270
					return $result;
24103
			
24271
			
24104
			#取得含有 client IP 的字串
24272
				}#switch end
24105
			$clientIpStr=$spiltString["dataArray"][1];
-
 
24106
			
24273
			
24107
			#用 "," 切割
24274
			}#foreach end
24108
			#函式說明:
-
 
24109
			#將固定格式的字串分開,並回傳分開的結果。
-
 
24110
			#回傳結果:
-
 
24111
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24112
			#$result["error"],錯誤訊息陣列
-
 
24113
			#$result["function"],當前執行的函式名稱.
-
 
24114
			#$result["argu"],使用的參數.
-
 
24115
			#$result["oriStr"],要分割的原始字串內容
-
 
24116
			#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
-
 
24117
			#$result["dataCounts"],爲總共分成幾段
-
 
24118
			#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
-
 
24119
			#必填參數:
-
 
24120
			#$conf["stringIn"],字串,要處理的字串.
-
 
24121
			$conf["stringProcess::spiltString"]["stringIn"]=$clientIpStr;
-
 
24122
			#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
-
 
24123
			$conf["stringProcess::spiltString"]["spiltSymbol"]=",";
-
 
24124
			#可省略參數:
-
 
24125
			#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
-
 
24126
			$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
-
 
24127
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
-
 
24128
			unset($conf["stringProcess::spiltString"]);
-
 
24129
		
24275
		
24130
			#如果分割失敗
24276
		#初始化儲存可能有問題的ip資訊
24131
			if($spiltString["status"]==="false"){
24277
		$risky_ips=array();
24132
			
24278
		
24133
				#設置執行失敗
24279
		#針對每個ip
24134
				$result["status"]="false";
-
 
24135
 
-
 
24136
				#設置錯誤訊息
-
 
24137
				$result["error"]=$spiltString;
24280
		foreach($rickyIpsOfDocecot as $ip => $info){
24138
 
-
 
24139
				#回傳結果
-
 
24140
				return $result;
-
 
24141
				
-
 
24142
				}#if end
-
 
24143
				
24281
		
24144
			#取得來源ip
24282
			#如果同樣ip不到3次
24145
			$clientIp=$spiltString["dataArray"][0];
24283
			if(count($rickyIpsOfDocecot[$ip])<3){
24146
			
24284
			
24147
			#如果計數不存在
-
 
24148
			if(!isset($attacker_ips_imap[$time][$clientIp])){
-
 
24149
				
-
 
24150
				#初始計數為1
24285
				#換看下個ip
24151
				$attacker_ips_imap[$time][$clientIp]=1;
-
 
24152
				
24286
				continue;
24153
				#記錄log行內容
-
 
24154
				$attacker_ips_imap[$time]["log"]=$line;
-
 
24155
			
24287
			
24156
				}#if end
24288
				}#if end
24157
				
24289
				
24158
			#反之
24290
			#儲存可能有問題的ip資訊
24159
			else{
24291
			$risky_ips[]=$rickyIpsOfDocecot[$ip];
24160
			
24292
			
24161
				#計數加1
-
 
24162
				$attacker_ips_imap[$time][$clientIp]++;
-
 
24163
				
-
 
24164
				}#else end
-
 
24165
		
-
 
24166
			}#foreach end
24293
			}#foreach end
24167
		
24294
		
24168
		#存放真正有問題的imap ip清單
24295
		#針對每個可能有問題ip的每個log行
24169
		$ips_imap=array();
-
 
24170
 
-
 
24171
		#針對每個時間點
-
 
24172
		foreach($attacker_ips_imap as $time=>$times){
-
 
24173
	
-
 
24174
			#針對每個列管的ip
-
 
24175
			foreach($times as $ip => $count){
24296
		foreach($risky_ips as $ip => $infos){
24176
		
24297
		
-
 
24298
			#初始化記錄ip來訪不大於1分鐘的計數
-
 
24299
			$count=0;
-
 
24300
			
-
 
24301
			#針對每個log行資訊
-
 
24302
			foreach($infos as $index => $info){
-
 
24303
			
-
 
24304
				#unixtime
-
 
24305
				$time=strtotime($info["time"]);
-
 
24306
			
-
 
24307
				#如果沒有下筆記錄
-
 
24308
				if(!isset($risky_ips[$index+1])){
-
 
24309
				
-
 
24310
					#結束 foreach
-
 
24311
					break;
-
 
24312
					
-
 
24313
					}#if end
-
 
24314
			
24177
				#如果是log
24315
				#取得unixtime
-
 
24316
				$next_time=strtotime($risky_ips[$index+1]["time"]);
-
 
24317
			
-
 
24318
				#如果時間間隔大於60秒
24178
				if($ip==="log"){
24319
				if($next_time-$time>60){
24179
				
24320
				
24180
					#跳過
24321
					#跳過,看下筆記錄
24181
					continue;
24322
					continue;
24182
				
24323
				
24183
					}#if end
24324
					}#if end
24184
		
-
 
24185
				#如果累積次數達到3
-
 
24186
				if($count>=3){
-
 
24187
			
-
 
24188
					#儲存有問題ip的資訊
-
 
24189
					$info=array();
-
 
24190
					
24325
					
24191
					#記錄 ip
24326
				#計數+1
24192
					$info["ip"]=$ip;
24327
				$count++;
24193
					
-
 
24194
					#記錄該行 log 內容
-
 
24195
					$info["log"]=$attacker_ips_imap[$time]["log"];
-
 
24196
			
-
 
24197
					#記錄時間
-
 
24198
					$info["time"]=$time;
-
 
24199
			
24328
			
-
 
24329
				}#foreach end
-
 
24330
				
24200
					#記錄有問題的imap ip清單
24331
			#如果有達到3次
24201
					$ips_imap[]=$info;
24332
			if($count>=3){
24202
			
24333
			
24203
					}#if end
24334
				#記錄攻擊者的IP
-
 
24335
				$$attacker_ips_imap[]=$ip;
24204
			
24336
			
24205
				}#foreach end
24337
				}#if end
24206
 
-
 
24207
			}#foreach end
-
 
24208
		
24338
		
-
 
24339
			}#foreach end
-
 
24340
				
24209
		#記錄ssh攻擊者的IP
24341
		#記錄ssh攻擊者的IP
24210
		$attacker_ips_ssh=array();
24342
		$attacker_ips_ssh=array();
24211
		
24343
		
24212
		#透過 journalctl -a -e | grep sshd | grep failed 來取得 ssh 攻擊者的 IP
24344
		#透過 journalctl -a -e --unit=sshd.service來取得 ssh 攻擊者的 IP
24213
		#函式說明:
24345
		#函式說明:
24214
		#呼叫shell執行系統命令,並取得回傳的內容.
24346
		#呼叫shell執行系統命令,並取得回傳的內容.
24215
		#回傳的結果:
24347
		#回傳的結果:
24216
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
24348
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
24217
		#$result["error"],錯誤訊息陣列.
24349
		#$result["error"],錯誤訊息陣列.
Line 24226... Line 24358...
24226
		#$result["statusCode"],執行結束後的代碼.
24358
		#$result["statusCode"],執行結束後的代碼.
24227
		#必填參數:
24359
		#必填參數:
24228
		#$conf["command"],字串,要執行的指令與.
24360
		#$conf["command"],字串,要執行的指令與.
24229
		$conf["external::callShell"]["command"]="journalctl";
24361
		$conf["external::callShell"]["command"]="journalctl";
24230
		#$conf["fileArgu"],字串,變數__FILE__的內容.
24362
		#$conf["fileArgu"],字串,變數__FILE__的內容.
24231
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];		
24363
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];
24232
		#可省略參數:
24364
		#可省略參數:
24233
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
24365
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
24234
		$conf["external::callShell"]["argu"]=array("-a","-e","|","grep","sshd","|","grep","failed");
24366
		$conf["external::callShell"]["argu"]=array("-a","-e","--unit=sshd.service");
24235
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函式的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
24367
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函式的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
24236
		#$conf["arguIsAddr"]=array();	
24368
		#$conf["arguIsAddr"]=array();	
24237
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
24369
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
24238
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
24370
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
24239
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
24371
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
Line 24263... Line 24395...
24263
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
24395
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
24264
		$callShell=external::callShell($conf["external::callShell"]);
24396
		$callShell=external::callShell($conf["external::callShell"]);
24265
		unset($conf["external::callShell"]);
24397
		unset($conf["external::callShell"]);
24266
		
24398
		
24267
		#如果執行失敗
24399
		#如果執行失敗
24268
		if($callShell["status"]==="false" && ( $callShell["statusCode"]!==0 && $callShell["statusCode"]!==1 ) ){
24400
		if($callShell["status"]==="false"){
24269
		
24401
		
24270
			#設置執行失敗
24402
			#設置執行失敗
24271
			$result["status"]="false";
24403
			$result["status"]="false";
24272
 
24404
 
24273
			#設置錯誤訊息
24405
			#設置錯誤訊息
Line 24278... Line 24410...
24278
			
24410
			
24279
			}#if end
24411
			}#if end
24280
			
24412
			
24281
		/*
24413
		/*
24282
		得到的範例輸出
24414
		得到的範例輸出
24283
		Apr 03 07:35:05 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858888]: USER_LOGIN pid=2858888 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=login acct="(unknown)" exe="/usr/sbin/sshd" hostname=? addr=81.30.179.11 terminal=ssh res=failed'
24415
		Sep 30 00:36:36 silverblue-guest.qbpwcf.org sshd-session[1607010]: Invalid user lizepeng from 60.213.10.69 port 39156
24284
		Apr 03 07:35:07 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858891]: USER_AUTH pid=2858891 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=PAM:authentication grantors=? acct="root" exe="/usr/sbin/sshd" hostname=49.88.112.77 addr=49.88.112.77 terminal=ssh res=failed'
24416
		Sep 30 00:36:36 silverblue-guest.qbpwcf.org sshd-session[1607010]: pam_unix(sshd:auth): check pass; user unknown
24285
		Apr 03 07:35:09 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858891]: USER_AUTH pid=2858891 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=PAM:authentication grantors=? acct="root" exe="/usr/sbin/sshd" hostname=49.88.112.77 addr=49.88.112.77 terminal=ssh res=failed'
24417
		Sep 30 00:36:36 silverblue-guest.qbpwcf.org sshd-session[1607010]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=60.213.10.69
24286
		Apr 03 07:35:12 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858891]: USER_AUTH pid=2858891 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=PAM:authentication grantors=? acct="root" exe="/usr/sbin/sshd" hostname=49.88.112.77 addr=49.88.112.77 terminal=ssh res=failed'
24418
		Sep 30 00:36:38 silverblue-guest.qbpwcf.org sshd-session[1607010]: Failed password for invalid user lizepeng from 60.213.10.69 port 39156 ssh2
24287
		Apr 03 07:35:14 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858891]: USER_LOGIN pid=2858891 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=login acct="root" exe="/usr/sbin/sshd" hostname=? addr=49.88.112.77 terminal=ssh res=failed'
24419
		Sep 30 00:36:40 silverblue-guest.qbpwcf.org sshd-session[1607010]: Connection closed by invalid user lizepeng 60.213.10.69 port 39156 [preauth]
24288
		Apr 03 07:35:22 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858896]: USER_AUTH pid=2858896 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=PAM:authentication grantors=? acct="root" exe="/usr/sbin/sshd" hostname=49.88.112.77 addr=49.88.112.77 terminal=ssh res=failed'
24420
		Sep 30 05:54:01 silverblue-guest.qbpwcf.org sshd-session[2346022]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=60.213.10.81  user=root
24289
		Apr 03 07:35:23 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858901]: USER_AUTH pid=2858901 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=PAM:authentication grantors=? acct="bot" exe="/usr/sbin/sshd" hostname=103.149.27.65 addr=103.149.27.65 terminal=ssh res=failed'
24421
		Sep 30 05:54:03 silverblue-guest.qbpwcf.org sshd-session[2346022]: Failed password for root from 60.213.10.81 port 41644 ssh2
24290
		Apr 03 07:35:23 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858899]: USER_AUTH pid=2858899 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=PAM:authentication grantors=? acct="insserver" exe="/usr/sbin/sshd" hostname=129.146.81.43 addr=129.146.81.43 terminal=ssh res=failed'
24422
		Sep 30 05:54:04 silverblue-guest.qbpwcf.org sshd-session[2346022]: Connection closed by authenticating user root 60.213.10.81 port 41644 [preauth]
24291
		Apr 03 07:35:24 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858896]: USER_AUTH pid=2858896 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=PAM:authentication grantors=? acct="root" exe="/usr/sbin/sshd" hostname=49.88.112.77 addr=49.88.112.77 terminal=ssh res=failed'
24423
		Sep 30 05:54:20 silverblue-guest.qbpwcf.org sshd-session[2346860]: Invalid user qy from 60.213.10.81 port 42960
24292
		Apr 03 07:35:25 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858901]: USER_LOGIN pid=2858901 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=login acct="(unknown)" exe="/usr/sbin/sshd" hostname=? addr=103.149.27.65 terminal=ssh res=failed'
24424
		Sep 30 05:54:21 silverblue-guest.qbpwcf.org sshd-session[2346860]: pam_unix(sshd:auth): check pass; user unknown
24293
		Apr 03 07:35:25 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858899]: USER_LOGIN pid=2858899 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=login acct="(unknown)" exe="/usr/sbin/sshd" hostname=? addr=129.146.81.43 terminal=ssh res=failed'
24425
		Sep 30 05:54:21 silverblue-guest.qbpwcf.org sshd-session[2346860]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=60.213.10.81
24294
		Apr 03 07:35:27 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858896]: USER_AUTH pid=2858896 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=PAM:authentication grantors=? acct="root" exe="/usr/sbin/sshd" hostname=49.88.112.77 addr=49.88.112.77 terminal=ssh res=failed'
24426
		Sep 30 05:54:22 silverblue-guest.qbpwcf.org sshd-session[2346860]: Failed password for invalid user qy from 60.213.10.81 port 42960 ssh2
24295
		Apr 03 07:35:29 ip-172-31-46-128.ap-southeast-1.compute.internal audit[2858896]: USER_LOGIN pid=2858896 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=login acct="root" exe="/usr/sbin/sshd" hostname=? addr=49.88.112.77 terminal=ssh res=failed'
24427
		Sep 30 05:54:23 silverblue-guest.qbpwcf.org sshd-session[2346860]: Connection closed by invalid user qy 60.213.10.81 port 42960 [preauth]
24296
		Mar 07 12:14:24 mail.qbpwcf.org sshd[3253686]: error: connect_to 127.0.0.1 port 5950: failed.
24428
		Sep 30 06:08:06 silverblue-guest.qbpwcf.org sshd-session[2378295]: Connection closed by 167.94.145.105 port 55712 [preauth]
24297
		*/	
-
 
24298
					
-
 
24299
		#針對每列輸出
-
 
24300
		foreach($callShell["output"] as $line){
-
 
24301
		
-
 
24302
			#用 "addr=" 區分內容
-
 
24303
			#函式說明:
-
 
24304
			#將固定格式的字串分開,並回傳分開的結果。
-
 
24305
			#回傳結果:
-
 
24306
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24307
			#$result["error"],錯誤訊息陣列
-
 
24308
			#$result["function"],當前執行的函式名稱.
-
 
24309
			#$result["argu"],使用的參數.
-
 
24310
			#$result["oriStr"],要分割的原始字串內容
-
 
24311
			#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
-
 
24312
			#$result["dataCounts"],爲總共分成幾段
-
 
24313
			#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
24429
		Sep 30 08:09:51 silverblue-guest.qbpwcf.org sshd-session[2662907]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=183.13.207.69  user=root
24314
			#必填參數:
-
 
24315
			#$conf["stringIn"],字串,要處理的字串.
-
 
24316
			$conf["stringProcess::spiltString"]["stringIn"]=$line;
-
 
24317
			#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
-
 
24318
			$conf["stringProcess::spiltString"]["spiltSymbol"]="addr=";
-
 
24319
			#可省略參數:
-
 
24320
			#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
24430
		Sep 30 08:09:54 silverblue-guest.qbpwcf.org sshd-session[2662907]: Failed password for root from 183.13.207.69 port 18258 ssh2
24321
			$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
-
 
24322
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
-
 
24323
			unset($conf["stringProcess::spiltString"]);
-
 
24324
		
-
 
24325
			#如果分割失敗
-
 
24326
			if($spiltString["status"]==="false"){
-
 
24327
			
-
 
24328
				#設置執行失敗
-
 
24329
				$result["status"]="false";
-
 
24330
 
-
 
24331
				#設置錯誤訊息
-
 
24332
				$result["error"]=$spiltString;
-
 
24333
 
-
 
24334
				#回傳結果
-
 
24335
				return $result;
-
 
24336
				
-
 
24337
				}#if end
-
 
24338
				
-
 
24339
			#如果沒有找到分割用的關鍵字
-
 
24340
			if($spiltString["found"]==="false"){
-
 
24341
			
-
 
24342
				#設置執行失敗
-
 
24343
				$result["status"]="true";
-
 
24344
 
-
 
24345
				#設置警告訊息
-
 
24346
				$result["warning"][]=$spiltString;
-
 
24347
				
-
 
24348
				#跳過
-
 
24349
				continue;
-
 
24350
				
-
 
24351
				}#if end
-
 
24352
			
-
 
24353
			#取得IP位址開頭的字串	
-
 
24354
			$ipStr=$spiltString["dataArray"][1];
-
 
24355
			
-
 
24356
			#用 " " 區分內容
-
 
24357
			#函式說明:
-
 
24358
			#將固定格式的字串分開,並回傳分開的結果。
-
 
24359
			#回傳結果:
-
 
24360
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24361
			#$result["error"],錯誤訊息陣列
-
 
24362
			#$result["function"],當前執行的函式名稱.
-
 
24363
			#$result["argu"],使用的參數.
-
 
24364
			#$result["oriStr"],要分割的原始字串內容
-
 
24365
			#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
24431
		Sep 30 08:09:54 silverblue-guest.qbpwcf.org sshd-session[2662907]: Connection closed by authenticating user root 183.13.207.69 port 18258 [preauth]
24366
			#$result["dataCounts"],爲總共分成幾段
-
 
24367
			#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
24432
		Sep 30 08:10:11 silverblue-guest.qbpwcf.org sshd-session[2663658]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=183.13.207.69  user=root
24368
			#必填參數:
-
 
24369
			#$conf["stringIn"],字串,要處理的字串.
-
 
24370
			$conf["stringProcess::spiltString"]["stringIn"]=$ipStr;
-
 
24371
			#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
-
 
24372
			$conf["stringProcess::spiltString"]["spiltSymbol"]=" ";
-
 
24373
			#可省略參數:
-
 
24374
			#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
24433
		Sep 30 08:10:13 silverblue-guest.qbpwcf.org sshd-session[2663658]: Failed password for root from 183.13.207.69 port 17293 ssh2
24375
			$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
-
 
24376
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
24434
		Sep 30 08:10:13 silverblue-guest.qbpwcf.org sshd-session[2663658]: Connection closed by authenticating user root 183.13.207.69 port 17293 [preauth]
24377
			unset($conf["stringProcess::spiltString"]);
-
 
24378
		
-
 
24379
			#如果分割失敗
-
 
24380
			if($spiltString["status"]==="false"){
-
 
24381
			
-
 
24382
				#設置執行失敗
-
 
24383
				$result["status"]="false";
-
 
24384
 
-
 
24385
				#設置錯誤訊息
-
 
24386
				$result["error"]=$spiltString;
-
 
24387
 
-
 
24388
				#回傳結果
-
 
24389
				return $result;
-
 
24390
				
-
 
24391
				}#if end
-
 
24392
				
24435
		*/
24393
			#如果沒有找到分割用的關鍵字
-
 
24394
			if($spiltString["found"]==="false"){
-
 
24395
			
-
 
24396
				#設置執行失敗
-
 
24397
				$result["status"]="false";
-
 
24398
 
-
 
24399
				#設置錯誤訊息
-
 
24400
				$result["error"]=$spiltString;
-
 
24401
 
24436
 
24402
				#回傳結果
-
 
24403
				return $result;
-
 
24404
				
-
 
24405
				}#if end
-
 
24406
			
-
 
24407
			#取得IP位址	
24437
		#認證失敗的關鍵字
24408
			$ip=$spiltString["dataArray"][0];
24438
		$keyWordFP=" Failed password ";
24409
				
-
 
24410
			#用 " " 區分內容
-
 
24411
			#函式說明:
-
 
24412
			#將固定格式的字串分開,並回傳分開的結果。
-
 
24413
			#回傳結果:
-
 
24414
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24415
			#$result["error"],錯誤訊息陣列
-
 
24416
			#$result["function"],當前執行的函式名稱.
-
 
24417
			#$result["argu"],使用的參數.
-
 
24418
			#$result["oriStr"],要分割的原始字串內容
-
 
24419
			#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
-
 
24420
			#$result["dataCounts"],爲總共分成幾段
-
 
24421
			#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
-
 
24422
			#必填參數:
-
 
24423
			#$conf["stringIn"],字串,要處理的字串.
-
 
24424
			$conf["stringProcess::spiltString"]["stringIn"]=$line;
-
 
24425
			#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
-
 
24426
			$conf["stringProcess::spiltString"]["spiltSymbol"]=" ";
-
 
24427
			#可省略參數:
-
 
24428
			#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
-
 
24429
			$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
-
 
24430
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
-
 
24431
			unset($conf["stringProcess::spiltString"]);
-
 
24432
		
24439
		
24433
			#如果分割失敗
-
 
24434
			if($spiltString["status"]==="false"){
-
 
24435
			
-
 
24436
				#設置執行失敗
-
 
24437
				$result["status"]="false";
-
 
24438
 
-
 
24439
				#設置錯誤訊息
-
 
24440
				$result["error"]=$spiltString;
-
 
24441
 
-
 
24442
				#回傳結果
-
 
24443
				return $result;
-
 
24444
				
-
 
24445
				}#if end
-
 
24446
				
-
 
24447
			#如果沒有找到分割用的關鍵字
24440
		#不存在使用者的關鍵字
24448
			if($spiltString["found"]==="false"){
-
 
24449
			
-
 
24450
				#設置執行失敗
-
 
24451
				$result["status"]="false";
24441
		$keyWordIU=" Invalid user ";
24452
 
-
 
24453
				#設置錯誤訊息
-
 
24454
				$result["error"]=$spiltString;
-
 
24455
 
-
 
24456
				#回傳結果
-
 
24457
				return $result;
-
 
24458
				
-
 
24459
				}#if end
-
 
24460
				
-
 
24461
			#取得含有秒數的時間點	
-
 
24462
			$timeWithSec=$spiltString["dataArray"][2];	
-
 
24463
				
-
 
24464
			#用 ":" 區分內容
-
 
24465
			#函式說明:
-
 
24466
			#將固定格式的字串分開,並回傳分開的結果。
-
 
24467
			#回傳結果:
-
 
24468
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24469
			#$result["error"],錯誤訊息陣列
-
 
24470
			#$result["function"],當前執行的函式名稱.
-
 
24471
			#$result["argu"],使用的參數.
-
 
24472
			#$result["oriStr"],要分割的原始字串內容
-
 
24473
			#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
-
 
24474
			#$result["dataCounts"],爲總共分成幾段
-
 
24475
			#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
-
 
24476
			#必填參數:
-
 
24477
			#$conf["stringIn"],字串,要處理的字串.
-
 
24478
			$conf["stringProcess::spiltString"]["stringIn"]=$timeWithSec;
-
 
24479
			#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
-
 
24480
			$conf["stringProcess::spiltString"]["spiltSymbol"]=":";
-
 
24481
			#可省略參數:
-
 
24482
			#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
-
 
24483
			$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
-
 
24484
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
-
 
24485
			unset($conf["stringProcess::spiltString"]);
-
 
24486
		
24442
		
24487
			#如果分割失敗
-
 
24488
			if($spiltString["status"]==="false"){
-
 
24489
			
-
 
24490
				#設置執行失敗
-
 
24491
				$result["status"]="false";
-
 
24492
 
-
 
24493
				#設置錯誤訊息
-
 
24494
				$result["error"]=$spiltString;
-
 
24495
 
-
 
24496
				#回傳結果
-
 
24497
				return $result;
-
 
24498
				
-
 
24499
				}#if end
-
 
24500
				
-
 
24501
			#如果沒有找到分割用的關鍵字
-
 
24502
			if($spiltString["found"]==="false"){
-
 
24503
			
-
 
24504
				#設置執行失敗
-
 
24505
				$result["status"]="false";
-
 
24506
 
-
 
24507
				#設置錯誤訊息
-
 
24508
				$result["error"]=$spiltString;
-
 
24509
 
-
 
24510
				#回傳結果
-
 
24511
				return $result;
-
 
24512
				
-
 
24513
				}#if end	
-
 
24514
				
-
 
24515
			#時間包含到分鐘的字串
24443
		#搜尋符合關鍵字的log行
24516
			$timeWithMin=$spiltString["dataArray"][0].":".$spiltString["dataArray"][1];
-
 
24517
				
-
 
24518
			#如果該時間分對應的ip不存在
-
 
24519
			if(!isset($attacker_ips_ssh[$timeWithMin][$ip])){
-
 
24520
			
-
 
24521
				#初始化為1次
-
 
24522
				$attacker_ips_ssh[$timeWithMin][$ip]["count"]=1;	
-
 
24523
			
-
 
24524
				#記錄內容
-
 
24525
				$attacker_ips_ssh[$timeWithMin][$ip]["line"][]=$line;
-
 
24526
			
-
 
24527
				}#if end	
-
 
24528
				
-
 
24529
			#反之
-
 
24530
			else{
-
 
24531
				#計數+1
-
 
24532
				$attacker_ips_ssh[$timeWithMin][$ip]["count"]++;
-
 
24533
				
-
 
24534
				#記錄內容
-
 
24535
				$attacker_ips_ssh[$timeWithMin][$ip]["line"][]=$line;
-
 
24536
			
-
 
24537
				}#else end
-
 
24538
				
-
 
24539
			}#foreach end
-
 
24540
 
-
 
24541
		#透過 journalctl -a -e | grep sshd | grep 'Invalid user ' | grep ' from ' 來取得 ssh 攻擊者的 IP
-
 
24542
		#函式說明:
24444
		#函式說明:
24543
		#呼叫shell執行系統命令,並取得回傳的內容.
24445
		#檢查多個字串中的每個字串是否有多個關鍵字
24544
		#回傳的結果:
24446
		#回傳結果:
24545
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
24447
		#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
24546
		#$result["error"],錯誤訊息陣列.
24448
		#$result["function"],當前執行的函數名稱.
24547
		#$result["function"],當前執行的函式名稱.
24449
		#$result["error"],錯誤訊息.
24548
		#$result["argu"],使用的參數.
24450
		#$result["argu"],使用的參數.
-
 
24451
		#$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列,與其數值內容.
24549
		#$result["cmd"],執行的指令內容.
24452
		#$result["foundedKeyWords"],找到的關鍵字陣列.
24550
		#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
24453
		#$result["foundedFalseKey"],結果為"false"的被搜尋元素key陣列,與其數值內容.
24551
		#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
24454
		#$result["foundedTrueKeyWords"],二維陣列,各個字串有找到的關鍵字陣列.
24552
		#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
24455
		#$result["foundedAll"],是否每個關鍵字都有找到,"true"代表每個都有找到,"false"代表沒有每個都找到.
24553
		#$result["running"],是否還在執行.
-
 
24554
		#$result["pid"],pid.
-
 
24555
		#$result["statusCode"],執行結束後的代碼.
24456
		#$result["keyWordsIncludeStr"],陣列,儲存有找到關鍵字的來源的索引(sourceIndex)與其內容(sourceVal)跟找到的關鍵字項目陣列(KeyWords).
24556
		#必填參數:
24457
		#必填參數:
24557
		#$conf["command"],字串,要執行的指令與.
24458
		#$conf["keyWords"],字串陣列,想要搜尋的關鍵字.
24558
		$conf["external::callShell"]["command"]="journalctl";
24459
		$conf["search::findManyKeyWordsFromManyString"]["keyWords"]=array($keyWordFP,$keyWordIU);
24559
		#$conf["fileArgu"],字串,變數__FILE__的內容.
24460
		#$conf["stringArray"],字串陣列,要被搜尋的字串內容陣列.
24560
		$conf["external::callShell"]["fileArgu"]=$conf["fileArgu"];		
24461
		$conf["search::findManyKeyWordsFromManyString"]["stringArray"]=$callShell["output"];
24561
		#可省略參數:
24462
		#可省略參數:
24562
		#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
-
 
24563
		$conf["external::callShell"]["argu"]=array("-a","-e","|","grep","sshd","|","grep","Invalid user","|","grep"," from ");
-
 
24564
		#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函式的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
-
 
24565
		#$conf["arguIsAddr"]=array();	
-
 
24566
		#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
-
 
24567
		#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
-
 
24568
		#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
-
 
24569
		#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
24463
		#$conf["completeEqual"],字串,是否內容要完全符合,不能多出任何不符合的內容,預設為"false"不需要完全符合.
24570
		#$conf["enablePrintDescription"]="true";
24464
		#$conf["completeEqual"]="true";
24571
		#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
-
 
24572
		#$conf["printDescription"]="";
-
 
24573
		#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
-
 
24574
		$conf["external::callShell"]["escapeshellarg"]="true";
-
 
24575
		#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
-
 
24576
		#$conf["username"]="";
24465
		#參考資料:
24577
		#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
-
 
24578
		#$conf["password"]="";
24466
		#無.
24579
		#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
-
 
24580
		#$conf["useScript"]="";
-
 
24581
		#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
-
 
24582
		#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
-
 
24583
		#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
-
 
24584
		#$conf["inBackGround"]="";
-
 
24585
		#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
-
 
24586
		#$conf["getErr"]="false";
-
 
24587
		#備註:
24467
		#備註:
24588
		#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
-
 
24589
		#參考資料:
24468
		#無.
24590
		#exec=>http://php.net/manual/en/function.exec.php
-
 
24591
		#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
-
 
24592
		#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
24469
		$findManyKeyWordsFromManyString=search::findManyKeyWordsFromManyString($conf["search::findManyKeyWordsFromManyString"]);
24593
		$callShell=external::callShell($conf["external::callShell"]);
-
 
24594
		unset($conf["external::callShell"]);
24470
		unset($conf["search::findManyKeyWordsFromManyString"]);
24595
		
24471
		
24596
		#如果執行失敗
24472
		#如果執行失敗
24597
		if($callShell["status"]==="false" && ( $callShell["statusCode"]!==0 && $callShell["statusCode"]!==1 ) ){
24473
		if($findManyKeyWordsFromManyString["status"]==="false"){
24598
		
24474
		
24599
			#設置執行失敗
24475
			#設置執行失敗
24600
			$result["status"]="false";
24476
			$result["status"]="false";
24601
 
24477
 
24602
			#設置錯誤訊息
24478
			#設置錯誤訊息
24603
			$result["error"]=$callShell;
24479
			$result["error"]=$findManyKeyWordsFromManyString;
24604
 
24480
 
24605
			#回傳結果
24481
			#回傳結果
24606
			return $result;
24482
			return $result;
24607
			
24483
			
24608
			}#if end
24484
			}#if end
24609
 
-
 
24610
		#得到的範例輸出
-
 
24611
		#Dec 22 15:51:48 mail.qbpwcf.org sshd[3518693]: Invalid user xl from 159.89.131.172 port 44458
-
 
24612
 
-
 
24613
		#debug
-
 
24614
		#var_dump(__LINE__,$callShell["output"]);
-
 
24615
 
-
 
24616
		#針對每列輸出
-
 
24617
		foreach($callShell["output"] as $line){
-
 
24618
		
24485
		
24619
			#備份原始的內容
-
 
24620
			$ori_line=$line;
-
 
24621
		
-
 
24622
			#移除多餘的資訊
-
 
24623
			#函式說明:
-
 
24624
			#將字串特定關鍵字與其前面的內容剔除
24486
		#初始化儲存可能有問題的存取ssh服務遠端ip
24625
			#回傳結果:
-
 
24626
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24627
			#$result["error"],錯誤訊息陣列.
-
 
24628
			#$result["warning"],警告訊息鎮列.
-
 
24629
			#$result["founded"],有無找到定字串"true"代表有,"false"代表沒有.
-
 
24630
			#$result["function"],當前執行的函數名稱.
-
 
24631
			#$result["argu"],使用的參數.
-
 
24632
			#$result["oriStr"],要處理的原始字串內容.
-
 
24633
			#$result["content"],處理好的的字串內容.	
-
 
24634
			#必填參數:
-
 
24635
			#$conf["stringIn"],字串,要處理的字串.
-
 
24636
			$conf["stringProcess::delStrBeforeKeyWord"]["stringIn"]=$line;
-
 
24637
			#$conf["keyWord"],字串,特定字串.
-
 
24638
			$conf["stringProcess::delStrBeforeKeyWord"]["keyWord"]=" Invalid user ";
-
 
24639
			#可省略參數:
-
 
24640
			#$conf["recursive"],字串,預設為"false"代表找到一個關鍵字就會停止;"true"代表會即重複執行,知道沒有關鍵字為止.
-
 
24641
			#$conf["recursive"]="true";
24487
		$risky_ips=array();
24642
			#$conf["lastResult"],陣列,本函式前次執行的結果,若沒有找到關鍵字,則回改回傳該內容.
-
 
24643
			#$conf["lastResult"]=$delStrBeforeKeyWord;
-
 
24644
			#參考資料:
-
 
24645
			#無.
-
 
24646
			#備註:
-
 
24647
			#無.
-
 
24648
			$delStrBeforeKeyWord=stringProcess::delStrBeforeKeyWord($conf["stringProcess::delStrBeforeKeyWord"]);
-
 
24649
			unset($conf["stringProcess::delStrBeforeKeyWord"]);
-
 
24650
		
24488
		
24651
			#debug
24489
		#針對每個符合的log行
24652
			#var_dump(__LINE__,$delStrBeforeKeyWord);
24490
		foreach($findManyKeyWordsFromManyString["foundedTrueKey"] as $index => $oriLog){
24653
		
24491
		
24654
			#如果執行失敗
24492
			#找到的關鍵字
24655
			if($delStrBeforeKeyWord["status"]==="false"){
24493
			$foundKeyWord=$findManyKeyWordsFromManyString["foundedTrueKeyWords"][$index][0];
24656
			
24494
			
-
 
24495
			#依照符合的關鍵字
-
 
24496
			switch($foundKeyWord){
-
 
24497
			
-
 
24498
				#如果是秘密碼錯誤
-
 
24499
				case $keyWordFP:
-
 
24500
		
-
 
24501
					#範例
-
 
24502
					#Sep 30 08:10:13 silverblue-guest.qbpwcf.org sshd-session[2663658]: Failed password for root from 183.13.207.69 port 17293 ssh2
-
 
24503
					#${month} ${day} ${hour}:${min}:${sec} ${doNotNeed} Failed password for ${account} from ${ip} ${doNotNeed}
-
 
24504
		
-
 
24505
					#取得時間點跟遠端IP
-
 
24506
					#函式說明:
-
 
24507
					#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
-
 
24508
					#回傳結果:
-
 
24509
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24510
					#$reuslt["error"],執行不正常結束的錯訊息陣列.
-
 
24511
					#$result["function"],當前執行的函式名稱.
-
 
24512
					#$result["argu"],所使用的參數.
-
 
24513
					#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
-
 
24514
					#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
-
 
24515
					#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
-
 
24516
					#必填參數:
-
 
24517
					#$conf["input"],字串,要檢查的字串.
-
 
24518
					$conf["search::findSpecifyStrFormat"]["input"]=$oriLog;
-
 
24519
					#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
24520
					$conf["search::findSpecifyStrFormat"]["format"]="\${month} \${day} \${hour}:\${min}:\${sec} \${doNotNeed} Failed password for \${account} from \${ip} \${doNotNeed}";
-
 
24521
					#可省略參數:
-
 
24522
					#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
24523
					#$conf["search::findSpecifyStrFormat"]["varEqual"]=array(null,"found");
24657
				#設置執行失敗
24524
					#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
-
 
24525
					#$conf["varCon"]=array("no_tail"=>" not");
-
 
24526
					#參考資料:
-
 
24527
					#無.
-
 
24528
					#備註:
-
 
24529
					#無.
-
 
24530
					$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
-
 
24531
					unset($conf["search::findSpecifyStrFormat"]);
-
 
24532
					
-
 
24533
					#如果分割失敗
-
 
24534
					if($findSpecifyStrFormat["status"]==="false"){
-
 
24535
					
-
 
24536
						#設置執行失敗
24658
				$result["status"]="false";
24537
						$result["status"]="false";
24659
 
24538
 
24660
				#設置錯誤訊息
24539
						#設置錯誤訊息
24661
				$result["error"]=$delStrBeforeKeyWord;
24540
						$result["error"]=$findSpecifyStrFormat;
24662
 
24541
 
24663
				#回傳結果
24542
						#回傳結果
24664
				return $result;
24543
						return $result;
-
 
24544
						
-
 
24545
						}#if end
-
 
24546
					
-
 
24547
					#如果沒有符合格式
-
 
24548
					if($findSpecifyStrFormat["found"]==="false"){
-
 
24549
						
-
 
24550
						#跳過該行
-
 
24551
						continue 2;
-
 
24552
					
-
 
24553
						}#if end
24665
			
24554
					
-
 
24555
					#取得時間點
24666
				}#if end
24556
					$time=$findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0].":".$findSpecifyStrFormat["parsedVar"]["sec"][0];
24667
				
24557
				
-
 
24558
					#取得ip
-
 
24559
					$ip=$findSpecifyStrFormat["parsedVar"]["ip"][0];
-
 
24560
					
-
 
24561
					#儲存其資訊
-
 
24562
					$risky_ips[$ip][]=array("time"=>$time,"ip"=>$ip,"log"=>$oriLog);
-
 
24563
				
-
 
24564
					#跳出  switch
-
 
24565
					break;
-
 
24566
				
-
 
24567
				#如果是不存在的使用者
-
 
24568
				case $keyWordIU:
-
 
24569
				
-
 
24570
					#範例
-
 
24571
					#Sep 30 00:36:36 silverblue-guest.qbpwcf.org sshd-session[1607010]: Invalid user lizepeng from 60.213.10.69 port 39156
-
 
24572
					#${month} ${day} ${hour}:${min}:${sec} ${doNotNeed} Invalid user ${account} from ${ip} port ${doNotNeed}
-
 
24573
					
-
 
24574
					#取得時間點跟遠端IP
-
 
24575
					#函式說明:
-
 
24576
					#尋找字串中是否含有符合格式的內容,且回傳解析好的變數數值.
-
 
24577
					#回傳結果:
-
 
24578
					#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24579
					#$reuslt["error"],執行不正常結束的錯訊息陣列.
-
 
24580
					#$result["function"],當前執行的函式名稱.
-
 
24581
					#$result["argu"],所使用的參數.
-
 
24582
					#$result["found"],是否有找到符合格式的字串內容,"true"代表有找到,"false"代表沒有找到.
-
 
24583
					#$result["content"],陣列,若為n個${*},則當found為"true"時,就會回傳n個元素.
-
 
24584
					#$result["parsedVar"][varName],陣列,解析好的變數陣列,varName為${}中的內容.
-
 
24585
					#必填參數:
-
 
24586
					#$conf["input"],字串,要檢查的字串.
-
 
24587
					$conf["search::findSpecifyStrFormat"]["input"]=$oriLog;
-
 
24588
					#$conf["format"],格式字串,要尋找的格式字串.格式為固定的字串("fixedStr format")與變數("${keyWordVarName}")組成.
-
 
24589
					$conf["search::findSpecifyStrFormat"]["format"]="\${month} \${day} \${hour}:\${min}:\${sec} \${doNotNeed} Invalid user \${account} from \${ip} port \${doNotNeed}";
-
 
24590
					#可省略參數:
-
 
24591
					#$conf["varEqual"],陣列,變數對應的數值,null代表不指定,其他內容代表該變數解析出來必須要為該內容.
-
 
24592
					#$conf["search::findSpecifyStrFormat"]["varEqual"]=array(null,"found");
24668
			#如果沒有理應存在內容
24593
					#$conf["varCon"],陣列,每個varEqual為null者,其是否有其他條件,預設為null代表無其他條件,條件的表示是用陣列的key與value來表達,例如:array("no_tail"=>" not"),就代表變數的結尾不能為" not",可以用的key有"head",代表開頭要有什麼;"no_head",代表不能為什麼開頭;"tail",代表要什麼結尾;"no_tail",代表不能什麼結尾.
-
 
24594
					#$conf["varCon"]=array("no_tail"=>" not");
-
 
24595
					#參考資料:
-
 
24596
					#無.
-
 
24597
					#備註:
-
 
24598
					#無.
-
 
24599
					$findSpecifyStrFormat=search::findSpecifyStrFormat($conf["search::findSpecifyStrFormat"]);
-
 
24600
					unset($conf["search::findSpecifyStrFormat"]);
-
 
24601
					
-
 
24602
					#如果分割失敗
24669
			if($delStrBeforeKeyWord["founded"]==="false"){
24603
					if($findSpecifyStrFormat["status"]==="false"){
24670
			
24604
					
24671
				#設置執行失敗
24605
						#設置執行失敗
24672
				$result["status"]="false";
24606
						$result["status"]="false";
24673
 
24607
 
24674
				#設置錯誤訊息
24608
						#設置錯誤訊息
24675
				$result["error"]=$delStrBeforeKeyWord;
24609
						$result["error"]=$findSpecifyStrFormat;
24676
 
24610
 
24677
				#回傳結果
24611
						#回傳結果
24678
				return $result;
24612
						return $result;
24679
			
24613
						
24680
				}#if end
24614
						}#if end
24681
		
24615
					
24682
			#取得需要的部分
24616
					#如果沒有符合格式
24683
			$line=$delStrBeforeKeyWord["content"];
24617
					if($findSpecifyStrFormat["found"]==="false"){
24684
		
24618
						
24685
			#解析 ip
24619
						#跳過該行
24686
			#函式說明:
24620
						continue 2;
24687
			#將字串進行解析,取得兩個關鍵字中間的內容.
-
 
-
 
24621
					
24688
			#回傳結果:
24622
						}#if end
24689
			#$result["status"],執行正常與否,"false"代表不正常,"true"代表正常.
-
 
24690
			#$result["function"],當前執行的函式內容.
-
 
24691
			#$result["error"],錯誤訊息陣列.
24623
					
24692
			#$result["content"],取得的內容.
24624
					#取得時間點
24693
			#$result["oriStr"],原始的內容.
-
 
24694
			#$result["found"],是否有找到符合條件的內容.
24625
					$time=$findSpecifyStrFormat["parsedVar"]["month"][0]." ".$findSpecifyStrFormat["parsedVar"]["day"][0]." ".$findSpecifyStrFormat["parsedVar"]["hour"][0].":".$findSpecifyStrFormat["parsedVar"]["min"][0].":".$findSpecifyStrFormat["parsedVar"]["sec"][0];
-
 
24626
				
24695
			#必填參數:
24627
					#取得ip
24696
			#$conf["input"],字串,要處理的字串.
-
 
24697
			$conf["stringProcess::getContentBetweenKeyWord"]["input"]=$line;
24628
					$ip=$findSpecifyStrFormat["parsedVar"]["ip"][0];
24698
			#$conf["startKeyWord"],字串,開頭的關鍵字.
-
 
24699
			$conf["stringProcess::getContentBetweenKeyWord"]["startKeyWord"]=" from ";
-
 
24700
			#$conf["endKeyWord"],字串,結束的關鍵字.
-
 
24701
			$conf["stringProcess::getContentBetweenKeyWord"]["endKeyWord"]=" port ";
-
 
24702
			#可省略參數:
-
 
24703
			#無.
24629
					
24704
			#參考資料:
24630
					#儲存其資訊
-
 
24631
					$risky_ips[$ip][]=array("time"=>$time,"ip"=>$ip,"log"=>$oriLog);
24705
			#無.
24632
					
24706
			#備註:
24633
					#跳出  switch
24707
			#無.
24634
					break;
24708
			$getContentBetweenKeyWord=stringProcess::getContentBetweenKeyWord($conf["stringProcess::getContentBetweenKeyWord"]);
-
 
24709
			unset($conf["stringProcess::getContentBetweenKeyWord"]);
-
 
24710
			
24635
					
24711
			#如果執行失敗
24636
				#不應該執行到這邊
24712
			if($getContentBetweenKeyWord["status"]==="false"){
24637
				default:
24713
			
24638
			
24714
				#設置執行失敗
24639
					#設置執行失敗
24715
				$result["status"]="false";
24640
					$result["status"]="false";
24716
 
24641
 
24717
				#設置錯誤訊息
24642
					#設置錯誤訊息
24718
				$result["error"]=$getContentBetweenKeyWord;
24643
					$result["error"][]="unexpected error";
24719
 
24644
 
24720
				#回傳結果
-
 
24721
				return $result;
-
 
24722
			
-
 
24723
				}#if end
-
 
24724
			
-
 
24725
			#如果沒有有該有的關鍵字
-
 
24726
			if($getContentBetweenKeyWord["found"]==="false"){
-
 
24727
			
-
 
24728
				#設置執行失敗
-
 
24729
				$result["status"]="false";
-
 
24730
 
-
 
24731
				#設置錯誤訊息
24645
					#設置錯誤訊息
24732
				$result["error"]=$getContentBetweenKeyWord;
24646
					$result["error"][]=$findManyKeyWordsFromManyString;
24733
 
24647
 
24734
				#回傳結果
24648
					#回傳結果
24735
				return $result;
24649
					return $result;
24736
			
24650
			
24737
				}#if end
24651
				}#switch end
24738
				
24652
				
24739
			#取得ip
24653
			}#foreach end
24740
			$ip=$getContentBetweenKeyWord["content"];	
-
 
24741
			
24654
			
24742
			#解析time
-
 
24743
			#用 " " 區分內容
-
 
24744
			#函式說明:
-
 
24745
			#將固定格式的字串分開,並回傳分開的結果。
-
 
24746
			#回傳結果:
-
 
24747
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24748
			#$result["error"],錯誤訊息陣列
-
 
24749
			#$result["function"],當前執行的函式名稱.
24655
		#針對每個可能有問題ip的每個log行
24750
			#$result["argu"],使用的參數.
-
 
24751
			#$result["oriStr"],要分割的原始字串內容
-
 
24752
			#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
-
 
24753
			#$result["dataCounts"],爲總共分成幾段
-
 
24754
			#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
-
 
24755
			#必填參數:
-
 
24756
			#$conf["stringIn"],字串,要處理的字串.
-
 
24757
			$conf["stringProcess::spiltString"]["stringIn"]=$ori_line;
-
 
24758
			#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
-
 
24759
			$conf["stringProcess::spiltString"]["spiltSymbol"]=" ";
-
 
24760
			#可省略參數:
-
 
24761
			#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
-
 
24762
			$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
-
 
24763
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
-
 
24764
			unset($conf["stringProcess::spiltString"]);
24656
		foreach($risky_ips as $ip => $infos){
24765
		
24657
		
24766
			#如果分割失敗
24658
			#初始化記錄ip來訪不大於1分鐘的計數
24767
			if($spiltString["status"]==="false"){
24659
			$count=0;
24768
			
24660
			
24769
				#設置執行失敗
24661
			#針對每個log行資訊
24770
				$result["status"]="false";
-
 
24771
 
-
 
24772
				#設置錯誤訊息
-
 
24773
				$result["error"]=$spiltString;
-
 
24774
 
-
 
24775
				#回傳結果
-
 
24776
				return $result;
-
 
24777
				
-
 
24778
				}#if end
-
 
24779
				
-
 
24780
			#如果沒有找到分割用的關鍵字
-
 
24781
			if($spiltString["found"]==="false"){
24662
			foreach($infos as $index => $info){
24782
			
24663
			
24783
				#設置執行失敗
-
 
24784
				$result["status"]="false";
-
 
24785
 
-
 
24786
				#設置錯誤訊息
-
 
24787
				$result["error"]=$spiltString;
-
 
24788
 
-
 
24789
				#回傳結果
-
 
24790
				return $result;
-
 
24791
				
-
 
24792
				}#if end
24664
				#unixtime
24793
				
-
 
24794
			#取得含有秒數的時間點
-
 
24795
			$timeWithSec=$spiltString["dataArray"][2];
24665
				$time=strtotime($info["time"]);
24796
				
-
 
24797
			#用 ":" 區分內容
-
 
24798
			#函式說明:
-
 
24799
			#將固定格式的字串分開,並回傳分開的結果。
-
 
24800
			#回傳結果:
-
 
24801
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
-
 
24802
			#$result["error"],錯誤訊息陣列
-
 
24803
			#$result["function"],當前執行的函式名稱.
-
 
24804
			#$result["argu"],使用的參數.
-
 
24805
			#$result["oriStr"],要分割的原始字串內容
-
 
24806
			#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
-
 
24807
			#$result["dataCounts"],爲總共分成幾段
-
 
24808
			#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
-
 
24809
			#必填參數:
-
 
24810
			#$conf["stringIn"],字串,要處理的字串.
-
 
24811
			$conf["stringProcess::spiltString"]["stringIn"]=$timeWithSec;
-
 
24812
			#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
-
 
24813
			$conf["stringProcess::spiltString"]["spiltSymbol"]=":";
-
 
24814
			#可省略參數:
-
 
24815
			#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
-
 
24816
			$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
-
 
24817
			$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
-
 
24818
			unset($conf["stringProcess::spiltString"]);
-
 
24819
		
-
 
24820
			#如果分割失敗
-
 
24821
			if($spiltString["status"]==="false"){
-
 
24822
			
24666
			
24823
				#設置執行失敗
-
 
24824
				$result["status"]="false";
-
 
24825
 
-
 
24826
				#設置錯誤訊息
24667
				#如果沒有下筆記錄
24827
				$result["error"]=$spiltString;
24668
				if(!isset($risky_ips[$index+1])){
24828
 
-
 
24829
				#回傳結果
-
 
24830
				return $result;
-
 
24831
				
24669
				
-
 
24670
					#結束 foreach
24832
				}#if end
24671
					break;
24833
				
24672
					
24834
			#如果沒有找到分割用的關鍵字
-
 
24835
			if($spiltString["found"]==="false"){
24673
					}#if end
24836
			
24674
			
24837
				#設置執行失敗
-
 
24838
				$result["status"]="false";
-
 
24839
 
-
 
24840
				#設置錯誤訊息
-
 
24841
				$result["error"]=$spiltString;
-
 
24842
 
-
 
24843
				#回傳結果
24675
				#取得unixtime
24844
				return $result;
-
 
24845
				
-
 
24846
				}#if end	
-
 
24847
				
-
 
24848
			#時間包含到分鐘的字串
-
 
24849
			$timeWithMin=$spiltString["dataArray"][0].":".$spiltString["dataArray"][1];
-
 
24850
				
-
 
24851
			#如果該時間分對應的ip不存在
-
 
24852
			if(!isset($attacker_ips_ssh[$timeWithMin][$ip])){
24676
				$next_time=strtotime($risky_ips[$index+1]["time"]);
24853
			
24677
			
24854
				#初始化為1次
24678
				#如果時間間隔大於60秒
24855
				$attacker_ips_ssh[$timeWithMin][$ip]["count"]=1;
24679
				if($next_time-$time>60){
24856
				
24680
				
24857
				#記錄內容
24681
					#跳過,看下筆記錄
24858
				$attacker_ips_ssh[$timeWithMin][$ip]["line"][]=$ori_line;
-
 
24859
			
-
 
24860
				}#if end	
24682
					continue;
24861
				
-
 
24862
			#反之
-
 
24863
			else{
-
 
24864
				
24683
				
-
 
24684
					}#if end
-
 
24685
					
24865
				#計數+1
24686
				#計數+1
24866
				$attacker_ips_ssh[$timeWithMin][$ip]["count"]++;
-
 
24867
				
-
 
24868
				#記錄內容
-
 
24869
				$attacker_ips_ssh[$timeWithMin][$ip]["line"][]=$ori_line;
-
 
24870
			
-
 
24871
				}#else end
24687
				$count++;
24872
		
-
 
24873
			}#foreach end
-
 
24874
			
24688
			
24875
		#存放真正有問題的imap ip清單
-
 
24876
		$ips_ssh=array();
-
 
24877
 
-
 
24878
		#針對每個時間點
-
 
24879
		foreach($attacker_ips_ssh as $times){
-
 
24880
	
-
 
24881
			#針對每個列管的ip
-
 
24882
			foreach($times as $ip => $info){
-
 
24883
		
-
 
24884
				#取得計數
24689
				}#foreach end
24885
				$count=$info["count"];
-
 
24886
				
24690
				
24887
				#取得該行
-
 
24888
				$ori_line=$info["line"];
-
 
24889
		
-
 
24890
				#如果累積次數達到3
24691
			#如果有達到3次
24891
				if($count>=3){
24692
			if($count>=3){
24892
			
-
 
24893
					#儲存該行log
-
 
24894
					$info["log"]=$ori_line;
-
 
24895
			
-
 
24896
					#儲存ip
-
 
24897
					$info["ip"]=$ip;
-
 
24898
			
24693
			
24899
					#記錄有問題的imap ip清單
24694
				#記錄攻擊者的IP
24900
					$ips_ssh[]=$info;
24695
				$attacker_ips_ssh[]=$ip;
24901
			
24696
			
24902
					}#if end
24697
				}#if end
24903
			
24698
		
24904
				}#foreach end
-
 
24905
 
-
 
24906
			}#foreach end
24699
			}#foreach end
24907
					
24700
		
24908
		#合併取得的有問題IP集合
24701
		#合併取得的有問題IP集合
24909
		#$ips,$ips_https,$ips_smtp,$ips_imap,$attacker_ips_namd,ips_ssh
24702
		#$ips,$ips_https,$ips_smtp,$attacker_ips_imap,$attacker_ips_namd,$attacker_ips_ssh
24910
		
24703
		
24911
		#函式說明:
24704
		#函式說明:
24912
		#將多個一維陣列串聯起來,key從0開始排序.
24705
		#將多個一維陣列串聯起來,key從0開始排序.
24913
		#回傳的結果:
24706
		#回傳的結果:
24914
		#$result["status"],"true"表執行正常,"false"代表執行不正常.
24707
		#$result["status"],"true"表執行正常,"false"代表執行不正常.
24915
		#$result["error"],錯誤訊息陣列.
24708
		#$result["error"],錯誤訊息陣列.
24916
		#$result["function"],當前執行的函式.
24709
		#$result["function"],當前執行的函式.
24917
		#$result["content"],合併好的一維陣列.
24710
		#$result["content"],合併好的一維陣列.
24918
		#必填參數:
24711
		#必填參數:
24919
		#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
24712
		#$conf["inputArray"],陣列,要合併的一維陣列變數,例如:=array($array1,$array2);
24920
		$conf["arrays::mergeArray"]["inputArray"]=array($ips,$ips_https,$ips_smtp,$attacker_ips_namd,$ips_imap,$ips_ssh);
24713
		$conf["arrays::mergeArray"]["inputArray"]=array($ips,$ips_https,$ips_smtp,$attacker_ips_namd,$attacker_ips_imap,$attacker_ips_ssh);
24921
		#可省略參數:
24714
		#可省略參數:
24922
		#$conf["allowRepeat"],字串,預設為"true",允許重複的結果;若為"false"則不會出現重複的元素內容.
24715
		#$conf["allowRepeat"],字串,預設為"true",允許重複的結果;若為"false"則不會出現重複的元素內容.
24923
		$conf["arrays::mergeArray"]["allowRepeat"]="false";
24716
		$conf["arrays::mergeArray"]["allowRepeat"]="false";
24924
		$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);
24717
		$mergeArray=arrays::mergeArray($conf["arrays::mergeArray"]);
24925
		unset($conf["arrays::mergeArray"]);
24718
		unset($conf["arrays::mergeArray"]);