Rev 621 | Rev 906 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/php<?php/*QBPWCF, Quick Build PHP website Component base on Fedora Linux.Copyright (C) 2015~2024 Min-Jhin,ChenThis file is part of QBPWCF.QBPWCF is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.QBPWCF is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with QBPWCF. If not, see <http://www.gnu.org/licenses/>.*/#使用命名空間qbpwcfnamespace qbpwcf;#以該檔案的實際位置的 lib path 為 include path 首位exec("cd ".pathinfo(__FILE__)["dirname"]."/../../;pwd;",$output,$status);set_include_path($output[0].PATH_SEPARATOR.get_include_path());#匯入外部套件include("allInOne.php");#初始化 httpd log 的位置$logAddr="/var/log/httpd/";#初始化儲存可能有問題的IP陣列$ips_byIp=array();#函式說明:#抓取命令列的參數.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["argu"],使用的參數陣列.#$result["content"],要回傳的參數陣列.#$result["count"],參數的數量.#必填參數:#無#可省略參數:#$conf["echo"],"true"代表要將抓到的參數一個個印出來,"false"代表用回傳的方式,預設為"false".$conf["echo"]="false";$getArgu=cmd::getArgu($conf);unset($conf);#如果有參數if($getArgu["count"]>1){#如果有參數 -h 或 --helpif($getArgu["content"][1]==="-h" || $getArgu["content"][1]==="--help"){#印出指南,並結束執行.echo basename(__FILE__) . " 用法:".PHP_EOL;echo basename(__FILE__) . " -h/--help 顯示本說明.".PHP_EOL;echo basename(__FILE__) . " -logAddr httpd log 的路徑.".PHP_EOL;exit;}#if end}#if end#函式說明:#解析參數.#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$reuslt["error"],執行不正常結束的錯訊息陣列.#$result["function"],當前執行的函式名稱.#$result["content"],解析好的參數陣列.#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.#$result["program"],字串,執行的程式名稱.#必填參數:#無#可省略參數:#無#備註:#僅能在命令列底下執行.#建議:#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .$conf=array();$parseArgu=cmd::parseArgu($conf);unset($conf);#var_dump($parseArgu);exit;#如果有指定參數if(count($parseArgu["content"])>0){#如果有 logAddr 參數if(isset($parseArgu["content"]["logAddr"])){#取得 logAddr 參數$logAddr=$parseArgu["content"]["logAddr"][0];}#if end}#if end#取得 http 存取記錄的 log#函數說明:#檢查指令的輸出是否含有關鍵字#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["grepCmd"],截取關鍵字的指令.#$result["founded"],是否找到關鍵字,"true"代表有,"false"代表沒有.#$result["content"],關鍵字所在列的輸出.#$result["count"],輸出的列數.#必填參數:#$conf["cmd"],字串,要執行的指令.$conf["cmd"]="cat";#$conf["keyWord"],字串,要檢查是否有關鍵字.$conf["keyWord"]=" 404 ";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["binPath"],字串,應用程式的路徑,預設為"/usr/bin".#$conf["binPath"]="";#$conf["argu"],陣列字串,指令搭配的參數,預設不使用.$conf["argu"]=array($logAddr."access_log");#$conf["excludeGrep"],字串,"true"代表排除含有 " | grep ".$conf["keyWord"] 字樣的輸出;預設為"false",不排除.#$conf["excludeGrep"]="false";#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";$searchOutPut=cmd::searchOutPut($conf);unset($conf);#如果執行失敗if($searchOutPut["status"]==="false"){#設置執行失敗$result["status"]="false";#設置錯誤訊息$result["error"]=$searchOutPut;#回傳結果return $result;}#if end#如果有找到關鍵字 " 404 "if($searchOutPut["founded"]==="true"){#過濾掉對於含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.#函式說明:#檢查多個字串中的每個字串是否有多個關鍵字#回傳的結果:#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。#$result["function"],當前執行的函數名稱.#$result["error"],錯誤訊息.#$result["argu"],使用的參數.#$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列.#$result["foundedKeyWords"].找到的關鍵字.#$result["foundedFalseKey"],結果為"false"的被搜尋元素key陣列.#$result["foundedTrueKeyWords"],二維陣列,各個字串有找到的關鍵字陣列.#$result["foundedAll"],是否每個關鍵字都有找到,"true"代表每個都有找到,"false"代表沒有每個都找到.#必填的參數:$conf["search::findManyKeyWordsFromManyString"]["keyWords"]=array(".css",".js","favicon.ico",".jpg",".png");#想要搜尋的關鍵字$conf["search::findManyKeyWordsFromManyString"]["stringArray"]=$searchOutPut["content"];#要被搜尋的字串內容陣列#可省略的參數:#$conf["completeEqual"]="true";#是否內容要完全符合,不能多出任何不符合的內容,預設為"false"不需要完全符合.$findManyKeyWordsFromManyString=search::findManyKeyWordsFromManyString($conf["search::findManyKeyWordsFromManyString"]);unset($conf["search::findManyKeyWordsFromManyString"]);#如果執行出錯if($findManyKeyWordsFromManyString["status"]==="false"){#設置執行失敗$result["status"]="false";#設置錯誤訊息$result["error"]=$findManyKeyWordsFromManyString;#回傳結果return $result;}#if end#取得不含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.$searchOutPut["content"]=$findManyKeyWordsFromManyString["foundedFalseKey"];#更新計數$searchOutPut["count"]=count($searchOutPut["content"]);#針對每列輸出foreach($searchOutPut["content"] as $line){#涵式說明:#呼叫shell執行系統命令,並取得回傳的內容.#回傳的結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["cmd"],執行的指令內容.#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["running"],是否還在執行.#$result["pid"],pid.#$result["statusCode"],執行結束後的代碼.#必填的參數#$conf["command"],字串,要執行的指令與.$conf["command"]="parse";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.$conf["argu"]=array($line);#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").#$conf["arguIsAddr"]=array();#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.#$conf["printDescription"]="";#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".$conf["escapeshellarg"]="true";#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.#$conf["inBackGround"]="";#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.#$conf["getErr"]="false";#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php$callShell=external::callShell($conf);unset($conf);#如果執行失敗if($callShell["status"]==="false"){#印出結果var_dump($callShell);#結束執行exit;}#if end#取得 request ip$ip=$callShell["output"][1];#取得 request addr & method$urlAndMethod=$callShell["output"][6];#儲存可能有問題的ip$ips_byIp[$ip][]=$urlAndMethod;}#foreach end}#if end#取得 httpd 存取記錄的 log#函數說明:#檢查指令的輸出是否含有關鍵字#回傳結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["grepCmd"],截取關鍵字的指令.#$result["founded"],是否找到關鍵字,"true"代表有,"false"代表沒有.#$result["content"],關鍵字所在列的輸出.#$result["count"],輸出的列數.#必填參數:#$conf["cmd"],字串,要執行的指令.$conf["cmd"]="cat";#$conf["keyWord"],字串,要檢查是否有關鍵字.$conf["keyWord"]=" 404 ";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["binPath"],字串,應用程式的路徑,預設為"/usr/bin".#$conf["binPath"]="";#$conf["argu"],陣列字串,指令搭配的參數,預設不使用.$conf["argu"]=array($logAddr."ssl_access_log");#$conf["excludeGrep"],字串,"true"代表排除含有 " | grep ".$conf["keyWord"] 字樣的輸出;預設為"false",不排除.#$conf["excludeGrep"]="false";#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";$searchOutPut=cmd::searchOutPut($conf);unset($conf);#如果執行失敗if($searchOutPut["status"]==="false"){#設置執行失敗$result["status"]="false";#設置錯誤訊息$result["error"]=$searchOutPut;#回傳結果return $result;}#if end#如果有找到關鍵字 " 404 "if($searchOutPut["founded"]==="true"){#過濾掉對於含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.#函式說明:#檢查多個字串中的每個字串是否有多個關鍵字#回傳的結果:#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。#$result["function"],當前執行的函數名稱.#$result["error"],錯誤訊息.#$result["argu"],使用的參數.#$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列.#$result["foundedKeyWords"].找到的關鍵字.#$result["foundedFalseKey"],結果為"false"的被搜尋元素key陣列.#$result["foundedTrueKeyWords"],二維陣列,各個字串有找到的關鍵字陣列.#$result["foundedAll"],是否每個關鍵字都有找到,"true"代表每個都有找到,"false"代表沒有每個都找到.#必填的參數:$conf["search::findManyKeyWordsFromManyString"]["keyWords"]=array(".css",".js","favicon.ico",".jpg",".png");#想要搜尋的關鍵字$conf["search::findManyKeyWordsFromManyString"]["stringArray"]=$searchOutPut["content"];#要被搜尋的字串內容陣列#可省略的參數:#$conf["completeEqual"]="true";#是否內容要完全符合,不能多出任何不符合的內容,預設為"false"不需要完全符合.$findManyKeyWordsFromManyString=search::findManyKeyWordsFromManyString($conf["search::findManyKeyWordsFromManyString"]);unset($conf["search::findManyKeyWordsFromManyString"]);#如果執行出錯if($findManyKeyWordsFromManyString["status"]==="false"){#設置執行失敗$result["status"]="false";#設置錯誤訊息$result["error"]=$findManyKeyWordsFromManyString;#回傳結果return $result;}#if end#var_dump($findManyKeyWordsFromManyString);exit;#取得不含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.$searchOutPut["content"]=$findManyKeyWordsFromManyString["foundedFalseKey"];#更新計數$searchOutPut["count"]=count($searchOutPut["content"]);#針對每列輸出foreach($searchOutPut["content"] as $line){#涵式說明:#呼叫shell執行系統命令,並取得回傳的內容.#回傳的結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["argu"],使用的參數.#$result["cmd"],執行的指令內容.#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.#$result["running"],是否還在執行.#$result["pid"],pid.#$result["statusCode"],執行結束後的代碼.#必填的參數#$conf["command"],字串,要執行的指令與.$conf["command"]="parse";#$conf["fileArgu"],字串,變數__FILE__的內容.$conf["fileArgu"]=__FILE__;#可省略參數:#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.$conf["argu"]=array($line);#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").#$conf["arguIsAddr"]=array();#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".#$conf["enablePrintDescription"]="true";#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.#$conf["printDescription"]="";#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".$conf["escapeshellarg"]="true";#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.#$conf["username"]="";#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.#$conf["password"]="";#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.#$conf["inBackGround"]="";#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.#$conf["getErr"]="false";#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php$callShell=external::callShell($conf);unset($conf);#如果執行失敗if($callShell["status"]==="false"){#印出結果var_dump($callShell);#結束執行exit;}#if end#取得 request ip$ip=$callShell["output"][1];#取得 request addr & method$urlAndMethod=$callShell["output"][6];#儲存可能有問題的ip$ips_byIp[$ip][]=$urlAndMethod;}#foreach end}#if end#針對每個有問題的ipforeach($ips_byIp as $ip=>$urls){#提示有問題的 ipecho "IP:".$ip." 拜訪過以下位置:".PHP_EOL;foreach($urls as $url){echo $url.PHP_EOL;}#提示是否要 block itecho "是否要 block it?".PHP_EOL;#涵式說明:#讀取標準I/O的一行輸入.並提供提示說明.#回傳的結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息.#$result["function"],當前執行的函數名稱.#$result["content"],取得的輸入內容.#必填的參數:#$conf["commentsArray"],字串陣列,提示輸入的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.$conf["commentsArray"]=array("請輸入(y/n)");#可省略的參數:#$conf["newLineBreak"],字串,是否$conf["commentsArray"]的每個元素後面都要斷行,"false"代表不要,預設為"true"要斷行.#$conf["newLineBreak"]="false";$readLine=cmd::readLine($conf);unset($conf);#如果讀取失敗if($readLine["status"]==="false"){#印出結果var_dump($readLine);#結束執行exit;}#if end#取得輸入的內容$input=$readLine["content"];#判斷輸入switch($input){#如果輸入 "y"case "y":#封鎖該ip#涵式說明:#呼叫shell依序執行系統命令,並取得回傳的內容.#回傳的結果:#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.#$result["error"],錯誤訊息陣列.#$result["function"],當前執行的函數名稱.#$result["cmd"],執行的指令內容.#$result["output"],爲執行完每個指令後的輸出陣列.#必填的參數#$conf["command"],字串陣列,要執行的指令.$conf["external::callShellMulti"]["command"]=array("echo");#$conf["fileArgu"],字串,變數__FILE__的內容,預設為當前檔案的路徑與名稱.$conf["external::callShellMulti"]["fileArgu"]=__FILE__;#可省略參數:#$conf["argu"],陣字串列,執行各個$conf["command"]時指令搭配的參數,預設為空陣列.$conf["external::callShellMulti"]["argu"]=array(array($ip,"|","block_ip.sh"));#$conf["enablePrintDescription"],字串陣列,執行各個$conf["command"]時是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.#$conf["enablePrintDescription"]=array("false");#$conf["printDescription"],字串陣列,執行各個$conf["command"]前要印出來的的文字,預設為$conf["command"]的內容,其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.#$conf["printDescription"]=array("");$conf["external::callShellMulti"]["escapeshellarg"],字串陣列,執行各個$conf["command"]時是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false",其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.$conf["external::callShellMulti"]["escapeshellarg"]=array("true");#$conf["useScript"],字串,每個指令的執行是否要啟用Linux的script指令來記錄輸出,"true"代表要;"false"代表不要,預設為"false".#$conf["useScript"]="";#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 ".qbpwcf_tmp/external/callShell/".#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";#$conf["inBackGround"],字串,每個指令的執行是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用";"區隔的多個指令將會出錯.#$conf["inBackGround"]="";#備註:#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行.#參考資料:#exec=>http://php.net/manual/en/function.exec.php#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php$callShellMulti=external::callShellMulti($conf["external::callShellMulti"]);unset($conf["external::callShellMulti"]);#如果執行失敗if($callShellMulti["status"]==="false"){#設置執行失敗$result["status"]="false";#設置錯誤訊息$result["error"]=$callShellMulti;#回傳結果return $result;}#if end#結束執行break;#如果輸入 "n"case "n":#換下一個ipcontinue 2;#結束執行break;#其他輸入內容default:#換下一個ipcontinue 2;}#switch end}#foreach end?>