forked from cve-search/cve-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuery.py
149 lines (130 loc) · 3.6 KB
/
Query.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Query tools
#
# Software is free software released under the "GNU Affero General Public License v3.0"
#
# Copyright (c) 2014-2018 Alexandre Dulaunoy - [email protected]
# Copyright (c) 2014-2018 Pieter-Jan Moreels - [email protected]
import urllib.parse
import requests
import os
import sys
runPath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(runPath, ".."))
import lib.CVEs as cves
import lib.DatabaseLayer as db
import lib.Toolkit as tk
from lib.Config import Configuration
rankinglookup = True
redisdb = Configuration.getRedisVendorConnection()
def findranking(cpe=None, loosy=True):
if cpe is None:
return False
result = False
if loosy:
for x in cpe.split(':'):
if x is not '':
i = db.findRanking(cpe, regex=True)
if i is None:
continue
if 'rank' in i:
result = i['rank']
else:
i = db.findRanking(cpe, regex=True)
print (cpe)
if i is None:
return result
if 'rank' in i:
result = i['rank']
return result
def lookupcpe(cpeid=None):
e = db.getCPE(cpeid)
if e is None:
return cpeid
if 'id' in e:
return e['title']
def lastentries(limit=5, namelookup=False, rankinglookup=True):
entries = []
for item in db.getCVEs(limit):
if not namelookup and rankinglookup is not True:
entries.append(item)
else:
if "vulnerable_configuration" in item:
vulconf = []
ranking = []
for conf in item['vulnerable_configuration']:
if namelookup:
vulconf.append(lookupcpe(cpeid=conf))
else:
vulconf.append(conf)
if rankinglookup:
rank = findranking(cpe=conf)
if rank and rank not in ranking:
ranking.append(rank)
item['vulnerable_configuration'] = vulconf
if rankinglookup and len(ranking) > 0:
item['ranking'] = ranking
entries.append(item)
return entries
def apigetcve(api, cveid=None):
if cveid is None:
return False
url = urllib.parse.urljoin(api, "api/cve/"+cveid)
urltoget = urllib.parse.urljoin(url, cveid)
r = requests.get(urltoget)
if r.status_code is 200:
return r.text
else:
return False
def apibrowse(api, vendor=None):
url = urllib.parse.urljoin(api, "api/browse")
if vendor is None:
r = requests.get(url)
else:
urlvendor = url + "/" + vendor
r = requests.get(urlvendor)
if r.status_code is 200:
return r.text
else:
return False
def apisearch(api, query=None):
if query is None:
return False
url = urllib.parse.urljoin(api, "api/search/")
url = url+query
r = requests.get(url)
if r.status_code is 200:
return r.text
else:
return False
# Lastly added
def cvesForCPE(cpe):
cpe = tk.toStringFormattedCPE(cpe)
data = []
if cpe:
cvesp = cves.last(rankinglookup=False, namelookup=False, via4lookup=True, capeclookup=False)
for x in db.cvesForCPE(cpe):
data.append(cvesp.getcve(x['id']))
return data
def getBrowseList(vendor):
result = {}
if (vendor is None) or type(vendor) == list:
v1 = redisdb.smembers("t:/o")
v2 = redisdb.smembers("t:/a")
v3 = redisdb.smembers("t:/h")
vendor = sorted(list(set(list(v1) + list(v2) + list(v3))))
cpe = None
else:
cpenum = redisdb.scard("v:" + vendor)
if cpenum < 1:
return None
p = redisdb.smembers("v:" + vendor)
cpe = sorted(list(p))
result["vendor"] = vendor
result["product"] = cpe
return result
def getVersionsOfProduct(product):
p = redisdb.smembers("p:" + product)
return sorted(list(p))