forked from cve-search/cve-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_dump.py
72 lines (61 loc) · 1.69 KB
/
db_dump.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Tool to dump in JSON the database along with the associated ranking
#
# Software is free software released under the "Modified BSD license"
#
# Copyright (c) 2012-2013 Alexandre Dulaunoy - [email protected]
import pymongo
import os
import json
from bson import json_util
connect = pymongo.Connection()
db = connect.cvedb
collection = db.cves
def lookupcpe(cpeid = None):
e = db.cpe.find_one({'id': cpeid})
if e is None:
return cpeid
if 'id' in e:
return e['title']
def findranking(cpe = None, loosy = True):
if cpe is None:
return False
r = db.ranking
result = False
if loosy:
for x in cpe.split(':'):
if x is not '':
i = r.find_one({'cpe': {'$regex':x}})
if i is None:
continue
if 'rank' in i:
result = i['rank']
else:
i = r.find_one({'cpe': {'$regex':cpe}})
print (cpe)
if i is None:
return result
if 'rank' in i:
result = i['rank']
return result
def dumpallcveid ():
cveid = []
for x in collection.find({}).sort('_id',1):
cveid.append(x['id'])
return cveid
def getcve (cveid=None):
if cveid is None:
return False
return collection.find_one({'id': cveid})
for cveid in dumpallcveid():
item = getcve(cveid=cveid)
title = item['summary'][0:70]
ranking = []
for conf in item['vulnerable_configuration']:
rank = findranking(cpe=conf)
if rank and rank not in ranking:
ranking.append(rank)
item['ranking'] = ranking
print (json.dumps(item, sort_keys=True, default=json_util.default))