我们可以在 Modsecurity 2.9 rsub 运算符中使用反向引用吗?

问题描述 投票:0回答:1

我想知道我们是否可以使用 Modsecurity 2.9 rsub 运算符 (Apache) 的反向引用。

例如我有这 2 个 JSON 响应主体:

BODY1“非洲”:

{
  "error": "null",
  "id": "1",
  "result": {
    "group": [
    {
      "name": "glossina",
      "class": "insect"
    },
    {
      "name": "latrodectus",
      "class": "arachnid"
    }
        ],
        "climate": "tropical",
        "continent": "africa"
  }
}

又名一行,africa1.json:

{ "error": "null", "id": "1", "result": { "group": [ { "name": "glossina", "class": "insect" }, { "name": "latrodectus", "class": "arachnid" } ], "climate": "tropical", "continent": "africa" }}

BODY2“美国”:

{
  "error": "null",
  "id": "1",
  "result": {
    "group": [
    {
      "name": "trichopoda",
      "class": "insect"
    },
    {
      "name": "marma",
      "class": "arachnid"
    }
        ],
        "climate": "tropical",
        "continent": "america"
  }
}

又名一行,america1.json:

{ "error": "null", "id": "1", "result": { "group": [ { "name": "trichopoda", "class": "insect" }, { "name": "marma", "class": "arachnid" } ], "climate": "tropical", "continent": "america" }}

我想要的是,如果大陆是“美洲”,则将“组”列表清空。

我很容易实现,例如使用 sed:

sed -E 's/"group": \[(.*)\](.*"continent": "america")/"group": \[\] \2/'

见下图:

$ cat africa1.json | sed -E 's/"group": \[(.*)\](.*"continent": "america")/"group": \[\] \2/'
{ "error": "null", "id": "1", "result": { "group": [ { "name": "glossina", "class": "insect" }, { "name": "latrodectus", "class": "arachnid" } ], "climate": "tropical", "continent": "africa" }}

$ cat america1.json | sed -E 's/"group": \[(.*)\](.*"continent": "america")/"group": \[\] \2/'
{ "error": "null", "id": "1", "result": { "group": [] , "climate": "tropical", "continent": "america" }}

这只是一个例子,我可以有多个大陆,以及“组”和“大陆”之间的许多字段(不仅仅是“气候”)。

我们可以对 ModSecurity rsub 运算符使用相同的方法吗? 我很确定还有其他解决方案,但这个非常简单(我使用额外的 LUA 脚本完成了它,但我希望尽可能避免这种情况)。

谢谢

旋转

apache mod-security
1个回答
0
投票

我有一个带有 LUA 脚本的解决方案(正则表达式是近似的,但对于示例来说“足够了”)。

规则:

SecRule RESPONSE_BODY "@rx .*" "id:172,phase:4,exec:/mypath/exec-replace-str.lua,auditlog,log,msg:'From RESPONSE BODY TX.stringrep=%{TX.stringrep}'"
SecRule STREAM_OUTPUT_BODY '@rsub s/%{TX.stringrep}/[ ]' "phase:4,capture,id:173,t:none,nolog,pass"

LUA 脚本 /mypath/exec-replace-str.lua:

function main()

local respBody = m.getvar("RESPONSE_BODY")

_,_,stringrep = string.find( respBody ,'"group": (%[.*%]).*"continent": "america"' )

stringrep = string.gsub( stringrep, "[%[%]]", ".")
 
m.setvar("TX.stringrep", stringrep)

return 1
end

但如果可能的话,我正在寻找没有LUA脚本的答案。

© www.soinside.com 2019 - 2024. All rights reserved.