Skip to content

Commit

Permalink
Minor 1.4.5
Browse files Browse the repository at this point in the history
Added 'fields' parameter to the supported search methods.
It will limit the returned fields in the data element.
It most cases it will be using self.default_fields.
  • Loading branch information
vulnersCom committed Feb 27, 2019
1 parent d50d1f2 commit 4e82e05
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion vulners/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-

__version__ = "1.4.4"
__version__ = "1.4.5"

from vulners.api import Vulners
43 changes: 24 additions & 19 deletions vulners/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class Vulners(object):
# Default search size parameter
search_size = 100

# Default search fields
# Can be extended or reduced for the query performance

default_fields = ["id", "title", "description", "type", "bulletinFamily", "cvss", "published", "modified", "href"]


def __init__(self, api_key = None, proxies=None, persistent=True):
"""
Expand Down Expand Up @@ -206,7 +211,7 @@ def __archive(self, type, datefrom, dateto):
raise TypeError("Dateto expected to be a string")
return self.vulners_get_request('archive', {'type':type, 'datefrom':datefrom, 'dateto':dateto})

def __search(self, query, skip, size, fields=()):
def __search(self, query, skip, size, fields):
"""
Tech search wrapper for internal lib usage
Expand All @@ -223,7 +228,7 @@ def __search(self, query, skip, size, fields=()):
raise TypeError("Size expected to be a int in range 0-10000")
return self.vulners_post_request('search', {"query":query, 'skip': skip or 0, 'size': size or 0, 'fields': fields or []})

