2012-04-05 41 views
1

這個腳本的目的是當有404/500等服務器錯誤時,將日誌郵寄給他的網站管理員。這是PHP腳本安全嗎? fwrite並得到

該腳本利用fwrite對日誌進行計數,然後保存10個日誌,並在日誌達到10個日誌時發送郵件。 它使用了一些值並通過echo顯示它們,我怎麼能確定它沒有XSS或其他可破解的問題。 我知道腳本可能不像先進,高效或乾淨的書寫,但它對我有用。 我只關心它的安全性。

.htaccess文件

ErrorDocument 400 /errors/error.php?err=400 
ErrorDocument 401 /errors/error.php?err=401 
ErrorDocument 403 /errors/error.php?err=403 
ErrorDocument 404 /errors/error.php?err=404 
ErrorDocument 500 /errors/error.php?err=500 
ErrorDocument 410 /errors/error.php?err=410 

PHP文件/errors/error.php

<?php 

$fp = fopen("counterlog.txt", "r"); 
$count = fread($fp, 1024); 
fclose($fp); 


$errorNum = (int)$_GET['err']; 
$err_str = array(404=>'Type of error: Not Found (404)', 400=>'Type of error: Bad Request (400)', 401=>'Type of error: Unauthorized (401)', 403=>'Type of error: Forbidden (403)', 410=>'Type of error: Gone (410)', 500=>'Type of error: Internal Server Error (500)'); 

$ip = getenv ("REMOTE_ADDR"); 
$requri = getenv ("REQUEST_URI"); 
$servname = getenv ("SERVER_NAME"); 
$combine = $ip . " tried to load " . $servname . $requri; 

$httpref = getenv ("HTTP_REFERER"); 

if (empty($httpref)) { 
$httpref = "Unknown Location"; 
} 

$httpagent = getenv ("HTTP_USER_AGENT"); 

$today = date("F j, Y, H:i:s"); 

$note = "This information has been sent to the webmaster." ; 

$message = "On $today \n <br> $combine <br> \n User Agent = $httpagent \n <br>User got there from: $httpref <br><br> $err_str[$errorNum] <br><br> $note\n "; 
$message2 = "#$count \n $today \n $combine \n User Agent = $httpagent \n User got there  from: $httpref \n $err_str[$errorNum] \n\n "; 

$fh = fopen("errorlogje.txt", "a") or die("can't open file"); 
$stringData = $message2; 
fwrite($fh, $stringData); 
fclose($fh); 

if ($count == 10) { 
$count = 0; 
$fh = fopen("errorlogje.txt", "r"); 
$bericht = fread($fh, 4096); 
$to = "[email protected]"; // webmaster email 
$subject = "errorpage guardian has a message"; // email bericht 
$from = "From: [email protected]\r\n"; // email afzender (makelijk voor het sorteren) 
mail($to, $subject, $bericht, $from); 

$fh = fopen("errorlogje.txt", "w"); 
fclose($fh); 
} 
else { 
$count = $count + 1; 
} 

$fp = fopen("counterlog.txt", "w"); 
fwrite($fp, $count); 
fclose($fp); 

echo " $message "; 

?> 
+1

如果您認爲XSS,請考慮['htmlspecialchars'](http://php.net/htmlspecialchars)。 – hakre 2012-04-05 08:17:35

+0

文件寫入是安全的,因爲您不使用任何用戶提供的值來打開文件。用戶代理,http referer或請求uri可能包含html/javascript代碼,請遵循hakre的建議。 – knittl 2012-04-05 08:25:19

回答

1

這是完全安全的,是的。 您正在使用的唯一$_GET值被轉換爲整數,因此可以消除任何可能的問題。

+0

謝謝,所以如果我將前面提到的那些htmlspecialchars添加到回聲的輸出它的完全安全?或者甚至沒有問題? – Scriptor 2012-04-05 09:06:43

+0

這不是問題。 – Narf 2012-04-05 09:10:11

+0

只有一個?用戶代理如何? – Erlend 2012-04-06 02:42:19