示例#1
0
文件: api.py 项目: eaydin/cve-search
    def start(self):
        # get properties
        flaskHost = Configuration.getFlaskHost()
        if self.argument_port is not None:
            if type(self.argument_port) == int:
                flaskPort = self.argument_port
            else:
                flaskPort = Configuration.getFlaskPort()
        else:
            flaskPort = Configuration.getFlaskPort()
        flaskDebug = Configuration.getFlaskDebug()
        # logging
        if Configuration.getLogging():
            logfile = Configuration.getLogfile()
            pathToLog = logfile.rsplit('/', 1)[0]
            if not os.path.exists(pathToLog):
                os.makedirs(pathToLog)
            maxLogSize = Configuration.getMaxLogSize()
            backlog = Configuration.getBacklog()
            file_handler = RotatingFileHandler(logfile,
                                               maxBytes=maxLogSize,
                                               backupCount=backlog)
            file_handler.setLevel(logging.ERROR)
            formatter = logging.Formatter(
                "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
            file_handler.setFormatter(formatter)
            self.app.logger.addHandler(file_handler)

        if flaskDebug:
            # start debug flask server
            self.app.run(host=flaskHost, port=flaskPort, debug=flaskDebug)
        else:
            # start asynchronous server using tornado wrapper for flask
            # ssl connection
            print("Server starting...")
            if Configuration.useSSL():
                ssl_options = {
                    "certfile":
                    os.path.join(_runPath, "../", Configuration.getSSLCert()),
                    "keyfile":
                    os.path.join(_runPath, "../", Configuration.getSSLKey())
                }
            else:
                ssl_options = None
            signal.signal(signal.SIGTERM, self.sig_handler)
            signal.signal(signal.SIGINT, self.sig_handler)

            self.http_server = HTTPServer(WSGIContainer(self.app),
                                          ssl_options=ssl_options)
            self.http_server.bind(flaskPort, address=flaskHost)
            # self.http_server.start(0)  # Forks multiple sub-processes
            self.http_server.start(
            )  # Don't fork, since MongoDB connection is made earlier and not Fork-safe.
            IOLoop.instance().start()
示例#2
0
    def start(self):
        # get properties
        flaskHost = Configuration.getFlaskHost()
        flaskPort = Configuration.getFlaskPort()
        flaskDebug = Configuration.getFlaskDebug()
        # logging
        # if Configuration.getLogging():
        #     logfile = Configuration.getLogfile()
        #     pathToLog = logfile.rsplit("/", 1)[0]
        #     if not os.path.exists(pathToLog):
        #         os.makedirs(pathToLog)
        #     maxLogSize = Configuration.getMaxLogSize()
        #     backlog = Configuration.getBacklog()
        #     file_handler = RotatingFileHandler(
        #         logfile, maxBytes=maxLogSize, backupCount=backlog
        #     )
        #     file_handler.setLevel(logging.ERROR)
        #     formatter = logging.Formatter(
        #         "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        #     )
        #     file_handler.setFormatter(formatter)
        #     self.app.logger.addHandler(file_handler)

        if flaskDebug:
            # start debug flask server
            self.app.run(host=flaskHost, port=flaskPort, debug=flaskDebug)
        else:
            # start asynchronous server using tornado wrapper for flask
            # ssl connection
            print("Server starting...")
            if Configuration.useSSL():
                ssl_options = {
                    "certfile":
                    os.path.join(_runPath, "../../../",
                                 Configuration.getSSLCert()),
                    "keyfile":
                    os.path.join(_runPath, "../../../",
                                 Configuration.getSSLKey()),
                }
            else:
                ssl_options = None
            signal.signal(signal.SIGTERM, self.sig_handler)
            signal.signal(signal.SIGINT, self.sig_handler)

            self.http_server = HTTPServer(WSGIContainer(self.app),
                                          ssl_options=ssl_options)
            self.http_server.bind(flaskPort, address=flaskHost)
            self.http_server.start(0)  # Forks multiple sub-processes
            IOLoop.instance().start()
示例#3
0
  def start(self):
    # get properties
    context = SSL.Context(SSL.SSLv23_METHOD)
    context.use_certificate_file('cvesite.crt')
    context.use_privatekey_file('cvesite.key')
    
    flaskHost = '0.0.0.0'
    flaskPort = 443
    flaskDebug = Configuration.getFlaskDebug()
    # logging
    if Configuration.getLogging():
      logfile = Configuration.getLogfile()
      pathToLog = logfile.rsplit('/', 1)[0]
      if not os.path.exists(pathToLog):
        os.makedirs(pathToLog)
      maxLogSize = Configuration.getMaxLogSize()
      backlog = Configuration.getBacklog()
      file_handler = RotatingFileHandler(logfile, maxBytes=maxLogSize, backupCount=backlog)
      file_handler.setLevel(logging.ERROR)
      formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
      file_handler.setFormatter(formatter)
      self.app.logger.addHandler(file_handler)

    if flaskDebug:
      # start debug flask server
      context = ('cvesite.crt', 'cvesite.key')

      self.app.run(host=flaskHost, port=flaskPort, debug=False,ssl_context=context,threaded=True)
    else:
      # start asynchronous server using tornado wrapper for flask
      # ssl connection
      print("Server starting...")
      if Configuration.useSSL():
        ssl_options = {"certfile": os.path.join(_runPath, "../", Configuration.getSSLCert()),
                        "keyfile": os.path.join(_runPath, "../", Configuration.getSSLKey())}
      else:
        ssl_options = None
      signal.signal(signal.SIGTERM, self.sig_handler)
      signal.signal(signal.SIGINT,  self.sig_handler)

      self.http_server = HTTPServer(WSGIContainer(self.app), ssl_options=context)
      self.http_server.bind(flaskPort, address=flaskHost)
      self.http_server.start(0)  # Forks multiple sub-processes
      IOLoop.instance().start()
示例#4
0
    def start(self):
        # get properties
        flaskHost = Configuration.getFlaskHost()
        flaskPort = Configuration.getFlaskPort()
        flaskDebug = Configuration.getFlaskDebug()
        # logging
        if Configuration.getLogging():
            logfile = Configuration.getLogfile()
            pathToLog = logfile.rsplit('/', 1)[0]
            if not os.path.exists(pathToLog):
                os.makedirs(pathToLog)
            maxLogSize = Configuration.getMaxLogSize()
            backlog = Configuration.getBacklog()
            file_handler = RotatingFileHandler(logfile, maxBytes=maxLogSize, backupCount=backlog)
            file_handler.setLevel(logging.ERROR)
            formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
            file_handler.setFormatter(formatter)
            self.app.logger.addHandler(file_handler)

        # Placing routes - Doing this later allows us to define multiple API instances, without flask problems
        for route in web.Routes.get_routes(self): self.addRoute(route)

        if flaskDebug:
            # start debug flask server
            self.app.run(host=flaskHost, port=flaskPort, debug=flaskDebug)
        else:
            # start asynchronous server using tornado wrapper for flask
            # ssl connection
            print("Server starting...")
            if Configuration.useSSL():
                ssl_options = {"certfile": os.path.join(_runPath, "../", Configuration.getSSLCert()),
                                "keyfile": os.path.join(_runPath, "../", Configuration.getSSLKey())}
            else:
                ssl_options = None
            signal.signal(signal.SIGTERM, self.sig_handler)
            signal.signal(signal.SIGINT,  self.sig_handler)

            self.http_server = HTTPServer(WSGIContainer(self.app), ssl_options=ssl_options)
            self.http_server.bind(flaskPort, address=flaskHost)
            self.http_server.start(0)  # Forks multiple sub-processes
            IOLoop.instance().start()
示例#5
0
  def start(self):
    # get properties
    flaskHost = Configuration.getFlaskHost()
    flaskPort = Configuration.getFlaskPort()
    flaskDebug = Configuration.getFlaskDebug()
    # logging
    if Configuration.getLogging():
      logfile = Configuration.getLogfile()
      pathToLog = logfile.rsplit('/', 1)[0]
      if not os.path.exists(pathToLog):
        os.makedirs(pathToLog)
      maxLogSize = Configuration.getMaxLogSize()
      backlog = Configuration.getBacklog()
      file_handler = RotatingFileHandler(logfile, maxBytes=maxLogSize, backupCount=backlog)
      file_handler.setLevel(logging.ERROR)
      formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
      file_handler.setFormatter(formatter)
      self.app.logger.addHandler(file_handler)

    if flaskDebug:
      # start debug flask server
      self.app.run(host=flaskHost, port=flaskPort, debug=flaskDebug)
    else:
      # start asynchronous server using tornado wrapper for flask
      # ssl connection
      print("Server starting...")
      if Configuration.useSSL():
        ssl_options = {"certfile": os.path.join(_runPath, "../", Configuration.getSSLCert()),
                        "keyfile": os.path.join(_runPath, "../", Configuration.getSSLKey())}
      else:
        ssl_options = None
      signal.signal(signal.SIGTERM, self.sig_handler)
      signal.signal(signal.SIGINT,  self.sig_handler)

      self.http_server = HTTPServer(WSGIContainer(self.app), ssl_options=ssl_options)
      self.http_server.bind(flaskPort, address=flaskHost)
      self.http_server.start(0)  # Forks multiple sub-processes
      IOLoop.instance().start()
示例#6
0
    deadline = time.time() + MAX_WAIT_SECONDS_BEFORE_SHUTDOWN

    def stop_loop():
        now = time.time()
        if now < deadline and (io_loop._callbacks or io_loop._timeouts):
            io_loop.add_timeout(now + 1, stop_loop)
        else:
            io_loop.stop()
            print('Shutdown')
    stop_loop()

if __name__ == '__main__':
    # get properties
    flaskHost = Configuration.getFlaskHost()
    flaskPort = Configuration.getFlaskPort()
    flaskDebug = Configuration.getFlaskDebug()
    # logging
    if Configuration.getLogging():
        logfile = Configuration.getLogfile()
        pathToLog = os.path.join(_runPath, logfile.rsplit('/', 1)[0])
        #pathToLog = os.path.join(_runPath, pathToLog)
        logfile = os.path.join(_runPath, logfile)
        if not os.path.exists(pathToLog):
            os.makedirs(pathToLog)
        maxLogSize = Configuration.getMaxLogSize()
        backlog = Configuration.getBacklog()
        file_handler = RotatingFileHandler(logfile, maxBytes=maxLogSize, backupCount=backlog)
        file_handler.setLevel(logging.ERROR)
        formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        file_handler.setFormatter(formatter)
        app.logger.addHandler(file_handler)
示例#7
0
    def stop_loop():
        now = time.time()
        if now < deadline and (io_loop._callbacks or io_loop._timeouts):
            io_loop.add_timeout(now + 1, stop_loop)
        else:
            io_loop.stop()
            print('Shutdown')

    stop_loop()


if __name__ == '__main__':
    # get properties
    flaskHost = Configuration.getFlaskHost()
    flaskPort = Configuration.getFlaskPort()
    flaskDebug = Configuration.getFlaskDebug()
    # logging
    if Configuration.getLogging():
        logfile = Configuration.getLogfile()
        pathToLog = logfile.rsplit('/', 1)[0]
        if not os.path.exists(pathToLog):
            os.makedirs(pathToLog)
        maxLogSize = Configuration.getMaxLogSize()
        backlog = Configuration.getBacklog()
        file_handler = RotatingFileHandler(logfile,
                                           maxBytes=maxLogSize,
                                           backupCount=backlog)
        file_handler.setLevel(logging.ERROR)
        formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        file_handler.setFormatter(formatter)
    def stop_loop():
        now = time.time()
        if now < deadline and (io_loop._callbacks or io_loop._timeouts):
            io_loop.add_timeout(now + 1, stop_loop)
        else:
            io_loop.stop()
            print('Shutdown')

    stop_loop()


if __name__ == '__main__':
    flaskHost = conf.getFlaskHost()
    flaskPort = conf.getFlaskPort()
    flaskDebug = conf.getFlaskDebug()

    if flaskDebug:
        #start debug flask server
        app.run(host=flaskHost, port=flaskPort, debug=flaskDebug)
    else:
        # start asynchronous server using tornado wrapper for flask
        # ssl connection
        print("Server starting...")
        if conf.useSSL():
            ssl_options = {
                "certfile": conf.getSSLCert(),
                "keyfile": conf.getSSLKey()
            }
        else:
            ssl_options = None
示例#9
0
    def start(self, port=None, systems=None):
        app = Flask(__name__,
                    static_folder='static',
                    static_url_path='/static')
        # functions

        # routes
        @app.route('/')
        def index():
            return render_template('index.html', systems=systems)

        @app.route('/cve/<cveid>')
        def cve(cveid):
            host, port = Configuration.getCVESearch()
            data = (urlopen('http://%s:%s/api/cve/%s' %
                            (host, port, cveid)).read()).decode('utf8')
            cvejson = json.loads(str(data))
            if cvejson is {}:
                return page_not_found(404)
            return render_template('cve.html', cve=cvejson)

        # error handeling
        @app.errorhandler(404)
        def page_not_found(e):
            return render_template('404.html'), 404

        # filters
        @app.template_filter('product')
        def product(banner):
            if banner:
                r = make_dict(banner)
                return r['product']
            else:
                return "unknown"

        @app.template_filter('toHuman')
        def toHuman(cpe):
            cpe = cpe[7:]
            result = cpe.split(':')[0] + " - "
            for c in cpe.split(':')[1:]:
                c = c.replace(':', ' ')
                c = c.replace('_', ' ')
                result += (" %s" % (c))
            result = result.title()
            return result

        @app.template_filter('currentTime')
        def currentTime(utc):
            output = toLocalTime(utc)
            return output

        @app.template_filter('impact')
        def impact(string):
            if string.lower() == "none": return "good"
            elif string.lower() == "partial": return "medium"
            elif string.lower() == "complete": return "bad"

        @app.template_filter('vFeedName')
        def vFeedName(string):
            string = string.replace('map_', '')
            string = string.replace('cve_', '')
            return string.title()

        @app.template_filter('htmlEncode')
        def htmlEncode(string):
            return urllib.parse.quote_plus(string).lower()

        @app.template_filter('isURL')
        def isURL(string):
            urlTypes = [re.escape(x) for x in ['http://', 'https://', 'www.']]
            return re.match("^(" + "|".join(urlTypes) + ")", string)

        # debug filter
        @app.template_filter('type')
        def isType(var):
            return type(var)

        #start webserver
        host = Configuration.getFlaskHost()
        port = Configuration.getFlaskPort()
        debug = Configuration.getFlaskDebug()
        app.run(host=host, port=port, debug=debug)
    def stop_loop():
        now = time.time()
        if now < deadline and (io_loop._callbacks or io_loop._timeouts):
            io_loop.add_timeout(now + 1, stop_loop)
        else:
            io_loop.stop()
            print("Shutdown")

    stop_loop()


if __name__ == "__main__":
    flaskHost = conf.getFlaskHost()
    flaskPort = conf.getFlaskPort()
    flaskDebug = conf.getFlaskDebug()

    if flaskDebug:
        # start debug flask server
        app.run(host=flaskHost, port=flaskPort, debug=flaskDebug)
    else:
        # start asynchronous server using tornado wrapper for flask
        # ssl connection
        print("Server starting...")
        if conf.useSSL():
            ssl_options = {"certfile": conf.getSSLCert(), "keyfile": conf.getSSLKey()}
        else:
            ssl_options = None
        signal.signal(signal.SIGTERM, sig_handler)
        signal.signal(signal.SIGINT, sig_handler)
        global http_server
示例#11
0
  def start(self,port=None,scan=None):
    app = Flask(__name__, static_folder='static', static_url_path='/static')
    # functions
    

    # routes
    @app.route('/')
    def index():
      return render_template('index.html', scan=scan)

    @app.route('/cve/<cveid>')
    def cve(cveid):
      host,port=Configuration.getCVESearch()
      data = (urlopen('http://%s:%s/api/cve/%s'%(host,port,cveid)).read()).decode('utf8')
      cvejson=json.loads(str(data))
      if cvejson is {}:
        return page_not_found(404)
      return render_template('cve.html', cve=cvejson)

    # error handeling
    @app.errorhandler(404)
    def page_not_found(e):
      return render_template('404.html'), 404

    # filters
    @app.template_filter('product')
    def product(banner):
      if banner:
        r=make_dict(banner)
        return r['product'] if 'product' in r else 'unknown'
      else:
        return "unknown"
    @app.template_filter('toHuman')
    def humanify(cpe):
      return toHuman(cpe)

    @app.template_filter('currentTime')
    def currentTime(utc):
      return toLocalTime(utc)

    @app.template_filter('impact')
    def impact(string):
      if string.lower() == "none":       return "good"
      elif string.lower() == "partial":  return "medium"
      elif string.lower() == "complete": return "bad"

    @app.template_filter('vFeedName')
    def vFeedName(string):
      string=string.replace('map_','')
      string=string.replace('cve_','')
      return string.title()

    @app.template_filter('htmlEncode')
    def htmlEncode(string):
      return urllib.parse.quote_plus(string).lower()

    @app.template_filter('isURL')
    def isURL(string):
      urlTypes= [re.escape(x) for x in ['http://','https://', 'www.']]
      return re.match("^(" + "|".join(urlTypes) + ")", string)

    @app.template_filter('fromEpoch')
    def fromEpoch_filter(epoch):
      return fromEpoch(epoch)


    # debug filter
    @app.template_filter('type')
    def isType(var):
      return type(var)

    #start webserver
    host = Configuration.getFlaskHost()
    port = Configuration.getFlaskPort()
    debug = Configuration.getFlaskDebug()
    app.run(host=host, port=port, debug=debug)