Ejemplo n.º 1
0
    def to_JSON(self, timestamp_index=0):
        """
            Get the doc from DB and returns a JSON without the ObjectId
            Client must JSON.parse() it in browser before passing it to Vuex
        """

        # Doc is this resource json itself
        doc = self.resource

        # Needed because FE expect it to be there
        doc["plugins"] = []

        # We need how many plugins can deal with this current resource type
        plugins_names = PluginManager.get_plugins_names_for_resource(
            self.get_type_value())

        # Scan for results in all plugins resource type related
        for plugin_name in plugins_names:
            result_cursor = (DB(plugin_name).collection.find({
                "resource_id":
                self.resource_id,
                "results": {
                    "$exists": True
                },
            }).sort([("timestamp", pymongo.DESCENDING)]))

            result_cursor = list(result_cursor)
            if not len(result_cursor) == 0:
                # Test timestamp index
                if timestamp_index < 0 or timestamp_index > len(
                        result_cursor) - 1:
                    result = result_cursor[0]
                else:
                    result = result_cursor[timestamp_index]

                # Add name of the plugin, because we do not store it in database
                result["name"] = plugin_name

                # If this plugin results is a list of external references (case pastebin), load it:
                _load_external_results(result)

                # Load all timemachine timestamps
                timemachine = []

                # Add the last LIMIT_OF_TIMEMACHINE_RESULTS timestamps to timemachine
                for ts in result_cursor:
                    timemachine.append({
                        "timestamp": ts["timestamp"],
                        "result_status": ts["result_status"],
                    })

                # Plug timemachine results in our plugin results
                result["timemachine"] = timemachine

                # Plug this plugin results to doc
                doc["plugins"].append(result)

        return json.loads(json.dumps(doc, default=str))
Ejemplo n.º 2
0
    def to_JSON(self, timestamp_index=0):
        """
            Get the doc from DB and returns a JSON without the ObjectId
            Client must JSON.parse() it in browser before passing it to Vuex
        """

        # Doc is this resource json itself
        doc = self.resource

        # Needed because FE expect it to be there
        doc["plugins"] = []

        # We need how many plugins can deal with this current resource type
        plugins_names = PluginManager.get_plugins_names_for_resource(
            self.get_type_value()
        )

        # Scan for results in all plugins resource type related
        for plugin_name in plugins_names:
            result_cursor = (
                DB(plugin_name)
                .collection.find(
                    {"resource_id": self.resource_id, "results": {"$exists": True},}
                )
                .sort([("timestamp", pymongo.DESCENDING)])
            )

            result_cursor = list(result_cursor)
            if not len(result_cursor) == 0:
                # Test timestamp index
                if timestamp_index < 0 or timestamp_index > len(result_cursor) - 1:
                    result = result_cursor[0]
                else:
                    result = result_cursor[timestamp_index]

                # Add name of the plugin, because we do not store it in database
                result["name"] = plugin_name

                try:
                    raw_result = result.get("results")
                    if raw_result and type(raw_result) == bytes:
                        # From bytes of a Binary object to a str
                        raw_result = bson.json_util.dumps(raw_result)
                        # From str to Python object
                        raw_result = json.loads(raw_result)
                        # Extract $binary. Now we have a binary string in raw_result.
                        raw_result = raw_result["$binary"]
                        # Content is a b64 string...
                        result["results"] = raw_result
                except Exception as e:
                    tb1 = traceback.TracebackException.from_exception(e)
                    print("".join(tb1.format()))

                # If this plugin results is a list of external references (case pastebin), load it:
                _load_external_results(result)

                # Load all timemachine timestamps
                timemachine = []

                # Add the last LIMIT_OF_TIMEMACHINE_RESULTS timestamps to timemachine
                for ts in result_cursor:
                    timemachine.append(
                        {
                            "timestamp": ts["timestamp"],
                            "result_status": ts["result_status"],
                        }
                    )

                # Plug timemachine results in our plugin results
                result["timemachine"] = timemachine

                # Plug this plugin results to doc
                doc["plugins"].append(result)

        return json.loads(json.dumps(doc, default=str))