【问题标题】:Decrypt values with the same key at different levels from base64从base64解密不同级别的相同密钥的值
【发布时间】:2021-05-21 05:31:56
【问题描述】:

我的输入如下。我想搜索SearchString 密钥(您可以看到我们不能为它使用固定索引)并且当密钥出现时从base64 解密它的值(可能使用@base64d 过滤器)。 JQ可以吗?如果有,怎么做?

[
  {
    "Name": "searchblock",
    "Priority": 3,
    "Statement": {
      "RateBasedStatement": {
        "Limit": 100,
        "AggregateKeyType": "IP",
        "ScopeDownStatement": {
          "ByteMatchStatement": {
            "SearchString": "Y2F0YWxvZ3NlYXJjaA==",
            "FieldToMatch": {
              "UriPath": {}
            },
            "TextTransformations": [
              {
                "Priority": 0,
                "Type": "LOWERCASE"
              }
            ],
            "PositionalConstraint": "CONTAINS"
          }
        }
      }
    },
    "Action": {
      "Block": {}
    },
    "VisibilityConfig": {
      "SampledRequestsEnabled": true,
      "CloudWatchMetricsEnabled": true,
      "MetricName": "searchblock"
    }
  },
  {
    "Name": "bot-block",
    "Priority": 4,
    "Statement": {
      "ByteMatchStatement": {
        "SearchString": "Ym90",
        "FieldToMatch": {
          "SingleHeader": {
            "Name": "user-agent"
          }
        },
        "TextTransformations": [
          {
            "Priority": 0,
            "Type": "LOWERCASE"
          }
        ],
        "PositionalConstraint": "CONTAINS"
      }
    },
    "Action": {
      "Allow": {}
    },
    "VisibilityConfig": {
      "SampledRequestsEnabled": true,
      "CloudWatchMetricsEnabled": true,
      "MetricName": "user-agent"
    }
  }
]

【问题讨论】:

    标签: json base64 jq


    【解决方案1】:

    当固定路径不可用时,我们使用 pathpathsgetpathsetpath 内置函数进行此类操作。

    getpath(paths | select(.[-1] == "SearchString")) |= @base64d
    

    Online demo

    【讨论】:

    • 感谢您宝贵的时间,它在我的本地系统(jq 版本 1.6)中运行良好。但它在我的服务器中返回错误“jq: error (at my.json:253): Invalid结果为“ZnJlc”的路径表达式(jq 1.5 版),
    • 在使用之前检查您的 jq 版本。仅适用于 1.6
    • 我升级了 jq 版本并且运行良好,另一个答案只返回了相同的输入 JSON。非常感谢?
    【解决方案2】:

    walk 非常适合这种任务:

    walk(if type == "object" and .SearchString 
         then .SearchString |= @base64d else . end)
    

    使用这种方法,修改程序以使其更健壮也很简单,例如检查 .SearchString 是一个字符串:

    walk(if type == "object" and (.SearchString|type) == "string" 
         then .SearchString |= @base64d else . end)
    

    注意:如果您的 jq 不包含 walk,您可以简单地从任何有信誉的网站或 https://github.com/stedolan/jq/blob/master/src/builtin.jq 复制其 def

    【讨论】:

    • 输出和输入json一样,没有变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2023-01-10
    • 1970-01-01
    • 2016-10-15
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多