正则表达式喜欢在mongodb嵌套查询中搜索

时间:2014-12-12 15:32:16

标签: regex mongodb whitelist

我目前正在开展一个项目,我使用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"
}

我希望在mgt_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)

但那不起作用......我该怎么做呢?

提前致谢,

Pidgey

2 个答案:

答案 0 :(得分:0)

我发现了问题。要添加包含:和/的正则表达式,我需要使用新的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)

遗憾的是,我不能在一个声明中做任何事情(如果我错了请纠正我),但我可以用我的代码来解决这个问题。感谢您的建议,BatScream

答案 1 :(得分:0)

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);
相关问题