2022-10-18 23:59:50 +02:00
import json
import requests
from . import check_input_attribute , standard_error_message
2024-07-01 23:25:37 +02:00
from . _vulnerability_parser . vulnerability_parser import (
VulnerabilityMapping , VulnerabilityParser )
from pymisp import MISPObject
2022-10-18 23:59:50 +02:00
misperrors = { ' error ' : ' Error ' }
mispattributes = { ' input ' : [ ' vulnerability ' ] , ' format ' : ' misp_standard ' }
2024-08-12 11:23:10 +02:00
moduleinfo = {
' version ' : ' 1 ' ,
' author ' : ' Christian Studer ' ,
' description ' : ' An expansion module to query the VARIoT db API for more information about a vulnerability. ' ,
' module-type ' : [ ' expansion ' , ' hover ' ] ,
' name ' : ' VARIoT db Lookup ' ,
' logo ' : ' variot.png ' ,
' requirements ' : [ ' A VARIoT db API key (if you do not want to be limited to 100 queries / day) ' ] ,
' features ' : ' The module takes a vulnerability attribute as input and queries que VARIoT db API to gather additional information. \n \n The `vuln` endpoint is queried first to look for additional information about the vulnerability itself. \n \n The `exploits` endpoint is also queried then to look for the information of the potential related exploits, which are parsed and added to the results using the `exploit` object template. ' ,
' references ' : [ ' https://www.variotdbs.pl/ ' ] ,
' input ' : ' Vulnerability attribute. ' ,
' output ' : ' Additional information about the vulnerability, as it is stored on the VARIoT db, about the vulnerability itself, and the potential related exploits. ' ,
}
2022-10-18 23:59:50 +02:00
moduleconfig = [ ' API_key ' ]
variotdbs_url = ' https://www.variotdbs.pl/api '
2024-07-01 23:25:37 +02:00
class VariotMapping ( VulnerabilityMapping ) :
__exploit_mapping = {
' credits ' : ' credit ' ,
' description ' : ' description ' ,
' exploit ' : ' exploit ' ,
' title ' : ' title '
}
__exploit_multiple_mapping = {
' cve ' : {
' feature ' : ' cve_id ' ,
' relation ' : ' cve-id '
} ,
' references ' : {
' feature ' : ' url ' ,
' relation ' : ' reference '
2022-10-18 23:59:50 +02:00
}
2024-07-01 23:25:37 +02:00
}
2022-10-18 23:59:50 +02:00
2024-07-01 23:25:37 +02:00
@classmethod
def exploit_mapping ( cls ) - > dict :
return cls . __exploit_mapping
2022-10-24 15:01:54 +02:00
2024-07-01 23:25:37 +02:00
@classmethod
def exploit_multiple_mapping ( cls ) - > dict :
return cls . __exploit_multiple_mapping
2022-10-24 15:01:54 +02:00
2022-10-18 23:59:50 +02:00
2024-07-01 23:25:37 +02:00
class VariotdbsParser ( VulnerabilityParser ) :
def __init__ ( self , attribute ) :
super ( ) . __init__ ( attribute )
self . __mapping = VulnerabilityMapping
2022-10-18 23:59:50 +02:00
@property
2024-07-01 23:25:37 +02:00
def mapping ( self ) - > VulnerabilityMapping :
return self . __mapping
2022-10-18 23:59:50 +02:00
2022-10-24 15:01:54 +02:00
def parse_exploit_information ( self , query_results ) :
2022-10-24 16:18:22 +02:00
for exploit in query_results :
2022-10-24 15:01:54 +02:00
exploit_object = MISPObject ( ' exploit ' )
exploit_object . add_attribute ( ' exploitdb-id ' , exploit [ ' edb_id ' ] )
2024-07-01 23:25:37 +02:00
for field , relation in self . mapping . exploit_mapping ( ) . items ( ) :
if exploit . get ( field ) :
2022-10-24 15:01:54 +02:00
exploit_object . add_attribute (
2024-07-01 23:25:37 +02:00
relation , exploit [ field ] [ ' data ' ]
2022-10-24 15:01:54 +02:00
)
2024-07-01 23:25:37 +02:00
for field , relation in self . mapping . exploit_multiple_mapping ( ) . items ( ) :
if exploit . get ( field ) :
for value in exploit [ field ] [ ' data ' ] :
2022-10-24 15:01:54 +02:00
exploit_object . add_attribute (
2024-07-01 23:25:37 +02:00
relation [ ' relation ' ] , value [ relation [ ' feature ' ] ]
2022-10-24 15:01:54 +02:00
)
exploit_object . add_reference ( self . misp_attribute . uuid , ' related-to ' )
self . misp_event . add_object ( exploit_object )
2022-10-18 23:59:50 +02:00
def handler ( q = False ) :
if q is False :
return False
request = json . loads ( q )
if not request . get ( ' attribute ' ) or not check_input_attribute ( request [ ' attribute ' ] ) :
return { ' error ' : f ' { standard_error_message } , which should contain at least a type, a value and an uuid. ' }
attribute = request [ ' attribute ' ]
if attribute . get ( ' type ' ) != ' vulnerability ' :
return { ' error ' : ' Vulnerability id missing. ' }
headers = { ' Content-Type ' : ' application/json ' }
if request . get ( ' config ' , { } ) . get ( ' API_key ' ) :
headers [ ' Authorization ' ] = f " Token { request [ ' config ' ] [ ' API_key ' ] } "
2022-10-24 14:53:00 +02:00
empty = True
parser = VariotdbsParser ( attribute )
2022-10-18 23:59:50 +02:00
r = requests . get ( f " { variotdbs_url } /vuln/ { attribute [ ' value ' ] } / " , headers = headers )
if r . status_code == 200 :
2022-10-24 14:53:00 +02:00
vulnerability_results = r . json ( )
if vulnerability_results :
2024-07-01 23:25:37 +02:00
parser . _parse_variot_description ( vulnerability_results )
2022-10-24 14:53:00 +02:00
empty = False
2022-10-18 23:59:50 +02:00
else :
2022-10-24 15:43:04 +02:00
if r . reason != ' Not Found ' :
2022-10-24 14:53:00 +02:00
return { ' error ' : ' Error while querying the variotdbs API. ' }
2022-10-24 15:01:54 +02:00
r = requests . get ( f " { variotdbs_url } /exploits/?cve= { attribute [ ' value ' ] } " , headers = headers )
if r . status_code == 200 :
exploit_results = r . json ( )
if exploit_results :
2022-10-24 16:18:22 +02:00
parser . parse_exploit_information ( exploit_results [ ' results ' ] )
2022-10-24 15:01:54 +02:00
empty = False
2022-10-27 09:50:24 +02:00
if exploit_results [ ' next ' ] is not None :
while ( 1 ) :
exploit_results = requests . get ( exploit_results [ ' next ' ] , headers = headers )
if exploit_results . status_code != 200 :
break
exploit_results = exploit_results . json ( )
parser . parse_exploit_information ( exploit_results [ ' results ' ] )
if exploit_results [ ' next ' ] is None :
break
2022-10-24 15:01:54 +02:00
else :
return { ' error ' : ' Error while querying the variotdbs API. ' }
2022-10-24 14:53:00 +02:00
if empty :
return { ' error ' : ' Empty results ' }
2022-10-18 23:59:50 +02:00
return parser . get_results ( )
def introspection ( ) :
return mispattributes
def version ( ) :
moduleinfo [ ' config ' ] = moduleconfig
2022-10-24 14:53:00 +02:00
return moduleinfo