def __id(self, identificator, references):
def __id(self, identificator, references, fields):
"""
Tech ID vulnerability get wrapper
Expand All @@ -236,7 +241,7 @@ def __id(self, identificator, references):
if not isinstance(references, bool):
raise TypeError("References expected to be a bool")

search_request = {"id": identificator}
search_request = {"id": identificator, "fields":fields or []}
if references == True:
search_request['references'] = "True"
return self.vulners_post_request('id', search_request)
Expand Down Expand Up @@ -313,7 +318,7 @@ def __autocomplete(self, query):
raise TypeError("Query expected to be a string")
return self.vulners_post_request('autocomplete', {"query":query})

def search(self, query, limit=100, offset=0, fields=("id", "title", "description", "type", "bulletinFamily", "cvss", "published", "modified", "href")):
def search(self, query, limit=100, offset=0, fields=None):
"""
Search Vulners database for the abstract query
Expand All @@ -327,13 +332,13 @@ def search(self, query, limit=100, offset=0, fields=("id", "title", "description
dataDocs = []
total = 0
for skip in range(offset, total_bulletins, min(self.search_size, limit or self.search_size)):
results = self.__search(query, skip, min(self.search_size, limit or self.search_size), fields or [])
results = self.__search(query, skip, min(self.search_size, limit or self.search_size), fields or self.default_fields)
total = max(results.get('total'), total)
for element in results.get('search'):
dataDocs.append(element.get('_source'))
return AttributeList(dataDocs, total = total)

def searchPage(self, query, pageSize = 20, offset=0, fields=("id", "title", "description", "type", "bulletinFamily", "cvss", "published", "modified", "href")):
def searchPage(self, query, pageSize = 20, offset=0, fields=None):
"""
Search Vulners database for the abstract query, page mode
Expand All @@ -344,12 +349,12 @@ def searchPage(self, query, pageSize = 20, offset=0, fields=("id", "title", "des
:return: List of the found documents, total found bulletins
"""

results = self.__search(query, offset, min(pageSize, self.search_size), fields or [])
results = self.__search(query, offset, min(pageSize, self.search_size), fields or self.default_fields)
total = results.get('total')
dataDocs = [element.get('_source') for element in results.get('search')]
return AttributeList(dataDocs, total = total)

def searchExploit(self, query, lookup_fields=None, limit=500, offset=0, fields=("id", "title", "description", "cvss", "href", "sourceData")):
def searchExploit(self, query, lookup_fields=None, limit=500, offset=0, fields=None):
"""
Search Vulners database for the exploits
Expand All @@ -373,14 +378,14 @@ def searchExploit(self, query, lookup_fields=None, limit=500, offset=0, fields=(
dataDocs = []

for skip in range(offset, total_bulletins, min(self.search_size, limit or self.search_size)):
results = self.__search(searchQuery, skip, min(self.search_size, limit or self.search_size), fields or [])
results = self.__search(searchQuery, skip, min(self.search_size, limit or self.search_size), fields or self.default_fields + ['sourceData'])
total = max(results.get('total'), total)
for element in results.get('search'):
dataDocs.append(element.get('_source'))
return AttributeList(dataDocs, total = total)


def searchExploitPage(self, query, lookup_fields=None, pageSize=20, offset=0, fields=("id", "title", "description", "cvss", "href", "sourceData")):
def searchExploitPage(self, query, lookup_fields=None, pageSize=20, offset=0, fields=None):
"""
Search Vulners database for the exploits, page mode
Expand All @@ -399,7 +404,7 @@ def searchExploitPage(self, query, lookup_fields=None, pageSize=20, offset=0, fi
else:
searchQuery = "bulletinFamily:exploit AND %s" % query

results = self.__search(searchQuery, offset, min(pageSize, self.search_size), fields or [])
results = self.__search(searchQuery, offset, min(pageSize, self.search_size), fields or self.default_fields + ['sourceData'])
total = results.get('total')
dataDocs = [element.get('_source') for element in results.get('search')]
return AttributeList(dataDocs, total = total)
Expand Down Expand Up @@ -443,15 +448,15 @@ def cpeVulnerabilities(self, cpeString, maxVulnerabilities = 50):
dataDocs[elementData.get('bulletinFamily')] = dataDocs.get(elementData.get('bulletinFamily'), []) + [elementData]
return dataDocs

def document(self, identificator):
def document(self, identificator, fields = None):
"""
Fetch information about bulletin by identificator
:param identificator: Bulletin ID. As example - CVE-2017-14174
:param references: Search for the references in all collections
:return: bulletin data dict
"""
results = self.__id(identificator, references=False)
results = self.__id(identificator, references=False, fields = fields or self.default_fields)
return results.get('documents', {}).get(identificator, {})

def audit(self, os, os_version, package):
Expand All @@ -468,7 +473,7 @@ def audit(self, os, os_version, package):
"""
return self.__audit(os, os_version, package)

def documentList(self, identificatorList):
def documentList(self, identificatorList, fields = None):
"""
Fetch information about multiple bulletin identificators
Expand All @@ -478,20 +483,20 @@ def documentList(self, identificatorList):

if not isinstance(identificatorList, (list,set)) or not all(isinstance(item, string_types) for item in identificatorList):
raise TypeError('Identificator list is expected to be a list of strings')
return self.__id(identificatorList, references=False).get('documents')
return self.__id(identificatorList, references=False, fields=fields or self.default_fields).get('documents')

def references(self, identificator):
def references(self, identificator, fields = None):
"""
Fetch information about bulletin references by identificator
:param identificator: Bulletin ID. As example - CVE-2017-14174
:param references: Search for the references in all collections
:return: bulletin data dict
"""
results = self.__id(identificator, references=True)
results = self.__id(identificator, references=True, fields=fields or self.default_fields)
return results.get('references', {}).get(identificator, {})

def referencesList(self, identificatorList):
def referencesList(self, identificatorList, fields = None):
"""
Fetch information about multiple bulletin references
Expand All @@ -500,7 +505,7 @@ def referencesList(self, identificatorList):
"""
if not isinstance(identificatorList, (list,set)) or not all(isinstance(item, string_types) for item in identificatorList):
raise TypeError('Identificator list is expected to be a list of strings')
return self.__id(identificatorList, references=True).get('references')
return self.__id(identificatorList, references=True, fields=fields or self.default_fields).get('references')

def collections(self):
"""
Expand Down

0 comments on commit 4e82e05

Please sign in to comment.