【问题标题】:Why regex pattern works with html comments but doesn't work with php and js comments?为什么正则表达式模式适用于 html 注释,但不适用于 php 和 js 注释?
【发布时间】:2012-12-27 11:54:09
【问题描述】:

我有这个问题:

我网站的各个页面(通常是:html、php 和 js)都受到特洛伊木马(JS/Kryptik.ADZ 基于 NOD32 扫描)的影响。

各类页面中的代码如下:

PHP:

#336988#
echo "<script type=\"text/javascript\" language=\"javascript\" > CODE OF MALWARE </script>";
#/336988#

JS:

/*336988*/
CODE OF MALWARE
/*/336988*/

HTML:

<!--336988-->
<script type="text/javascript" language="javascript" >CODE OF MALWARE</script>
<!--/336988-->

所以我使用 Notepad++ 和正则表达式将恶意软件替换为空白文本。 我的正则表达式是这样的:(&lt;!--|\#|/\*)336988.+/336988(--&gt;|\#|\*/)

但是这个表达式只能找到 HTML。为什么?

我不明白。

如果我的英语和正则表达式知识很差,我很抱歉。

谢谢

卡洛

【问题讨论】:

  • 在文本编辑器中,.+ 通常不会越界。
  • [\s\S]替换.
  • 立即开始使用版本控制,并从您的存储库进行部署。您不需要像这样“撤消”更改。
  • 我也尝试过[\s\S][\s\S]+,但不起作用。我正在使用 Espresso 验证正则表达式
  • @MarkPeters 我不明白你的意思

标签: regex comments


【解决方案1】:

试试这个:

'^.*336988.*[\s\S]*.*336988.*$'

【讨论】:

  • 编写一个python 脚本来为你做这件事比搜索工具要容易得多。
【解决方案2】:

试试这个,我遇到了同样的问题,它成功了。

/#336988#(.*?)#\/336988#/ism

【讨论】:

    【解决方案3】:

    Here 修复 336988、68c8c7、8f4d8e、a59dc4 的脚本。

    【讨论】:

      【解决方案4】:

      今天我遇到了同样的问题,但代码不同。此代码影响了 aspx、asp、htdocs、html、htm 和 js 文件。下面是我在 Powershell 中修复这些文件的代码。 JS文件需要换行:

          $regex = New-Object System.Text.RegularExpressions.Regex "<!--68c8c7-->((.|\n)*)<!--/68c8c7-->"
      

      到:

          $regex = New-Object System.Text.RegularExpressions.Regex "/\*68c8c7\*((.|\n)*)68c8c7\*/"
      

      和线

          Get-ChildItem . -Recurse -Include *.aspx,*asp,*.html,*.htm | where-object {$_.lastwritetime –gt $DateToCompare} |  %{Write-Host Examining file: $_.fullname; $_} | ForEach-Object { DoWork $_.Name $_.DirectoryName}
      

      到:

          Get-ChildItem . -Recurse -Include *.js | where-object {$_.lastwritetime –gt $DateToCompare} |  %{Write-Host Examining file: $_.fullname; $_} | ForEach-Object { DoWork $_.Name $_.DirectoryName}
      

      以下代码(此脚本将创建 Backup_* 文件,毕竟您可以删除这些文件):

      function tryFixFile($filepath, $filepathBackup)
      {   
          $infile = [string]::join([environment]::newline, (get-content -path $filepath))
          $regex = New-Object System.Text.RegularExpressions.Regex "<!--68c8c7-->((.|\n)*)<!--/68c8c7-->"
      
          if($regex.IsMatch($infile))
          {
              $intAnswer = $WScriptObject.popup("File needs to be change: " + $filepath + " do you want to continue?", 0,"Change File",4)
              If ($intAnswer -eq 6) 
              {
                  Write-Host "  Creating backup for file: "  $filepath
                  Copy-Item $filepath $filepathBackup
                  $replace = $regex.Replace($infile,"")
                  $replace | out-file $filepath
              } else 
              {
                  $a.popup("File " + $filepath + " won't be changed.")
              }
          }
      }
      
      function DoWork($filename, $directory)
      {   
          $filepath = $directory + '\' + $filename
          $filepathBackup = $directory + '\' + "Backup_" + $filename
      
          $WScriptObject = new-object -comobject wscript.shell
      
          tryFixFile $filepath $filepathBackup
      }
      
      
      
      $pathToCheck = Read-Host 'WARNING!! Path to check/change?'
      if (Test-Path $pathToCheck)
      {
          Set-Location $pathToCheck
      
          #files were affected no longer that 2 days ago, you can change this
          $DateToCompare = (Get-date).AddDays(-2)
      
          Get-ChildItem . -Recurse -Include *.aspx,*asp,*.html,*.htm | where-object {$_.lastwritetime –gt $DateToCompare} |  %{Write-Host Examining file: $_.fullname; $_} | ForEach-Object { DoWork $_.Name $_.DirectoryName}
      }else
      {
          write-host "Path doesn't exist"
      }
      

      【讨论】:

        猜你喜欢
        • 2022-08-18
        • 2016-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-29
        • 2015-10-13
        • 1970-01-01
        相关资源
        最近更新 更多