Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP strpos()返回奇数结果_Php_Security_Loops - Fatal编程技术网

PHP strpos()返回奇数结果

PHP strpos()返回奇数结果,php,security,loops,Php,Security,Loops,我已经为我的webapp编写了一个基本的“安全检查器”。我需要看一下用户提交的代码是否包含邪恶的东西 这是我现在运行此操作的代码的屏幕截图: 以下是我针对以下三位代码使用的PHP代码: <!-- The View --> <h2>Security analysis</h2> <?php echo securitycheck($html, $css, $js); ?> 安全性分析 - //控制器 函数securitycheck($html、$c

我已经为我的webapp编写了一个基本的“安全检查器”。我需要看一下用户提交的代码是否包含邪恶的东西

这是我现在运行此操作的代码的屏幕截图:

以下是我针对以下三位代码使用的PHP代码

<!-- The View -->
<h2>Security analysis</h2>
<?php echo securitycheck($html, $css, $js); ?>

安全性分析
-

//控制器
函数securitycheck($html、$css、$js)
{
//代码是附加在一起的html、css和js。我们正在扫描所有代码。
$code=$html.“$css.”$js;
//$unsecure是我们要寻找的一系列顽皮的东西。
$unsecure=array(
/*HTML元素*/
“小程序”,
“basefont”,
"基地",,
“行为”,
“声音”,
“眨眼”,
“嵌入”,
“表达式”,
“框架集”,
“框架”,
“ilayer”,
“iframe”,
“isindex”,
“javascript”,
“层”,
“链接”,
“元”,
“对象”,
“纯文本”,
“风格”,
“脚本”,
“xml”,
“xss”,
/*Javascript元素*/
"警惕",,
“cmd”,
"passthru",,
“eval”,
“执行官”,
“表达式”,
"制度",,
“fopen”,
“fromcharcode”,
“fsockopen”,
“文件”,
“文件获取内容”,
“readfile”,
“取消链接”,
/*杂项要素*/
“vbscript:”,
''
);
$found=“”;
$output=“发现的潜在不安全项目:”;
foreach($项目不安全)
{
if(($pos=strpos($code$item))!==FALSE)
{
$found.=“$item”;
}
}
如果($found==“”)
{
$output.=“无”
; } 其他的 { $output.=''.substr($found,0,-2)。'.'.''


;//从$found中剪切尾随逗号和空格 } 返回$output; }
最后,这里是返回输出的屏幕截图(HTML)

看到截图了吗?有几件事不对。尾随空格和逗号没有被截断(我使用了
substr()
来做这件事,而且它报告了两个
警报,正如您从第一个屏幕截图中看到的,只有一个已经运行过了

我做错了什么

谢谢

杰克


编辑:正如Fosco善意地指出的那样,
警报在我的数组(doh!)中列出了两次。我已经解决了这个问题,但是尾部逗号be left的问题仍然存在。我知道这是一个较小的问题,但我发誓它仍然不应该存在…

处理找到的项的更简单的方法是使用

$found = array();

foreach($insecure as $item)
{
    if (($pos = strpos($code, $item)) !== FALSE)
    {
        $found[] $item;
    }
}
$found = implode(', ', $found);

字符串中只有一个警报,但它在$unsecure列表中出现了两次,因此它在输出中出现了两次。为了避免这种情况,您必须单独扫描每个部分。

乍一看,您的代码似乎应该给出您想要的输出。我不确定出哪里出了问题

与其将
$found
构建为字符串,我建议将其构建为数组,然后使用
内爆()
获取字符串:

  • $found=“”;
    替换为
    $found=array();
  • $found.=“$item”;
    替换为
    $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
    }
    
    if($found==“”)
    {
    $output.=“无”
    ; } 其他的 { $output.=''.substr($found,0,-2)。'.'.''


    ;//从$found中剪切尾随逗号和空格 }
    为此:

    if (!count($found))
    {
        $output .= "None.<br/>";
    }
    else
    {
        $output .= "<span class=\"alert\">".implode(', ',$found)."</span>"."</p><br/>";  // cuts trailing comma and space from $found
    }
    
    if(!count($found))
    {
    $output.=“无”
    ; } 其他的 { $output.=''.implode(',',$found)。''.“


    ;//从$found中剪切尾随逗号和空格 }
    不要重新发明轮子


    您是否尝试过只回显$found,然后执行查看源代码?我猜问题在于您的一个项目没有基于HTML编码显示(“
    警报在您的数组中列出了两次。感谢@FractalizeR,我不是在重新发明轮子-当我需要向用户输出此“live”时,我实际上将使用HTML净化器。这纯粹是一种一目了然的方式来查看用户是否插入了不好的内容,以便我可以查看是否有进一步的预防措施(例如禁止)需要发生。非常感谢!我看到了第二个警报,该警报已被删除,现在正在使用此阵列()方法。感谢Hammerite,我现在把它作为一个数组来做,我想这是一个更好的主意!唯一的问题是-它仍然在
    eval,
    后用逗号输出,正如@Joseph所说,这可能与
    有关,我明白了。但是禁止应该小心…用户可以在不知道它是cont的地方粘贴HTMLents.@FractalizeR该网站的主题是原创CSS3、HTML5和JS代码片段。任何触发安全警告的内容我都会格外关注。显然,如果这看起来像是公然蓄意的黑客攻击,他们就会被禁止,如果只是复制粘贴错误,就会发送一封礼貌的电子邮件,要求他们修改提交的内容。你呢如果是正确的,则在将其显示在屏幕上之前,输出
    Use-htmlentities对其进行编码:
    htmlentities(substr($found,0,-2))
    --
    if (!count($found))
    {
        $output .= "None.<br/>";
    }
    else
    {
        $output .= "<span class=\"alert\">".implode(', ',$found)."</span>"."</p><br/>";  // cuts trailing comma and space from $found
    }