我已經爲我的webapp寫了一個基本的「安全檢查器」。我需要一眼就看到用戶提交的代碼是否包含惡意內容。PHP strpos()返回奇數結果
下面是代碼的截圖我快這對現在:http://cl.ly/677a6dc40034f096697f
這裏是PHP代碼我使用的是針對這些代碼三位:
<!-- The View -->
<h2>Security analysis</h2>
<?php echo securitycheck($html, $css, $js); ?>
-
// The controller
function securitycheck($html, $css, $js)
{
// The code is the html, css, and js, appended together. We're scanning it all.
$code = $html." ".$css." ".$js;
// $insecure is our array of naughty things to search for.
$insecure = array(
/* HTML Elements */
'applet',
'basefont',
'base',
'behavior',
'bgsound',
'blink',
'embed',
'expression',
'frameset',
'frame',
'ilayer',
'iframe',
'isindex',
'javascript',
'layer',
'link',
'meta',
'object',
'plaintext',
'style',
'script',
'xml',
'xss',
/* Javascript Elements */
'alert',
'cmd',
'passthru',
'eval',
'exec',
'expression',
'system',
'fopen',
'fromcharcode',
'fsockopen',
'file',
'file_get_contents',
'readfile',
'unlink',
/* Misc Elements */
'vbscript:',
'<?',
'<?php',
'?>'
);
$found = "";
$output = "<p><strong>Potentially insecure items found:</strong> ";
foreach($insecure as $item)
{
if (($pos = strpos($code, $item)) !== FALSE)
{
$found .= "$item, ";
}
}
if ($found == "")
{
$output .= "None.<br/>";
}
else
{
$output .= "<span class=\"alert\">".substr($found, 0, -2)."</span>"."</p><br/>"; // cuts trailing comma and space from $found
}
return $output;
}
最後,這裏是返回輸出的截圖(HTML格式):http://cl.ly/f246dc419fb499dd6bd7
查看截圖?有幾件事情是錯誤的。尾部空格和逗號沒有被切斷(我使用substr()
來做,而且它報告了兩個alert
s,正如你從第一個截圖看到的那樣,這裏只有一個被執行過。我做錯了
感謝
傑克
編輯:?!作爲Fosco好心指出,alert
在我的數組(!DOH)被列爲兩次我已經修復了,但是問題尾隨的逗號是lef t仍然存在。我知道這是一個小問題,但我發誓,它仍然不應該在那裏......
警報在您的陣列中列出兩次。 – Fosco 2010-08-13 13:33:45
@Fosco:* facepalm!* – Jack 2010-08-13 13:36:39