Mod 编码问题引起的安全错误?

Mod Security error caused by encoding issue?

我偶尔管理的网站出现以下错误,而且最近才出现,AFAIK。一切正常,但由于它是一个副项目,我已经一年多没有监视它了,也不知道它到底是什么时候出现的。这很可能是由于浏览器或服务器软件中的某些安全更改所致。不管怎样,错误是这样说的:

<head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html>

调用它的代码是这样的(为清楚起见缩短):

    $.ajax({
    url: "lus.php",
    data: { "SU": SU, // This is any alphanumeric string without spaces or special characters
            "LU": $("#LongURI").val() // This a URI e.g. https://example.com/abc.html
    },
    success: function(data) {
        alert(data);
    },
    error: function(jqXHR, exception) {
        var msg = '';
        if (jqXHR.status === 0) {
            msg = 'Not connected. Verify Network.';
        } else if (jqXHR.status == 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status == 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.\n' + jqXHR.responseText;
        }
        alert('There was an error! ' + msg);
    },

    type: 'GET'
});

如果我删除 LU 变量中的“://”,它就可以工作了!这意味着我需要对其进行编码,对吧,但这没有用(见下文)。我可以删除“https://”或“http://”然后将其添加回来,但这引出了一个问题,即我将如何传递“https://”或“http://”(它可能是或者) 到 lus.php 文件。

这是我目前尝试过的方法:

  1. 向 .htaccess 添加了以下行
<IfModule mod_security.c>
  SecFilterEngine Off
  SecFilterScanPOST Off
</IfModule>

基于。没用。

  1. 删除了 lus.php 中的所有代码(按步骤)以确保 php 代码没有问题。没有变化。

  2. 编码此行

             "LU": $("#LongURI").val()
    

对此:

            "LU": encodeURI($("#LongURI").val())

并尝试在 php 代码中进行和不进行以下更改。

if(isset($_GET['LU']))
    $LU = trim(urldecode(($_GET['LU'])));

没有骰子。

  1. 将 GET 更改为 POST(在这里变得绝望)。这给了我一个内部服务器错误(500)而不是上面的错误消息。但是,没有进一步的信息,因为在开发者工具中,Response 是空的。

编辑: 5. 我的 .htaccess 文件中有几个重写条件。所以我重命名了那个文件并尝试了同样的操作。同样 result/same 错误。

关于如何解决这个问题有什么想法吗?

如果可以,请查看您的 Apache 错误日志(在我的例子中位于 $HOME/logs/apache.error.log)。你可以看到这样的东西。

Fri Nov 19 08:35:22.757764 2021] [:error] [pid 16443:tid 140407413257984] [client ****] [client ****] ModSecurity: Access denied with code 403 (phase 2). Pattern match "<script\\b" at REQUEST_URI. [file "/etc/modsecurity/07_XSS_XSS.conf"] [line "65"] [id "212620"] [rev "3"] [msg "WAF: Cross-site Scripting (XSS) Attack||webs.ccnorte.es|F|2"] [data "Matched Data: <script found within REQUEST_URI: /panel/?q=\x22><script>alert(1)</script>"] [severity "CRITICAL"] [tag "CWAF"] [tag "XSS"] [hostname "****"] [uri "***"] [unique_id "YZdTulJinXUAAEA7KdcAAABC"]

查找 modsecurity 规则 ID。在此示例中,它是 [id "212620"],与 XSS 攻击相关。

然后您可以在您的 htaccess 中删除此规则或修改您的代码以确切了解错误原因。

# .htaccess
SecRuleRemoveById 212620

事实证明我的托管服务提供商阻止了被调用的页面 (lus.php)。这需要大量的探索和两次聊天。如果有人遇到此问题并且可以快速排除基本情况,请咨询您的托管服务提供商或 UFW 规则是否是您自己的服务器。