【问题标题】:Regex like search in mongo db nestex query正则表达式喜欢在 mongodb 嵌套查询中搜索
【发布时间】:2014-12-12 15:32:15
【问题描述】:

我目前正在使用 MongoDB 来存储我的数据的项目。这两个集合具有以下格式:

CVE

{
  "Modified" : "2014-09-27T06:55:04.867-04:00",
  "Published" : "2014-09-27T06:55:04.867-04:00",
  "_id" : ObjectId("542923711bb35a10e3053986"),
  "cvss" : "9.3",
  "cwe" : "Unknown",
  "id" : "CVE-2014-3062",
  "last-modified" : "2014-09-29T09:00:35.803-04:00",
  "references" : [
    "http://xforce.iss.net/xforce/xfdb/93540",
    "http://www-01.ibm.com/support/docview.wss?uid=swg21683609"
  ],
  "summary" : "Unspecified vulnerability in IBM Security QRadar SIEM 7.1 MR2 and 7.2 MR2 allows remote attackers to execute arbitrary code via unknown vectors.",
  "vulnerable_configuration" : [ 
    "cpe:/a:ibm:qradar_security_information_and_event_manager:7.1.0",
    "cpe:/a:ibm:qradar_security_information_and_event_manager:7.2.0" 
  ]
}

mgmt_whitelist

{
 "_id" : ObjectId("548855641bb35a2dc5675244"),
 "id" : "cpe:/a:ibm:qradar_security_information_and_event_manager:7.2.0"
}

我想在 mgmt_whitelist 中找到 CVE 中具有易受攻击配置的所有项目,我可以通过以下方式轻松实现:

db.cves.find(
    {'vulnerable_configuration':
        {'$in': db.mgmt_whitelist.distinct('id')}
    }
).sort({Modified: -1}).limit(10)

但是,白名单中也包含类似

的记录
{
  "_id" : ObjectId("54885ff41bb35a2f57a7c567"),
  "id" : "cpe:/a:7-zip:7-zip" 
}

这是一种没有版本的 CPE 格式。我希望能够进行类似正则表达式的搜索,以便在搜索中也能找到这些列入白名单的项目。

我试过了

db.cves.find(
    {'vulnerable_configuration':
        {'$in': 
            {'$regex':db.mgmt_whitelist.distinct('id')
        }
    }
).sort({Modified: -1}).limit(10)

但这不起作用...我应该怎么做?

提前致谢,

皮吉

【问题讨论】:

  • 对于每个不同的whitelist id,您需要组合一个regex 对象,具体取决于您需要如何匹配。然后你可以执行它:({ vulnerable_configuration: { $in: [ RegexOne, RegexTwo,...] } })

标签: regex mongodb whitelist


【解决方案1】:

我发现了问题。要添加一个包含 : 和 / 的正则表达式,我需要使用 new RegExp()。 所以最后,我使用这样的东西:

db.cves.find(
  {'vulnerable_configuration':
    {'$in': 
      [new RegExp("cpe:/a:gnu:bash"),new RegExp("cpe:/a:adobe:acrobat_reader")]
    }
  }
).sort({Modified: -1}).limit(10)

很遗憾我不能在一个语句中完成所有事情(如果我错了,请纠正我),但我可以用我的代码解决这个问题。谢谢你的建议,蝙蝠尖叫

【讨论】:

  • new RegExp("cpe:/a:adobe:acrobat_reader") 是错误的代码。您必须 regex-quote 提供给正则表达式构造函数的字符串。
【解决方案2】:
RegExp.quote = function (str) {
    return (str + '').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
};

function startsWithExp(string) {
    return new RegExp("^" + RegExp.quote(string));
}
// -----------------------------------------------------------------

db.cves.find({
    vulnerable_configuration: {
        $in: db.mgmt_whitelist.distinct('id').map(startsWithExp)
    }
}).sort({Modified: -1}).limit(10);

在更高级的场景中,您可能希望返回字符串和正则表达式的混合列表,以提高性能(如有必要 - 衡量增加的复杂性是否有任何真正的好处):

function cpeStartsWithOrMatches(cve_id) {
    var cpeWithVersion = /:\d+(?:\.\d+)+$/;

    return cpeWithVersion.test(string) ? string : startsWithExp(string);
}
// -----------------------------------------------------------------

db.cves.find({
    vulnerable_configuration: {
        $in: db.mgmt_whitelist.distinct('id').map(cpeStartsWithOrMatches)
    }
}).sort({Modified: -1}).limit(10);

或者你可以构造一个大表达式:

function startsWithAnyExp(strings) {
    return new RegExp("^(?:" + strings.map(RegExp.quote).join("|") + ")");
}

db.cves.find({
    vulnerable_configuration: {
        $regex: startsWithAnyExp(db.mgmt_whitelist.distinct('id'))
    }
}).sort({Modified: -1}).limit(10);

【讨论】:

  • 感谢代码片段,但我使用 Python 编写代码...到目前为止,我似乎无法让它工作...任何提示我正确方向? :)
  • 没关系,我想通了!非常感谢您提供正确方向的提示!
  • @pidgey 什么?您没有在自己的答案中使用 Python 代码,也没有在自己的问题中使用 Python 代码。 o_O
  • 哈哈抱歉造成混淆,该代码是原始的 mongo 查询,而不是编程代码
  • 如果你可以使用new RegExp(),它有点 Javascript。这让我很困惑。
猜你喜欢
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多