<s> import os <EOL> import sys <EOL> _runPath = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> sys . path . append ( os . path . join ( _runPath , "<STR_LIT:..>" ) ) <EOL> from tornado . wsgi import WSGIContainer <EOL> from tornado . httpserver import HTTPServer <EOL> from tornado . ioloop import IOLoop <EOL> from flask import Flask , render_template , request , jsonify <EOL> from redis import exceptions as redisExceptions <EOL> import json <EOL> import re <EOL> import argparse <EOL> import time <EOL> import urllib <EOL> import random <EOL> import signal <EOL> import logging <EOL> from logging . handlers import RotatingFileHandler <EOL> from lib . Config import Configuration <EOL> from lib . Toolkit import toStringFormattedCPE , toOldCPE , currentTime , isURL , vFeedName , convertDateToDBFormat <EOL> import lib . CVEs as cves <EOL> import lib . DatabaseLayer as dbLayer <EOL> argparser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> argparser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> args = argparser . parse_args ( ) <EOL> app = Flask ( __name__ , static_folder = '<STR_LIT>' , static_url_path = '<STR_LIT>' ) <EOL> app . config [ '<STR_LIT>' ] = Configuration . getMongoDB ( ) <EOL> app . config [ '<STR_LIT>' ] = str ( random . getrandbits ( <NUM_LIT> ) ) <EOL> pageLength = Configuration . getPageLength ( ) <EOL> redisdb = Configuration . getRedisVendorConnection ( ) <EOL> def getBrowseList ( vendor ) : <EOL> result = { } <EOL> if ( vendor is None ) or type ( vendor ) == list : <EOL> v1 = redisdb . smembers ( "<STR_LIT>" ) <EOL> v2 = redisdb . smembers ( "<STR_LIT>" ) <EOL> v3 = redisdb . smembers ( "<STR_LIT>" ) <EOL> vendor = sorted ( list ( set ( list ( v1 ) + list ( v2 ) + list ( v3 ) ) ) ) <EOL> cpe = None <EOL> else : <EOL> cpenum = redisdb . scard ( "<STR_LIT>" + vendor ) <EOL> if cpenum < <NUM_LIT:1> : <EOL> return page_not_found ( <NUM_LIT> ) <EOL> p = redisdb . smembers ( "<STR_LIT>" + vendor ) <EOL> cpe = sorted ( list ( p ) ) <EOL> result [ "<STR_LIT>" ] = vendor <EOL> result [ "<STR_LIT>" ] = cpe <EOL> return result <EOL> def getVersionsOfProduct ( product ) : <EOL> p = redisdb . smembers ( "<STR_LIT>" + product ) <EOL> return sorted ( list ( p ) ) <EOL> def filter_logic ( unlisted , timeSelect , startDate , endDate , <EOL> timeTypeSelect , cvssSelect , cvss , rejectedSelect , limit , skip ) : <EOL> query = [ ] <EOL> if rejectedSelect == "<STR_LIT>" : <EOL> exp = "<STR_LIT>" <EOL> query . append ( { '<STR_LIT>' : re . compile ( exp ) } ) <EOL> if cvssSelect != "<STR_LIT:all>" : <EOL> if cvssSelect == "<STR_LIT>" : <EOL> query . append ( { '<STR_LIT>' : { '<STR_LIT>' : float ( cvss ) } } ) <EOL> if cvssSelect == "<STR_LIT>" : <EOL> query . append ( { '<STR_LIT>' : float ( cvss ) } ) <EOL> if cvssSelect == "<STR_LIT>" : <EOL> query . append ( { '<STR_LIT>' : { '<STR_LIT>' : float ( cvss ) } } ) <EOL> if timeSelect != "<STR_LIT:all>" : <EOL> startDate = convertDateToDBFormat ( startDate ) <EOL> endDate = convertDateToDBFormat ( endDate ) <EOL> if timeSelect == "<STR_LIT>" : <EOL> query . append ( { timeTypeSelect : { '<STR_LIT>' : startDate } } ) <EOL> if timeSelect == "<STR_LIT>" : <EOL> query . append ( { timeTypeSelect : { '<STR_LIT>' : endDate } } ) <EOL> if timeSelect == "<STR_LIT>" : <EOL> query . append ( { timeTypeSelect : { '<STR_LIT>' : startDate , '<STR_LIT>' : endDate } } ) <EOL> if timeSelect == "<STR_LIT>" : <EOL> query . append ( { '<STR_LIT>' : [ { timeTypeSelect : { '<STR_LIT>' : startDate } } , { timeTypeSelect : { '<STR_LIT>' : endDate } } ] } ) <EOL> return dbLayer . getCVEs ( limit = limit , skip = skip , query = query ) <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> unlisted = "<STR_LIT>" <EOL> timeSelect = "<STR_LIT:all>" <EOL> startDate = None <EOL> endDate = None <EOL> timeTypeSelect = "<STR_LIT>" <EOL> cvssSelect = "<STR_LIT:all>" <EOL> cvss = None <EOL> rejectedSelect = "<STR_LIT>" <EOL> cve = filter_logic ( unlisted , timeSelect , startDate , endDate , <EOL> timeTypeSelect , cvssSelect , cvss , rejectedSelect , pageLength , <NUM_LIT:0> ) <EOL> return render_template ( '<STR_LIT>' , cve = cve , r = <NUM_LIT:0> , pageLength = pageLength , minimal = True ) <EOL> @ app . route ( '<STR_LIT:/>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def filterPost ( ) : <EOL> unlisted = request . form . get ( '<STR_LIT>' ) <EOL> timeSelect = request . form . get ( '<STR_LIT>' ) <EOL> startDate = request . form . get ( '<STR_LIT>' ) <EOL> endDate = request . form . get ( '<STR_LIT>' ) <EOL> timeTypeSelect = request . form . get ( '<STR_LIT>' ) <EOL> cvssSelect = request . form . get ( '<STR_LIT>' ) <EOL> cvss = request . form . get ( '<STR_LIT>' ) <EOL> rejectedSelect = request . form . get ( '<STR_LIT>' ) <EOL> settings = { '<STR_LIT>' : unlisted , '<STR_LIT>' : timeSelect , <EOL> '<STR_LIT>' : startDate , '<STR_LIT>' : endDate , <EOL> '<STR_LIT>' : timeTypeSelect , '<STR_LIT>' : cvssSelect , <EOL> '<STR_LIT>' : cvss , '<STR_LIT>' : rejectedSelect } <EOL> cve = filter_logic ( unlisted , timeSelect , startDate , endDate , <EOL> timeTypeSelect , cvssSelect , cvss , rejectedSelect , pageLength , <NUM_LIT:0> ) <EOL> return render_template ( '<STR_LIT>' , settings = settings , cve = cve , r = <NUM_LIT:0> , pageLength = pageLength , minimal = True ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' , '<STR_LIT:GET>' ] ) <EOL> def filterLast ( r ) : <EOL> if not r : <EOL> r = <NUM_LIT:0> <EOL> blacklist = request . form . get ( '<STR_LIT>' ) <EOL> whitelist = request . form . get ( '<STR_LIT>' ) <EOL> unlisted = request . form . get ( '<STR_LIT>' ) <EOL> timeSelect = request . form . get ( '<STR_LIT>' ) <EOL> startDate = request . form . get ( '<STR_LIT>' ) <EOL> endDate = request . form . get ( '<STR_LIT>' ) <EOL> timeTypeSelect = request . form . get ( '<STR_LIT>' ) <EOL> cvssSelect = request . form . get ( '<STR_LIT>' ) <EOL> cvss = request . form . get ( '<STR_LIT>' ) <EOL> rejectedSelect = request . form . get ( '<STR_LIT>' ) <EOL> settings = { '<STR_LIT>' : blacklist , '<STR_LIT>' : whitelist , <EOL> '<STR_LIT>' : unlisted , '<STR_LIT>' : timeSelect , <EOL> '<STR_LIT>' : startDate , '<STR_LIT>' : endDate , <EOL> '<STR_LIT>' : timeTypeSelect , '<STR_LIT>' : cvssSelect , <EOL> '<STR_LIT>' : cvss , '<STR_LIT>' : rejectedSelect } <EOL> cve = filter_logic ( unlisted , timeSelect , startDate , endDate , <EOL> timeTypeSelect , cvssSelect , cvss , rejectedSelect , pageLength , r ) <EOL> return render_template ( '<STR_LIT>' , settings = settings , cve = cve , r = r , pageLength = pageLength , minimal = True ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def cpe23 ( cpe ) : <EOL> cpe = toStringFormattedCPE ( cpe ) <EOL> if not cpe : cpe = '<STR_LIT:None>' <EOL> return cpe <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def cpe22 ( cpe ) : <EOL> cpe = toOldCPE ( cpe ) <EOL> if not cpe : cpe = '<STR_LIT:None>' <EOL> return cpe <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def apiCVEFor ( cpe ) : <EOL> cpe = urllib . parse . unquote_plus ( cpe ) <EOL> cpe = toStringFormattedCPE ( cpe ) <EOL> if not cpe : cpe = '<STR_LIT:None>' <EOL> r = [ ] <EOL> cvesp = cves . last ( rankinglookup = False , namelookup = False , vfeedlookup = True , capeclookup = False ) <EOL> for x in dbLayer . cvesForCPE ( cpe ) : <EOL> r . append ( cvesp . getcve ( x [ '<STR_LIT:id>' ] ) ) <EOL> return json . dumps ( r ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def apiCVE ( cveid ) : <EOL> cvesp = cves . last ( rankinglookup = True , namelookup = True , vfeedlookup = True , capeclookup = True ) <EOL> cve = cvesp . getcve ( cveid = cveid ) <EOL> if cve is None : <EOL> cve = { } <EOL> return ( jsonify ( cve ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def apibrowse ( vendor = None ) : <EOL> if vendor is not None : <EOL> vendor = urllib . parse . quote_plus ( vendor ) . lower ( ) <EOL> browseList = getBrowseList ( vendor ) <EOL> if isinstance ( browseList , dict ) : <EOL> return ( jsonify ( browseList ) ) <EOL> else : <EOL> return ( jsonify ( { } ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def apilast ( ) : <EOL> limit = <NUM_LIT:30> <EOL> cvesp = cves . last ( rankinglookup = True , namelookup = True , vfeedlookup = True , capeclookup = True ) <EOL> cve = cvesp . get ( limit = limit ) <EOL> return ( jsonify ( { "<STR_LIT>" : cve } ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def apisearch ( vendor = None , product = None ) : <EOL> if vendor is None or product is None : <EOL> return ( jsonify ( { } ) ) <EOL> search = vendor + "<STR_LIT::>" + product <EOL> return ( json . dumps ( dbLayer . cvesForCPE ( search ) ) ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def cve ( cveid ) : <EOL> cveid = cveid . upper ( ) <EOL> cvesp = cves . last ( rankinglookup = True , namelookup = True , vfeedlookup = True , capeclookup = True , subscorelookup = True , misplookup = True ) <EOL> cve = cvesp . getcve ( cveid = cveid ) <EOL> if cve is None : <EOL> return render_template ( '<STR_LIT>' , status = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:info>' : { '<STR_LIT>' : cveid } } , minimal = True ) <EOL> return render_template ( '<STR_LIT>' , cve = cve , minimal = True ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def apidbInfo ( ) : <EOL> return ( json . dumps ( dbLayer . getDBStats ( ) ) ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def browse ( vendor = None ) : <EOL> try : <EOL> if vendor is not None : <EOL> vendor = urllib . parse . quote_plus ( vendor ) . lower ( ) <EOL> browseList = getBrowseList ( vendor ) <EOL> vendor = browseList [ "<STR_LIT>" ] <EOL> product = browseList [ "<STR_LIT>" ] <EOL> return render_template ( '<STR_LIT>' , product = product , vendor = vendor , minimal = True ) <EOL> except redisExceptions . ConnectionError : <EOL> return render_template ( '<STR_LIT>' , <EOL> status = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:info>' : { '<STR_LIT:host>' : Configuration . getRedisHost ( ) , '<STR_LIT:port>' : Configuration . getRedisPort ( ) } } ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def cwe ( ) : <EOL> cwes = [ x for x in dbLayer . getCWEs ( ) if x [ "<STR_LIT>" ] . lower ( ) == "<STR_LIT:class>" ] <EOL> return render_template ( '<STR_LIT>' , cwes = cwes , capec = None , minimal = True ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def relatedCWE ( cweid ) : <EOL> cwes = { x [ "<STR_LIT:id>" ] : x [ "<STR_LIT:name>" ] for x in dbLayer . getCWEs ( ) } <EOL> return render_template ( '<STR_LIT>' , cwes = cwes , cwe = cweid , capec = dbLayer . getCAPECFor ( cweid ) , minimal = True ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def capec ( capecid ) : <EOL> cwes = { x [ "<STR_LIT:id>" ] : x [ "<STR_LIT:name>" ] for x in dbLayer . getCWEs ( ) } <EOL> return render_template ( '<STR_LIT>' , cwes = cwes , capec = dbLayer . getCAPEC ( capecid ) , minimal = True ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def searchText ( ) : <EOL> search = request . form . get ( '<STR_LIT>' ) <EOL> result = dbLayer . getSearchResults ( search ) <EOL> cve = result [ '<STR_LIT:data>' ] <EOL> errors = result [ '<STR_LIT>' ] if '<STR_LIT>' in result else [ ] <EOL> return render_template ( '<STR_LIT>' , cve = cve , errors = errors , minimal = True ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def search ( vendor = None , product = None ) : <EOL> search = vendor + "<STR_LIT::>" + product <EOL> cve = dbLayer . cvesForCPE ( search ) <EOL> return render_template ( '<STR_LIT>' , vendor = vendor , product = product , cve = cve , minimal = True ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def link ( vFeedMap = None , field = None , value = None ) : <EOL> vFeedMap = htmlDedode ( vFeedMap ) <EOL> field = htmlDedode ( field ) <EOL> value = htmlDedode ( value ) <EOL> search = "<STR_LIT>" % ( vFeedMap , field ) <EOL> regex = re . compile ( re . escape ( value ) , re . I ) <EOL> cve = dbLayer . vFeedLinked ( search , regex ) <EOL> cvssList = [ float ( x [ '<STR_LIT>' ] ) for x in cve if '<STR_LIT>' in x ] <EOL> if cvssList : <EOL> stats = { '<STR_LIT>' : max ( cvssList ) , '<STR_LIT>' : min ( cvssList ) , '<STR_LIT:count>' : len ( cve ) } <EOL> else : <EOL> stats = { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:count>' : len ( cve ) } <EOL> return render_template ( '<STR_LIT>' , vFeedMap = vFeedMap , field = field , value = value , cve = cve , stats = stats , minimal = True ) <EOL> @ app . errorhandler ( <NUM_LIT> ) <EOL> def page_not_found ( e ) : <EOL> return render_template ( '<STR_LIT>' , minimal = True ) , <NUM_LIT> <EOL> @ app . template_filter ( '<STR_LIT>' ) <EOL> def currentTimeFilter ( utc ) : <EOL> return currentTime ( utc ) <EOL> @ app . template_filter ( '<STR_LIT>' ) <EOL> def htmlDedode ( string ) : <EOL> return urllib . parse . unquote_plus ( string ) <EOL> @ app . template_filter ( '<STR_LIT>' ) <EOL> def htmlEncode ( string ) : <EOL> return urllib . parse . quote_plus ( string ) . lower ( ) <EOL> @ app . template_filter ( '<STR_LIT>' ) <EOL> def isURLFilter ( string ) : <EOL> return isURL ( string ) <EOL> @ app . template_filter ( '<STR_LIT>' ) <EOL> def vFeedNameFilter ( string ) : <EOL> return vFeedName ( string ) <EOL> @ app . template_filter ( '<STR_LIT>' ) <EOL> def sortIntLikeStrFilter ( datalist ) : <EOL> return sorted ( datalist , key = lambda k : int ( k ) ) <EOL> def sig_handler ( sig , frame ) : <EOL> print ( '<STR_LIT>' % sig ) <EOL> IOLoop . instance ( ) . add_callback ( shutdown ) <EOL> def shutdown ( ) : <EOL> MAX_WAIT_SECONDS_BEFORE_SHUTDOWN = <NUM_LIT:3> <EOL> print ( '<STR_LIT>' ) <EOL> http_server . stop ( ) <EOL> print ( '<STR_LIT>' % MAX_WAIT_SECONDS_BEFORE_SHUTDOWN ) <EOL> io_loop = IOLoop . instance ( ) <EOL> deadline = time . time ( ) + MAX_WAIT_SECONDS_BEFORE_SHUTDOWN <EOL> def stop_loop ( ) : <EOL> now = time . time ( ) <EOL> if now < deadline and ( io_loop . _callbacks or io_loop . _timeouts ) : <EOL> io_loop . add_timeout ( now + <NUM_LIT:1> , stop_loop ) <EOL> else : <EOL> io_loop . stop ( ) <EOL> print ( '<STR_LIT>' ) <EOL> stop_loop ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> flaskHost = Configuration . getFlaskHost ( ) <EOL> flaskPort = Configuration . getFlaskPort ( ) <EOL> flaskDebug = Configuration . getFlaskDebug ( ) <EOL> if Configuration . getLogging ( ) : <EOL> logfile = Configuration . getLogfile ( ) <EOL> pathToLog = logfile . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if not os . path . exists ( pathToLog ) : <EOL> os . makedirs ( pathToLog ) <EOL> maxLogSize = Configuration . getMaxLogSize ( ) <EOL> backlog = Configuration . getBacklog ( ) <EOL> file_handler = RotatingFileHandler ( logfile , maxBytes = maxLogSize , backupCount = backlog ) <EOL> file_handler . setLevel ( logging . ERROR ) <EOL> formatter = logging . Formatter ( "<STR_LIT>" ) <EOL> file_handler . setFormatter ( formatter ) <EOL> app . logger . addHandler ( file_handler ) <EOL> if flaskDebug : <EOL> app . run ( host = flaskHost , port = flaskPort , debug = flaskDebug ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> if Configuration . useSSL ( ) : <EOL> cert = os . path . join ( _runPath , "<STR_LIT>" , Configuration . getSSLCert ( ) ) <EOL> key = os . path . join ( _runPath , "<STR_LIT>" , Configuration . getSSLKey ( ) ) <EOL> ssl_options = { "<STR_LIT>" : cert , <EOL> "<STR_LIT>" : key } <EOL> else : <EOL> ssl_options = None <EOL> signal . signal ( signal . SIGTERM , sig_handler ) <EOL> signal . signal ( signal . SIGINT , sig_handler ) <EOL> global http_server <EOL> http_server = HTTPServer ( WSGIContainer ( app ) , ssl_options = ssl_options ) <EOL> http_server . bind ( flaskPort , address = flaskHost ) <EOL> http_server . start ( <NUM_LIT:0> ) <EOL> IOLoop . instance ( ) . start ( ) </s>
