def update(self, cpeOld, cpeNew, cpeType): try: cpeOld = cpeOld.strip() cpeNew = cpeNew.strip() # translate cpes cpeOld = toStringFormattedCPE(cpeOld) cpeNew = toStringFormattedCPE(cpeNew) if cpeOld and cpeNew: # already in db? if self.check(cpeOld.split('#')[0]): cpeID = cpeNew.split('#')[0] cpeID.strip() # comments comments = cpeNew.split('#') del comments[0] getattr(db, "update" + self.collection)(cpeOld.split('#')[0], cpeID, cpeType, comments) return True return False except Exception as ex: print(ex) print("Error updating item in database: %s" % (ex)) sys.exit()
def update(self, cpeOld, cpeNew): try: cpeOld = cpeOld.strip() cpeNew = cpeNew.strip() # translate cpes cpeOld = toStringFormattedCPE(cpeOld) cpeNew = toStringFormattedCPE(cpeNew) if cpeOld and cpeNew: # already in db? if self.check(cpeOld.split('#')[0]) != 0: if '#' in cpeNew: # there are extra comments cpeID = cpeNew.split('#')[0] cpeID.strip() # allow multiple comments comments = cpeNew.split('#') del comments[0] cpeListElement = {'id': cpeID, 'comments': comments} else: cpeListElement = {'id': cpeNew} cpeDeleteElement = {'id': cpeOld.split('#')[0]} self.collection.update(cpeDeleteElement, cpeListElement, upsert=False, multi=False) return True return False except Exception as ex: print(ex) print("Error updating item in database: {:d}".format(ex)) sys.exit()
def remove(self, cpe): try: cpe = cpe.strip() # translate cpe if toStringFormattedCPE(cpe): cpe = toStringFormattedCPE(cpe) # check if the cpe is in the list if self.check(cpe): getattr(db, "removeFrom" + self.collection)(cpe) return True else: return False except Exception as ex: print("Error removing item from database: %s" % (ex)) sys.exit()
def remove(self, cpe): try: cpe = cpe.strip() # translate cpe if toStringFormattedCPE(cpe): cpe = toStringFormattedCPE(cpe) # check if the cpe is in the list if self.check(cpe): getattr(db, "removeFrom"+self.collection)(cpe) return True else: return False except Exception as ex: print("Error removing item from database: %s"%(ex)) sys.exit()
def remove(self, cpe): try: cpe = cpe.strip() # translate cpe if toStringFormattedCPE(cpe): cpe = toStringFormattedCPE(cpe) # check if the cpe is in the list amount = self.check(cpe) if amount > 0: cpeListElement = {'id': cpe} self.collection.remove(cpeListElement) return amount except Exception as ex: print("Error removing item from database: {:d}".format(ex)) sys.exit()
def _enhance(self, scan): cvesp = cves.last(rankinglookup=False, namelookup=False, vfeedlookup=True, capeclookup=False) for system in scan['systems']: cpe = system['cpes'] if 'cpes' in system else None if cpe: cpes = [] for c in cpe: c = c.lower() cpes.append({ 'cpe': c, 'cves': [ cvesp.getcve(x['id']) for x in db.cvesForCPE(toStringFormattedCPE(c)) ] }) system['cpes'] = cpes for service in system['services']: if 'cpe' in service: service['cves'] = db.cvesForCPE(service['cpe']) scan['enhanced'] = {"time": int(datetime.now().strftime('%s'))} return scan
def __init__(self, cpe): self.verifyVars(cpe) self.cpe = toStringFormattedCPE(cpe.strip()) splits = self.cpe.split(":") x = 1 if cpe.startswith("cpe:2.3:") else 0 self.vendor = splits[2 + x].title() self.product = splits[3 + x].title() self.version = splits[4 + x].title() if len(splits) > 4 + x else None
def __init__(self, cpe): self.verifyVars(cpe) self.cpe=toStringFormattedCPE(cpe.strip()) splits=self.cpe.split(":") x = 1 if cpe.startswith("cpe:2.3:") else 0 self.vendor =splits[2+x].title() self.product=splits[3+x].title() self.version=splits[4+x].title()if len(splits)>4+x else None
def apiCVEFor(cpe): cpe=urllib.parse.unquote_plus(cpe) cpe=toStringFormattedCPE(cpe) r = [] cvesp = cves.last(rankinglookup=False, namelookup=False, vfeedlookup=True, capeclookup=False) for x in db.cvesForCPE(cpe): r.append(cvesp.getcve(x['id'])) return json.dumps(r)
def apiCVEFor(cpe): cpe=urllib.parse.unquote_plus(cpe) cpe=toStringFormattedCPE(cpe) r = [] cvesp = cves.last(rankinglookup=False, namelookup=False, vfeedlookup=True, capeclookup=False) for x in dbLayer.cvesForCPE(cpe): r.append(cvesp.getcve(x['id'])) return json.dumps(r)
def apiCVEFor(cpe): cpe=urllib.parse.unquote_plus(cpe) cpe=toStringFormattedCPE(cpe) if not cpe: cpe='None' r = [] cvesp = cves.last(rankinglookup=False, namelookup=False, reflookup=True, capeclookup=False) for x in dbLayer.cvesForCPE(cpe): r.append(cvesp.getcve(x['id'])) return json.dumps(r, default=json_util.default)
def startElement(self, name, attrs): if name == 'entry': self.cves.append({ 'id': attrs.get('id'), 'references': [], 'vulnerable_configuration': [], 'vulnerable_configuration_cpe_2_2': [], 'vulnerable_product': [] }) self.ref = attrs.get('id') elif name == 'cpe-lang:fact-ref': self.cves[-1]['vulnerable_configuration'].append( toStringFormattedCPE(attrs.get('name'))) self.cves[-1]['vulnerable_configuration_cpe_2_2'].append( attrs.get('name')) elif name == 'vuln:product': self.inVPElem = 1 self.VP = "" elif name == 'cvss:score': self.inCVSSElem = 1 self.CVSS = "" elif name == 'cvss:access-vector': self.inAccessvElem = 1 self.accessv = "" elif name == 'cvss:access-complexity': self.inAccesscElem = 1 self.accessc = "" elif name == 'cvss:authentication': self.inAccessaElem = 1 self.accessa = "" elif name == 'cvss:confidentiality-impact': self.inImpactcElem = 1 self.impactc = "" elif name == 'cvss:integrity-impact': self.inImpactiElem = 1 self.impacti = "" elif name == 'cvss:availability-impact': self.inImpactaElem = 1 self.impacta = "" elif name == 'cvss:generated-on-datetime': self.inCVSSgenElem = 1 self.cvssgen = "" elif name == 'vuln:summary': self.inSUMMElem = 1 self.SUMM = "" elif name == 'vuln:published-datetime': self.inDTElem = 1 self.DT = "" elif name == 'vuln:last-modified-datetime': self.inPUBElem = 1 self.PUB = "" elif name == 'vuln:reference': self.cves[-1]['references'].append(attrs.get('href')) elif name == 'vuln:cwe': self.cves[-1]['cwe'] = attrs.get('id')
def apiCVEFor(cpe): col = db['cves'] cpe=urllib.parse.unquote_plus(cpe) cpe=toStringFormattedCPE(cpe) if not cpe: cpe='None' vulns = col.find({"vulnerable_configuration": {'$regex': cpe}}).sort("Modified", -1) r = [] cvesp = cves.last(rankinglookup=False, namelookup=False, vfeedlookup=True, capeclookup=False) for x in vulns: r.append(cvesp.getcve(x['id'])) return json.dumps(r)
def apiCVEFor(cpe): col = db['cves'] cpe=urllib.parse.unquote_plus(cpe) cpe=toStringFormattedCPE(cpe) if not cpe: cpe='None' vulns = col.find({"vulnerable_configuration": {'$regex': cpe}}).sort("Modified", -1) r = [] for x in vulns: x.pop('_id') r.append(x) return json.dumps(r)
def qcvesForCPE(cpe, limit=0): cpe = toStringFormattedCPE(cpe) data = [] if cpe: cvesp = CveHandler( rankinglookup=False, namelookup=False, via4lookup=True, capeclookup=False ) r = cvesForCPE(cpe, limit=limit) for x in r["results"]: data.append(cvesp.getcve(x["id"])) return data
def update(self, cpeOld, cpeNew, cpeType): try: cpeOld = cpeOld.strip() cpeNew = cpeNew.strip() # translate cpes cpeOld = toStringFormattedCPE(cpeOld) cpeNew = toStringFormattedCPE(cpeNew) if cpeOld and cpeNew: # already in db? if self.check(cpeOld.split('#')[0]): cpeID = cpeNew.split('#')[0] cpeID.strip() # comments comments = cpeNew.split('#') del comments[0] getattr(db, "update"+self.collection)(cpeOld.split('#')[0], cpeID, cpeType, comments) return True return False except Exception as ex: print(ex) print("Error updating item in database: %s"%(ex)) sys.exit()
def startElement(self, name, attrs): if name == "entry": self.cves.append( { "id": attrs.get("id"), "references": [], "vulnerable_configuration": [], "vulnerable_configuration_cpe_2_2": [], } ) self.ref = attrs.get("id") elif name == "cpe-lang:fact-ref": self.cves[-1]["vulnerable_configuration"].append(toStringFormattedCPE(attrs.get("name"))) self.cves[-1]["vulnerable_configuration_cpe_2_2"].append(attrs.get("name")) elif name == "cvss:score": self.inCVSSElem = 1 self.CVSS = "" elif name == "cvss:access-vector": self.inAccessvElem = 1 self.accessv = "" elif name == "cvss:access-complexity": self.inAccesscElem = 1 self.accessc = "" elif name == "cvss:authentication": self.inAccessaElem = 1 self.accessa = "" elif name == "cvss:confidentiality-impact": self.inImpactcElem = 1 self.impactc = "" elif name == "cvss:integrity-impact": self.inImpactiElem = 1 self.impacti = "" elif name == "cvss:availability-impact": self.inImpactaElem = 1 self.impacta = "" elif name == "cvss:generated-on-datetime": self.inCVSSgenElem = 1 self.cvssgen = "" elif name == "vuln:summary": self.inSUMMElem = 1 self.SUMM = "" elif name == "vuln:published-datetime": self.inDTElem = 1 self.DT = "" elif name == "vuln:last-modified-datetime": self.inPUBElem = 1 self.PUB = "" elif name == "vuln:reference": self.cves[-1]["references"].append(attrs.get("href")) elif name == "vuln:cwe": self.cves[-1]["cwe"] = attrs.get("id")
def get(self, cpe): """ convert 2 to 3 Converts a CPE code to the CPE2.3 standard, stripped of appendices. CPE2.3 is the newer standard, and is a lot more uniform and easier to read than the CPE2.2 standard. """ cpe = toStringFormattedCPE(cpe) if cpe is False: return api.abort( 400, "Conversion failed, the CPE code you provided cannot be converted" ) else: return cpe
def apiCVEFor(cpe): col = db['cves'] cpe = urllib.parse.unquote_plus(cpe) cpe = toStringFormattedCPE(cpe) if not cpe: cpe = 'None' vulns = col.find({ "vulnerable_configuration": { '$regex': cpe } }).sort("Modified", -1) r = [] for x in vulns: x.pop('_id') r.append(x) return json.dumps(r)
def _enhance(self, scan): cvesp = cves.last(rankinglookup=False, namelookup=False, via4lookup=True, capeclookup=False) for system in scan['systems']: cpe=system['cpes'] if 'cpes' in system else None if cpe: cpes=[] for c in cpe: c=c.lower() cpes.append({'cpe':c, 'cves':[cvesp.getcve(x['id']) for x in db.cvesForCPE(toStringFormattedCPE(c))]}) system['cpes']=cpes for service in system['services']: if 'cpe' in service: service['cves']=db.cvesForCPE(service['cpe']) scan['enhanced']={"time": int(datetime.now().strftime('%s'))} return scan
def apiCVEFor(cpe): col = db['cves'] cpe = urllib.parse.unquote_plus(cpe) cpe = toStringFormattedCPE(cpe) if not cpe: cpe = 'None' vulns = col.find({ "vulnerable_configuration": { '$regex': cpe } }).sort("Modified", -1) r = [] cvesp = cves.last(rankinglookup=False, namelookup=False, vfeedlookup=True, capeclookup=False) for x in vulns: r.append(cvesp.getcve(x['id'])) return json.dumps(r)
def insert(self, cpe, cpeType): try: # split comments from cpe comments = cpe.split('#') del comments[0] cpeID = cpe.split('#')[0] if cpeType.lower() == "cpe": cpeID = toStringFormattedCPE(cpeID) # check format if cpeID: # already in db? if not self.check(cpeID): getattr(db, "addTo"+self.collection)(cpeID, cpeType, comments) return True return False except Exception as ex: print("Error inserting item in database: %s"%(ex)) sys.exit()
def startElement(self, name, attrs): if name == 'entry': self.cves.append({'id': attrs.get('id'), 'references': [], 'vulnerable_configuration': [], 'vulnerable_configuration_cpe_2_2':[]}) self.ref = attrs.get('id') elif name == 'cpe-lang:fact-ref': self.cves[-1]['vulnerable_configuration'].append(toStringFormattedCPE(attrs.get('name'))) self.cves[-1]['vulnerable_configuration_cpe_2_2'].append(attrs.get('name')) elif name == 'cvss:score': self.inCVSSElem = 1 self.CVSS = "" elif name == 'cvss:access-vector': self.inAccessvElem = 1 self.accessv = "" elif name == 'cvss:access-complexity': self.inAccesscElem = 1 self.accessc = "" elif name == 'cvss:authentication': self.inAccessaElem = 1 self.accessa = "" elif name == 'cvss:confidentiality-impact': self.inImpactcElem = 1 self.impactc = "" elif name == 'cvss:integrity-impact': self.inImpactiElem = 1 self.impacti = "" elif name == 'cvss:availability-impact': self.inImpactaElem = 1 self.impacta = "" elif name == 'cvss:generated-on-datetime': self.inCVSSgenElem = 1 self.cvssgen = "" elif name == 'vuln:summary': self.inSUMMElem = 1 self.SUMM = "" elif name == 'vuln:published-datetime': self.inDTElem = 1 self.DT = "" elif name == 'vuln:last-modified-datetime': self.inPUBElem = 1 self.PUB = "" elif name == 'vuln:reference': self.cves[-1]['references'].append(attrs.get('href')) elif name == 'vuln:cwe': self.cves[-1]['cwe'] = attrs.get('id')
def insert(self, cpe, cpeType): try: # split comments from cpe if '#' in cpe: comments = cpe.split('#') del comments[0] cpeID = cpe.split('#')[0] if cpeType.lower() == "cpe": cpeID = toStringFormattedCPE(cpeID) # check format if cpeID: # already in db? if self.check(cpeID) == 0: if '#' in cpe: cpeListElement = {'id': cpeID, 'type':cpeType, 'comments': comments} else: cpeListElement = {'id': cpeID, 'type':cpeType} self.collection.insert(cpeListElement) return True return False except Exception as ex: print("Error inserting item in database: %s"%(ex)) sys.exit()
def cpe23(cpe): cpe = toStringFormattedCPE(cpe) if not cpe: cpe = 'None' return cpe
# dict cpedict = Configuration.getCPEDict() # make parser parser = make_parser() ch = CPEHandler() parser.setContentHandler(ch) # check modification date try: f = Configuration.getFile(cpedict) except: sys.exit("Cannot open url %s. Bad URL or not connected to the internet?"%(cpedict)) i = db.getLastModified('cpe') if i is not None: if f.headers['last-modified'] == i: print("Not modified") sys.exit(0) # parse xml and store in database parser.parse(f) cpeList=[] for x in progressbar(ch.cpe): x['id']= toStringFormattedCPE(x['name']) x['title']=x['title'][0] x['cpe_2_2'] = x.pop('name') if not x['references']: x.pop('references') cpeList.append(x) db.bulkUpdate("cpe", cpeList) #update database info after successful program-run db.setColUpdate('cpe', f.headers['last-modified'])
def get(self, id): cpe = self.db.cpe_get(toStringFormattedCPE(id)) return cpe if cpe else CPE(id)
info = db.info # make parser parser = make_parser() ch = CPEHandler() parser.setContentHandler(ch) # check modification date try: f = urlopen(cpedict) except: sys.exit("Cannot open url %s. Bad URL or not connected to the internet?"%(cpedict)) i = info.find_one({'db': 'cpe'}) if i is not None: if f.headers['last-modified'] == i['last-modified']: sys.exit("Not modified") # parse xml and store in database parser.parse(f) bulk = cpe.initialize_ordered_bulk_op() for x in progressbar(ch.cpe): name = toStringFormattedCPE(x['name']) oldCPE = x['name'] title = x['title'][0] if x['references']: bulk.find({'id': name}).upsert().update({"$set":{'title': title, 'cpe_2_2':oldCPE, 'references': x['references']}}) else: bulk.find({'id': name}).upsert().update({"$set":{'title': title, 'cpe_2_2':oldCPE}}) bulk.execute() #update database info after successful program-run info.update({'db': 'cpe'}, {"$set": {'last-modified': f.headers['last-modified']}}, upsert=True)
}] padtext2 = [{ 'in': ['a', 'b', 'c'], 'expect': ['a', 'b', 'c', 'text', 'text'] }, { 'in': ['a', 'b', 'c', 'd', 'e'], 'expect': ['a', 'b', 'c', 'd', 'e'] }, { 'in': ['a', 'b', 'c', 'd', 'e', 'f'], 'expect': ['a', 'b', 'c', 'd', 'e', 'f'] }] result = [] for x in extend: result.append( resultOf(x['in'], toStringFormattedCPE(x['in'], autofill=True), x['expect'])) printResults('Translate to 2.3 - success/autofill', result) result = [] for x in trans: result.append(resultOf(x['in'], toStringFormattedCPE(x['in']), x['expect'])) printResults('Translate to 2.3 - success/no autofill', result) result = [] for x in old: result.append(resultOf(x['in'], toOldCPE(x['in']), x['expect'])) printResults('Translate to 2.2 - success/no autofill', result) result = []
# make parser parser = make_parser() ch = CPEHandler() parser.setContentHandler(ch) # check modification date f = urlopen(cpedict) i = info.find_one({'db': 'cpe'}) if i is not None: if f.headers['last-modified'] == i['last-modified']: sys.exit("Not modified") # parse xml and store in database parser.parse(f) bulk = cpe.initialize_ordered_bulk_op() for x in progressbar(ch.cpe): name = toStringFormattedCPE(x['name']) oldCPE = x['name'] title = x['title'][0] if x['references']: bulk.find({ 'id': name }).upsert().update({ "$set": { 'title': title, 'cpe_2_2': oldCPE, 'references': x['references'] } }) else: bulk.find({ 'id': name
def api_cpe23(self, cpe): cpe = toStringFormattedCPE(cpe) return cpe, 200 if cpe else "None", 404
# make parser parser = make_parser() ch = CPEHandler() parser.setContentHandler(ch) # check modification date try: (f, r) = Configuration.getFeedData('cpe') except: sys.exit("Cannot open url %s. Bad URL or not connected to the internet?" % (Configuration.getFeedURL("cpe"))) i = db.getLastModified('cpe') last_modified = parse_datetime(r.headers['last-modified'], ignoretz=True) if i is not None: if last_modified == i: print("Not modified") sys.exit(0) # parse xml and store in database parser.parse(f) cpeList = [] for x in progressbar(ch.cpe): x['id'] = toStringFormattedCPE(x['name']) x['title'] = x['title'][0] x['cpe_2_2'] = x.pop('name') if not x['references']: x.pop('references') cpeList.append(x) db.bulkUpdate("cpe", cpeList) #update database info after successful program-run db.setColUpdate('cpe', last_modified)
def cpe23(cpe): cpe = toStringFormattedCPE(cpe) if not cpe: cpe='None' return cpe
{'in':'cpe:2.3:a:7-zip:7-zip:4.65:-:-:-:-:-:x64', 'expect':'cpe:/a:7-zip:7-zip:4.65::~~~~x64~'}, {'in':'cpe:2.3:a:acl:acl:9.1.0.213', 'expect':'cpe:/a:acl:acl:9.1.0.213'}] pad1=[{'in':['a','b','c'], 'expect':['a','b','c',None,None]}, {'in':['a','b','c','d','e'], 'expect':['a','b','c','d','e']}, {'in':['a','b','c','d','e','f'], 'expect':['a','b','c','d','e','f']}] padtext1=[{'in':['a','b','c'], 'expect':['a','b','c','-','-']}, {'in':['a','b','c','d','e'], 'expect':['a','b','c','d','e']}, {'in':['a','b','c','d','e','f'], 'expect':['a','b','c','d','e','f']}] padtext2=[{'in':['a','b','c'], 'expect':['a','b','c','text','text']}, {'in':['a','b','c','d','e'], 'expect':['a','b','c','d','e']}, {'in':['a','b','c','d','e','f'], 'expect':['a','b','c','d','e','f']}] result=[] for x in extend: result.append(resultOf(x['in'],toStringFormattedCPE(x['in'],autofill=True),x['expect'])) printResults('Translate to 2.3 - success/autofill',result) result=[] for x in trans: result.append(resultOf(x['in'],toStringFormattedCPE(x['in']),x['expect'])) printResults('Translate to 2.3 - success/no autofill',result) result=[] for x in old: result.append(resultOf(x['in'],toOldCPE(x['in']),x['expect'])) printResults('Translate to 2.2 - success/no autofill',result) result=[] for x in pad1: result.append(resultOf(x['in'],pad(x['in'],5),x['expect']))
{"in": ["a", "b", "c", "d", "e", "f"], "expect": ["a", "b", "c", "d", "e", "f"]}, ] padtext1 = [ {"in": ["a", "b", "c"], "expect": ["a", "b", "c", "-", "-"]}, {"in": ["a", "b", "c", "d", "e"], "expect": ["a", "b", "c", "d", "e"]}, {"in": ["a", "b", "c", "d", "e", "f"], "expect": ["a", "b", "c", "d", "e", "f"]}, ] padtext2 = [ {"in": ["a", "b", "c"], "expect": ["a", "b", "c", "text", "text"]}, {"in": ["a", "b", "c", "d", "e"], "expect": ["a", "b", "c", "d", "e"]}, {"in": ["a", "b", "c", "d", "e", "f"], "expect": ["a", "b", "c", "d", "e", "f"]}, ] result = [] for x in extend: result.append(resultOf(x["in"], toStringFormattedCPE(x["in"], autofill=True), x["expect"])) printResults("Translate to 2.3 - success/autofill", result) result = [] for x in trans: result.append(resultOf(x["in"], toStringFormattedCPE(x["in"]), x["expect"])) printResults("Translate to 2.3 - success/no autofill", result) result = [] for x in old: result.append(resultOf(x["in"], toOldCPE(x["in"]), x["expect"])) printResults("Translate to 2.2 - success/no autofill", result) result = [] for x in pad1: result.append(resultOf(x["in"], pad(x["in"], 5), x["expect"]))
# make parser parser = make_parser() ch = CPEHandler() parser.setContentHandler(ch) # check modification date try: f = Configuration.getFile(cpedict) except: sys.exit("Cannot open url %s. Bad URL or not connected to the internet?" % (cpedict)) i = db.getLastModified("cpe") last_modified = parse_datetime(f.headers["last-modified"]) if i is not None: if last_modified == i: print("Not modified") sys.exit(0) # parse xml and store in database parser.parse(f) cpeList = [] for x in progressbar(ch.cpe): x["id"] = toStringFormattedCPE(x["name"]) x["title"] = x["title"][0] x["cpe_2_2"] = x.pop("name") if not x["references"]: x.pop("references") cpeList.append(x) db.bulkUpdate("cpe", cpeList) # update database info after successful program-run db.setColUpdate("cpe", last_modified)