| 3 |
liveuser |
1 |
#!/usr/bin/php
|
|
|
2 |
<?php
|
|
|
3 |
|
|
|
4 |
/*
|
|
|
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
| 239 |
liveuser |
6 |
Copyright (C) 2014~2026 MIN ZHI, CHEN
|
| 3 |
liveuser |
7 |
|
|
|
8 |
This file is part of QBPWCF.
|
|
|
9 |
|
|
|
10 |
QBPWCF is free software: you can redistribute it and/or modify
|
|
|
11 |
it under the terms of the GNU General Public License as published by
|
|
|
12 |
the Free Software Foundation, either version 3 of the License, or
|
|
|
13 |
(at your option) any later version.
|
|
|
14 |
|
|
|
15 |
QBPWCF is distributed in the hope that it will be useful,
|
|
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 |
GNU General Public License for more details.
|
|
|
19 |
|
|
|
20 |
You should have received a copy of the GNU General Public License
|
|
|
21 |
along with QBPWCF. If not, see <http://www.gnu.org/licenses/>.
|
| 226 |
liveuser |
22 |
|
| 3 |
liveuser |
23 |
*/
|
|
|
24 |
|
|
|
25 |
#使用命名空間qbpwcf
|
|
|
26 |
namespace qbpwcf;
|
|
|
27 |
|
|
|
28 |
#取得 lib path
|
|
|
29 |
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
|
|
|
30 |
|
|
|
31 |
#如果執行失敗
|
|
|
32 |
if($status!==0){
|
|
|
33 |
|
|
|
34 |
#debug
|
|
|
35 |
var_dump(__LINE__,$output);
|
|
|
36 |
|
|
|
37 |
#結束執行,回傳shell 1.
|
|
|
38 |
exit(1);
|
|
|
39 |
|
|
|
40 |
}#if end
|
|
|
41 |
|
|
|
42 |
#儲存lib path
|
|
|
43 |
$folderOfUsrLib=$output[0];
|
|
|
44 |
|
|
|
45 |
#以該檔案的實際位置的 lib path 為 include path 首位
|
|
|
46 |
$output=array();
|
|
|
47 |
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
|
|
|
48 |
|
|
|
49 |
#如果執行失敗
|
|
|
50 |
if($status!==0){
|
|
|
51 |
|
|
|
52 |
#debug
|
|
|
53 |
var_dump(__LINE__,$output);
|
|
|
54 |
|
|
|
55 |
#結束執行,回傳shell 1.
|
|
|
56 |
exit(1);
|
|
|
57 |
|
|
|
58 |
}#if end
|
|
|
59 |
|
| 226 |
liveuser |
60 |
#設置 include path
|
| 3 |
liveuser |
61 |
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
|
|
|
62 |
|
|
|
63 |
#匯入外部套件
|
|
|
64 |
include("allInOne.php");
|
|
|
65 |
|
|
|
66 |
#初始化 httpd log 的位置
|
|
|
67 |
$logAddr="/var/log/httpd/";
|
|
|
68 |
|
|
|
69 |
#初始化儲存可能有問題的IP陣列
|
|
|
70 |
$ips_byIp=array();
|
|
|
71 |
|
|
|
72 |
#函式說明:
|
|
|
73 |
#抓取命令列的參數.
|
|
|
74 |
#回傳結果:
|
|
|
75 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
76 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
77 |
#$result["function"],當前執行的函式名稱.
|
|
|
78 |
#$result["argu"],使用的參數陣列.
|
|
|
79 |
#$result["content"],要回傳的參數陣列.
|
|
|
80 |
#$result["count"],參數的數量.
|
|
|
81 |
#必填參數:
|
|
|
82 |
#無
|
|
|
83 |
#可省略參數:
|
|
|
84 |
#$conf["echo"],"true"代表要將抓到的參數一個個印出來,"false"代表用回傳的方式,預設為"false".
|
|
|
85 |
$conf["echo"]="false";
|
|
|
86 |
$getArgu=cmd::getArgu($conf);
|
|
|
87 |
unset($conf);
|
|
|
88 |
|
|
|
89 |
#如果有參數
|
|
|
90 |
if($getArgu["count"]>1)
|
|
|
91 |
{
|
|
|
92 |
#如果有參數 -h 或 --help
|
|
|
93 |
if($getArgu["content"][1]==="-h" || $getArgu["content"][1]==="--help")
|
|
|
94 |
{
|
| 226 |
liveuser |
95 |
|
| 3 |
liveuser |
96 |
#印出指南,並結束執行.
|
|
|
97 |
echo basename(__FILE__) . " 用法:".PHP_EOL;
|
|
|
98 |
echo basename(__FILE__) . " -h/--help 顯示本說明.".PHP_EOL;
|
|
|
99 |
echo basename(__FILE__) . " -logAddr httpd log 的路徑.".PHP_EOL;
|
|
|
100 |
exit;
|
| 226 |
liveuser |
101 |
|
| 3 |
liveuser |
102 |
}#if end
|
|
|
103 |
|
|
|
104 |
}#if end
|
|
|
105 |
|
|
|
106 |
#函式說明:
|
|
|
107 |
#解析參數.
|
|
|
108 |
#回傳結果:
|
|
|
109 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
110 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
111 |
#$result["function"],當前執行的函式名稱.
|
|
|
112 |
#$result["content"],解析好的參數陣列.
|
|
|
113 |
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
|
|
|
114 |
#$result["program"],字串,執行的程式名稱.
|
|
|
115 |
#必填參數:
|
|
|
116 |
#無
|
|
|
117 |
#可省略參數:
|
|
|
118 |
#無
|
|
|
119 |
#備註:
|
|
|
120 |
#僅能在命令列底下執行.
|
|
|
121 |
#建議:
|
|
|
122 |
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
|
|
|
123 |
$conf=array();
|
|
|
124 |
$parseArgu=cmd::parseArgu($conf);
|
|
|
125 |
unset($conf);
|
|
|
126 |
|
|
|
127 |
#var_dump($parseArgu);exit;
|
|
|
128 |
|
|
|
129 |
#如果有指定參數
|
|
|
130 |
if(count($parseArgu["content"])>0)
|
|
|
131 |
{
|
|
|
132 |
#如果有 logAddr 參數
|
|
|
133 |
if(isset($parseArgu["content"]["logAddr"]))
|
|
|
134 |
{
|
|
|
135 |
#取得 logAddr 參數
|
|
|
136 |
$logAddr=$parseArgu["content"]["logAddr"][0];
|
| 226 |
liveuser |
137 |
|
| 3 |
liveuser |
138 |
}#if end
|
|
|
139 |
|
|
|
140 |
}#if end
|
|
|
141 |
|
|
|
142 |
#取得 http 存取記錄的 log
|
|
|
143 |
#函數說明:
|
|
|
144 |
#檢查指令的輸出是否含有關鍵字
|
|
|
145 |
#回傳結果:
|
|
|
146 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
147 |
#$result["error"],錯誤訊息.
|
|
|
148 |
#$result["function"],當前執行的函數名稱.
|
|
|
149 |
#$result["argu"],使用的參數.
|
|
|
150 |
#$result["grepCmd"],截取關鍵字的指令.
|
|
|
151 |
#$result["founded"],是否找到關鍵字,"true"代表有,"false"代表沒有.
|
|
|
152 |
#$result["content"],關鍵字所在列的輸出.
|
|
|
153 |
#$result["count"],輸出的列數.
|
|
|
154 |
#必填參數:
|
|
|
155 |
#$conf["cmd"],字串,要執行的指令.
|
|
|
156 |
$conf["cmd"]="cat";
|
|
|
157 |
#$conf["keyWord"],字串,要檢查是否有關鍵字.
|
|
|
158 |
$conf["keyWord"]=" 404 ";
|
|
|
159 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
160 |
$conf["fileArgu"]=__FILE__;
|
|
|
161 |
#可省略參數:
|
|
|
162 |
#$conf["binPath"],字串,應用程式的路徑,預設為"/usr/bin".
|
|
|
163 |
#$conf["binPath"]="";
|
|
|
164 |
#$conf["argu"],陣列字串,指令搭配的參數,預設不使用.
|
|
|
165 |
$conf["argu"]=array($logAddr."access_log");
|
|
|
166 |
#$conf["excludeGrep"],字串,"true"代表排除含有 " | grep ".$conf["keyWord"] 字樣的輸出;預設為"false",不排除.
|
|
|
167 |
#$conf["excludeGrep"]="false";
|
|
|
168 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
169 |
#$conf["username"]="";
|
|
|
170 |
#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
171 |
#$conf["password"]="";
|
|
|
172 |
$searchOutPut=cmd::searchOutPut($conf);
|
|
|
173 |
unset($conf);
|
|
|
174 |
|
|
|
175 |
#如果執行失敗
|
|
|
176 |
if($searchOutPut["status"]==="false"){
|
|
|
177 |
|
|
|
178 |
#設置執行失敗
|
|
|
179 |
$result["status"]="false";
|
|
|
180 |
|
|
|
181 |
#設置錯誤訊息
|
|
|
182 |
$result["error"]=$searchOutPut;
|
|
|
183 |
|
|
|
184 |
#回傳結果
|
|
|
185 |
return $result;
|
|
|
186 |
|
|
|
187 |
}#if end
|
| 226 |
liveuser |
188 |
|
| 3 |
liveuser |
189 |
#如果有找到關鍵字 " 404 "
|
|
|
190 |
if($searchOutPut["founded"]==="true"){
|
|
|
191 |
|
| 226 |
liveuser |
192 |
#過濾掉對於含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.
|
| 3 |
liveuser |
193 |
#函式說明:
|
|
|
194 |
#檢查多個字串中的每個字串是否有多個關鍵字
|
|
|
195 |
#回傳的結果:
|
|
|
196 |
#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
|
|
|
197 |
#$result["function"],當前執行的函數名稱.
|
|
|
198 |
#$result["error"],錯誤訊息.
|
|
|
199 |
#$result["argu"],使用的參數.
|
|
|
200 |
#$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列.
|
|
|
201 |
#$result["foundedKeyWords"].找到的關鍵字.
|
|
|
202 |
#$result["foundedFalseKey"],結果為"false"的被搜尋元素key陣列.
|
|
|
203 |
#$result["foundedTrueKeyWords"],二維陣列,各個字串有找到的關鍵字陣列.
|
|
|
204 |
#$result["foundedAll"],是否每個關鍵字都有找到,"true"代表每個都有找到,"false"代表沒有每個都找到.
|
|
|
205 |
#必填的參數:
|
|
|
206 |
$conf["search::findManyKeyWordsFromManyString"]["keyWords"]=array(".css",".js","favicon.ico",".jpg",".png");#想要搜尋的關鍵字
|
|
|
207 |
$conf["search::findManyKeyWordsFromManyString"]["stringArray"]=$searchOutPut["content"];#要被搜尋的字串內容陣列
|
|
|
208 |
#可省略的參數:
|
|
|
209 |
#$conf["completeEqual"]="true";#是否內容要完全符合,不能多出任何不符合的內容,預設為"false"不需要完全符合.
|
|
|
210 |
$findManyKeyWordsFromManyString=search::findManyKeyWordsFromManyString($conf["search::findManyKeyWordsFromManyString"]);
|
|
|
211 |
unset($conf["search::findManyKeyWordsFromManyString"]);
|
|
|
212 |
|
|
|
213 |
#如果執行出錯
|
|
|
214 |
if($findManyKeyWordsFromManyString["status"]==="false"){
|
| 226 |
liveuser |
215 |
|
| 3 |
liveuser |
216 |
#設置執行失敗
|
|
|
217 |
$result["status"]="false";
|
|
|
218 |
|
|
|
219 |
#設置錯誤訊息
|
|
|
220 |
$result["error"]=$findManyKeyWordsFromManyString;
|
|
|
221 |
|
|
|
222 |
#回傳結果
|
|
|
223 |
return $result;
|
| 226 |
liveuser |
224 |
|
|
|
225 |
}#if end
|
|
|
226 |
|
| 3 |
liveuser |
227 |
#取得不含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.
|
| 226 |
liveuser |
228 |
$searchOutPut["content"]=$findManyKeyWordsFromManyString["foundedFalseKey"];
|
| 3 |
liveuser |
229 |
|
|
|
230 |
#更新計數
|
|
|
231 |
$searchOutPut["count"]=count($searchOutPut["content"]);
|
|
|
232 |
|
|
|
233 |
#針對每列輸出
|
|
|
234 |
foreach($searchOutPut["content"] as $line){
|
|
|
235 |
|
|
|
236 |
#涵式說明:
|
|
|
237 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
238 |
#回傳的結果:
|
|
|
239 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
240 |
#$result["error"],錯誤訊息陣列.
|
|
|
241 |
#$result["function"],當前執行的函數名稱.
|
|
|
242 |
#$result["argu"],使用的參數.
|
|
|
243 |
#$result["cmd"],執行的指令內容.
|
|
|
244 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
245 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
246 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
247 |
#$result["running"],是否還在執行.
|
|
|
248 |
#$result["pid"],pid.
|
|
|
249 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
250 |
#必填的參數
|
|
|
251 |
#$conf["command"],字串,要執行的指令與.
|
|
|
252 |
$conf["command"]="parse";
|
|
|
253 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
254 |
$conf["fileArgu"]=__FILE__;
|
| 3 |
liveuser |
255 |
#可省略參數:
|
|
|
256 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
257 |
$conf["argu"]=array($line);
|
|
|
258 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
| 226 |
liveuser |
259 |
#$conf["arguIsAddr"]=array();
|
| 3 |
liveuser |
260 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
261 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
262 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
263 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
264 |
#$conf["enablePrintDescription"]="true";
|
|
|
265 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
266 |
#$conf["printDescription"]="";
|
|
|
267 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
268 |
$conf["escapeshellarg"]="true";
|
|
|
269 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
270 |
#$conf["username"]="";
|
|
|
271 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
272 |
#$conf["password"]="";
|
|
|
273 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
274 |
#$conf["useScript"]="";
|
|
|
275 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
276 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
277 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
278 |
#$conf["inBackGround"]="";
|
|
|
279 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
280 |
#$conf["getErr"]="false";
|
|
|
281 |
#備註:
|
|
|
282 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
283 |
#參考資料:
|
|
|
284 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
285 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
286 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
287 |
$callShell=external::callShell($conf);
|
| 226 |
liveuser |
288 |
unset($conf);
|
|
|
289 |
|
| 3 |
liveuser |
290 |
#如果執行失敗
|
|
|
291 |
if($callShell["status"]==="false"){
|
| 226 |
liveuser |
292 |
|
| 3 |
liveuser |
293 |
#印出結果
|
|
|
294 |
var_dump($callShell);
|
| 226 |
liveuser |
295 |
|
| 3 |
liveuser |
296 |
#結束執行
|
|
|
297 |
exit;
|
| 226 |
liveuser |
298 |
|
| 3 |
liveuser |
299 |
}#if end
|
|
|
300 |
|
|
|
301 |
#取得 request ip
|
|
|
302 |
$ip=$callShell["output"][1];
|
| 226 |
liveuser |
303 |
|
| 3 |
liveuser |
304 |
#取得 request addr & method
|
|
|
305 |
$urlAndMethod=$callShell["output"][6];
|
| 226 |
liveuser |
306 |
|
| 3 |
liveuser |
307 |
#儲存可能有問題的ip
|
|
|
308 |
$ips_byIp[$ip][]=$urlAndMethod;
|
| 226 |
liveuser |
309 |
|
| 3 |
liveuser |
310 |
}#foreach end
|
| 226 |
liveuser |
311 |
|
|
|
312 |
}#if end
|
|
|
313 |
|
| 3 |
liveuser |
314 |
#取得 httpd 存取記錄的 log
|
|
|
315 |
#函數說明:
|
|
|
316 |
#檢查指令的輸出是否含有關鍵字
|
|
|
317 |
#回傳結果:
|
|
|
318 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
319 |
#$result["error"],錯誤訊息.
|
|
|
320 |
#$result["function"],當前執行的函數名稱.
|
|
|
321 |
#$result["argu"],使用的參數.
|
|
|
322 |
#$result["grepCmd"],截取關鍵字的指令.
|
|
|
323 |
#$result["founded"],是否找到關鍵字,"true"代表有,"false"代表沒有.
|
|
|
324 |
#$result["content"],關鍵字所在列的輸出.
|
|
|
325 |
#$result["count"],輸出的列數.
|
|
|
326 |
#必填參數:
|
|
|
327 |
#$conf["cmd"],字串,要執行的指令.
|
|
|
328 |
$conf["cmd"]="cat";
|
|
|
329 |
#$conf["keyWord"],字串,要檢查是否有關鍵字.
|
|
|
330 |
$conf["keyWord"]=" 404 ";
|
|
|
331 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
|
|
332 |
$conf["fileArgu"]=__FILE__;
|
|
|
333 |
#可省略參數:
|
|
|
334 |
#$conf["binPath"],字串,應用程式的路徑,預設為"/usr/bin".
|
|
|
335 |
#$conf["binPath"]="";
|
|
|
336 |
#$conf["argu"],陣列字串,指令搭配的參數,預設不使用.
|
|
|
337 |
$conf["argu"]=array($logAddr."ssl_access_log");
|
|
|
338 |
#$conf["excludeGrep"],字串,"true"代表排除含有 " | grep ".$conf["keyWord"] 字樣的輸出;預設為"false",不排除.
|
|
|
339 |
#$conf["excludeGrep"]="false";
|
|
|
340 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
341 |
#$conf["username"]="";
|
|
|
342 |
#$conf["password"],字串,與$conf["username"]搭配的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
343 |
#$conf["password"]="";
|
|
|
344 |
$searchOutPut=cmd::searchOutPut($conf);
|
|
|
345 |
unset($conf);
|
|
|
346 |
|
|
|
347 |
#如果執行失敗
|
|
|
348 |
if($searchOutPut["status"]==="false"){
|
|
|
349 |
|
|
|
350 |
#設置執行失敗
|
|
|
351 |
$result["status"]="false";
|
|
|
352 |
|
|
|
353 |
#設置錯誤訊息
|
|
|
354 |
$result["error"]=$searchOutPut;
|
|
|
355 |
|
|
|
356 |
#回傳結果
|
|
|
357 |
return $result;
|
|
|
358 |
|
|
|
359 |
}#if end
|
| 226 |
liveuser |
360 |
|
| 3 |
liveuser |
361 |
#如果有找到關鍵字 " 404 "
|
|
|
362 |
if($searchOutPut["founded"]==="true"){
|
|
|
363 |
|
| 226 |
liveuser |
364 |
#過濾掉對於含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.
|
| 3 |
liveuser |
365 |
#函式說明:
|
|
|
366 |
#檢查多個字串中的每個字串是否有多個關鍵字
|
|
|
367 |
#回傳的結果:
|
|
|
368 |
#$result["status"],整體來說,執行是否成功,"true"代表執行成功,"false"代表執行失敗。
|
|
|
369 |
#$result["function"],當前執行的函數名稱.
|
|
|
370 |
#$result["error"],錯誤訊息.
|
|
|
371 |
#$result["argu"],使用的參數.
|
|
|
372 |
#$result["foundedTrueKey"],結果為"true"的被搜尋元素key陣列.
|
|
|
373 |
#$result["foundedKeyWords"].找到的關鍵字.
|
|
|
374 |
#$result["foundedFalseKey"],結果為"false"的被搜尋元素key陣列.
|
|
|
375 |
#$result["foundedTrueKeyWords"],二維陣列,各個字串有找到的關鍵字陣列.
|
|
|
376 |
#$result["foundedAll"],是否每個關鍵字都有找到,"true"代表每個都有找到,"false"代表沒有每個都找到.
|
|
|
377 |
#必填的參數:
|
|
|
378 |
$conf["search::findManyKeyWordsFromManyString"]["keyWords"]=array(".css",".js","favicon.ico",".jpg",".png");#想要搜尋的關鍵字
|
|
|
379 |
$conf["search::findManyKeyWordsFromManyString"]["stringArray"]=$searchOutPut["content"];#要被搜尋的字串內容陣列
|
|
|
380 |
#可省略的參數:
|
|
|
381 |
#$conf["completeEqual"]="true";#是否內容要完全符合,不能多出任何不符合的內容,預設為"false"不需要完全符合.
|
|
|
382 |
$findManyKeyWordsFromManyString=search::findManyKeyWordsFromManyString($conf["search::findManyKeyWordsFromManyString"]);
|
|
|
383 |
unset($conf["search::findManyKeyWordsFromManyString"]);
|
|
|
384 |
|
|
|
385 |
#如果執行出錯
|
|
|
386 |
if($findManyKeyWordsFromManyString["status"]==="false"){
|
| 226 |
liveuser |
387 |
|
| 3 |
liveuser |
388 |
#設置執行失敗
|
|
|
389 |
$result["status"]="false";
|
|
|
390 |
|
|
|
391 |
#設置錯誤訊息
|
|
|
392 |
$result["error"]=$findManyKeyWordsFromManyString;
|
|
|
393 |
|
|
|
394 |
#回傳結果
|
|
|
395 |
return $result;
|
| 226 |
liveuser |
396 |
|
| 3 |
liveuser |
397 |
}#if end
|
| 226 |
liveuser |
398 |
|
|
|
399 |
#var_dump($findManyKeyWordsFromManyString);exit;
|
|
|
400 |
|
| 3 |
liveuser |
401 |
#取得不含有 ".css" ".js" "favicon.ico" ".jpg" ".png" 關鍵字串的結果.
|
| 226 |
liveuser |
402 |
$searchOutPut["content"]=$findManyKeyWordsFromManyString["foundedFalseKey"];
|
| 3 |
liveuser |
403 |
|
|
|
404 |
#更新計數
|
|
|
405 |
$searchOutPut["count"]=count($searchOutPut["content"]);
|
|
|
406 |
|
|
|
407 |
#針對每列輸出
|
|
|
408 |
foreach($searchOutPut["content"] as $line){
|
|
|
409 |
|
|
|
410 |
#涵式說明:
|
|
|
411 |
#呼叫shell執行系統命令,並取得回傳的內容.
|
|
|
412 |
#回傳的結果:
|
|
|
413 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
414 |
#$result["error"],錯誤訊息陣列.
|
|
|
415 |
#$result["function"],當前執行的函數名稱.
|
|
|
416 |
#$result["argu"],使用的參數.
|
|
|
417 |
#$result["cmd"],執行的指令內容.
|
|
|
418 |
#$result["fullCmd"],如果參數 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
419 |
#$result["output"],爲執行完二元碼後的輸出陣列,若 $conf["inBackGround"] 為 "true",則為當下的輸出.
|
|
|
420 |
#$result["tmpFileOutput"],儲存輸出的暫存檔案名稱,若 $conf["inBackGround"] 為 "true" 則會回傳該值.
|
|
|
421 |
#$result["running"],是否還在執行.
|
|
|
422 |
#$result["pid"],pid.
|
|
|
423 |
#$result["statusCode"],執行結束後的代碼.
|
|
|
424 |
#必填的參數
|
|
|
425 |
#$conf["command"],字串,要執行的指令與.
|
|
|
426 |
$conf["command"]="parse";
|
|
|
427 |
#$conf["fileArgu"],字串,變數__FILE__的內容.
|
| 226 |
liveuser |
428 |
$conf["fileArgu"]=__FILE__;
|
| 3 |
liveuser |
429 |
#可省略參數:
|
|
|
430 |
#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
|
|
|
431 |
$conf["argu"]=array($line);
|
|
|
432 |
#$conf["arguIsAddr"],陣列字串,指令搭配的哪些參數為路徑,為路徑的參數會進行轉換以便符合呼叫當前函數的位置,預設不指定,若有3個參數,其中第3個參數為路徑,則表示為array("false","false","true").
|
| 226 |
liveuser |
433 |
#$conf["arguIsAddr"]=array();
|
| 3 |
liveuser |
434 |
#$conf["pre"],陣列,要在本指令前執行的每個指令與參數.
|
|
|
435 |
#$conf["pre"][$i]["cmd"],字串,要在本指令前執行的第$i+1個指令.
|
|
|
436 |
#$conf["pre"][$i]["param"],陣列字串,要在本指令前執行的第$i+1個指令的參數.
|
|
|
437 |
#$conf["enablePrintDescription"],字串,是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,預設為"false".
|
|
|
438 |
#$conf["enablePrintDescription"]="true";
|
|
|
439 |
#$conf["printDescription"],字串,執行該外部程式前要印出來的的文字,預設為$conf["command"]的內容加上使用的$conf["argu"]參數.
|
|
|
440 |
#$conf["printDescription"]="";
|
|
|
441 |
#$conf["escapeshellarg"],字串,是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false".
|
|
|
442 |
$conf["escapeshellarg"]="true";
|
|
|
443 |
#$conf["username"],字串,要用什麼使用者來執行,預設為執行php的使用者,該參數不適用於apache環境.
|
|
|
444 |
#$conf["username"]="";
|
|
|
445 |
#$conf["password"],字串,root的使用者密碼,預設不使用密碼,該參數不適用於apache環境.
|
|
|
446 |
#$conf["password"]="";
|
|
|
447 |
#$conf["useScript"],字串,是否要啟用Linux的script指令來記錄輸出,"true"代表要,Fedora的selinux會擋住該操作;"false"代表不要,預設為"false".
|
|
|
448 |
#$conf["useScript"]="";
|
|
|
449 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 "/tmp/.qbpwcf_tmp/external/callShell/".
|
|
|
450 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
451 |
#$conf["inBackGround"],字串,是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用「;」區隔的多個指令將會出錯.
|
|
|
452 |
#$conf["inBackGround"]="";
|
|
|
453 |
#$conf["getErr"],字串,"true"代表將錯誤輸出變成標準輸出,反之"false"為不變動.
|
|
|
454 |
#$conf["getErr"]="false";
|
|
|
455 |
#備註:
|
|
|
456 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行,使用root身份可能會被selinux阻擋.
|
|
|
457 |
#參考資料:
|
|
|
458 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
459 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
|
|
460 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
|
|
461 |
$callShell=external::callShell($conf);
|
| 226 |
liveuser |
462 |
unset($conf);
|
|
|
463 |
|
| 3 |
liveuser |
464 |
#如果執行失敗
|
|
|
465 |
if($callShell["status"]==="false"){
|
| 226 |
liveuser |
466 |
|
| 3 |
liveuser |
467 |
#印出結果
|
|
|
468 |
var_dump($callShell);
|
| 226 |
liveuser |
469 |
|
| 3 |
liveuser |
470 |
#結束執行
|
|
|
471 |
exit;
|
| 226 |
liveuser |
472 |
|
| 3 |
liveuser |
473 |
}#if end
|
|
|
474 |
|
|
|
475 |
#取得 request ip
|
|
|
476 |
$ip=$callShell["output"][1];
|
| 226 |
liveuser |
477 |
|
| 3 |
liveuser |
478 |
#取得 request addr & method
|
|
|
479 |
$urlAndMethod=$callShell["output"][6];
|
| 226 |
liveuser |
480 |
|
| 3 |
liveuser |
481 |
#儲存可能有問題的ip
|
|
|
482 |
$ips_byIp[$ip][]=$urlAndMethod;
|
| 226 |
liveuser |
483 |
|
| 3 |
liveuser |
484 |
}#foreach end
|
|
|
485 |
|
| 226 |
liveuser |
486 |
}#if end
|
|
|
487 |
|
| 3 |
liveuser |
488 |
#針對每個有問題的ip
|
|
|
489 |
foreach($ips_byIp as $ip=>$urls){
|
|
|
490 |
|
|
|
491 |
#提示有問題的 ip
|
|
|
492 |
echo "IP:".$ip." 拜訪過以下位置:".PHP_EOL;
|
| 226 |
liveuser |
493 |
|
| 3 |
liveuser |
494 |
foreach($urls as $url)
|
|
|
495 |
{
|
|
|
496 |
echo $url.PHP_EOL;
|
|
|
497 |
}
|
| 226 |
liveuser |
498 |
|
| 3 |
liveuser |
499 |
#提示是否要 block it
|
|
|
500 |
echo "是否要 block it?".PHP_EOL;
|
| 226 |
liveuser |
501 |
|
| 3 |
liveuser |
502 |
#涵式說明:
|
|
|
503 |
#讀取標準I/O的一行輸入.並提供提示說明.
|
|
|
504 |
#回傳的結果:
|
|
|
505 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
506 |
#$result["error"],錯誤訊息.
|
|
|
507 |
#$result["function"],當前執行的函數名稱.
|
|
|
508 |
#$result["content"],取得的輸入內容.
|
|
|
509 |
#必填的參數:
|
|
|
510 |
#$conf["commentsArray"],字串陣列,提示輸入的文字描述,$conf["commentsArray"][$i]代表第($+1)行的描述.
|
|
|
511 |
$conf["commentsArray"]=array("請輸入(y/n)");
|
|
|
512 |
#可省略的參數:
|
|
|
513 |
#$conf["newLineBreak"],字串,是否$conf["commentsArray"]的每個元素後面都要斷行,"false"代表不要,預設為"true"要斷行.
|
|
|
514 |
#$conf["newLineBreak"]="false";
|
|
|
515 |
$readLine=cmd::readLine($conf);
|
|
|
516 |
unset($conf);
|
| 226 |
liveuser |
517 |
|
| 3 |
liveuser |
518 |
#如果讀取失敗
|
|
|
519 |
if($readLine["status"]==="false")
|
|
|
520 |
{
|
|
|
521 |
#印出結果
|
|
|
522 |
var_dump($readLine);
|
| 226 |
liveuser |
523 |
|
| 3 |
liveuser |
524 |
#結束執行
|
|
|
525 |
exit;
|
| 226 |
liveuser |
526 |
|
| 3 |
liveuser |
527 |
}#if end
|
| 226 |
liveuser |
528 |
|
| 3 |
liveuser |
529 |
#取得輸入的內容
|
|
|
530 |
$input=$readLine["content"];
|
| 226 |
liveuser |
531 |
|
| 3 |
liveuser |
532 |
#判斷輸入
|
|
|
533 |
switch($input){
|
| 226 |
liveuser |
534 |
|
| 3 |
liveuser |
535 |
#如果輸入 "y"
|
|
|
536 |
case "y":
|
| 226 |
liveuser |
537 |
|
| 3 |
liveuser |
538 |
#封鎖該ip
|
|
|
539 |
#涵式說明:
|
|
|
540 |
#呼叫shell依序執行系統命令,並取得回傳的內容.
|
|
|
541 |
#回傳的結果:
|
|
|
542 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
543 |
#$result["error"],錯誤訊息陣列.
|
|
|
544 |
#$result["function"],當前執行的函數名稱.
|
|
|
545 |
#$result["cmd"],執行的指令內容.
|
|
|
546 |
#$result["output"],爲執行完每個指令後的輸出陣列.
|
|
|
547 |
#必填的參數
|
|
|
548 |
#$conf["command"],字串陣列,要執行的指令.
|
|
|
549 |
$conf["external::callShellMulti"]["command"]=array("echo");
|
|
|
550 |
#$conf["fileArgu"],字串,變數__FILE__的內容,預設為當前檔案的路徑與名稱.
|
| 226 |
liveuser |
551 |
$conf["external::callShellMulti"]["fileArgu"]=__FILE__;
|
| 3 |
liveuser |
552 |
#可省略參數:
|
|
|
553 |
#$conf["argu"],陣字串列,執行各個$conf["command"]時指令搭配的參數,預設為空陣列.
|
|
|
554 |
$conf["external::callShellMulti"]["argu"]=array(array($ip,"|","block_ip.sh"));
|
|
|
555 |
#$conf["enablePrintDescription"],字串陣列,執行各個$conf["command"]時是否要印出$conf["printDescription"]的內容,"true"代表要,"false"代表不要,其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.
|
|
|
556 |
#$conf["enablePrintDescription"]=array("false");
|
|
|
557 |
#$conf["printDescription"],字串陣列,執行各個$conf["command"]前要印出來的的文字,預設為$conf["command"]的內容,其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.
|
|
|
558 |
#$conf["printDescription"]=array("");
|
|
|
559 |
$conf["external::callShellMulti"]["escapeshellarg"],字串陣列,執行各個$conf["command"]時是否要啟用過濾參數,用了比較安全,但可能會出錯,"true"為啟用,"false"為不啟用,預設為"false",其數量須與$conf["command"]的元素數量相同,若只有一個元素,則代表是每個$conf["command"]執行時都用此參數.
|
|
|
560 |
$conf["external::callShellMulti"]["escapeshellarg"]=array("true");
|
|
|
561 |
#$conf["useScript"],字串,每個指令的執行是否要啟用Linux的script指令來記錄輸出,"true"代表要;"false"代表不要,預設為"false".
|
|
|
562 |
#$conf["useScript"]="";
|
|
|
563 |
#$conf["logFilePath"],字串,當 $conf["useScript"] 為 "true" 時,輸出的內容要暫存到哪裡,預設為 ".qbpwcf_tmp/external/callShell/".
|
|
|
564 |
#$conf["logFilePath"]=".qbpwcf_tmp/external/callShell/";
|
|
|
565 |
#$conf["inBackGround"],字串,每個指令的執行是否要在背景執行,且不會等待程式執行結束再執行下一個指令,"true"代表是,"false"代表不要,預設為"false",如果$conf["command"]有用";"區隔的多個指令將會出錯.
|
|
|
566 |
#$conf["inBackGround"]="";
|
|
|
567 |
#備註:
|
|
|
568 |
#不是所有指令都能用apache的身份執行,目前已知java,javac指令無法執行.
|
|
|
569 |
#參考資料:
|
|
|
570 |
#exec=>http://php.net/manual/en/function.exec.php
|
|
|
571 |
#escapeshellcmd=>http://php.net/manual/en/function.escapeshellcmd.php
|
| 226 |
liveuser |
572 |
#escapeshellarg=>http://php.net/manual/en/function.escapeshellarg.php
|
| 3 |
liveuser |
573 |
$callShellMulti=external::callShellMulti($conf["external::callShellMulti"]);
|
|
|
574 |
unset($conf["external::callShellMulti"]);
|
| 226 |
liveuser |
575 |
|
| 3 |
liveuser |
576 |
#如果執行失敗
|
|
|
577 |
if($callShellMulti["status"]==="false"){
|
| 226 |
liveuser |
578 |
|
| 3 |
liveuser |
579 |
#設置執行失敗
|
|
|
580 |
$result["status"]="false";
|
|
|
581 |
|
|
|
582 |
#設置錯誤訊息
|
|
|
583 |
$result["error"]=$callShellMulti;
|
|
|
584 |
|
|
|
585 |
#回傳結果
|
|
|
586 |
return $result;
|
| 226 |
liveuser |
587 |
|
| 3 |
liveuser |
588 |
}#if end
|
| 226 |
liveuser |
589 |
|
| 3 |
liveuser |
590 |
#結束執行
|
|
|
591 |
break;
|
| 226 |
liveuser |
592 |
|
| 3 |
liveuser |
593 |
#如果輸入 "n"
|
|
|
594 |
case "n":
|
| 226 |
liveuser |
595 |
|
| 3 |
liveuser |
596 |
#換下一個ip
|
|
|
597 |
continue 2;
|
| 226 |
liveuser |
598 |
|
| 3 |
liveuser |
599 |
#結束執行
|
|
|
600 |
break;
|
| 226 |
liveuser |
601 |
|
| 3 |
liveuser |
602 |
#其他輸入內容
|
|
|
603 |
default:
|
| 226 |
liveuser |
604 |
|
| 3 |
liveuser |
605 |
#換下一個ip
|
|
|
606 |
continue 2;
|
| 226 |
liveuser |
607 |
|
| 3 |
liveuser |
608 |
}#switch end
|
|
|
609 |
|
| 226 |
liveuser |
610 |
}#foreach end
|
|
|
611 |
|
| 3 |
liveuser |
612 |
?>
|